Eclipse GMF: a graphical editor for EMF models
Join the DZone community and get the full member experience.Join For Free
now we are going to see how to generate a complete graphical editor for the emf model we created in preceding tutorials, library.ecore .
a s the official wiki states, gmf “provides a generative component and runtime infrastructure for developing graphical editors based on emf and gef ”, that is it acts like a bridge between emf (that allows the model definition) and gef (a lightweight graphical framework, based on mvc architecture), generating the complete infrastructure needed to coordinate the model’s objects lifecycle (emf eobject) and their corresponding graphical representation (gef editpart).
to use gmf in our projects, we need to install the gmf tooling component ; the fastest and easiest way to do this is to select (from main menu of our eclipse installation) help >> install modeling components :
in next screen, we choose “ gmf modeling framework tooling ”:
we continue by clicking on “finish”, accept the default settings and then we click “restart now” to complete the installation.
now we are ready to start developing our editor; for this purpose we will use a tool included in gmf tooling: the gmf dashboard . to display it, we select window >> show view >> other >> gmf dashboard :
after this operation, our ide is looking like this:
let’s take a minute to examine this new tool: dashboard exists to guide the developer in the process of generating the correct configuration file of a project gmf, allowing you to generate all the files and the code needed following an iterative process (very similar to that already seen for emf model generation ) starting from the template file (*. ecore) and ends with the creation of the build file of the diagram (*. gmfgen); this process can be performed as many times as you want to gradually refine the final result.
at this point we have two choices: we can create a new project (gmf project type, category that now we will find in the list of wizards) or continue to develop the original emf project; we will follow this second path, as the simplest and most suitable way for this tutorial.
then select the project it.rcpvision.rcptutorial.model in the package explorer window; in the dashboard, domain model pane, click on select: this opens a dialog box that allows us to select the template file; for in our workspace there is only one file with these characteristics, we find preset:
we accept the proposals and continue clicking ok; the next step is to create (clicking on the button derive) or selection (if already exist) to the *. genmodel file: since we have already created in the early stages of the tutorial on emf, in this phase we click on the domain gen model pane, select option, and accept the default already identified by the tool:
at this point, clicking on the button derive above the domain model pane, we proceed with the creation of the *. gmfgraph:
we accept the default values for the file name and destination folder, and continue clicking on the next button; we will get a dialog box that asks the uri of the template file and which element we want to set as the diagram element:
to understand this request we must come a time in the generation logic of the editor that we are going to create : the tooling creates an editor with (potentially) many diagrams, in which you can create, edit and delete graphic objects that are instances of model objects … but the diagram itself corresponds to which object? as a rule (but not always, and not necessarily) is sufficient to match the chart with “root” object of our model; fortunately, since the model that we are running is very simple, so simple is the answer to the question, then select, as diagram element, the library object. as the editor we’ll get to the end of the process can handle n diagrams, this implies that for each diagram we will open again, we will be implicitly creating a new instance of the library object. we continue by clicking on the next button will open a new window:
in this window we are asked to declare which objects and properties of the model we want to be in the form of nodes (plane figures), connections (lines) and labels (text labels); then as selected in the window, we plot the author and book objects represented as figures, author’s name and title of the book represented by labels and finally the relationship between book and author represented as a connection. we accept the proposals and continue clicking on finish: this causes library.gmfgraph file creation and its opening with a dedicated editor
we continue by clicking on the button derive beneath the box domain model: this causes the opening of a window where we can choose the destination folder and the name of file *. gmftool:
the gmftool model is the file that defines what and how many tools we want to create to appear in the palette that will be generated in our editor; accepting the defaults and clicking next will open another window of choice:
we are again asked the uri of the model and the diagram element: confirm the library object and click next, open the following window:
the tools that appear in the palette are of two types: creationtool (corresponding to nodes) and connectiontool (corresponding to connections): as the default, which gives us the tool is perfectly suited to our needs, we confirm with finish, this involves the creation and the opening (with dedicated editor) library.gmftool file:
now click on the combine button to the right of the box domain model: it will get the opening of the window generating the *. gmfmap:
we accept the defaults and continue clicking next: there is the proposed new window to confirm the model, the diagram element and the package
click on next, the window appears in which we confirm that we intend to use librarypalette:
clicking on next, the next screen asks us confirmation of graphics:
finally, the last screen gives us the final mapping between objects and images
because the default is what we want, we finish the process by clicking finish: this causes the creation and opening of file library.gmfmap:
this causes file creation library.gmfgen
and finally from the dashboard, box diagram editor gen model , we click the item generate diagram editor
this operation starts the process of generating a new project, it.rcpvision.rcptutorial.diagram ; the end, our ide looks like this:
finally we are ready to see the result of our effort: we open the plugin.xml file from the new project, the overview tab, testing section, select the launch an eclipse application link e. .. voila, here is the result of our efforts!
let’s look at work: from the file menu choose new>> diagram library, starts a wizard that asks us to name the files that contain the diagram model and the domain model
clicking finish will have the following result:
and, using the palette, we can finally begin to populate our model.
not bad for not having written a single line of code, don’t you think? sure, the graphic representation of the objects could be better, we have those pesky icons next to text labels, the names of the tools in the palette are little explanatory… in the next installment we will explore some of the endless possibilities of “customization” offered to us by the gmf tooling, going to change these and other imperfections, and will explore the infinite capacity to regenerate the gmf code.
Opinions expressed by DZone contributors are their own.