rich client 2.0

Best-Practice: Developing a Model-Based Editor - Preparation


3. March 2006
Tom Seidel @ 09:35

Developing a Model-Based Editor - TOC

  1. Intro
  2. Preparation
  3. Editor-Inputs
  4. Editor-Implementation

The following describes the basics of creating editors.

Declaring the extension-point

In our example we want to provide an simple editor for the object org.javawiki.model.SubProcess and a MultiPageEditor with two pages for the object org.javawiki.model.Step. At first we define the extension points.

XML:
  1. contributorClass="org.javawiki.editors.ProcessEditorContributor"
  2.      default="false"
  3.      extensions="step"
  4.      icon="icons/sample.gif"
  5.      id="org.javawiki.editors.StepEditor"
  6.      name="StepEditor"/>
  7.      contributorClass="org.javawiki.editors.ProcessEditorContributor"
  8.      default="false"
  9.      extensions="subp"
  10.      icon="icons/sample.gif"
  11.      id="org.javawiki.editors.parts.SubProcessEditor"
  12.      name="SubProcessEditor"/>

Declaration in the plugin.xml

Managing the Object-Editor-Mapping

The assignment which object-signature is linked with a special editor is realized in the EditorContributor. This class knows the correct editor-id (specified in your plugin.xml) to a special Object.

JAVA:
  1. public static final String STEP_EDITOR_ID = "org.javawiki.editors.StepEditor"; //$NON-NLS-1$
  2. public static final String SUBPROCESS_EDITOR_ID = "org.javawiki.editors.parts.SubProcessEditor"; //$NON-NLS-1$
  3. private static Map editorMapping;
  4. static {
  5.      editorMapping = new HashMap();
  6.      editorMapping.put(Step.class,STEP_EDITOR_ID);
  7.      editorMapping.put(SubProcess.class,SUBPROCESS_EDITOR_ID);
  8. }
  9. /**
  10. * Returns the correct editor id by the given
  11. * class-signature     
  12. * @param clazz the class of the object that will be
  13. * edited with an Editor
  14. * @return the editor-id definded in the <code>plugin.xml</code>
  15. */
  16. public static String getEditorIdByClass(Class clazz) {
  17.      return (String) editorMapping.get(clazz);
  18. }

Open programmatically an editor

The command for opening an editor is:

JAVA:
  1. PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().openEditor(
  2.      myEditorInput,"editor.id.specified.in.plugin.xml");

In the Best-Practice example we wrote a wrapper for opening an editor, the ResourceHandler, that has a Method #openEditor(final AbstractBaseElement model) and a method for checking if the model is already represented by an open editor #getEditorIndex(IEditorInput input)

Open the editor on doubleClick on an item in the JFace-Viewer

Just add an IDoubleClickListener and call the ResourceHandler

JAVA:
  1. this.viewer.addDoubleClickListener(new IDoubleClickListener() {
  2.      public void doubleClick(DoubleClickEvent event) {
  3.           ResourceHandler.getDefault().openEditor(
  4.                (AbstractBaseElement) ((IStructuredSelection) event.getSelection()).getFirstElement());
  5.      }
  6. });

Conclusion

OK, what do we have? - We have two editors defined (without any implementation), the assignment from editor to model-object and the capability to open an editor on double-click on an elemnt in the JFace-Viewer. Now only the editors are missing.

Let's take the rest!!!

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress