Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public class CodeEditor
ImplementsAnimation, Editable, EditorHost, Iterable<Component>, StyleListener
An IDE style source code editor with syntax highlighting, a line-number gutter and asynchronous code completion.
CodeEditor is built for editing source code on both touch devices (with the virtual keyboard) and
desktops (with a physical keyboard). Code completion is driven by a CodeCompletionProvider, which
can resolve proposals locally or from a remote language server.
Like RichTextArea, ports with low-level text input use the pure Codename One text engine
(com.codename1.ui.editor) with a built-in incremental syntax highlighter. Ports without that input
contract use an editable BrowserComponent fallback, and a port can transparently supply a native
code editor instead.
Example
CodeEditor editor = new CodeEditor();
editor.setLanguage("java");
editor.setText("public class Main {\n\n}");
editor.setCompletionProvider((ed, code, cursor, results) -> {
List<CodeCompletion> out = new ArrayList<>();
out.add(new CodeCompletion("System.out.println(", "System.out.println()").setType("method"));
results.onSucess(out);
});
form.add(BorderLayout.CENTER, editor);Fields
public static final int KEY_ENTER = 1 | Enter or the keyboard’s action key, in an editor of one line. |
public static final int KEY_TAB = 2 | Tab, in an editor of one line. |
public static final int KEY_BACK_TAB = 3 | Shift+Tab, in an editor of one line. |
public static final int KEY_ESCAPE = 4 | Escape, in an editor of one line. |
public static final int KEY_UP = 5 | The up arrow, in an editor of one line. |
public static final int KEY_DOWN = 6 | The down arrow, in an editor of one line. |
public static final int KEY_F4 = 7 | F4, in an editor of one line, on a port with a keyboard that delivers it. |
Constructors
public CodeEditor() | Creates an empty code editor. |
public CodeEditor(String language, String text) | Creates a code editor initialized with the supplied source and language. |
Methods
Inherited fields
From Component
DEFAULT_CURSOR, CROSSHAIR_CURSOR, TEXT_CURSOR, WAIT_CURSOR, SW_RESIZE_CURSOR, SE_RESIZE_CURSOR, NW_RESIZE_CURSOR, NE_RESIZE_CURSOR, N_RESIZE_CURSOR, S_RESIZE_CURSOR, W_RESIZE_CURSOR, E_RESIZE_CURSOR, HAND_CURSOR, MOVE_CURSOR, DRAG_REGION_NOT_DRAGGABLE, DRAG_REGION_POSSIBLE_DRAG_X, DRAG_REGION_POSSIBLE_DRAG_Y, DRAG_REGION_POSSIBLE_DRAG_XY, DRAG_REGION_LIKELY_DRAG_X, DRAG_REGION_LIKELY_DRAG_Y, DRAG_REGION_LIKELY_DRAG_XY, DRAG_REGION_IMMEDIATELY_DRAG_X, DRAG_REGION_IMMEDIATELY_DRAG_Y, DRAG_REGION_IMMEDIATELY_DRAG_XY, BRB_CONSTANT_ASCENT, BRB_CONSTANT_DESCENT, BRB_CENTER_OFFSET, BRB_OTHER, CENTER, TOP, LEFT, BOTTOM, RIGHT, BASELINE
Inherited methods
From AbstractEditorComponent
fireEditorEvent, command, query, onReady, isEditorReady, isNativeEditor, addChangeListener, removeChangeListener, addReadyListener, removeReadyListener, setEditable, isEditable, focusEditor, blurEditor, isTextInputSupported, startTextInput, updateTextInputState, stopTextInput, editorChanged
From Container
encloseIn, encloseIn, initLaf, getUIManager, setUIManager, isSurface, add, addAll, add, add, add, add, add, getLeadComponent, setLeadComponent, getLeadParent, keyPressed, keyReleased, getLayout, setLayout, invalidate, setShouldLayout, setShouldCalcPreferredSize, getLayoutWidth, getLayoutHeight, applyRTL, constrainWidthWhenScrollable, constrainHeightWhenScrollable, addComponent, addComponent, addComponent, addComponent, replaceAndWait, replaceAndWait, replace, replaceAndWait, replace, createReplaceTransition, isEnabled, setEnabled, removeComponent, cancelRepaints, flushReplace, removeAll, revalidateWithAnimationSafety, revalidate, revalidateLater, forceRevalidate, clearClientProperties, paint, paintGlass, layoutContainer, isSafeArea, setSafeArea, isSafeAreaRoot, getSafeAreaRoot, setSafeAreaRoot, getComponentCount, getComponentAt, getComponentIndex, contains, scrollComponentToVisible, getClosestComponentTo, getResponderAt, getComponentAt, findDropTargetAt, pointerPressed, calcPreferredSize, paramString, refreshTheme, isScrollableX, setScrollableX, isScrollableY, setScrollableY, getSideGap, getBottomGap, setScrollable, setCellRenderer, getScrollIncrement, setScrollIncrement, findFirstFocusable, dragInitiated, fireClicked, isSelectableInteraction, getGridPosY, paintComponentBackground, getGridPosX, animateHierarchyAndWait, createAnimateHierarchy, animateHierarchy, animateHierarchyFadeAndWait, createAnimateHierarchyFade, animateHierarchyFade, animateLayoutFadeAndWait, createAnimateLayoutFadeAndWait, animateLayoutFade, createAnimateLayoutFade, animateLayoutAndWait, animateLayout, updateTabIndices, createAnimateLayout, drop, createAnimateMotion, morph, morphAndWait, animateUnlayout, animateUnlayoutAndWait, createAnimateUnlayout, getChildrenAsList, iterator, iterator
From Component
setSameSize, isSetCursorSupported, parsePreferredSize, getDefaultDragTransparency, setDefaultDragTransparency, getEditingDelegate, setEditingDelegate, getCursor, setCursor, showNativeOverlay, hideNativeOverlay, updateNativeOverlay, getNativeOverlay, getAllStyles, getSameWidth, setSameWidth, getSameHeight, setSameHeight, getX, setX, getOuterX, getInnerX, getY, setY, getOuterY, getInnerY, isVisible, setVisible, getClientProperty, stripMarginAndPadding, putClientProperty, getDirtyRegion, setDirtyRegion, isOpaque, setOpaque, getWidth, setWidth, getOuterWidth, getInnerWidth, getHeight, setHeight, getOuterHeight, getInnerHeight, isDragRegion, getDragRegionStatus, getBaseline, getBaselineResizeBehavior, getPreferredSizeStr, setPreferredSizeStr, getPreferredSize, setPreferredSize, getScrollDimension, calcScrollSize, setScrollSize, getPreferredW, setPreferredW, getPreferredH, setPreferredH, getOuterPreferredH, getInnerPreferredH, getOuterPreferredW, getInnerPreferredW, setSize, getUIID, setUIID, setUIIDFinal, setUIID, getInlineAllStyles, setInlineAllStyles, getInlineSelectedStyles, setInlineSelectedStyles, getInlineUnselectedStyles, setInlineUnselectedStyles, getInlineDisabledStyles, setInlineDisabledStyles, getInlinePressedStyles, setInlinePressedStyles, remove, getParent, getOwner, setOwner, isOwnedBy, containsOrOwns, addFocusListener, removeFocusListener, addScrollListener, removeScrollListener, getSelectCommandText, setSelectCommandText, getLabelForComponent, setLabelForComponent, focusGained, focusLost, paintBackgrounds, paintShadows, getAbsoluteX, getAbsoluteY, isInClippingRegion, paintIntersectingComponentsAbove, paintScrollbars, paintScrollbarX, getScrollOpacity, getSelectedRect, paintScrollbarY, paintComponent, paintComponent, getBorder, getScrollable, paintBackground, isScrollable, getScrollX, setScrollX, getScrollY, setScrollY, onScrollX, onScrollY, getDraggedx, getDraggedy, contains, visibleBoundsContains, hasFixedPreferredSize, getBounds, getBounds, getVisibleBounds, getVisibleBounds, isFocusable, setFocusable, onSetFocusable, resetFocusable, getTabIndex, setTabIndex, getPreferredTabIndex, setPreferredTabIndex, isTraversable, setTraversable, handlesInput, setHandlesInput, consumesRawTextInput, hasFocus, setFocus, getComponentForm, getTopLevelContainer, repaint, repaint, longKeyPress, keyRepeated, registerForAnimation, deregisterFromAnimation, getAnimationManager, getScrollAnimationSpeed, setScrollAnimationSpeed, isBlockLead, setBlockLead, isIgnorePointerEvents, setIgnorePointerEvents, isRippleEffect, setRippleEffect, getInlineStylesTheme, setInlineStylesTheme, shouldRenderComponentSelection, isHideInLandscape, setHideInLandscape, createStyleAnimation, isSmoothScrolling, setSmoothScrolling, pointerHover, stopScrollMomentum, pointerHoverReleased, pointerHoverPressed, pinch, pinchReleased, pinch, rotation, isPinchBlocksDragAndDrop, setPinchBlocksDragAndDrop, pointerDragged, getDragImage, getDragTransparency, setDragTransparency, toImage, drawDraggedImage, draggingOver, dragEnter, dragExit, addPullToRefresh, setPullToRefresh, respondsToPointerEvents, pointerDragged, isStickyDrag, pointerPressed, isDragAndDropOperation, pointerReleased, longPointerPress, pointerReleased, setVerticalScrollBounds, setHorizontalScrollBounds, isVScrollThumbGrabbed, isHScrollThumbGrabbed, isVScrollThumbHover, isHScrollThumbHover, isTensileDragEnabled, setTensileDragEnabled, getTextSelectionSupport, addDropListener, removeDropListener, addDragOverListener, removeDragOverListener, isNativeDragSource, setNativeDragSource, getNativeDragOperation, setNativeDragOperation, createNativeDragOperation, isNativeDropTarget, setNativeDropTarget, getAcceptedDropMimeTypes, setAcceptedDropMimeTypes, getAcceptedDropActions, setAcceptedDropActions, canAcceptNativeDrop, nativeDragEnter, nativeDragOver, nativeDragExit, nativeDrop, addNativeDropListener, removeNativeDropListener, addNativeDragOverListener, removeNativeDragOverListener, dragFinished, addDragFinishedListener, addStateChangeListener, removeStateChangeListener, addPointerPressedListener, addLongPressListener, addContextMenuListener, removeContextMenuListener, addMouseWheelListener, removeMouseWheelListener, addStylusListener, removeStylusListener, getContextMenuCommands, setContextMenuCommands, mouseWheel, paintRippleOverlay, removePointerPressedListener, removeLongPressListener, removeDragFinishedListener, addPointerReleasedListener, removePointerReleasedListener, addPointerDraggedListener, removePointerDraggedListener, getDragSpeed, getStyle, getPressedStyle, getHoverStyle, setHoverStyle, isHovered, setHovered, setPressedStyle, initUnselectedStyle, initPressedStyle, initDisabledStyle, initSelectedStyle, getUnselectedStyle, setUnselectedStyle, getSelectedStyle, setSelectedStyle, getDisabledStyle, setDisabledStyle, installDefaultPainter, requestFocus, toString, refreshTheme, refreshTheme, isDragActivated, animate, scrollRectToVisible, scrollRectToVisible, paintBorder, paintBorderBackground, isCellRenderer, isScrollVisible, setScrollVisible, setIsScrollVisible, startEditingAsync, stopEditing, isEditing, laidOut, deinitialize, initComponent, isInitialized, setInitialized, styleChanged, getNextFocusDown, setNextFocusDown, getNextFocusUp, setNextFocusUp, getNextFocusLeft, setNextFocusLeft, getNextFocusRight, setNextFocusRight, getName, setName, initCustomStyle, deinitializeCustomStyle, isRTL, setRTL, isTactileTouch, isTactileTouch, setTactileTouch, getPropertyNames, getPropertyTypes, getPropertyTypeNames, getPropertyValue, setPropertyValue, paintLockRelease, paintLock, isSnapToGrid, setSnapToGrid, shouldBlockSideSwipe, shouldBlockSideSwipeLeft, shouldBlockSideSwipeRight, blocksSideSwipe, isFlatten, setFlatten, getTensileLength, setTensileLength, isGrabsPointerEvents, setGrabsPointerEvents, getScrollOpacityChangeSpeed, setScrollOpacityChangeSpeed, growShrink, isAlwaysTensile, setAlwaysTensile, isDraggable, setDraggable, isDropTarget, setDropTarget, isChildOf, isHideInPortrait, setHideInPortrait, getBindablePropertyNames, getBindablePropertyTypes, bindProperty, unbindProperty, getBoundPropertyValue, setBoundPropertyValue, getCloudBoundProperty, setCloudBoundProperty, getCloudDestinationProperty, setCloudDestinationProperty, getComponentState, setComponentState, setHidden, isHidden, setHidden, isHidden, announceForAccessibility, getAccessibilityText, setAccessibilityText, getSemantics, getAccessibilityNode, accessibilityChanged, accessibilityChanged, getTooltip, setTooltip
From Iterable
Field details
KEY_ENTER
public static final int KEY_ENTER = 1#setSingleLine(boolean).KEY_TAB
public static final int KEY_TAB = 2KEY_BACK_TAB
public static final int KEY_BACK_TAB = 3KEY_ESCAPE
public static final int KEY_ESCAPE = 4KEY_UP
public static final int KEY_UP = 5KEY_DOWN
public static final int KEY_DOWN = 6KEY_F4
public static final int KEY_F4 = 7Constructor details
CodeEditor
public CodeEditor()CodeEditor
public CodeEditor(String language, String text)Parameters
languageString- the language id used for syntax highlighting (e.g.
"java","javascript","kotlin","css","xml","json","python") textString- the initial source code
Method details
setText
public void setText(String text)Parameters
textString- the source code
getText
public void getText(SuccessCallback<String> callback)Parameters
callbackSuccessCallback<String>- receives the source code
setLanguage
public void setLanguage(String language)Parameters
languageString- the language id (e.g.
"java","javascript","kotlin","css","xml","json","python")
getLanguage
public String getLanguage()registerSyntaxHighlighter
public static void registerSyntaxHighlighter(String language, SyntaxHighlighter highlighter)getRegisteredSyntaxHighlighter
public static SyntaxHighlighter getRegisteredSyntaxHighlighter(String language)setTheme
public void setTheme(String theme)"light" and "dark" are supported.Parameters
themeString- the theme id
getTheme
public String getTheme()setShowLineNumbers
public void setShowLineNumbers(boolean show)Parameters
showboolean- true to show line numbers
isShowLineNumbers
public boolean isShowLineNumbers()setTabSize
public void setTabSize(int tabSize)Parameters
tabSizeint- the indentation width in spaces
getTabSize
public int getTabSize()setReadOnly
public void setReadOnly(boolean readOnly)AbstractEditorComponent#setEditable(boolean).Parameters
readOnlyboolean- true to prevent editing
isReadOnly
public boolean isReadOnly()insertAtCursor
public void insertAtCursor(String text)Parameters
textString- the text to insert
setProtectedRegionMarkers
public void setProtectedRegionMarkers(String startMarker, String endMarker)addProtectedEditListener
public void addProtectedEditListener(ActionListener listener)Parameters
listenerActionListener- invoked once per refused edit
removeProtectedEditListener
public void removeProtectedEditListener(ActionListener listener)#addProtectedEditListener(ActionListener).Parameters
listenerActionListener- the listener to remove
setCursorPosition
public void setCursorPosition(int offset)getCursorPosition
public void getCursorPosition(SuccessCallback<Integer> callback)Parameters
callbackSuccessCallback<Integer>- receives the caret offset as an Integer
setDiagnostics
public void setDiagnostics(List<CodeDiagnostic> diagnostics)Parameters
diagnosticsList<CodeDiagnostic>- the diagnostics to display
setCompletionProvider
public void setCompletionProvider(CodeCompletionProvider provider)Parameters
providerCodeCompletionProvider- the completion provider, or null to disable completion
setSingleLine
public void setSingleLine(boolean singleLine)#addKeyListener(ActionListener) instead, which decide what it does (end the edit,
move to the next field, pick from a list the application shows). Line numbers are
hidden, and a line break typed, pasted or set becomes a space.Parameters
singleLineboolean- true for one line
isSingleLine
public boolean isSingleLine()Returns
addKeyListener
public void addKeyListener(ActionListener l)ActionEvent#getKeyEvent() is one of #KEY_ENTER, #KEY_TAB,
#KEY_BACK_TAB, #KEY_ESCAPE, #KEY_UP, #KEY_DOWN and #KEY_F4.Parameters
lActionListener- the listener
removeKeyListener
public void removeKeyListener(ActionListener l)#addKeyListener(ActionListener).Parameters
lActionListener- the listener
getCompletionProvider
public CodeCompletionProvider getCompletionProvider()