Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Copyright (c) 2011-2014, David H. Hovemeyer <david.hovemeyer@gmail.com>
  //
  // Permission is hereby granted, free of charge, to any person obtaining a copy
  // of this software and associated documentation files (the "Software"), to deal
  // in the Software without restriction, including without limitation the rights
  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  // copies of the Software, and to permit persons to whom the Software is
  // furnished to do so, subject to the following conditions:
  //
 // The above copyright notice and this permission notice shall be included in
 // all copies or substantial portions of the Software.
 //
 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 // THE SOFTWARE.
 
 package org.uberfire.ext.widgets.common.client.ace;
 
 import  com.google.gwt.core.client.JavaScriptObject;
 import  com.google.gwt.core.client.JsArray;
 import  com.google.gwt.dom.client.Element;
 import  com.google.gwt.user.client.TakesValue;
 import  com.google.gwt.user.client.ui.Composite;
 import  com.google.gwt.user.client.ui.FlowPanel;
 import  com.google.gwt.user.client.ui.HasText;
 import  com.google.gwt.user.client.ui.RequiresResize;

A GWT widget for the Ajax.org Code Editor (ACE).

See also:
Ajax.org Code Editor
 
 public class AceEditor extends Composite implements RequiresResize,
                                                     HasText,
                                                     TakesValue<String> {
 
     // Used to generate unique element ids for Ace widgets.
     private static int nextId = 0;
 
     private final String elementId;
 
     private JavaScriptObject editor;
 
     private JsArray<AceAnnotationannotations = JavaScriptObject.createArray().cast();
 
     private Element divElement;

    
Preferred constructor.
 
     public AceEditor() {
          = "_aceGWT" + ;
         ++;
         FlowPanel div = new FlowPanel();
         div.getElement().setId(  );
         initWidget( div );
          = div.getElement();
     }

    
Do not use this constructor: just use the default constructor.
 
     @Deprecated
     public AceEditorboolean unused ) {
         this();
     }

    
Call this method to start the editor. Make sure that the widget has been attached to the DOM tree before calling this method.
 
     public native void startEditor() /*-{
         var editor = $wnd.ace.edit(this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::divElement);
         editor.getSession().setUseWorker(false);
         this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor = editor;
 
         // Store a reference to the (Java) AceEditor object in the
         // JavaScript editor object.
         editor._aceGWTAceEditor = this;
 
         // I have been noticing sporadic failures of the editor
         // to display properly and receive key/mouse events.
         // Try to force the editor to resize and display itself fully.  See:
         //    https://groups.google.com/group/ace-discuss/browse_thread/thread/237262b521dcea33
         editor.resize();
         this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::redisplay();
     }-*/;

    
Call this to force the editor contents to be redisplayed. There seems to be a problem when an AceEditor is embedded in a LayoutPanel: the editor contents don't appear, and it refuses to accept focus and mouse events, until the browser window is resized. Calling this method works around the problem by forcing the underlying editor to redisplay itself fully. (?)
    public native void redisplay() /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        editor.renderer.onResize(true);
        editor.renderer.updateFull();
        editor.resize();
        editor.focus();
    }-*/;

    
Cleans up the entire editor.
    public native void destroy() /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        editor.destroy();
    }-*/;

    
Set the theme.

Parameters:
theme the theme (one of the values in the AceEditorTheme enumeration)
    public void setThemefinal AceEditorTheme theme ) {
        setThemeByNametheme.getName() );
    }

    
Set the theme by name.

Parameters:
themeName the theme name (e.g., "twilight")
    public native void setThemeByNameString themeName ) /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        editor.setTheme("ace/theme/" + themeName);
    }-*/;

    
Set the mode.

Parameters:
mode the mode (one of the values in the AceEditorMode enumeration)
    public void setModefinal AceEditorMode mode ) {
        setModeByNamemode.getName() );
    }

    
Set the mode by name.

Parameters:
shortModeName name of mode (e.g., "eclipse")
    public native void setModeByNameString shortModeName ) /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        var modeName = "ace/mode/" + shortModeName;
        var TheMode = $wnd.require(modeName).Mode;
        editor.getSession().setMode(new TheMode());
    }-*/;

    
Register a handler for change events generated by the editor.

Parameters:
callback the change event handler
    public native void addOnChangeHandlerAceEditorCallback callback ) /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        editor.getSession().on("change", function (e) {
            callback.@org.uberfire.ext.widgets.common.client.ace.AceEditorCallback::invokeAceCallback(Lcom/google/gwt/core/client/JavaScriptObject;)(e);
        });
    }-*/;

    
Register a handler for cursor position change events generated by the editor.

