posts - 36, comments - 30, trackbacks - 0, articles - 3

                哪個模型的控制器能接受在活動之間建立轉(zhuǎn)移的請求呢,只有活動模型的控制器了,因為活動模型中維護著活動的輸入和輸出轉(zhuǎn)移的列表,在活動控制器增加策略,使該控制器能接受建立轉(zhuǎn)移的請求,代碼如下:

          protectedvoid createEditPolicies() {
                 
          //allow removal of the associated model element
                 installEditPolicy(EditPolicy.COMPONENT_ROLE, new AbstractActivityComponentEditPolicy

          ());
                 
          //allow the creation of transitions and 
                 
          // and the reconnection of transitions between AbstractActivity instances
                 installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new 

          TransitionGraphicalNodeEditPolicy());
                 
              }
          這里安裝了TransitionGraphicalNodeEditPolicy策略,這個策略的代碼如下:
          package com.example.workflow.policy;
           
          import org.eclipse.gef.commands.Command;
          import org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy;
          import org.eclipse.gef.requests.CreateConnectionRequest;
          import org.eclipse.gef.requests.ReconnectRequest;
           
          import com.example.workflow.commands.TransitionCreateCommand;
          import com.example.workflow.model.AbstractActivity;
           
          public class TransitionGraphicalNodeEditPolicy extends GraphicalNodeEditPolicy{
                        
                 
          protected Command getConnectionCompleteCommand(CreateConnectionRequest request) {
                        TransitionCreateCommand cmd 
          = 
                               (TransitionCreateCommand)request.getStartCommand();
                        cmd.setTarget((AbstractActivity)getHost().getModel());
                        
          return cmd;
                 }

                 
                 
          protected Command getConnectionCreateCommand(CreateConnectionRequest request) {
                        AbstractActivity source 
          = (AbstractActivity)getHost().getModel();
                        TransitionCreateCommand cmd 
          = new TransitionCreateCommand(source);
                        request.setStartCommand(cmd);
                        
          return cmd;
                 }

           
                 
          protected Command getReconnectSourceCommand(ReconnectRequest request) {
                        
          // TODO Auto-generated method stub
                        return null;
                 }

           
                 
          protected Command getReconnectTargetCommand(ReconnectRequest request) {
                        
          // TODO Auto-generated method stub
                        return null;
                 }

           
          }

          這個類繼承了GraphicalNodeEditPolicy,并且覆蓋了getConnectionCreateCommand和getConnectionCompleteCommand,從這兩個方法的字面意思就明白它們在什么時候執(zhí)行,在getConnectionCreateCommand方法中,新建一個TransitionCreateCommand命令對象,并且把轉(zhuǎn)移的起始活動放入該命令對象中,在getConnectionCompleteCommand方法中,得到在getConnectionCreateCommand方法中創(chuàng)建的命令,并且把轉(zhuǎn)移的目標活動放入命令中。TransitionCreateCommand命令的代碼如下:

          package com.example.workflow.commands;
           
          import java.util.Iterator;
           
          import org.eclipse.gef.commands.Command;
           
          import com.example.workflow.model.AbstractActivity;
          import com.example.workflow.model.Transition;
           
          public class TransitionCreateCommand extends Command{
                 
                 
          /** The Transition instance. */
                 
          private Transition transition;
                 
          /** Start endpoint for the Transition. */
                 
          private final AbstractActivity source;
                 
          /** Target endpoint for the Transition. */
                 
          private AbstractActivity target;
                        
                 
          public TransitionCreateCommand(AbstractActivity source){
                        
          if (source == null{
                               
          throw new IllegalArgumentException();
                        }

                        setLabel(
          "connection creation");
                        
          this.source = source;
                 }

                 
                 
                 
          public boolean canExecute() {
                        
          // disallow source -> source connections
                        if (source.equals(target)) {
                               
          return false;
                        }

                        
          // return false, if the source -> target connection exists already
                        for (Iterator iter = source.getSourceTransitions().iterator(); iter.hasNext

          ();) 
          {
                               Transition tran 
          = (Transition) iter.next();
                               
          if (tran.getTarget().equals(target)) {
                                      
          return false;
                               }

                        }

                        
          return true;
                 }

                        
                 
          public void execute() {
                        
          // create a new transition between source and target
                        transition = new Transition(source, target);         
                 }

           
                 
                 
          public void redo() {
                        transition.reconnect();
                 }

                 
                 
                 
          public void setTarget(AbstractActivity target) {
                        
          if (target == null{
                               
          throw new IllegalArgumentException();
                        }

                        
          this.target = target;
                 }

           
                 
                 
          public void undo() {
                        transition.disconnect();
                 }

          }

          在這個命令中,用剛才放入的兩個活動構(gòu)建一個轉(zhuǎn)移對象,其實通過代碼我們知道最終執(zhí)行的是在起始活動的輸入轉(zhuǎn)移列表中加入剛才新建的轉(zhuǎn)移對象,在目標活動的輸入轉(zhuǎn)移列表中加入新建的轉(zhuǎn)移對象。代碼如下:

          void addTransition(Transition tran) {
                 
          if (tran == null || tran.getSource() == tran.getTarget()) {
                     thrownew IllegalArgumentException();
                 }

                 
          if (tran.getSource() == this{
                     sourceTransitions.add(tran);
                     firePropertyChange(SOURCE_CONNECTIONS_PROP, 
          null, tran);
                 }
           elseif (tran.getTarget() == this{
                     targetTransitions.add(tran);
                     firePropertyChange(TARGET_CONNECTIONS_PROP, 
          null, tran);
                 }

              }

          我們看到,向活動維護的轉(zhuǎn)移列表中加入轉(zhuǎn)移時,活動模型通知控制器它的SOURCE_CONNECTIONS_PROP和TARGET_CONNECTIONS_PROP屬性發(fā)生變化了,因此在活動控制器中應(yīng)該根據(jù)這兩個屬性來屬性活動的視圖,代碼如下:

          publicvoid propertyChange(PropertyChangeEvent evt) {
                 String prop 
          = evt.getPropertyName();      
                 
          if(AbstractActivity.SIZE_PROP.equals(prop) 
                        
          ||AbstractActivity.LOCATION_PROP.equals(prop)){
                     refreshVisuals();
                 }
          elseif(AbstractActivity.SOURCE_TRANSITIONS_PROP.equals(prop)){
                     refreshSourceConnections();
                 }
          elseif(AbstractActivity.TARGET_TRANSITIONS_PROP.equals(prop)){
                     refreshTargetConnections();
                 }
                
              }

          這里刷新的不是活動的視圖,而是活動的輸入和輸入轉(zhuǎn)移對應(yīng)的視圖。只修改這些代碼,還不能在編輯器中展示出新建的轉(zhuǎn)移,還應(yīng)該讓活動控制器實現(xiàn)NodeEditPart接口,同時要實現(xiàn)這接口中的四個方法,代碼如下:

          private ConnectionAnchor anchor;
          protected ConnectionAnchor getConnectionAnchor(){
                 
          if (anchor == null{
                     anchor 
          = new ChopboxAnchor(getFigure());;
                 }

                 returnanchor;
              }

           
              
          public ConnectionAnchor getSourceConnectionAnchor(ConnectionEditPart arg0) {
                 
          return getConnectionAnchor();
              }

           
              
          public ConnectionAnchor getSourceConnectionAnchor(Request arg0) {
                 
          return getConnectionAnchor();
              }

           
              
          public ConnectionAnchor getTargetConnectionAnchor(ConnectionEditPart arg0) {
                 
          return getConnectionAnchor();
              }

           
              
          public ConnectionAnchor getTargetConnectionAnchor(Request arg0) {
                 
          return getConnectionAnchor();
              }

          這四個方法就是實現(xiàn)轉(zhuǎn)移和活動連接的錨點。另外我們還要覆蓋父類中的兩個方法,得到活動模型維護的輸入轉(zhuǎn)移和輸出轉(zhuǎn)移,代碼如下:
           protected List getModelSourceConnections() {
                 
          return getCastedModel().getSourceTransitions();
              }

              
          protected List getModelTargetConnections() {
                 
          return getCastedModel().getTargetTransitions();
              }
          這樣我們就可以在活動之間建立轉(zhuǎn)移了,效果下圖:
           

          這樣我們就在兩個活動之間建立了轉(zhuǎn)移。那么如何刪除轉(zhuǎn)移呢,要刪除活動之間的轉(zhuǎn)移,應(yīng)該在轉(zhuǎn)移控制器中安裝策略,代碼如下:

          private Transition getCastedModel(){
                 
          return (Transition)getModel();
              }

              
              protectedvoid createEditPolicies() 
          {
                 
          //Allows the removal of the transition model element
                 installEditPolicy(EditPolicy.CONNECTION_ROLE, new ConnectionEditPolicy() {
                     
          protected Command getDeleteCommand(GroupRequest request) {
                        returnnew TransitionDeleteCommand(getCastedModel());
                     }

                 }
          );
                 
              }

          這里安裝的是gef框架提供的策略,我們沒有創(chuàng)建自己的策略,而是直接覆蓋其中的getDeleteCommand,方法,在這個方法中新建了一個ransitionDeleteCommand命令,這個命令的代碼如下:

          package com.example.workflow.commands;
           
          import org.eclipse.gef.commands.Command;
           
          import com.example.workflow.model.Transition;
           
          public class TransitionDeleteCommand extends Command {
              
          /** Transition instance to disconnect. */
              
          private final Transition transition;
               
              
          public TransitionDeleteCommand(Transition tran) {
                 
          if (tran == null{
                     
          throw new IllegalArgumentException();
                 }

                 setLabel(
          "Transition deletion");
                 
          this.transition = tran;
              }

              
          public void execute() {
                 transition.disconnect();
              }

              
              
          public void undo() {
                 transition.reconnect();
              }

          }

          這個類最終執(zhí)行的其實活動模型中的removeTransition方法,從活動模型維護的轉(zhuǎn)移列表中刪除它,而此時活動模型通知控制器自己的OURCE_CONNECTIONS_PROP和TARGET_CONNECTIONS_PROP屬性發(fā)生變化了,要刷新視圖,而這個我們在新建轉(zhuǎn)移時,已經(jīng)實現(xiàn),因而這兒不用在實現(xiàn)了。這樣運行程序,我們就可以刪除活動之間的轉(zhuǎn)移了。
          接下來我們再給轉(zhuǎn)移控制器安裝一個策略,目的是當(dāng)選擇轉(zhuǎn)移,轉(zhuǎn)移有個反饋,給用戶感覺是已經(jīng)選擇了轉(zhuǎn)移,要不然,選擇和不選擇轉(zhuǎn)移,效果一樣,代碼如下:


          protectedvoid createEditPolicies() {
                 
          //Selection handle edit policy. 
                 
          // Makes the transition show a feedback, when selected by the user.
                 installEditPolicy(EditPolicy.CONNECTION_ENDPOINTS_ROLE,
                        
          new ConnectionEndpointEditPolicy());
                 
          //Allows the removal of the transition model element
                 installEditPolicy(EditPolicy.CONNECTION_ROLE, new ConnectionEditPolicy() {
                     
          protected Command getDeleteCommand(GroupRequest request) {
                        returnnew TransitionDeleteCommand(getCastedModel());
                     }

                 }
          );
                     }
          下一節(jié)我們介紹如何在轉(zhuǎn)移上新建,刪除和移動拐點。

          Feedback

          # re: 流程設(shè)計器開發(fā)六(轉(zhuǎn)移建立和刪除)[未登錄]  回復(fù)  更多評論   

          2008-01-13 17:02 by fisher
          請問addTransition()是加在哪個類里?

          # re: 流程設(shè)計器開發(fā)六(轉(zhuǎn)移建立和刪除)  回復(fù)  更多評論   

          2008-01-14 08:39 by 玩轉(zhuǎn)Java
          加在模型AbstractActivity中。
          主站蜘蛛池模板: 晋城| 左云县| 石城县| 永顺县| 武邑县| 鸡西市| 淄博市| 上饶县| 鱼台县| 海安县| 山东| 清丰县| 衢州市| 南华县| 兰考县| 长武县| 汤阴县| 佛坪县| 佛冈县| 和田县| 巴彦淖尔市| 马边| 海林市| 安宁市| 贵德县| 和顺县| 东港市| 东山县| 南安市| 嘉禾县| 南漳县| 信宜市| 青州市| 铜梁县| 广安市| 新建县| 时尚| 监利县| SHOW| 昌都县| 余江县|