我是靠谱客的博主 微笑奇异果,最近开发中收集的这篇文章主要介绍流程设计器开发六(大纲视图部分),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

 

       要实现大纲视图,我们先回顾一下以前在编辑器中实现GraphicalViewer视图,是如何实现的,找到以前的代码,在WorkflowProcessEditor的configureGraphicalViewer方法中,
GraphicalViewer viewer = getGraphicalViewer();
viewer.setEditPartFactory(new WorkflowProcessEditPartFactory());
我们给GraphicalViewer设置了模型和控制映射的工厂类,大纲视图的实现和GraphicalViewer视图类似,也需要为模型定义控制器,不过这个控制器不同于以前的控制器,这些控制器必须继承AbstractTreeEditPart,(注意:一个模型可以对应多个控制器,多个视图),由于在大纲视图中我们不用显示转移,因此,我们只需定义两个控制器:流程和活动,代码如下:
流程控制器
package com.example.workflow.parts;
 
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
 
import org.eclipse.gef.editparts.AbstractTreeEditPart;
 
import com.example.workflow.model.ModelElement;
import com.example.workflow.model.WorkflowProcess;
 
public class WorkflowProcessTreeEditPart extends AbstractTreeEditPart
       implements PropertyChangeListener{
             
       public void activate() {
              if (!isActive()) {
                     super.activate();
                     ((ModelElement) getModel()).addPropertyChangeListener(this);
              }
       }
             
       public void deactivate() {
              if (isActive()) {
                     super.deactivate();
                     ((ModelElement) getModel()).removePropertyChangeListener(this);
              }
       }
      
       private WorkflowProcess getCastedModel() {
              return (WorkflowProcess) getModel();
       }
      
       protected List getModelChildren() {
              return getCastedModel().getChildren(); // return a list of activities
       }
      
       //当往编辑器中增加活动时,刷新活动模型对应的视图
       public void propertyChange(PropertyChangeEvent arg0) {
              String prop = evt.getPropertyName();
       if (WorkflowProcess. CHILD_ADDED_PROP .equals(prop)
              || WorkflowProcess. CHILD_REMOVED_PROP .equals(prop)) {
           refreshChildren();
       }
             
       }
 
}
//活动控制器
package com.example.workflow.parts;
 
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
 
import org.eclipse.gef.editparts.AbstractTreeEditPart;
import org.eclipse.swt.graphics.Image;
 
 
import com.example.workflow.model.AbstractActivity;
import com.example.workflow.model.ModelElement;
 
public class AbstractActivityTreeEditPart extends AbstractTreeEditPart
       implements    PropertyChangeListener{
             
       public void activate() {
              if (!isActive()) {
                     super.activate();
                     ((ModelElement) getModel()).addPropertyChangeListener(this);
              }
       }
             
       public void deactivate() {
              if (isActive()) {
                     super.deactivate();
                     ((ModelElement) getModel()).removePropertyChangeListener(this);
              }
       }
 
       private AbstractActivity getCastedModel() {
              return (AbstractActivity) getModel();
       }
             
 
      
       protected String getText() {
              return getCastedModel().getName();
       }
 
       public void propertyChange(PropertyChangeEvent evt) {
             
       }
 
}
       这些类中方法大家应该很熟悉,我只说一下getText()方法,就是活动在大纲视图中显示的名称,还有个getImage()方法,可以定义显示的图标,定义完这些控制器后,我们还必须定义模型和这些控制器的映射类,代码如下:
package com.example.workflow.parts;
 
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartFactory;
 
import com.example.workflow.model.AbstractActivity;
import com.example.workflow.model.WorkflowProcess;
 
public class WorkflowProcessTreeEditPartFactory implements EditPartFactory{
 
       public EditPart createEditPart(EditPart context, Object modelElement) {
              //get EditPart for model element
              EditPart part = getPartForElement(modelElement);
              // store model element in EditPart
              part.setModel(modelElement);
              return part;
       }
      
       /**
        * Maps an object to an EditPart.
        * @throws RuntimeException if no match was found (programming error)
        */
       private EditPart getPartForElement(Object modelElement) {
              if (modelElement instanceof WorkflowProcess) {
                     return new WorkflowProcessTreeEditPart();
              }
              if (modelElement instanceof AbstractActivity) {
                     return new AbstractActivityTreeEditPart();
              }
              //because the outline view of the WorkflowProcessEditor dont
              //need to display the Trasntion ,we dont create the TransitionTreeEditPart
              throw new RuntimeException(
                            "Can't create part for model element: "
                            + ((modelElement != null) ? modelElement.getClass().getName() : "null"));
       }
 
}
 
这个映射类和以前GraphicalViewer视图基本一样,我也不解释了。
定义完控制器和映射类之后,我们还需定义一个大纲视图对应类,由于大纲视图和编辑器联系紧密,因此我们把这个类作为WorkflowProcessEditor的内部类,代码如下:
class WorkflowProcessOutlinePage extends ContentOutlinePage{
 
              /**
               * Create a new outline page for the WorkflowProcess editor.
               * @param viewer a viewer (TreeViewer instance) used for this outline page
               * @throws IllegalArgumentException if editor is null
               */
              public WorkflowProcessOutlinePage(EditPartViewer viewer) {
                     super(viewer);                    
              }
 
              public void createControl(Composite parent) {
                     //create outline viewer page
                     getViewer().createControl(parent);
                     // configure outline viewer
                     getViewer().setEditDomain(getEditDomain());
                     getViewer().setEditPartFactory(new WorkflowProcessTreeEditPartFactory());
                    
                     //让大纲视图和编辑器的GraphicalViewer视图同步选择
                     getSelectionSynchronizer().addViewer(getViewer());
                    
                     //设置大纲视图的根内容为流程模型
                     getViewer().setContents(getModel());
              }
 
              public Control getControl() {
                     return getViewer().getControl();
              }    
             
              public void dispose() {
                     // unhook outline viewer
                     getSelectionSynchronizer().removeViewer(getViewer());
                     // dispose
                     super.dispose();
              }
             
       }
 
为了在大纲视图中显示内容,我们还必须在WorkflowProcessEditor类中重写getAdapter方法,代码如下:
public Object getAdapter(Class type) {
              if (type == IContentOutlinePage.class)
                     return new WorkflowProcessOutlinePage(new TreeViewer());
              return super.getAdapter(type);
       }
同时在插件依赖项中增加 
 org.eclipse.ui.ide,
 org.eclipse.ui.views
这样运行程序,就可以看到编辑器大纲视图的效果了,如图:
 

最后

以上就是微笑奇异果为你收集整理的流程设计器开发六(大纲视图部分)的全部内容,希望文章能够帮你解决流程设计器开发六(大纲视图部分)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部