Parameters:
callback the cursor position change event handler
    public native void addOnCursorPositionChangeHandlerAceEditorCallback callback ) /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        editor.getSession().selection.on("changeCursor", function (e) {
            callback.@org.uberfire.ext.widgets.common.client.ace.AceEditorCallback::invokeAceCallback(Lcom/google/gwt/core/client/JavaScriptObject;)(e);
        });
    }-*/;

    
Set font size.
    public native void setFontSizeString fontSize ) /*-{
        var elementId = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::elementId;
        var elt = $doc.getElementById(elementId);
        elt.style.fontSize = fontSize;
    }-*/;

    
Get the complete text in the editor as a String.

Returns:
the text in the editor
    public native String getText() /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        return editor.getSession().getValue();
    }-*/;

    
Set the complete text in the editor from a String.

Parameters:
text the text to set in the editor
    public native void setTextString text ) /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        editor.getSession().setValue(text);
    }-*/;

    
Get the line of text at the given row number.

Parameters:
row the row number
Returns:
the line of text at that row number
    public native String getLineint row ) /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        return editor.getSession().getDocument().getLine(row);
    }-*/;

    
Insert given text at the cursor.

Parameters:
text text to insert at the cursor
    public native void insertAtCursorString text ) /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        editor.insert(text);
    }-*/;

    
Get the current cursor position.

Returns:
the current cursor position
    public native AceEditorCursorPosition getCursorPosition() /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        var pos = editor.getCursorPosition();
        return this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::getCursorPositionImpl(DD)(pos.row, pos.column);
    }-*/;
    private AceEditorCursorPosition getCursorPositionImplfinal double row,
                                                           final double column ) {
        return new AceEditorCursorPosition( (introw, (intcolumn );
    }

    
Gets the given document position as a zero-based index.

Parameters:
position the position to obtain the absolute index of (base zero)
Returns:
An index to the current location in the document
    public int getIndexFromPositionAceEditorCursorPosition position ) {
        return getIndexFromPositionImplposition.toJsObject() );
    }
    private native int getIndexFromPositionImpl( JavaScriptObject jsPosition ) /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        return editor.getSession().getDocument().positionToIndex(jsPosition);
    }-*/;

    
Gets a document position from a supplied zero-based index.

Parameters:
index (base zero)
Returns:
A position object showing the row and column of the supplied index in the document
    public native AceEditorCursorPosition getPositionFromIndexint index ) /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        var jsPosition = editor.getSession().getDocument().indexToPosition(index);
        return @org.uberfire.ext.widgets.common.client.ace.AceEditorCursorPosition::create(II)(
                jsPosition.row,
                jsPosition.column
        );
    }-*/;

    
Set whether or not soft tabs should be used.

Parameters:
useSoftTabs true if soft tabs should be used, false otherwise
    public native void setUseSoftTabsboolean useSoftTabs ) /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        editor.getSession().setUseSoftTabs(useSoftTabs);
    }-*/;

    
Set tab size. (Default is 4.)

Parameters:
tabSize the tab size to set
    public native void setTabSizeint tabSize ) /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        editor.getSession().setTabSize(tabSize);
    }-*/;

    
Go to given line.

Parameters:
line the line to go to
    public native void gotoLineint line ) /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        editor.gotoLine(line);
    }-*/;

    
Go to given line.

Parameters:
line the line to go to
    public native void scrollToLineint line ) /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        editor.resize(true);
        editor.scrollToLine(line, true, true, function () {});
    }-*/;


    
Set whether or not the horizontal scrollbar is always visible.

Parameters:
hScrollBarAlwaysVisible true if the horizontal scrollbar is always visible, false if it is hidden when not needed
    public native void setHScrollBarAlwaysVisibleboolean hScrollBarAlwaysVisible ) /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        editor.renderer.setHScrollBarAlwaysVisible(hScrollBarAlwaysVisible);
    }-*/;

    
Set whether or not the gutter is shown.

Parameters:
showGutter true if the gutter should be shown, false if it should be hidden
    public native void setShowGutterboolean showGutter ) /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        editor.renderer.setShowGutter(showGutter);
    }-*/;

    
Set or unset read-only mode.

Parameters:
readOnly true if editor should be set to readonly, false if the editor should be set to read-write
    public native void setReadOnlyboolean readOnly ) /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        editor.setReadOnly(readOnly);
    }-*/;

    
Set or unset highlighting of currently selected word.

Parameters:
highlightSelectedWord true to highlight currently selected word, false otherwise
    public native void setHighlightSelectedWordboolean highlightSelectedWord ) /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        editor.setHighlightSelectedWord(highlightSelectedWord);
    }-*/;

    
Set or unset the visibility of the print margin.

Parameters:
showPrintMargin true if the print margin should be shown, false otherwise
    public native void setShowPrintMarginboolean showPrintMargin ) /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        editor.renderer.setShowPrintMargin(showPrintMargin);
    }-*/;

    
