Chapter 3. Draw2D Infrastructure

This chapter discusses the architecture behind the code in the previous chapter. Before we dive deeper into every aspect of the program, it’s time to step back and look at Draw2D as a whole. The simple example started in Chapter 2—the GenealogyView—provides a concrete basis on which to discuss the Draw2D architecture.

3.1 Architecture

SWT provides the developer with all of the standard widgets and layouts needed to design and deliver rich client cross-platform applications. For information or data that would be best displayed graphically, however, SWT may not be the complete solution. This is where Draw2D comes into play for many applications. Draw2D is a lightweight drawing framework built on top of SWT that provides a set of standard figures and graphical layout algorithms to provide the developer with all of the tools necessary to convey complicated information and data to the end user (see Figure 3–1).

Figure 3–1. Draw2D Architecture.

image

Draw2D is a lightweight drawing framework that renders figures onto a SWT canvas. The SWT canvas and the SWT widgets that contain that canvas are “heavyweight,” meaning that each SWT widget has an OS-specific resource associated with it. In contrast, Draw2D objects are “lightweight,” meaning that these figures drawn by Draw2D are represented by a Java object and aren’t tied to an operating system resource.

The LightweightSystem (aka Lightweight Drawing System) coordinates all aspects of display and interaction for a particular Draw2D diagram. The UpdateManager tracks which areas of the diagram have changed and need to be redrawn, notifying only those figures that need to be redrawn to display themselves (see Section 3.2 on page 23 for more about the UpdateManager). As the user interacts with the diagram, the EventDispatcher routes SWT events, such as mouse and keyboard events, to the appropriate figures (see Section 3.3 on page 24 for more about the EventManager). The LightweightSystem also has a root figure that is the ancestor of all other figures. This root figure inherits settings such as the background and foreground colors from the canvas if they aren’t specifically set on the root figure.

Figures can be “nested” or “composed” of other figures so that complex figures can be built from simple geometric shapes and images (see Chapter 4 on page 27 for more about figures). In our example from the prior chapter, each person figure (RectangleFigure) has a child figure (Label) to display the person’s name within that figure. Each figure has a bounding box determining where the figure is painted and where the user clicks to interact with the figure. Rendering and user interaction with child figures is cropped to the parent figure’s bounding box.

Each figure also has a layout algorithm for specifying the position of any child figures within the parent figure (see Chapter 5 on page 55 for more about layout managers). In our example from the prior chapter, each person figure uses a ToolbarLayout layout manager to position the name figure within itself. This particular layout manager arranges figures in a single row or column. We also use the XYLayout in our example to statically position the person and marriage figures within the root figure.

Special figures can “connect” an anchor point on one figure to an anchor point on another figure (see Chapter 6 on page 69 for more about connections). In our example, we instantiate a PolylineConnection to connect each person figure to the marriage figure. We use the ChopboxAnchor to position the anchor point for the PolylineConnection along the bounding box of each figure so that the connecting line does not overlap the figure itself. A routing algorithm can be associated with a connection to reshape the line between two figures.

3.2 Drawing

Rather than redrawing the entire diagram every time, the UpdateManager tracks which areas of the diagram have changed and need to be redrawn, notifying only those figures that need to be redrawn to display themselves. By default, the LightweightSystem instantiates a simple update manager that draws directly on the canvas. While this is appropriate for some diagrams, if you have a diagram with many figures or that rapidly changes as a result of user interaction or external events, then the user may see the diagram flicker as the figures are drawn on the canvas. To alleviate this problem, our example uses the SWT.DOUBLE_BUFFERED style.

new Canvas(parent, SWT.DOUBLE_BUFFERED);

As a figure is dragged around the diagram, our example’s mouse listener (see Section 2.5 on page 17) must not only move the figure and update the layout information, but also notify the update manager that diagram needs to be redrawn (see Figure 3–2). Rather than indicating that the entire diagram needs to be redrawn, our listener marks both the figure’s original area and the figure’s new area as “dirty” by calling the update manager’s addDirtyRegion(...) method.

Figure 3–2. Genealogy view showing area to be redrawn.

image

3.3 Processing Events

Every LightweightSystem has an EventDispatcher which listens to SWT events on the canvas and notifies affected figures. While a custom EventDispatcher could be implemented and set to the LightweightSystem, by default an SWTEventDispatcher is created which provides enough support for most applications. This subclass dispatches all SWT events to the appropriate figures (see Figure 3–3). Draw2D provides a set of Listener and Event classes that the SWTEventDispatcher class creates and sends to the figures.

Figure 3–3. Event dispatching.

image

Whenever a figure receives an event from the EventDispatcher, it redirects that event to any listeners attached to it. In our example, we attached a mouse listener to each person figure and the marriage figure so that the user can drag those figures around the diagram (see Section 2.5 on page 17). There are a number of other types of listeners that can be attached to a figure for different purposes (see Table 3–1), but be aware that the higher-level GEF framework already provides much of this functionality such as dragging a figure around a diagram.

Table 3–1. Figure Listeners

images

3.4 Summary

Draw2D provides a lightweight framework on top of Eclipse’s SWT to allow developers to implement visual representations of data. The framework provides its own drawing and event infrastructure.

References

Chapter source (see Section 2.6 on page 20).

Clayberg, Eric, and Dan Rubel, Eclipse Plug-ins, Third Edition. Addison-Wesley, Boston, 2009.

GEF and Draw2D Plug-in Developer Guide, Eclipse Documentation (see http://help.eclipse.org/).

Moore, Bill, David Dean, Anna Gerber, Gunnar Wagenknecht, and Philippe Vanderheyden, Eclipse Development Using the Graphical Editing Framework and the Eclipse Modeling Framework. IBM Redbooks, February 2004.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.107.100