5.3 Using JET in GEF-based editor development

 < Day Day Up > 



5.3 Using JET in GEF-based editor development

In this section, we discuss how JET may be used to speed up development of an editor based on EMF and GEF.

We provide an example that generates skeletons for some classes that are used in a GEF editor, from a model. We can use the technique described in this section regardless of whether we take the approach described in 5.2, "Using an EMF model within a GEF-based application" on page 167, or whether we are using GEF to augment an EMF.Edit-based editor. You can flesh out the generated code into an application as described in Chapter 3, "Introduction to GEF" on page 87.

When developing your GEF-based application based on an EMF model, you will notice that you are usually creating many similar classes, for example, often you will create NodeEditParts for most of the classes in your model, perhaps using ConnectionEditParts for some of them. Often you will use a custom figure for your NodeEditParts. In the following example, we use JET templates to generate EditParts and Figures from classes in our model. This is a very basic example, to illustrate concepts. We do not provide a complete example due to time constraints, as the templates required to generate more complete implementations would be non-trivial. You would probably want to provide more detail in the templates if you wanted to generate EditParts specific to your application.

Refer to the JET Tutorial, Part one for an introduction to using JET. We use a similar process to the example described to generate our skeleton EditParts and Figures from the WorkflowModel. We take the following steps:

  1. To begin with, we create a project, and add a JET Nature to the project from the right-click context menu. This sets up the template directory.

  2. In the template directory, we create a new file NodeEditPart.javajet.

  3. We edit the NodeEditPart to create all of the required methods. We base the content of the template on the NetworkNodeEditPart from the NetworkEditor described in 5.2, "Using an EMF model within a GEF-based application" on page 167. Example 5-12 shows an excerpt from the template. Our example only really uses the name of the class so far to generate the skeleton, however you could use methods on the EClass to get more detail. For example, you might want to generate a skeleton notifyChanged() method with a switch that selected from all of the features of the class.

    Example 5-12: NodeEditPart template

    start example
     <%@ jet package="com.ibm.itso.sal330r.codegen" imports="org.eclipse.emf.ecore.*"  %> <%EClass eClass = (EClass) argument;%> ... imports ... <%String name = eClass.getName();%> public class <%=name%>EditPart    extends AbstractGraphicalEditPart    implements NodeEditPart, Adapter {    private IPropertySource propertySource = null;    private Notifier target;     public <%=name%>EditPart(<%=name%> o)     {       setModel(o);     }    public <%=name%> get<%=name%>() {     return (<%=name%>)getModel();     }    /* (non-Javadoc)     * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#getModelSourceConnections()     */    protected List getModelSourceConnections() {       // TODO: implement to return the objects represented by the connections sourcing from this node       throw new UnsupportedOperationException();    }    /* (non-Javadoc)     * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#getModelTargetConnections() ... } 
    end example

  4. We also create NodeFigure.javajet, to generate a figure for each EditPart.

  5. We change the JET properties for our project to ensure that the translated templates are compiled into the src directory. To do this, we open the properties of the project, select JET Settings, and then set Source Container to src.

  6. We compile each template by selecting the template and then selecting Compile Template from the right-click context menu. Now, we should see the translated templates appear in the src directory.

  7. We create a class EditPartGenerator in the com.ibm.itso.sal330r.codegen package that was created for the translated templates.

  8. In the main method of EditPartGenerator, we add code to get classes from the model, and use them as arguments to the generate() method of our compiled templates. Example 5-13 shows the code that we add to facilitate this. Note that we must use the init() method on the NetworkPackage to initialize it before use.

    Example 5-13: Using the templates

    start example
     NodeEditPartTemplate n = new NodeEditPartTemplate(); NodeFigureTemplate f = new NodeFigureTemplate(); WorkflowPackageImpl.init(); Map registry = EPackage.Registry.INSTANCE; String workflowURI = WorkflowPackage.eNS_URI; WorkflowPackage workflowPackage = (WorkflowPackage) registry.get(workflowURI); // Generate TaskEditPart EClass taskClass = workflowPackage.getTask(); String result = n.generate(taskClass)); 
    end example

  9. The result of calling generate() on the template is a string containing the text generated from the template. In our simple example, we print this to System.out, however if you were really generating code, you would want to create a resource containing the contents of the String.

  10. If you run the EditPartGenerator as a Java application, you will see the resulting code printed to the console.

Using a similar approach to the EMF codegen for the model, edit, and editor plug-ins, you could generate a generic graphical editor for any model using JET. You would probably want to use your own GenModel to represent options such as whether a class maps to a Node or Connection EditPart, whether it can contain other nodes, and possibly also to specify the type of Figure used to represent the class. You could then generate from instances of that model rather than from the application model directly.



 < Day Day Up > 



Eclipse Development using the Graphical Editing Framework and the Eclipse Modeling Framework
Eclipse Development Using the Graphical Editing Framework And the Eclipse Modeling Framework
ISBN: 0738453161
EAN: 2147483647
Year: 2004
Pages: 70
Authors: IBM Redbooks

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net