Tuesday, November 3, 2015

GEF4 Tutorial - Part 3 - Model tree and update

In step 3 of this tutorial, the model, the parts and the visuals are compositions of multiple objects. Each model node type is mapped with the ModelPartFactory to a corresponding content part (ModelPart and TextNodePart).

For the source of this tutorial step see github - gef4.mvc.tutorial3.



The model nodes implement PropertyChangeNotifier. When the model is changed, it can notify listeners. The TextNodeParts register themself as listener, when they are activated.

This example has a button on the top, that applies changes to the model. So the text and the position of the boxes is varied.








GEF4 Tutorial - Part 2 - Text box

Note: there is a GEF5 Tutorial

In this step 2 of the tutorial, the model shall be changed to hold only a position, a text and the color. The size of the rectangle surrounding the text is done automatically by the part.

For the source code see github gef4.mvc.tutorial2

The ModelPart is now extending AbstractFXContentPart<Group>, so the visual representation can be composed of a rounded rectangle and a text.

The ModelPart first creates the Text object, uses a "through away Scene" to be able to retrieve the measurements of the text, than uses this information to setup the rectangle bounds and text position.

MVC structure

Compared to step 1, the visual is now a composition of multiple objects.


The result







GEF4 Tutorial - Part 1 - Minimal MVC

Note: there is a GEF5 Tutorial

The source for this tutorial can be retrieved from github: gef4.mvc.tutorial1.

GEF4 is the new version (10-2015) of the Graphical Editing Framework in the Eclipse ecosystem. See https://wiki.eclipse.org/GEF/GEF4

I have special interest in the Model-View-Controller part of this framework, but at the time of my writing, there is only the "Logo" example available. I found it quite complex, for understanding the basic concepts. So I started to reduce it and thought others could have a benefit from my work. Let's see.

Tutorial step 1


The most trivial example for MVC, is having a model, a controller (in GEF4 speak "Part") and the view.
The model is a POJO, storing information only application specific information. Here i store the rectangle coordinates and the color, of what i want to visualize.

The part is a the ModelPart class, that is the one transforming the model information into a presentation.

GEF4 makes use of Google Guice. You may have a read about it to understand the concept: https://github.com/google/guice, see the linked video there and the user guide.

The only binding needed for this minimal example is the interface IContentPartFactory to the single instance of ModelPartFactory.
protected void configure() {
    super.configure();
    binder()
        .bind(new TypeLiteral<IContentPartFactory<Node>>(){})
        .toInstance(new ModelPartFactory());
}
The ModelPartFactory uses instanceof tests to check for the model types and create the appropriate part class instance. Also information of the model may be used to determine the correct type for the part.

The ModelPart extends AbstractFXContentPart<FXGeometryNode<RoundedRectangle>>.
Ok this is not easy. I want to draw a RoundedRectangle, which is a GEF4 geometry (not depending on FX). The FXGeometryNode is an adaption to have it as FX Path. The AbstractFXContentPart is an abstract content part implementation, acting on the given JavaFX node element, here the FXGeometryNode. 

MVC structure


There is a single model object that a single part transfers into a single visual object.
Information is only propagated in one direction.

The result

Now it looks like this:



Realize what you can do with it:
  • Mousewheel can do up/down left/right scrolling
  • Ctrl+Mousewheel is zooming
  • With the mouse, you can drag a mark rectangle open, but it is not yet marking