Time for action – creating an editor

The example will be based on a (made-up) markup language called minimark, which is essentially a plain text file with blank delimited paragraphs that can be translated into an HTML file. This will involve creating an editor for text-based content, for minimark files. Perform the following steps:

  1. Create a new plug-in project called com.packtpub.e4.minimark.ui by going to File | New | Project | Plug-in project and filling in the following details:
    • Project name: com.packtpub.e4.minimark.ui
  2. Click on Next and fill in the following details:
    • ID: com.packtpub.e4.minimark.ui
    • Version: 1.0.0.qualifier
    • Name: Minimark
    • Vendor: PACKTPUB
    • Select the checkbox for Create an Activator
    • Select the checkbox for This plug-in will make contributions to the UI
    • Unselect the checkbox for Create a Rich Client Application
  3. Click on Finish and a new plug-in will be created.
  4. The next step is to create an editor for the minimark files. Open the plug-in's manifest by right-clicking on the project and selecting Plug-in Tools | Open Manifest or by double-clicking on the plugin.xml file.
  5. Go to the Extensions tab and click on Add. The extension points dialog will show; search for editors and it should show up in the list. (If it doesn't, uncheck the Show only extension points from the required plug-ins and it will prompt to add org.eclipse.ui.editors to the required dependencies.)
  6. Once the extension point has been added, right-click on the extension point and select New | Editor from the menu. This will add a template extension point; fill it in as follows:
    • id: com.packtpub.e4.minimark.ui.minimarkeditor
    • name: Minimark
    • extensions: minimark
    • class: com.packtpub.e4.minimark.ui.MinimarkEditor
  7. The resulting plugin.xml will look like the following code:
    <extension point="org.eclipse.ui.editors">
      <editor name="Minimark" extensions="minimark" default="false"class="com.packtpub.e4.minimark.ui.MinimarkEditor"id="com.packtpub.e4.minimark.ui.minimarkeditor"/>
    </extension>
  8. Now, add the required dependencies in the Dependencies tab:
    • org.eclipse.jface.text: Provides text-processing libraries
    • org.eclipse.ui.editors: The general editor support
    • org.eclipse.ui.workbench.texteditor: The general text editor
  9. Use the File | New | Class wizard to create MinimarkEditor in the com.packtpub.e4.minimark.ui package as a subclass of AbstractTextEditor:
    public class MinimarkEditor extends AbstractTextEditor {
    }
  10. Run the Eclipse instance, and create a project with File | New | Project | General Project called EditorTest. Then use the File | New | File to create a file called test.minimark. Double-click on this file and an error will be seen, as shown in the following screenshot:
    Time for action – creating an editor
  11. This happens because an editor needs to be hooked up to a document provider which synchronizes the content of the document with any other open editors. (This allows Eclipse to open multiple editors on the same file and still show the same changes in both.) To solve the error, add a constructor that sets the editor's document provider to a general TextFileDocumentProvider:
    import org.eclipse.ui.editors.text.TextFileDocumentProvider;
    public class MinimarkEditor extends AbstractTextEditor {
      public MinimarkEditor() {
        setDocumentProvider(new TextFileDocumentProvider());
      }
    }
  12. Run the Eclipse instance again, double-click on the test.minimark file and an empty text editor will be opened.

What just happened?

A basic text editor was created and associated with files ending in .minimark.

To add an editor type, the following bundles are needed:

  • org.eclipse.core.runtime
  • org.eclipse.jface.text
  • org.eclipse.ui
  • org.eclipse.ui.editors
  • org.eclipse.ui.workbench.texteditor

The editor needs to be a subtype of an EditorPart. In this case, AbstractTextEditor provides the basic functionality for editing text-based files. It also needs to be registered with the org.eclipse.ui.editors extension point.

Note that building editors and the document providers that underpin them is a book in its own right; the implementation of the editor here is to support the resource-processing examples. More information on writing custom editors is available in the online help.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset