Draw2d--1. Design ideas and related patterns

Source: Internet
Author: User
Tags event listener implement interface range requires valid
Design 1. draw2d Design Idea draw2d is a lightweight component (Widge) system that hosts the SWT composite control. A draw2d application consists of an SWT composite control, a lightweight system, and its contents (figures). Figures is the building block of draw2d. The following "Hello World" example program demonstrates how to implement one of the simplest draw2d programs. 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 label = new label ("Hello World");

Lws.setcontents (label); Set content

Display display = Display.getdefault ();

while (!shell.isdisposed ()) {

if (!display.readanddispatch ())

Display.sleep ();

}

}

}

The results of running this program are as follows: What happened to the back of the above example program? First look at the following draw2d design:





Lightweightsystem is the draw2d adhesive. The developer provides SWT composite and the root of the figure hierarchy that you want to draw, and then Lightweightsystem starts the remaining process with the default values. Lightweightsystem relies on canvas, and contains a eventdispatcher and a updatemanager. Lightweightsystem itself is an event listener capable of listening to multiple events. When passing canvas to Lightweightsystem (usually through the Lightweightsystem constructor), Lightweightsystem registers itself as a listener to canvas, As a listener, the method defined in Lightweightsystem is invoked when canvas produces events of various lightweightsystem interest. In each listener method defined in Lightweightsystem, the event from SWT is converted to a draw2d event and the Draw2d event is assigned to the currently selected graphical element figure by Eventdispatcher. Each graphical element figure can be used as a listener container.

SWT is a set of GUI components developed by IBM and is the same thing as swing in Java, except that the developers are different. In SWT there is a class of Fame canvas, a control that, according to its name, provides a drawing surface for a drawing; it raises various mouse events and keyboard events, a listener container that sends the interactive events it raises to an interested listener. Lightweightsystem is a very important class in draw2d, which is the dispatch center that DRAW2D can run, the main function of this class is: 1, hold the reference of canvas control. 2, making itself an interactive event listener and registering itself in the canvas, allowing Lightweightsystem to be notified when various interaction events are raised in the canvas. 3, hold the event forwarder. When Lightweightsystem obtains an interactive event notification from the canvas, it forwards the event directly to the event forwarder, which first converts the system event to a draw2d custom internal event and then distributes the event according to the state of draw2d. 4, hold the update manager. When the canvas is invalid and requires redrawing, Lightweightsystem obtains an invalid rectangular region from the redraw event from canvas and updates the invalid rectangular region to update Manager, when a graphic element in draw2d is invalid DRAW2D will also ask the Update Manager to update the invalid graphic elements. However, the graphical element is not valid and the control is not valid is two concepts; when the control is invalid, the control raises the OnPaint event, and when the graphic element is invalid, the system does not raise the OnPaint event. 5, holding the root graphic element (rootfigure). The root graphic element plays a very important role in draw2d, and it is very important to understand the position and function of the root graphic element in draw2d. The root graphic element is completely overwritten on the canvas (as large as the size of the canvas's work area), and its background color determines the back color of the application. The root graphic element is at the top of the tree level of the graphic element, it has only one child, and the child is the content to be displayed.

Another very important interface in DRAW2D is Ifigure, which represents the abstraction of graphical elements, and all the things that can be graphically displayed will implement this interface. Figure is the basic realization of ifigure. The figure class design involves a very famous pattern: the composite model. In draw2d, all the coordinates in the figure are of type int, and no float or double is used. I was also very strange at that time, but think about it, in fact, it is understandable, as for the specific reasons, I will explain in detail in future articles. DRAW2D only provides a view class that shows the model, and does not provide any editing-related functionality; If the developer intends to develop a graphical display software that does not require editing of the model, it is appropriate to use draw2d, and if you want to perform an edit action, you need both GEF and draw2d.

2. Several related mode 2. The 1 MVC pattern mentions the MVC pattern, and almost all developers will say, I know, is it not "model-view-controller"? All know, M represents model, V represents view, C represents controller. The model is responsible for providing the data, and the view is responsible for rendering the model (usually with graphical elements representing the data in the model), and the controller is responsible for creating graphical elements and modifying the model. Indeed, to say, MVC is a very simple thing, is not the data, graphics, control separate? But it's easy to say, but it's not easy to do. In a specific procedure, the general case is that the model, view and controller are Trinity, often a class represents three things. A class is both a model and a view as well as a controller. In draw2d, the concept of model-view is used extensively, but there is no controller, because the main purpose of the DRAW2D library is to display the model, as to how to edit the model is GEF's responsibility, how to define the model and map the model to the DRAW2D is the application developer color responsibility. All graphical elements derived from figure that are defined in draw2d are view classes that are used to render the model; When using DRAW2D with GEF, you can appreciate the meaning of the concept of "figure is view" in draw2d. Designing an application framework based on MVC is much more difficult than designing a class hierarchy based on MVC, and it requires superb design to separate editing and display to different layers in the frame. GEF and DRAW2D are application frameworks based on MVC design, GEF is the control layer, DRAW2D is the display layer. As for GEF, I will introduce in future articles that the GEF design is very exciting. I have a very deep impression of a model-view design in draw2d: The scope model and the scroll bar. For a scroll bar, it involves: minimum range, maximum range, current value, thumb size, page increment. In draw2d, an interface Rangemodel is defined that represents the data and is a listening container. The scroll bar holds the scope model and registers itself as a listener in the scope model. When a scroll bar scrolls, it modifies the scope model, and then the scope model notifies other external objects that are interested in the range model data changes, and when the scope model's data is changed by an external object, it notifies the scrollbar to change its display to keep the data consistent with the scope model. In future articles, I will focus on the viewport, the viewport is very exciting use of the scope model to communicate with the scroll bar and keep synchronized. It should be noted that the scroll bar described here is actually a combination of view and controller, which is the function of both the view and the controller. In draw2d, the MVC design pattern is more.

2. The graphic element class hierarchy in the 2 composite mode DRAW2D adopts composite mode. A graphical element abstraction is represented by ifigure, with figure representing the basic implementation of the ifigure and containing the Ifigure interface. The motivation of the composite pattern is to combine objects into a tree structure to represent a "partial-whole" hierarchy, composite to make users consistent with the use of individual objects and composite objects. The GUI control library in. NET is the composite design pattern, because there is a distinct part and whole relationship between the various controls, such as a form class that derives from control, but it is obvious that it is also a container of controls that can contain various controls. Draw2d is a lightweight widget widget system that provides a variety of graphical element artifacts (such as buttons, scroll bars) that are almost equivalent to commonly used controls, but these graphical element artifacts are not controls, they are tightly just graphic elements derived from figure, This is why DRAW2D is called a lightweight widget system. Because the draw2d is a component library, and the component essentially exists a "partial-whole" relationship, so the graphical elements in draw2d also automatically adopt composite mode. Because of the composite pattern, in a draw2d application, graphical elements are tree-like, strictly speaking, there is only one graphical element in an application that represents what the application will display (called the content graphic element)                    ; Content graphic elements can contain children, children can include children ..., and descendants are endless. draw2d graphic element Design Figure 2. The 3 observer pattern has been elevated to a very important position in Java, so that the Java class Library supports it directly. The event-handling mechanism in Java is implemented based on the observer pattern, since draw2d is a lightweight widget system, so graphical elements can raise events and receive events.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.