Design Model (command mode) in the GEF framework)

Source: Internet
Author: User

The structure of the command mode in our process designer implements the copy and paste function, that is, the command mode is adopted. Taking the cut operation as an example, when the activity is selected in the editor, select cut in the menu to generate a cut command (cutcommand object ). The cut operation uses the following classes:
Name Description
Org. Eclipse. GEF. commands. Command Abstract class of a command
Org. Eclipse. GEF. commands. Command. compoundcommand Class that combines multiple commands
Com. example. workflow. commands. cutcommand Cut command class
Com. example. workflow. model. workflowprocess Process
Com. example. workflow. Action. cutaction Cut category
Command abstract class, which has an execute () method. Compoundcommand indicates the class "commands with multiple commands". This class inherits the command class. The commandlist attribute of this class is Java. util. the arraylist type is used to store multiple objects that inherit commands. When executing the execute () method, this class executes the execute () method of the command in commandlist in the order of the arraylist, when executing the Undo () method, the Undo () method of the Command Used in the commandlist is executed in reverse order in the arraylist. The cutcommand class indicates the "cut activity command". This class inherits the command class, which has two attributes: Parent and Child. parent is a workflowprocess object, which indicates a process object. Child is a abstractactivity object, indicates the activity object to be cut. In the constructor, assign values to the two attributes, specifying the activity to be deleted from the process model, and () call the removechild () method of parent to delete the activity from the process model. The cutaction class is the class that calls commands. This class inherits from Org. eclipse. GEF. UI. actions. selectionaction: Create the cutcommand and compoundcommand objects in the run () method of this class, and add the cutcommand object to the commandlist of the compoundcommand object. If multiple activities are cut, multiple cutcommand commands are executed, but these commands are placed in the commandlist. In this way, a command is executed. Compared with the command mode structure, compoundcommand and cutcommand are equivalent to concretecommand, workflowprocess is equivalent to reciever, and removechild () is the reciever action () method, while cutaction plays both the client and invoker roles. Cutcommand class code: Package COM. example. workflow. commands; import Org. eclipse. GEF. commands. command; import COM. example. workflow. model. abstractactivity; import COM. example. workflow. model. workflowprocess; public class cutcommand extends command {private workflowprocess parent; private abstractactivity child; Public cutcommand (workflowprocess parent, abstractactivity child) {super (); If (parent = NULL | Chi LD = NULL) {Throw new illegalargumentexception ();} This. parent = parent; this. child = Child; setlabel ("Cut activity");} public Boolean canexecute () {return parent! = NULL & Child! = NULL;} public void execute () {Redo ();} public void Redo () {parent. removechild (child);} public void undo () {parent. addchild (child) ;}} cutaction class code: Package COM. example. workflow. action; import Java. util. list; import Org. eclipse. GEF. editpart; import Org. eclipse. GEF. commands. command; import Org. eclipse. GEF. commands. compoundcommand; import Org. eclipse. GEF. UI. actions. clipboard; import Org. eclipse. GEF. UI. actions. selectionaction; import Org. eclipse. jface. resource. imagedescriptor; import Org. eclipse. UI. iworkbenchpart; import Org. eclipse. UI. actions. actionfactory; import COM. example. workflow. activator; import COM. example. workflow. commands. cutcommand; import COM. example. workflow. model. abstractactivity; import COM. example. workflow. model. workflowprocess; import COM. example. workflow. parts. workflowprocessedit Part; public class cutaction extends selectionaction {private list selectionlist = NULL; private clipboard = NULL; private workflowprocess; Public cutaction (iworkbenchpart part) {super (part); setid (actionfactory. cut. GETID (); settext ("cut"); setimagedescriptor (imagedescriptor. createfromfile (activator. class, "icons/cut.gif");} public clipboard getclipboard () {return Cl Ipboard;} public void setclipboard (Clipboard clipboard) {This. clipboard = clipboard;} public workflowprocess getworkflowprocess () {return workflowprocess;} public void setworkflowprocess (workflowprocess) {This. workflowprocess = workflowprocess;} protected Boolean calculateenabled () {selectionlist = super. getselectedobjects (); If (selectionlist. isempty () | (selectionlist. siz E () = 1 & selectionlist. get (0) instanceof workflowprocesseditpart) {return false;} return true;} public void run () {getclipboard (). setcontents (selectionlist); execute (createcutcommand ();} private command createcutcommand () {compoundcommand compoundcmd = new compoundcommand (); For (INT I = 0; I <selectionlist. size (); I ++) {editpart part = (editpart) selectionlist. get (I); cutcommand cmd = new Cutco MmAnd (getworkflowprocess (), (abstractactivity) part. getModel (); compoundcmd. add (CMD) ;}return compoundcmd ;}} workflowprocess class code: Package COM. example. workflow. model; import Java. util. arraylist; import Java. util. list;/*** process model, which can contain multiple activity and transfer models * @ author administrator **/public class workflowprocess extends modelelement {Private Static final long serialversionuid =-54786936480758659l; /** propert Y id to use when a child is added to this workflowprocess. */public static final string child_added_prop = "workflowprocess. childadded ";/** property ID to use when a child is removed from this workflowprocess. */public static final string child_removed_prop = "workflowprocess. childremoved "; private list activities = new arraylist ();/*** Add a activity to this workflowprocess. * @ Param s a non -Null activity instance * @ return true, if the activity was added, false otherwise */Public Boolean addchild (abstractactivity A) {if (! = NULL & activities. add (A) {firepropertychange (child_added_prop, null, a); Return true;} return false;}/** return a list of activities in this workflowprocess. the returned list shocould not be modified. */public list getchildren () {return activities;}/*** remove a activity from this workflowprocess. * @ Param s a non-null activity instance; * @ return true, if the activity was removed, Fals E otherwise */Public Boolean removechild (abstractactivity A) {if (! = NULL & activities. Remove (A) {firepropertychange (child_removed_prop, null, a); Return true ;}return false ;}}

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.