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

Source: Internet
Author: User
Tags getcolor

Let's briefly review the role of request in GEF. Request is an Important Role in GEF. The tool converts the original mouse event to a request that can be recognized by editpart, And the request carries the request information. For example, the user selects the creationtool In the palette, And then presses the left mouse button in the canvas area, in this case, the mouse click event generated on the canvas will be converted into a createrequest by creationtool, which contains the object to be created, coordinates, and other information. If the editpolicy that can process createrequest is installed on the editpart, the corresponding editpolicy creates a command based on the createrequest, and the latter actually performs the necessary operations to create a new object.

GEF has provided many types of requests for us. The most common types are createrequest and its subclass createconnectionrequest. The other common types are selectionrequest, changeboundsrequest, and reconnectrequest. To implement a typical graphical ApplicationProgramFor example, in the UML class diagram Editor, these predefined requests are enough. However, I believe you have seen many strange demands and many of them are not in line with the conventional usage habits. Therefore, developers are more dependent on coding, rather than the convenience brought by the development framework. In this case, our only expectation is that the development framework provides enough extension mechanisms to allow us to write additionalCodeIt can coexist with other codes. Fortunately, GEF is sufficiently scalable. After a few questions, let's go back to the request. In order to explain under what circumstances a custom request is required, I assume a new requirement based on the example application in the previous article "application instance:

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

If you have used photoshop or similar software, this requirement is similar to the "paint bucket" or "coloring tool" that colors nodes. Of course, behind the user interface, in practice, these colors may indicate the importance, priority, or exception information of a node. Now, let's create a custom request to implement this requirement, which is based on the sample project in the previous article.

1. First, the node class in the original model does not have a member variable that reflects the color. Therefore, you must add a color attribute to the node class first, and the corresponding getter/setter method. Note that the setter method must pass the message of model change like the setter method of other member variables. Similar to other member variables, there should also be a static string variable to distinguish the attributes corresponding to the message.

 Final   Public   Static String prop_color = "color" ;  Protected RGB color = New RGB (1, 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 );} 

