Home
Categories
Dictionary
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
You can also use the JEditor.setSyntax(String) method. For example:
      JEditor editor = new JEditor();
      editor.setSyntax("xml");
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);
      }
You can also use the JEditor.setSyntax(String) method. For example:
      private void openFile(File file) {
        JEditor editor = new JEditor();
        // get the appropriate syntax
        editor.setSyntax("xml");
      
        // 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, but also copy / paste or undo / redo actions.

Note that the JEditor.setUnchanged() method allow to force that the underlying document is unchanged. i 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.

Inserting or replacing text

It is possible to insert or replace it externally (for example, with a Popup menu with the following methods:
Main Article: Popup menu

It is possible to add a Popup menu which will appear on a right-click in the editor area.

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

For example:
      CodeEditorDefaults JEditor(defaults) = new CodeEditorDefaults();
      defaults.setPopup(new DefaultEditorPopup());

      JEditor ta = new JEditor(defaults);
will show:
defaultPopup

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

Main Article: CodeEditorDefaults

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