View Javadoc
1   package org.vaadin.aceeditor.client.gwt;
2   
3   import com.google.gwt.core.client.JavaScriptObject;
4   import com.google.gwt.core.client.JsArray;
5   import com.google.gwt.core.client.JsArrayInteger;
6   import com.google.gwt.core.client.JsArrayString;
7   import com.google.gwt.dom.client.Element;
8   
9   /**
10   * A GWT adaptation of Ace editor.
11   * 
12   * @see <a href="http://ace.ajax.org">Ace Editor</a>
13   * 
14   */
15  public class GwtAceEditor extends JavaScriptObject {
16  
17  	protected GwtAceEditor() {
18  	}
19  
20  	public static native GwtAceEditor create(Element parent, String editorId) /*-{
21  		var aceDiv = $doc.createElement("div");
22  		if (editorId !== null) {
23  			aceDiv.id = editorId;
24  		}
25  		aceDiv.style.width = "100%";
26  		aceDiv.style.height = "100%";
27  		parent.appendChild(aceDiv);
28  		
29  		var editor = $wnd.ace.edit(aceDiv);
30  		
31  		
32  		return editor;
33  	}-*/;
34  	
35  	native static public void setAceConfig(String key, String value) /*-{
36  		$wnd.ace.config.set(key, value);
37  	}-*/;
38  
39  	public final void setMode(String mode) {
40  		setModeFullPath(getModeString(mode));
41  	}
42  	
43  	private final native void setModeFullPath(String mode) /*-{
44  		this.getSession().setMode(mode);
45  	}-*/;
46  
47  	public final void setTheme(String theme) {
48  		setThemeFullPath(getThemeString(theme));
49  	}
50  
51  	private final native void setThemeFullPath(String theme) /*-{
52  		this.setTheme(theme);
53  	}-*/;
54  	
55  	private static final String getModeString(String mode) {
56  		return "ace/mode/" + mode;
57  	}
58  	
59  	private static final String getThemeString(String theme) {
60  		return "ace/theme/" + theme;
61  	}
62  
63  	public final native String getText() /*-{
64  		return this.getSession().getValue();
65  	}-*/;
66  
67  	public final native void setText(String text) /*-{
68  		this.getSession().setValue(text);
69  	}-*/;
70  
71  	public final native void replace(GwtAceRange range, String text) /*-{
72  		this.getSession().getDocument().replace(range,text);
73  	}-*/;
74  
75  	public final native void insertLine(int index, String text) /*-{
76  		this.getSession().getDocument().insertLines(index,[text]);
77  	}-*/;
78  
79  	public final native void removeLines(int from, int to) /*-{
80  		this.getSession().getDocument().removeLines(from, to);
81  	}-*/;
82  
83  	public final native void addChangeHandler(GwtAceChangeHandler handler) /*-{
84  		var cb = function(event) {
85  			handler.@org.vaadin.aceeditor.client.gwt.GwtAceChangeHandler::onChange(Lorg/vaadin/v7/aceeditor/client/gwt/GwtAceChangeEvent;)(event);
86  		}
87  		this.getSession().addEventListener("change", cb, true);
88  	}-*/;
89  
90  	public final native void addChangeCursorHandler(
91  			GwtAceChangeCursorHandler handler) /*-{
92  		var cb = function(event) {
93  			handler.@org.vaadin.aceeditor.client.gwt.GwtAceChangeCursorHandler::onChangeCursor(Lorg/vaadin/v7/aceeditor/client/gwt/GwtAceEvent;)(event);
94  		}
95  		this.getSession().getSelection().addEventListener("changeCursor" ,cb, true);
96  	}-*/;
97  
98  	public final native void addChangeSelectionHandler(
99  			GwtAceChangeSelectionHandler handler) /*-{
100 		var cb = function(event) {
101 			handler.@org.vaadin.aceeditor.client.gwt.GwtAceChangeSelectionHandler::onChangeSelection(Lorg/vaadin/v7/aceeditor/client/gwt/GwtAceEvent;)(event);
102 		}
103 		this.getSession().getSelection().addEventListener("changeSelection" ,cb, true);
104 	}-*/;
105 
106 	public final native void setKeyboardHandler(GwtAceKeyboardHandler handler) /*-{
107 		var h = { handleKeyboard: function(data, hashId, keyString, keyCode, e) {
108 			
109 			// For most keypresses, Ace sends two events where keyCode===undefined in one of them.
110 			// There are some keys where it sends only one,
111 			// so we can't just ignore the undefined keycode events, after all. (?)
112 			// 	if (keyCode===undefined) {
113 			//		return;
114 			//	}
115 			
116 			var command = handler.@org.vaadin.aceeditor.client.gwt.GwtAceKeyboardHandler::handleKeyboard(Lcom/google/gwt/core/client/JavaScriptObject;ILjava/lang/String;ILorg/vaadin/v7/aceeditor/client/gwt/GwtAceKeyboardEvent;)(data, hashId, keyString, keyCode, e);
117 			if (command===@org.vaadin.aceeditor.client.gwt.GwtAceKeyboardHandler$Command::NULL) {
118 				return {command: "null"};
119 			}
120 		}};
121 		this.setKeyboardHandler(h);
122 	}-*/;
123 
124 	public final native void addFocusListener(GwtAceFocusBlurHandler handler) /*-{
125 		var focus = function(event) {
126 			handler.@org.vaadin.aceeditor.client.gwt.GwtAceFocusBlurHandler::onFocus(Lorg/vaadin/v7/aceeditor/client/gwt/GwtAceEvent;)(event);
127 		}
128 		var blur = function(event) {
129 			handler.@org.vaadin.aceeditor.client.gwt.GwtAceFocusBlurHandler::onBlur(Lorg/vaadin/v7/aceeditor/client/gwt/GwtAceEvent;)(event);
130 		}
131 		this.addEventListener("focus", focus, true);
132 		this.addEventListener("blur", blur, true);
133 	}-*/;
134 
135 	public final native void moveCursorTo(int row, int col) /*-{
136 		this.moveCursorTo(row, col);
137 	}-*/;
138 
139 	public final native void moveCursorToPosition(GwtAcePosition pos) /*-{
140 		this.moveCursorToPosition(pos);
141 	}-*/;
142 
143 	public final native GwtAcePosition getCursorPosition() /*-{
144 		return this.getCursorPosition();
145 	}-*/;
146 
147 	public final native void scrollToRow(int row) /*-{
148 		this.scrollToRow(row);
149 	}-*/;
150 
151 	public final native JsArrayInteger getCursorCoords() /*-{
152 		var p = this.getCursorPositionScreen();
153 		var c = this.renderer.textToScreenCoordinates(p.row,p.column);
154 		return [c.pageX,c.pageY];
155 	}-*/;
156 
157 	public final native JsArrayInteger getCoordsOfRowCol(int row, int column) /*-{
158 		var p = this.getSession().documentToScreenPosition({row:row, column:column});
159 		var c = this.renderer.textToScreenCoordinates(p.row, p.column);
160 		return [c.pageX,c.pageY];
161 	}-*/;
162 
163 	public final native JsArrayInteger getCoordsOf(GwtAcePosition pos) /*-{
164 		var p = this.getSession().documentToScreenPosition(pos);
165 		var c = this.renderer.textToScreenCoordinates(p.row, p.column);
166 		return [c.pageX,c.pageY];
167 	}-*/;
168 
169 	public final native void focus() /*-{
170 		this.focus();
171 	}-*/;
172 
173 	public final native void blur() /*-{
174 		this.blur();
175 	}-*/;
176 
177 	public final boolean isFocused() {
178 		return this.isFocused();
179 	}
180 
181 	public final native void setAnnotations(JsArray<GwtAceAnnotation> anns) /*-{
182 		this.getSession().setAnnotations(anns);
183 	}-*/;
184 
185 	public final native void clearAnnotations() /*-{
186 		this.getSession().clearAnnotations();
187 	}-*/;
188 
189 	public final native JsArray<GwtAceAnnotation> getAnnotations() /*-{
190 		return this.getSession().getAnnotations();
191 	}-*/;
192 
193 	public final native void removeMarker(String markerId) /*-{
194 		this.getSession().removeMarker(markerId);
195 	}-*/;
196 
197 	public final native String addMarker(GwtAceRange range, String cls,
198 			String type, boolean inFront) /*-{
199 		var id = this.getSession().addMarker(range, cls, type, inFront);
200 		return "" + id; // making sure it's a string
201 	}-*/;
202 
203 	public final native void setWidth(String width) /*-{
204 		this.container.style.width = width;
205 	}-*/;
206 
207 	public final native void setHeight(String height) /*-{
208 		this.container.style.height = height;
209 	}-*/;
210 
211 	public final native void resize() /*-{
212 		this.resize();
213 	}-*/;
214 
215 	public static native String keyName(int keyCode, boolean shift,
216 			boolean ctrl, boolean alt) /*-{	
217 		var keys = $wnd.ace.require("pilot/keys");
218 		
219 		var key = ""
220 		if (shift) { key += "Shift-"; }
221 		if (ctrl) { key += "Ctrl-"; }
222 		if (alt) { key += "Alt-"; }
223 		key += keys[keyCode];
224 		
225 		return key;
226 	}-*/;
227 
228 	public final native int getLength() /*-{
229 		return this.getSession().getLength();
230 	}-*/;
231 
232 	public final native String getLine(int row) /*-{
233 		return this.getSession().getLine(row);
234 	}-*/;
235 
236 	public final native JsArrayString getLines(int startRow, int endRow) /*-{
237 		return this.getSession().getLines(startRow, endRow);
238 	}-*/;
239 
240 	public final native int getLineLength(int row) /*-{
241 		return this.getSession().getLine(row).length;
242 	}-*/;
243 
244 	public final native int getLongestLineLength(int row1, int row2) /*-{
245 		var maxLen = 0;
246 		var rowLen, i;
247 		for (i=row1; i<=row2; ++i) {
248 			rowLen = this.getSession().getLine(i).length;
249 			if (rowLen>maxLen) {
250 				maxLen = rowLen;
251 			}
252 		}
253 		return maxLen;
254 	}-*/;
255 
256 	public final native GwtAceSelection getSelection() /*-{
257 		return this.getSelection();
258 	}-*/;
259 
260 	public final native void setSelection(GwtAceRange range, boolean reverse) /*-{
261 		this.getSelection().setSelectionRange(range, reverse);
262 	}-*/;
263 
264 	public final native void setReadOnly(boolean readOnly) /*-{
265 		this.setReadOnly(readOnly);
266 	}-*/;
267 
268 	public final native String getNewLineCharacter() /*-{
269 		return this.getSession().getDocument().getNewLineCharacter();
270 	}-*/;
271 
272 	public static native String keyCodeToString(int keyCode) /*-{
273 		return $wnd.ace.require("pilot/keys").keyCodeToString(keyCode);
274 	}-*/;
275 
276 	public static native int keyModsToHashId(boolean shift, boolean ctrl,
277 			boolean alt) /*-{
278 		var ret = 0;
279 		if (ctrl) ret += 1;
280 		if (alt) ret += 2;
281 		if (shift) ret += 4;
282 		// TODO? other mods like Command
283 		return ret;
284 	}-*/;
285 
286 	public final native void setHScrollBarAlwaysVisible(boolean visible) /*-{
287 		this.renderer.setHScrollBarAlwaysVisible(visible);
288 	}-*/;
289 
290 	public final native void setFontSize(String size) /*-{
291 		this.container.style.fontSize = size;
292 	}-*/;
293 
294 	public final native void scrollToY(int y) /*-{
295 		this.renderer.scrollToY(y);
296 	}-*/;
297 
298 	public final native void scrollToRow(double row) /*-{
299 		this.scrollToRow(row);
300 	}-*/;
301 
302 	public final native int getScrollTop() /*-{
303 		return this.renderer.getScrollTop();
304 	}-*/;
305 
306 	public final native double getScrollTopRow() /*-{
307 		return this.renderer.getScrollTopRow();
308 	}-*/;
309 
310     public final native void setShowGutter(boolean showGutter) /*-{
311         this.renderer.setShowGutter(showGutter);
312     }-*/;
313 
314     public final native void setShowPrintMargin(boolean showPrintMargin) /*-{
315         this.renderer.setShowPrintMargin(showPrintMargin);
316     }-*/;
317 
318     public final native void setHighlightActiveLineEnabled(boolean highlightActiveLine) /*-{
319         this.setHighlightActiveLine(highlightActiveLine);
320     }-*/;
321 
322 	//WRAPMODE
323 	public final native void setUseWrapMode(boolean useWrapMode) /*-{
324 		this.getSession().setUseWrapMode(useWrapMode);
325 	}-*/;
326 
327 	public final native void setUseWorker(boolean use) /*-{
328 		this.getSession().setUseWorker(use);
329 	}-*/;
330 
331 }