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.

  1. Create a new plug-in project called com.packtpub.e4.minimark.ui by going to File | New | Project | Plug-in project and fill in:
    1. Project name: com.packtpub.e4.minimark.ui
  2. Click on Next and fill in:
    1. ID: com.packtpub.e4.minimark.ui
    2. Version: 1.0.0.qualifier
    3. Name: Minimark
    4. Vendor: PACKTPUB
    5. Ensure that the Create an Activator option is selected
    6. Ensure that this plug-in will make contributions to the UI option is selected
    7. Ensure that the Create a Rich Client Application option is not selected
  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 MANIFEST.MF or plugin.xml files.
  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 option and it will prompt to add org.eclipse.ui.editors to the required dependencies).

    Tip

    If you see a warning saying Plug-ins declaring extension points must set the singleton directive to true, then go into the MANIFEST.MF file and add ;singleton:=true at the end of the line containing the Bundle-SymbolicName header. A quick fix is provided and can be chosen by clicking on the lightbulb in the margin or by pressing Ctrl + 1 when the cursor is on that line.

  6. Once the extension point has been added, select the name (editor) child of the org.eclipse.ui.editors extension point (if this is not added automatically, right-click on the extension point and select New | Editor from the menu to add a template extension point). Fill it in as follows:
    1. id: com.packtpub.e4.minimark.ui.minimarkeditor
    2. name: Minimark
    3. extensions: minimark
    4. class: com.packtpub.e4.minimark.ui.MinimarkEditor
  7. The resulting plugin.xml will look like:
    <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 bundle dependencies in the Dependencies tab:
    1. org.eclipse.jface.text: This provides text-processing libraries
    2. org.eclipse.ui.editors: This provides general editor support
    3. org.eclipse.ui.workbench.texteditor: This provides general text editor
  9. Use the File | New | Class wizard to create a MinimarkEditor class in the com.packtpub.e4.minimark.ui package as a subclass of AbstractTextEditor:
    public class MinimarkEditor extends AbstractTextEditor {
  10. Run the target 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:
    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 show changes in both editors). To resolve 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
3.144.161.116