2. Modify the propertychanged () and refreshsponals () methods in nodepart to reflect the color attribute changes of node to the image, add a response to the color attribute in the former, and set the background color of nodefigure to the color of the node color attribute in the latter. (Note: The color object is a system resource object and needs to be cached in actual use to avoid system resource depletion. To save space, the sample code is new color ().)

  Public   void   propertychange (propertychangeevent EVT) {  If  (EVT. getpropertyname (). equals (node. prop_color) ///   response to color change   refreshals () ;}  protected   void   refreshals () {(nodefigure)   This . getfigure ()). setbackgroundcolor ( New  color ( null , node. getcolor (); ///   todo cache color instances }

3. create our own request now, because the purpose is to change the color, so it may be called changecolorrequest. It should inherit from org. Eclipse. GEF. request. We need two types of information on changecolorrequest: 1. nodes that need to change colors; 2. Target colors. Therefore, it should have these two member variables.

 Import  Org. Eclipse. GEF. request;  Import  Org. Eclipse. SWT. Graphics. RGB;  Import  Com. example. model. node;  Public   Class Changecolorrequest Extends  Request {  Final   Static  Public String req_change_color = "req_change_color" ;  Private  Node node;  Private  RGB color;  Public  Changecolorrequest (node, RGB color ){  Super  ();  This . Color = Color;  This . Node =Node; settype (req_change_color );}  Public  RGB getcolor (){  Return  Color ;}  Public  Node getnode (){  Return  Node ;}  Public   Void  Setnode (node ){  This . Node = Node ;} Public   Void  Setcolor (RGB color ){  This . Color = Color ;}} 

Changecolorrequest looks similar to a JavaBean, because the request is used to pass the mouse event after translation. If you look at Org. eclipse. GEF. request Code, you will find that the request has a type attribute, which is generally a string (predefines some in requestconstants of GEF, such as requestconstants. req_selection_hover), editpolicy can decide whether to process this request based on it. In our example, we define such a constant string req_change_color, which will be used in the changecoloreditpolicy.

4. Now there is a question: where should the request instance be generated? The answer is that when you press the left mouse button in the canvas area in the tool, the selected tool in the palette is responsible for creating a request. We need to create a new tool: changecolortool. Let's let changecolortool inherit from org. Eclipse. GEF. Tools. selectiontool, because the usage of "coloring tool" is similar to "selection tool. Obviously, we need to overwrite the handlebuttondown () method to tell GEF what will happen if you press the mouse in the canvas area if you have selected this tool. The Code is as follows:

Import  Org. Eclipse. GEF. editpart;  Import  Org. Eclipse. GEF. commands. Command;  Import  Org. Eclipse. GEF. Tools. selectiontool;  Import  Org. Eclipse. SWT. Graphics. RGB;  Import  Com. example. model. node;  Import  Com. example. parts. nodepart;  Public   Class ChangecolortoolExtends  Selectiontool {  Private  RGB color;  Public  Changecolortool (RGB color ){  Super  ();  This . Color = Color ;}  /**  * If target editpart is {  @ Link  Nodepart}, create {  @ Link Changecolorrequest} instance, * GET command from target editpart with this request and execute.  */  @ Override  Protected   Boolean Handlebuttondown ( Int  Button ){  //  Get selected editpart Editpart = This  . Gettargeteditpart ();  If (EditpartInstanceof  Nodepart) {nodepart = (Nodepart) editpart; node Node = (Node) nodepart. GetModel ();  //  Create an instance of changecolorrequest Changecolorrequest request = New  Changecolorrequest (node, color );  //  GET command from the editpart Command command =Editpart. getcommand (request );  //  Execute the command              This  .Getdomain().getcommandstack(cmd.exe cute (command );  Return   True  ;}  Return   False  ;}} 

5. With the tool, you also need to use the toolentry to wrap it and add it to the palette. Therefore, we create a class named changecolortoolentry that inherits org. Eclipse. GEF. palette. toolentry, overwrite the createtool () method, and let it return our changecolortool instance. The changecolortoolentry code should be easy to understand:

Import  Org. Eclipse. GEF. sharedcursors;  Import  Org. Eclipse. GEF. tool;  Import  Org. Eclipse. GEF. palette. toolentry;  Import  Org. Eclipse. jface. Resource. imagedescriptor;  Import  Org. Eclipse. SWT. Graphics. RGB;  Public   Class Changecolortoolentry Extends  Toolentry { Private  RGB color;  Public  Changecolortoolentry (RGB color, string label, string includesc, imagedescriptor iconsmall, imagedescriptor iconlarge ){  Super  (Label, includesc, iconsmall, iconlarge );  This . Color = Color;} @ override  Public  Tool createtool () {changecolortool Tool = New Changecolortool (color); tool. setunloadwhenfinished (  False ); //  Switch to selection tool after saved med? Tool. setdefacurcursor (sharedcursors. Cross ); //  Any cursor you like          Return  Tool ;}} 

6. Add three such toolentries to the palette, by modifying the original palettefactory class. To save space, we will not post its code here. You can download and refer to the createcategories () and createcolordrawer () methods in the sample code palettefactory. java.

So far, changecolorrequest can be sent. The problem to be solved next is how to let the editpart process the request.

7. We know that any modification to the Model in GEF is done by command, so a changecolorcommand is required. Its execute () method and undo () method are as follows:

Public ClassChangecolorcommandExtendsCommand {PrivateRGB oldcolor; @ overridePublic VoidExecute () {oldcolor=Node. getcolor (); node. setcolor (color) ;}@ overridePublic VoidUndo () {node. setcolor (oldcolor );}}

8. editpolicy is responsible for receiving all requests. Therefore, a changecoloreditpolicy must be created. In the Code listed below, you will see that we have defined a new "role" string. Later, when we install this editpolicy on the editpart, we will use this string as the key, to avoid overwriting the existing editpolicy on the editpart.

 Import Org. Eclipse. GEF. request;  Import  Org. Eclipse. GEF. commands. Command;  Import  Org. Eclipse. GEF. editpolicies. abstracteditpolicy;  Import  Org. Eclipse. SWT. Graphics. RGB;  Import  Com. example. model. node;  Public   Class Changecoloreditpolicy Extends  Abstracteditpolicy {  Final  Static   Public String change_color_role = "change_color_role" ; @ Override  Public  Command getcommand (request ){  //  Judge whether this request is intersting by its type          If (Request. GetType () = Changecolorrequest. req_change_color) {changecolorrequest therequest = (Changecolorrequest) request;  // Get information from request Node node = Therequest. getnode (); RGB color = Therequest. getcolor ();  //  Create corresponding command and return it Changecolorcommand command = New  Changecolorcommand (node, color );  Return  Command ;}  Return   Null  ;}} 

9. Finally, return to the editpart. The last node in the nodepart we modified in the second step needs to be added, that is, add the newly created changecoloreditpolicy in the installeditpolicies () method:

 
Protected VoidCreateeditpolicies (){//Add change color editpolicyInstalleditpolicy (changecoloreditpolicy. change_color_role,NewChangecoloreditpolicy ());}

Now we have completed all necessary modifications. Let's take a look at the running results.

To sum up, you need to create the following classes: changecolorrequest, changecolortool, changecolortoolentry, changecolorcommand, and changecoloreditpolicy. The classes to be modified include node, nodepart, and palettefactory. In the instance project, all newly created classes are stored in the com. example. Request package for your convenience. We recommend that you put them in the corresponding packages in the actual project.

Download the sample code (compiled under eclipse3.2.1 and gef3.2)

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.