Monday, October 24, 2011

Reusing XML source editor

The Web Tools Platform Project from Eclipse features a nice XML editor which you can find at various places within the IDE. It is generally used to edit xml files, but you can also find it in the Plug-in Editor when editing the source of plugin.xml directly.

When you have your own config files that store xml content you can extend the default editor and bind it to your content. Basically you may achieve the same result by simply creating a new File Association (Preferences/General/Content Types) for your file type, but as we want to extend the editor in a later tutorial, we will provide an editor extension.

Source code for this tutorial is available on github as a single zip archive, as a Team Project Set or you can browse the files online.

Step 1: Defining content type

Create a new Plug-in project called com.codeandme.editor. Do not add an Activator or UI elements. Go to the Extensions tab of your plugin.xml and add a new extension for org.eclipse.core.contenttype.contentTypes. Right click and select New -> content-type.

Name your content type and select a unique id (we need that afterwards). Make sure the base-type is set to org.eclipse.core.runtime.xml and don't forget to add some file-extensions to bind that type to specific files.
 
Step 2: Creating the editor

Extend org.eclipse.ui.editors and create a new editor. Your editor needs a unique id and some nice name. Before we are going to implement the editor, we add a contentTypeBinding to it. Select the contentTypeId you entered before when we defined the content type.

Now go back to the editor definition and add a class com.codeandme.xmleditor.ExampleEditor with following source:
package com.example.editor;

import org.eclipse.wst.sse.ui.StructuredTextEditor;

public class ExampleEditor extends StructuredTextEditor {

    public ExampleEditor() {
    }
}

Once done your plugin.xml should have following content:
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         point="org.eclipse.core.contenttype.contentTypes">
      <content-type
            base-type="org.eclipse.core.runtime.xml"
            file-extensions="example"
            id="com.codeandme.xmleditor.content.example"
            name="Example Files"
            priority="normal">
      </content-type>
   </extension>
   <extension
         point="org.eclipse.ui.editors">
      <editor
            class="com.codeandme.xmleditor.ExampleEditor"
            id="com.codeandme.xmleditor.example"
            name="Example editor">
         <contentTypeBinding
               contentTypeId="com.codeandme.xmleditor.content.example">
         </contentTypeBinding>
      </editor>
   </extension>
</plugin>

It's easy as that. Now you have a basic XML source editor associated with your example files.

Step 3: Using the editor

When using the editor in your own RCP make sure you include the Plug-ins org.eclipse.wst.xml.core and org.eclipse.wst.xml.ui to your RCP.

5 comments:

  1. superb man.!!!!!!!! great job....

    ReplyDelete
  2. Awesom job! This is exactly what I was looking for. Great thanks!

    ReplyDelete
  3. Hi! Great post !
    I folowed this tutorial and got some errors when runned the editor as a plugin .
    Please can anyone help me with this ?

    org.eclipse.core.runtime.CoreException: Executable extension definition for "class" not found.
    at org.eclipse.core.internal.registry.ConfigurationElement.throwException(ConfigurationElement.java:62)
    at org.eclipse.core.internal.registry.ConfigurationElement.createExecutableExtension(ConfigurationElement.java:222)
    at org.eclipse.core.internal.registry.ConfigurationElementHandle.createExecutableExtension(ConfigurationElementHandle.java:55)
    at org.eclipse.ui.internal.WorkbenchPlugin$1.run(WorkbenchPlugin.java:273)
    at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
    at org.eclipse.ui.internal.WorkbenchPlugin.createExtension(WorkbenchPlugin.java:269)
    at org.eclipse.ui.internal.registry.EditorDescriptor.createEditor(EditorDescriptor.java:235)
    at org.eclipse.ui.internal.EditorReference.createPart(EditorReference.java:319)
    at org.eclipse.ui.internal.e4.compatibility.CompatibilityPart.createPart(CompatibilityPart.java:262)
    at org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor.createPart(CompatibilityEditor.java:61)
    at org.eclipse.ui.internal.e4.compatibility.CompatibilityPart.create(CompatibilityPart.java:299)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    ReplyDelete
    Replies
    1. Hi
      cannot help you with that stacktrace. Might be the case that some plugin is missing in your launch configuration.
      Best choice would be to download the sample source, try it out and check for differences in your implementation.

      Delete