Java Reference
In-Depth Information
Create and maintain a view
❍
Create and maintain child EditParts
❍
Create and maintain connection EditParts
❍
Support editing of the model
❍
As the controller,
EditPart
s are responsible for listening to model change
events and updating the view, and also for taking input from a user and updat-
ing the model. We cover both aspects in the following sections, beginning with a
description of how to create the graphical view.
9.2.2 Creating the Graphical View
Let's say that you have a model created in EMF and figures using Draw2d. Now
it's time to bring them together in a graphical editor. As indicated in the previous
section, you need to create
EditPart
s for each of the model elements you want
to render in a diagram. But first you need to set up a viewer for the Draw2d
FigureCanvas
. Typically, the GEF
ScrollingGraphicalViewer
is used with
a special root
EditPart
. This root
EditPart
does not correspond to a model
element; it provides a context for the remaining
EditPart
s and sets up the
viewer. The
ScalableFreeformRootEditPart
is typically used, while GMF
provides its own
DiagramRootEditPart
class to set up printing support and
discrete zoom levels.
Creating EditParts
With a viewer and the root
EditPart
established, we can populate the view
with content by passing in an
EditPartFactory
and our model. The
EditPartFactory
takes the base model object and creates the appropriate
EditPart
and adds it to the root, as seen in Figure 9-9. This
EditPart
is
known as the contents
EditPart
; do not confuse it with the “root
EditPart
,”
which, as stated earlier, has no relationship with the model. The contents
EditPart
creates its
Figure
, which is added to the root
Figure
.
The contents
EditPart
creates its children
EditPart
s by passing the
viewer's factory, which, in turn, is used to create their children, and so on. This
is done by calling
getModelChildren()
on itself, so the
EditPart
must know
what model elements require
EditPart
s to be created, although it is the respon-
sibility of the
EditPartFactory
to create them. Eventually, every model object
will be represented by an
EditPart
and corresponding
Figure
in the graphical
viewer. In summary, you need to implement both a contents
EditPart
that will
set up a diagram's backdrop figure and an
EditPartFactory
that is aware of
the model and can create the contents
EditPart
.
Search WWH ::
Custom Search