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

March 2nd, 2006

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.  
  2.  
  3.  
  4. contributorClass="org.javawiki.editors.ProcessEditorContributor"
  5. default="false"
  6. extensions="step"
  7. icon="icons/sample.gif"
  8. id="org.javawiki.editors.StepEditor"
  9. name="StepEditor"/>
  10.  
  11. contributorClass="org.javawiki.editors.ProcessEditorContributor"
  12. default="false"
  13. extensions="subp"
  14. icon="icons/sample.gif"
  15. id="org.javawiki.editors.parts.SubProcessEditor"
  16. name="SubProcessEditor"/>
  17.  
  18.  

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

Open programmatically an editor

The command for opening an editor is:

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

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.  
  2. this.viewer.addDoubleClickListener(new IDoubleClickListener() {
  3. public void doubleClick(DoubleClickEvent event) {
  4. ResourceHandler.getDefault().openEditor(
  5. (AbstractBaseElement) ((IStructuredSelection) event.getSelection()).getFirstElement());
  6. }
  7. });
  8.  

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!!!

Entry Filed under: Editors

3 Comments Add your own

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

March 2006
M T W T F S S
« Jan   Apr »
 12345
6789101112
13141516171819
20212223242526
2728293031  

Most Recent Posts