Home
Categories
Dictionnary
Download
Project Details
Changes Log
License

Code Editor component



The JEditor class is a code Editor component which can be integrated in any Swing application.

Overview

To create a JEditor, you just need to call:
      JEditor editor = new JEditor();

By default the JEditor component will use a default text syntax, but you can use another one by calling JEditor.setTokenMarker(TokenMarker). TokenMarkers can be created directly or using the SyntaxMapper class[1] .

For example:

      JEditor editor = new JEditor();
      SyntaxMapper mapper = SyntaxMapper.getInstance();
      TokenMarker tk = mapper.createTokenMarker("java");
      editor.setTokenMarker(tk);


jeditor


The editor manage its own scrolling, so you don't need to add the editor in a JScrollPane. However it is possible to add the editor in a JScrollPane in a Swing hierarchy, but it will not work correctly in all cases.

Contextual actions

  • CTRL+V denotes a copy
  • CTRL+X denotes a cut
  • CTRL+C denotes a paste
  • CTRL+A denotes a select all
  • CTRL+F starts a search
  • CTRL+Z denotes an Undo
  • CTRL+Y denotes a Redo


Note that it is possible to access the cut / copy / paste / search options by setting the CodeEditorDefaults.setPopup(JPopupMenu) (see Popup menu).

Setting the appropriate syntax

The JEditor.setTokenMarker(TokenMarker) allows to set the syntax used by the editor. See syntax highlighting for informations on how to get the appropriate syntax for a file.

For example if you want to get the appropriate syntax for a file:

      private void openFile(File file) {
        JEditor editor = new JEditor();
        // get the appropriate syntax
        SyntaxMapper mapper = SyntaxMapper.getInstance();
        Syntax syntax = mapper.getSyntaxForExtension(file);
        TokenMarker tk = syntax.createTokenMarker();
        editor.setTokenMarker(tk);
      
        // set the editor text content
        editor.setText(file);
      }

Undo / Redo

The JEditor.undo() and JEditor.redo() allow to apply an undo or redo.

Listening to the underlying document modifications

It is possible for an application which uses the JEditor to listen to the modifications of the underlying document by JEditor.addEditorStateListener(EditorStateListener): The state of modification can be modified by edits, bu also copy / paste or undo / redo actions. Note that the JEditor.setUnchanged() method allow to force that the underlying document is unchanged. it can be useful if you allow to save the content in a file, and you want to specify that the document is in an unchanged state after saving.

Listening to editor exceptions

It is possible to listen to the editor exceptions encountered during the component usage by JEditor.addExceptionListener(EditorExceptionListener).

By default, these exceptions will generally print the StackTrace of the exception. Adding a listener allows to handle them by the caller for example. Settings a CodeEditorDefaults.setPopup(JPopupMenu) value for the CodeEditorDefaults class allows to define a Popup which will be shown when right-clicking on the Editor. Any subclass of the AbstractEditorPopup class can be used for the Popup.

The DefaultEditorPopup class allows to show by default a Cut/ Copy / Paste and Search options.

For example:

      defaults.setPopup(new DefaultEditorPopup());

      // create CodeEditor
      JEditor ta = new JEditor(defaults);

will show:
defaultPopup


Note that setting the value true for the CodeEditorDefaults.hasPopup value will also show the DefaultEditorPopup. For example, the following code will also allow to show the same Popup:

      defaults.hasPopup = true;

      // create CodeEditor
      JEditor ta = new JEditor(defaults);

Editor customization

There are several ways to customize the component:
  • Specify the presentation of the syntax and the Editor
  • Highlight specified lines in the text
  • Customize the Gutter

Syntax and editor presentation

The CodeEditorDefaults class allows to specify how the editor will be presented:
  • Color for the keywords and comments
  • Used Fonts and Colors
  • Caret customization
  • etc...
For example:
      CodeEditorDefaults defaults = new CodeEditorDefaults();
      defaults.setStyle(Token.KEYWORD1, Color.BLUE, false, true);
      defaults.setStyle(Token.KEYWORD2, Color.BLUE, false, true);
      defaults.setStyle(Token.KEYWORD3, Color.BLUE, false, true);
      defaults.setStyle(Token.COMMENT1, Color.DARK_GRAY, false, false);
      defaults.setStyle(Token.COMMENT2, Color.DARK_GRAY, false, false);
      defaults.eolMarkers = false;
      defaults.paintInvalid = false;

      // create CodeEditor
      JEditor ta = new JEditor(defaults);

Lines highlighting

The CodeEditorHighlighter class specifies if some lines should be specifically highlighted. For example:

      CodeEditorHighlighter highlighter = new CodeEditorHighlighter();
      highlighter.append(1, Color.GREEN); // 1st line in Green
      highlighter.append(3, Color.RED); // 3rd line in Red

      // create CodeEditor
      JEditor ta = new JEditor(defaults);
      // sets the Highlighter
      ta.setHighlighter(highlighter);

We will have the following result:


customhighlights

Gutter customization

Main Article: Gutter

It is possible to customize the gutter, as for example:
  • Add additional images or drawings in the gutter alongside the line numbers
  • Show a Popup by clicking on the gutter

Notes


Categories: component

Copyright 2016-2019 Herve Girod. All Rights Reserved. Documentation and source under the BSD licence