GEF entry series (1, DRAW2D)

xiaoxiao2021-03-05  21

About Java2D believes that everyone will not be unfamiliar, it is based on AWT / SWING 2D graphics handle, and the sample program comes with JDK shows us a very powerful graphics processing capability of Java2D. Before Draw2D, the SWT application has been in this regard, while the Java2D in the Draw2D in this SWT changed this situation.

Many people may not know how to understand the relationship with GEF and DRAW2D: Some applications use Draw2D to look similar appearances with the GEF application. The reason is what, let's explain it first:

GEF is a graphic editing framework with a standard MVC (Model-View-Control) structure, where model is designed by our own business, it is able to provide a mechanism for change the notification of a model to tell the CONTROL layer; The Control layer is implemented by some EditPart, and EditPart is the core component of the entire GEF. About the mechanism and feature of EditPart will introduce in the posts; and the view layer (most case) is what we have to say here, its role is Draw the Model to the user in a graphical manner.

Although GEF can use any graphic bag as a View layer, it is actually a strong dependence on Draw2D. For example: Although the editpart (org.eclipse.gef.editpart) interface does not require an introduction of any Draw2D class, the CreateFigure () method of the AbstractGraphicaleditPart class we are using is required to return the iFigure type. For this reason, the SDK center of SDK in Gef contains the Draw2D package is not surprising, the same truth, only to understand Draw2D can master Gef.

Thus, the problem proposed in the beginning can be summarized as follows: DRAW2D is a SWT-based graphics processing package that is suitable for use as a View layer of GEF. If an application only needs to display graphics, only DRAW2D is enough; if the model of the application requires editing in a graphical manner, it is best to use the GEF framework.

Let us now take a look at what Draw2D, please see the picture below.

Figure 1 Draw2D structure

Draw2D is connected to a CANVAS instance called LightWeightSystem (hereinafter referred to as LWS). This Canvas is usually the application's shell in the Draw2D application. In the GEF application, more editor's Control (CREATEPARTCONTROL () Parameters in the method, although we can't see the existence of LWS, but all other graphics that can be seen are placed in it, these graphics form a tree-like hierarchy structure.

LWS is the core component of Draw2D, which contains three main components: rootfigure is the root of all graphics in LWS, which means that other graphics are directly or indirectly placed in rootfigure; EventDispatcher assists various events on Canvas. Rootfigure, which will eventually be assigned to the appropriate graph, please note that this rootfigure and the top level of iFigure in your application are not the same object, the former is not used inside the LWS, and the latter usually is a visible Canvas, it is placed directly in the former; UpdateManager is used to redraw graphics, and when CANVAS is asked to redraw, LWS calls its PerformUpdate () method.

LWS is a bridge that connects to SWT and DRAW2D, which uses it, we can easily create arbitrary shape graphics (not limited to rectangular), but also save system resources (because it is a lightweight component). A typical pure DRAW2D application code has the following structure: //

Create SWT CANVAS (Shell is the subclass of Canvas)

Shell shell

=

New

Shell (); shell.Open (); shell.settext

"

A Draw2D Application

"

);

//

Create LightWeightSystem, put on the shell

Lightweightsystem LWS

=

New

Lightweightsystem (shell);

//

Create the top graph in the application

IFigure Panel

=

New

Figure (); panel.setlayoutmanager

New

FlowLayout ());

//

Place this graphic in the Rootfigure of Lightweightsystem

LWS.SETCONTENTS (PANEL);

//

Create other graphics in your application and place it in the top graphics of your application

panel.add ();

While

(

!

Shell.isdisposed ()) {

IF

(

!

Display.readanddispatch ()) Display.sleep ();

Next, the graphics in Draw2D fully implements the iFigure (org.eclipse.draw2d.ifigure) interface, which is not just a shape on the screen you see, in addition to the size of the graphics, you You can also listen to events on the graph (mouse event, graphic structure change, etc., EventDispatcher from LWS), set the mouse pointer shape, let the graphic transparent, focus, etc., each graphic even has its own tooltip, very flexible .

Draw2D provides a lot of default graphics, the most common there are three categories: shape, such as rectangular, triangular, elliptical, etc., 2, control (Widget), such as tags, buttons, scroll bars, etc. Layer, they are used to provide scaling, scrolling functions in the graphic placed, in which GridLayer and Guideer are used to implement the "Adsorption to Grid" function in the 3.0 version of GEF. There are quite a few classes under the class trees that iFigure as root nodes, but I personally feel organized some confusion. Fortunately, we only use the part of commonly used.

Figure 2 A DRAW2D application

Each graphic can have a border (Border), the border type provided by Draw2D has GroupBoxBorder, Titlebarborder, ImageBorder, ButtonBorder, and CompoundBorder, as you can combine two borders. The border is occupied in the graph, which contains four integer values ​​up and down.

We know that a graphic can contain many sub-graphics, which must be arranged in some way when it is displayed, and is responsible for this task is the LayoutManager of the parent graphics. Similarly, Draw2D has provided us with a series of LayoutManager that can be used directly, such as FlowLayout is suitable for use in the table format arrangement, XYLAYOUT is suitable for users to use the mouse to change the graphic position, and so on. If you don't have a LayoutManager that is suitable for us, you can customize it yourself. Each LayoutManager contains an algorithm that considers the constraint object associated with each sub-graph, calculates the final position and size of the sub-graphic. A common task of graphical applications is to connect between two graphics, imagine a variety of connectors in the UML class diagram, or the lines of the data stream in the program flow chart, which have different appearances, some connecting lines Also display name, and it is best not to cross. With the Router, Anchor and Locator in Draw2D, you can implement a variety of connectivity styles, where ROUTER is responsible for the appearance and operation of the cable, the simplest is to set ROUTER to NULL (no ROUTER), which uses linear connection, other connection methods Including a folding line, a folding line with a control point, etc. (see Figure 3), if you want to control the connection line does not cross each other, you need to be in the article as an article in the router. Anchor Controls the position of the connection line on the graph, that is, "anchor" position, which is most readily used by ChopBoxAnchor, first assumes that the graphics center is the connection point, and then calculates the intersection of this imaginary connection with the graphical edge as the actual Anchor, other Anchor and EllipseAnchor, Labelanchor and Xyanchor, etc.; Locate this label, other Locator also has arrowlocator to locate rotatable modifications (Decoration, such as polygondecoration), bendPointerLocator is used to locate the connection control point, and the connectionndPointLocator is used to locate the connection endpoint (via the specified UDistance and Vdistance property can set with endpoints The coordinate of origin).

Figure 3 Three of the exterior of ROUTER

In addition, Draw2d provides several convenient types in org.eclipse.draw2d.geometry packages, such as Dimension, Rectangle, INSETS, POINT, and POINTLIST, etc., which are widely used inside Draw2D or by developers. To simplify the calculation. For example, Rectangle represents a rectangular area, which provides the GetIntersEction () method to easily calculate the overlap region of the area and another rectangular area, the GetTransposed () method can obtain a rectangular area after the long-wide value exchange, the scale () method Stretching of rectangles, etc. When you implement LayoutManager yourself, these classes are more recommended because they will involve more complex geometries.

The above introduces most of the features provided by Draw2D, using these we have been able to draw a very beautiful graphic. But for most practical applications, this is still far enough, we have to edit it and reflect the modification of the graph to the model. For the pretty completion of this arduous task, GEF is definitely not two. From the next beginning, we will officially enter the world of GEF.

Reference:

转载请注明原文地址:https://www.9cbs.com/read-38789.html

New Post(0)