[Eclipse] GEF Getting Started series (12, custom request)

Source: Internet
Author: User
Tags getcolor resource


Let's briefly review the role of request in GEF. Request is one of the more important roles in GEF, tool converts the original mouse event to a editpart identifiable request, which is hosted by request information. For example, the user selects the Create Node tool (Creationtool) in the Palette (Palette), and then presses the left mouse button in the canvas area, where the mouse click event generated on the canvas is converted Creationtool to a createrequest, which It contains information about the objects to be created, coordinates, locations, and so on. If a editpolicy that can handle createrequest is installed on the Editpart, the corresponding editpolicy creates a Command based on this createrequest, which actually performs the necessary actions to create the new object.



GEF has provided us with many kinds of request, the most commonly used is createrequest and its subclasses createconnectionrequest, and other more common selectionrequest, Changeboundsrequest and Reconnectrequest and so on. To implement a typical graphical application, such as the UML class Diagram editor, the predefined request is basically sufficient. But I'm sure you've seen a lot of strange needs, and many of them are not conforming to the customary usage, so it's more dependent on the coding of developers than the convenience of the development framework. At this point, our only expectation is that the development framework will provide enough extensibility to allow our extra code to coexist with other code, but fortunately, GEF is extensible enough. A little off the point, and then back to the question of request, to illustrate what needs to be customized for the request, I assume a new requirement based on the example application in the previous "application Example":



Add three tools to the palette to change the background color of the selected nodes to red, green, and blue.



If you have used Photoshop or similar software, the requirement is much like a "paint bucket" or "coloring tool" that colors the nodes, and of course, behind the user interface, these colors may represent the importance of a node, priority or exception information, and so on. Now let's implement this requirement by creating a custom request, or the example project in the previous article.



First, the original model in the node (nodes) class does not reflect the color of the member variables, so first to add a color attribute in the node class, and the corresponding Getter/setter method, note that this setter method to and other The setter method of the member variable passes the message of the model change as well. As with other member variables, there should also be a static string variable that distinguishes which property the message corresponds to.




final public static String PROP_COLOR = "COLOR";

protected RGB color = new RGB(255, 255, 255);

public RGB getColor() {
   return color;
}

public void setColor(RGB color) {
   if (this.color.equals(color)) {
     return;
   }
   this.color = color;
   firePropertyChange(PROP_COLOR, null, color);
}



Secondly, to change the color property of the node to reflect the graph, modify the propertychanged () and Refreshvisuals () methods in the Nodepart to increase the response to the color property in the latter Sets the background color of the nodefigure to the color of the node's Color property. (Note that the Color object is a system resource object that needs to be cached in order to avoid system resource depletion, for the sake of space, the sample code is directly new Color ())




public void propertyChange(PropertyChangeEvent evt) {

   if (evt.getPropertyName().equals(Node.PROP_COLOR))//Response to color change
     refreshVisuals();
}

protected void refreshVisuals() {

   ((NodeFigure) this.getFigure()).setBackgroundColor(new Color(null, node.getColor()));//TODO cache color instances
}






Related Article

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.