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"?>
            name="Example Files"
            name="Example editor">

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.


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

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

  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(
    at org.eclipse.core.internal.registry.ConfigurationElement.createExecutableExtension(
    at org.eclipse.core.internal.registry.ConfigurationElementHandle.createExecutableExtension(
    at org.eclipse.ui.internal.WorkbenchPlugin$
    at org.eclipse.swt.custom.BusyIndicator.showWhile(
    at org.eclipse.ui.internal.WorkbenchPlugin.createExtension(
    at org.eclipse.ui.internal.registry.EditorDescriptor.createEditor(
    at org.eclipse.ui.internal.EditorReference.createPart(
    at org.eclipse.ui.internal.e4.compatibility.CompatibilityPart.createPart(
    at org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor.createPart(
    at org.eclipse.ui.internal.e4.compatibility.CompatibilityPart.create(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    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.