Add an annotation to a the local annotations JsArray<AceAnnotation>, but does not set it on the editor

Parameters:
row to which the annotation should be added
column to which the annotation applies
text to display as a tooltip with the annotation
type to be displayed (one of the values in the AceAnnotationType enumeration)
    public void addAnnotationfinal int row,
                               final int column,
                               final String text,
                               final AceAnnotationType type ) {
        .push( AceAnnotation.createrowcolumntexttype.getName() ) );
    }

    
Set any annotations which have been added via addAnnotation on the editor
    public native void setAnnotations() /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        var annotations = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::annotations;
        editor.getSession().setAnnotations(annotations);
    }-*/;

    
Clear any annotations from the editor and reset the local annotations JsArray<AceAnnotation>
    public native void clearAnnotations() /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        editor.getSession().clearAnnotations();
        this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::resetAnnotations()();
    }-*/;

    
Reset any annotations in the local annotations JsArray<AceAnnotation>
    private void resetAnnotations() {
         = JavaScriptObject.createArray().cast();
    }

    
Remove a command from the editor.

Parameters:
command the command (one of the values in the AceCommand enumeration)
    public void removeCommandfinal AceCommand command ) {
        removeCommandByNamecommand.getName() );
    }

    
Remove commands, that may not be required, from the editor

Parameters:
command to be removed, one of "gotoline", "findnext", "findprevious", "find", "replace", "replaceall"
    public native void removeCommandByNameString command ) /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        editor.commands.removeCommand(command);
    }-*/;

    
Set whether to use wrap mode or not

Parameters:
useWrapMode true if word wrap should be used, false otherwise
    public native void setUseWrapModeboolean useWrapMode ) /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        editor.getSession().setUseWrapMode(useWrapMode);
    }-*/;
    /* (non-Javadoc)
     * @see com.google.gwt.user.client.ui.ResizeComposite#onResize()
     */
    @Override
    public void onResize() {
        redisplay();
    }
    @Override
    public void setValueString value ) {
        this.setTextvalue );
    }
    @Override
    public String getValue() {
        return this.getText();
    }

    
Set whether or not autocomplete is enabled.

Parameters:
b true if autocomplete should be enabled, false if not
    public native void setAutocompleteEnabledboolean b ) /*-{
        // See: https://github.com/ajaxorg/ace/wiki/How-to-enable-Autocomplete-in-the-Ace-editor
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        if (b) {
            $wnd.ace.require("ace/ext/language_tools");
            editor.setOptions({ enableBasicAutocompletion: true });
        } else {
            editor.setOptions({ enableBasicAutocompletion: false });
        }
    }-*/;

    
Removes all existing completers from the langtools

This can be used to disable all completers including local completers, which can be very useful when completers are used on very large files (as the local completer tokenizes every word to put in the selected list).

NOTE: This method may be removed, and replaced with another solution. It works at point of check-in, but treat this as unstable for now.
    public native static void removeAllExistingCompleters() /*-{
        var langTools = $wnd.ace.require("ace/ext/language_tools");
        langTools.removeCompleters();
    }-*/;

    
Add an AceCompletionProvider to provide custom code completions.

Warning: this is an experimental feature of AceGWT. It is possible that the API will change in an incompatible way in future releases.

Parameters:
provider the AceCompletionProvider
    public native static void addCompletionProviderAceCompletionProvider provider ) /*-{
        var langTools = $wnd.ace.require("ace/ext/language_tools");
        var completer = {
            getCompletions: function (editor, session, pos, prefix, callback) {
                var callbackWrapper =
                        @org.uberfire.ext.widgets.common.client.ace.AceEditor::wrapCompletionCallback(Lcom/google/gwt/core/client/JavaScriptObject;)(callback);
                var aceEditor = editor._aceGWTAceEditor;
                provider.@org.uberfire.ext.widgets.common.client.ace.AceCompletionProvider::getProposals(Lorg/uberfire/ext/widgets/common/client/ace/AceEditor;Lorg/uberfire/ext/widgets/common/client/ace/AceEditorCursorPosition;Ljava/lang/String;Lorg/uberfire/ext/widgets/common/client/ace/AceCompletionCallback;)(
                        aceEditor,
                        @org.uberfire.ext.widgets.common.client.ace.AceEditorCursorPosition::create(II)(pos.row, pos.column),
                        prefix,
                        callbackWrapper
                );
            }
        };
        langTools.addCompleter(completer);
    }-*/;
    private static AceCompletionCallback wrapCompletionCallback( JavaScriptObject jsCallback ) {
        return new AceCompletionCallbackImpljsCallback );
    }
    public native void setFocus()  /*-{
        var editor = this.@org.uberfire.ext.widgets.common.client.ace.AceEditor::editor;
        editor.focus();
    }-*/;
New to GrepCode? Check out our FAQ X