Skip to content

Commit 1a55d71

Browse files
committed
CodeMirror
1 parent dab77bd commit 1a55d71

File tree

2 files changed

+102
-7
lines changed

2 files changed

+102
-7
lines changed

forsrc-gwt-ui/src/main/java/com/forsrc/gwt/client/application/codemirror/CodemirrorView.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
import javax.inject.Inject;
44

5+
import org.geomajas.codemirror.client.CodeMirrorWrapper;
6+
import org.geomajas.codemirror.client.Config;
57
import org.geomajas.codemirror.client.widget.CodeMirrorPanel;
68

9+
import com.google.gwt.core.client.Scheduler;
710
import com.google.gwt.event.logical.shared.AttachEvent;
811
import com.google.gwt.uibinder.client.UiBinder;
912
import com.google.gwt.uibinder.client.UiField;
13+
import com.google.gwt.user.client.Command;
1014
import com.google.gwt.user.client.ui.HTMLPanel;
1115
import com.google.gwt.user.client.ui.PopupPanel;
1216
import com.google.gwt.user.client.ui.Widget;
@@ -29,8 +33,7 @@ interface Binder extends UiBinder<Widget, CodemirrorView> {
2933
@Inject
3034
CodemirrorView(Binder uiBinder) {
3135
initWidget(uiBinder.createAndBindUi(this));
32-
codeMirrorPanel = new CodeMirrorPanel();
33-
codeMirrorHtmlPanel.add(codeMirrorPanel);
36+
3437
}
3538

3639
@Override
@@ -44,11 +47,16 @@ public void onSearch(String text) {
4447
protected void onAttach() {
4548
// TODO Auto-generated method stub
4649
super.onAttach();
47-
codeMirrorHtmlPanel.addAttachHandler(new AttachEvent.Handler() {
48-
49-
@Override
50-
public void onAttachOrDetach(AttachEvent event) {
51-
codeMirrorPanel.getEditor().setContent("hello world");
50+
Scheduler.get().scheduleDeferred(new Command() {
51+
public void execute() {
52+
if (codeMirrorPanel == null) {
53+
Config config = Config.getDefault();
54+
config.setOption(Config.MODE, "xml");
55+
config.setOption("autoCloseTags", true);
56+
config.setOption("collapseRange", true);
57+
codeMirrorPanel = new MyCodeMirrorPanel(config);
58+
codeMirrorHtmlPanel.add(codeMirrorPanel);
59+
}
5260
}
5361
});
5462
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.forsrc.gwt.client.application.codemirror;
2+
3+
import org.geomajas.annotation.Api;
4+
import org.geomajas.codemirror.client.CodeMirrorWrapper;
5+
import org.geomajas.codemirror.client.Config;
6+
import org.geomajas.codemirror.client.widget.CodeMirrorPanel;
7+
8+
import com.google.gwt.core.client.Scheduler;
9+
import com.google.gwt.user.client.Command;
10+
11+
import com.google.gwt.user.client.ui.TextArea;
12+
13+
@Api(allMethods = true)
14+
public class MyCodeMirrorPanel extends CodeMirrorPanel {
15+
16+
private CodeMirrorWrapper editor;
17+
private TextArea textArea;
18+
private Config config;
19+
20+
/**
21+
* Create an new codemirrorpanel with the default configuration.
22+
*/
23+
public MyCodeMirrorPanel() {
24+
super();
25+
}
26+
27+
/**
28+
* Create an new codemirrorpanel with the default configuration.
29+
*
30+
* @param initialData
31+
* the initial content.
32+
*/
33+
public MyCodeMirrorPanel(String initialData) {
34+
super(initialData);
35+
}
36+
37+
/**
38+
* Create an new codemirrorpanel with a custom configuration.
39+
*
40+
* @param config
41+
* the configuration.
42+
*/
43+
public MyCodeMirrorPanel(Config config) {
44+
super(config);
45+
}
46+
47+
/**
48+
* Get the internal CodemirrorWrapper object.
49+
*
50+
* @return editor
51+
*/
52+
public CodeMirrorWrapper getEditor() {
53+
return editor;
54+
}
55+
56+
/**
57+
* Set the initial data to be displayed in the codemirror editor window.
58+
*
59+
* @param initialData
60+
*/
61+
public void setInitialData(String initialData) {
62+
textArea.setValue(initialData);
63+
}
64+
65+
// ------------------------------------------------------------------
66+
67+
@Override
68+
protected void onLoad() {
69+
// super.onLoad();
70+
attachtCodeMirror();
71+
}
72+
73+
private void attachtCodeMirror() {
74+
Scheduler.get().scheduleDeferred(new Command() {
75+
public void execute() {
76+
if (textArea == null) {
77+
textArea = new TextArea();
78+
setWidget(textArea);
79+
}
80+
if (editor == null) {
81+
// Window.alert(textArea == null? "null" : "OK");
82+
editor = CodeMirrorWrapper.fromTextArea(textArea, config == null ? Config.getDefault() : config);
83+
}
84+
}
85+
});
86+
}
87+
}

0 commit comments

Comments
 (0)