ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

GMF: Beyond the Wizards
Pages: 1, 2

Defining the Creation Tool

You will find that when you open the coffee.gmftool file, there isn't really much there (see Figure 4).

Image of the coffee.gmftool file
Figure 4. View of the coffee.gmftool file

There are a few types of tools and groups that you can make, but for our purposes we are interested in a creation tool. Right click the Tool Group and select New Child->Creation Tool. In the Properties view, fill in the Title and Description. The Title will be displayed on the palette to give a hint of what the tool does. The Description is used to generate the tool tip that displays when you hover over the tool.

That is all you need to do to make a creation tool; how easy is that?!

Making the Mapping Definition

With the gmfgraph and gmftool finished, it is now time to define how they work together, as well as what pieces of the model are involved. The modeling for the coffee shop is done in the coffee.gmfmap file (Figure 5).

Image of the coffee.gmfmap file
Figure 5. View of the coffee.gmfmap file

The coffee machine will be represented here by a Top Node Reference, so create one by right-clicking on Mapping and selecting New Child->Top Node Reference. The only property to set on this element is the Containment Feature. From this list, select the name of the collection in which the CoffeeMachines will be contained, in this case it is shopCoffeeMachines.

Create a Node Mapping by right-clicking the new Top Node Reference and select New Child->Node Mapping. This node is where everything comes together; notice the three main properties:

  • Element: the class (from the model) this mapping is for
  • Diagram Node: the figure that should be used for displaying the model class selected in the Element property
  • Tool: the tool that should be used to create the model class selected in the Element property

Selecting CoffeeMachine for the Element tells GMF that it is managing an instance of a CoffeeMachine. Looking at the drop-down for the Tool selection is quite intuitive; select the tool that has the correct description for creating a CoffeeMachine. The Diagram Node is right out of the gmfgraph, select the CoffeeMachineNode.

The last step is adding a Label Mapping. This will show attributes from the model that will give more of a visual aid to the user so he will know what he is looking at. Right-click the Node Mapping and select New Child->Label Mapping. In the Properties, the Diagram Label is CoffeeMachineDiagramLabel. Seeing a pattern yet? The Features element is a selection from a list of properties from the domain model that you want displayed.

Generating the Code

Now that the hard part is all assembled, the only thing left is to let GMF do its heavy lifting by generating the code. Right-click on the coffee.gmfmap file and select Create generator model, and follow the wizard. This will make the last configuration file that actually generates the editor code. Finally, we can generate the code by right-clicking the coffee.gmfgen file and selecting Generate diagram code. With that, you can sit back and watch it generate and compile the application's code. Now you can run the application again and start adding coffee machines to your store.


These are the steps that you would repeat for each model class in your diagram. There are a handful of details that you have to fill in when defining your application, but there is so much that you do not have to do when GMF does all the heavy lifting. You will find yourself copying and pasting similar nodes and changing a few properties. Once you are comfortable with the mapping files, you will find yourself flying through the development of your plug-ins.

GMF is a very powerful framework that will do most of the repetitive, error-prone work, as well as give you many advanced features. If you spend a little time looking at what the initial wizards generate, you will quickly learn about other features that we did not talk about, such as model connections, custom figures, and various decorators for the editor. The best way to learn more about GMF is playing with the configuration files and seeing what happens.


As a quick side note, all of the Eclipse projects, including GMF, are distributed with the Common Public License (CPL) and the Eclipse Public License (EPL). When you write new plug-ins with GMF, you are free to distribute them under any license that you wish to use (as long as you are not modifying any Eclipse code). Please see the resources section for links to more information about Eclipse licensing.


Jeff Richley

Return to ONJava.com.