1. Draw2D design ideas
Draw2D is a single-level component system in a host in the SWT Composite control. A DRAW2D application consists of a SWT Composite control, a lightweight system, and its content (Figures). Figures is the construction block of Draw2D. The following "Hello World" example program demonstrates how to implement a simplest DRAW2D program.
Listing for "Hello World"
Import org.eclipse.swt.widgets.shell;
Import org.eclipse.swt.widgets.display;
Import org.eclipse.swt.swt;
Import org.eclipse.draw2d. *;
Import org.eclipse.draw2d.geometry. *;
Public class helloworld {
Public static void main (string args []) {
Shell shell = new shell ();
shell.open ();
Shell.Settext ("Draw2D Hello World");
Lightweightsystem LWS = New Lightweightsystem (shell);
IFigure lael = new label ("Hello World");
LWS.SETCONTENTS (Label); // Setting content
Display display = display.getDefault ();
While (! shell.isdisposed ()) {
IF (! display.readDispatch ())
Display.sleep ();
}
}
}
The result of running this program is as follows:
What happened to the back of the above example program? First look at the Draw2D design:
LightWeightSystem is Draw2D binder. Developers provide SWT Composite and the root that wants to draw, then LightWeightSystem uses the default value to start the remaining process. Lightweightsystem relies on Canvas and contains EventDispatcher and an UpdateManager. Lightweightsystem itself is an event listener that can monitor multiple events. When the Canvas is transmitted to LightweightSystem (typically by the constructor LightweightSystem), LightweightSystem will register itself as a listener to the Canvas, Canvas generated when various events LightweightSystem interest, as a listener, the method is defined in LightweightSystem transfer. In the respective listener methods defined in LightWeightSystem, you want to convert events from SWT to Draw2D events and assign DRAW2D events to the currently selected graphic elements figure through EventDispatcher. Each graphic elementality can be used as a listener container.
SWT is a set of GUI components developed by IBM, and Swing in Java is the same class, but developers are different. There is a class in the SWT, this class is a control, according to its name, as the name suggests, it provides a drawing surface for the drawing; it triggered a variety of mouse events and keyboard events, it is a listener container, you can Send it to an interactive event to an interested listener.
LightWeightSystem is a very important class in Draw2D, which is the scheduling center of Draw2D, which is: 1, hold the reference to the Canvas control.
2. Make itself an interactive event listener and register itself into Canvas; when various interactive events are triggered in Canvas, LightWeightSystem can be notified.
3. Hold event transponders. When LightWeightSystem After obtaining an interactive event notification from Canvas, it transfers the event directly to the event transponder, and the event transponder first converts the system event into Draw2D custom internal events and then distributes the event according to the status of Draw2D.
4, hold the update manager. When Canvas is invalid and re-drawing, the LightWeightSystem will get the invalid rectangular area from the CANVAS's redraw event and update the manager to update the invalid rectangular area; when a graphic element in DRAW2D is invalid, DRAW2D will be updated. Manager updates invalid graphic elements. However, the graphic element is invalid and the control is two concepts; when the control is invalid, the control will trigger an onpaint event; when the graphic element is invalid, the system does not trigger an onpaint event.
5, hold root graphic elements (rootfigure). The root graphic element has a very important status and role in Draw2D, understanding the status and role of root graphic elements, which is very important to understand DRAW2D. The root graphic element completely covers on Canvas (as large as the size of Canvas's work area), its background color determines the background color of the application. The root graphic element is in the topmost end of the graphic element hierarchy, and it has only one child, this child is to display the content.
Another very important interface in Draw2D is iFigure, which represents graphical elements, all of which can be implemented with graphic display. Figure is the basic implementation of iFigure IFIGURE. The Figure design is related to a very famous mode: Composite mode.
In Draw2D, all coordinates in Figures are int type, no Float or Double type. I was also very strange at the time, but I thought about it. It is actually understandable. As for the specific reasons, I will explain in detail in future articles.
Draw2d only provides a view class that displays a model, does not provide any feature related to editing; if the developer intends to develop a graphic display software that does not need to perform editing of the model, then use DRAW2D is appropriate; if you want to perform editing action, Just use GEF and DRAW2D at the same time.
2. Several related modes
2.1 MVC mode
When it comes to the MVC mode, almost all developers will say, I know, is it "Model - View-Controller"? I know that m represents Model, V represents the view, and c represents the Controller. The model is responsible for providing data, and the view is responsible for rendering models (usually using data in the graphic element representation), the controller is responsible for creating graphical elements and modifying the model.
So, saying, MVC is a very simple thing, isn't it to separate data, graphics, control? But it is easy to say that it is not easy to do. When specifically proceeds, the usual situation is that models, views, and controllers are three integration, often a class representing three things. A class is both a model, and a view, but also a controller. In Draw2D, a large number of models-view concepts, but there is no controller, because the main purpose of the Draw2D library is to display models, as for how to edit models are GEF duties, how to define models and map models to DRAW2D is application software Developer's color duties. All graphical elements defined in Draw2D are all view classes, they are used to present models; when using Draw2D with GEF, you can experience the meaning of "Figure is a view" in Draw2D. . Designing a MVC-based application framework relative to design a MVC-based class level, it is necessary to have much larger; you need to be superior design levels on different layers in the framework. GEF and DRAW2D are based on the MVC design framework, GEF is a control layer, and DRAW2D is a display layer. Regarding GEF, I will introduce in the later article, GEF design is very exciting.
I have a model in Draw2D - view design Impression is very deep: range model and scroll bar. For a rolling bar, it involves: minimum range, maximum range, current value, thumb size, page increment. In Draw2D, an interface RangeModel defines these data and is a listener. Rolling bars hold range model and register itself as a listener into a range model. When the scroll bar is scrolling, it modifies the range model, then the range model will notify the other external objects that are interested in the range model data; when the data of the range model is changed by the external object, it notifies the scroll bar to change its own display. The data remains with the range model.
In the later article, I will focus on the viewport. The viewport uses the range model very exciting to communicate with the scroll bar and keep synchronization. It should be pointed out that the scroll strips described here are actually a view and controller-based synthesis, which is the merger of both the view and the controller.
In Draw2D, the MVC design mode is more.
2.2 Composite mode
The graphic element class level in Draw2D uses Composite mode. The graphical element is used to represent the graphical element, indicating the basic implementation of Ifigure, and contains the iFigure interface. The Motivation of the Composite mode is: combine the object into a tree structure to represent a "part-overall" hierarchy, composite enables the user to consistency for the use of a single object and combination objects.
The GUI control library in .NET is the Composite design mode, because there is a significant part and the overall relationship between the various controls, such as the FORM class derive from Control, but it is clear that it is a control container, which can contain various Control. Draw2d is a lightweight Widget component system that provides almost varied graphical elements (such as buttons, scroll bars) that are almost common to control pairs, etc., but these graphic element components are not controls, they are just from figure Delicious graphic elements, this is why Draw2D is called a lightweight Widget system. Because Draw2D is a component library, there is a "part-overall" relationship in nature, so the graphic elements in Draw2D will naturally adopt the Composite mode.
Because the Composite mode is used, in a DRAW2D application, the graphic element is tree-shaped, strict, there is only one graphic element in one application, which represents the content to display in the application (called The content graphic element); the content graphic element can contain children, and the child can contain children ..., the children are endless. Draw2d graphic element design
2
. 3 observer mode
This mode has been raised in Java to a very important location, so that the Java class library supports it. The event handling mechanism in Java is based on the observer mode, because Draw2D is a set of lightweight Widget component systems, so graphical elements can lead to events and can receive events.