6.12 增加对Palette的拖放支持

http://www.myexception.cn/open-source/414042.html

默认情况下,GEF中要创建一个新的结点都是通点在Palette上选择一个结点,然后在Editor区域单击实现的。

也有一些是通过拖放Palette中的一个对象到Editor区域来实现的。

要实现一个拖放支持也是很容易的。GEF中默认实现了一组drag-drop listener用来支持拖放实现:

1.TemplateTransferDropTargetListener

因为是要从palette上拖到Editor中,所以此监听事件显示是应该安放在Editor上,在GEF中就是GraphicalViewer,可以在initializeGraphicalViewer()方法中加上:

getGraphicalViewer().addDropTargetListener(new TemplateTransferDropTargetListener(getGraphicalViewer()));

2.TemplateTransferDragSourceListener

显示此事件需要加在palette上,也就是PaletteViewer上,可以在initializePaletteViewer()方法中加上:

getPaletteViewer().addDragSourceListener(new TemplateTransferDragSourceListener(getPaletteViewer()));

 

 

回头再看一下我们对Palette上的Item的声明:

CombinedTemplateCreationEntry(String label, String shortDesc,
  CreationFactory factory, ImageDescriptor iconSmall, ImageDescriptor iconLarge)

需要注意CombinedTemplateCreationEntry构造方法的选用。认为上述构造方法为默认选择,而CreationFactory类为SimpleFactory。

如果选择另一个构造方法:

CombinedTemplateCreationEntry(String label, String shortDesc, Object template,
  CreationFactory factory, ImageDescriptor iconSmall, ImageDescriptor iconLarge)

则我们需要注意:如果template和factory都是SimpleFactory则没有问题,因为默认的template就是SimpleFactory。

ToolEntry tool = new CombinedTemplateCreationEntry("Node", "Create a new Node", Node.class, new SimpleFactory(Node.class), null, null);

否则我们需要根据自己的template值(比如Node.class)来得到对应的model,也就是要实现自己的CreationFactory。

我们的调色板里需要有三种工具:选择工具、节点工具和连接工具。在GEF里,调色板里可以有抽屉(PaletteDrawer)把各种工具归类放置,每个工具都是一个ToolEntry,选择工具(SelectionToolEntry)和连接工具(ConnectionCreationToolEntry)是预先定义好的几种工具中的两个, 所以可以直接使用。对于节点工具,要使用CombinedTemplateCreationEntry

 

比如:

CombinedTemplateCreationEntry elementCreationEntry = new CombinedTemplateCreationEntry(  
        "Element"(显示名), "Element type"(简单描述),ModelCreationFactory.ELEMENT_TYPE,new ModelCreationFactory(ModelCreationFactory.ELEMENT_TYPE), Activator.getImageDescriptor("icons/element.png"), Activator.getImageDescriptor("icons/element.png"));  
CombinedTemplateCreationEntry attributeCreationEntry
= new CombinedTemplateCreationEntry( "Attribute", "Attribute type",ModelCreationFactory.ATTRIBUTE_TYPE, new ModelCreationFactory(ModelCreationFactory.ATTRIBUTE_TYPE), Activator .getImageDescriptor("icons/attribute.png"), Activator .getImageDescriptor("icons/attribute.png"));
 1 public class ModelCreationFactory implements CreationFactory {  
 2   
 3     private int objectType;  
 4   
 5     public static final int ELEMENT_TYPE = 0x01;  
 6     public static final int ATTRIBUTE_TYPE = 0x02;  
 7     public static final int ROOT_TYPE = 0x04;  
 8   
 9     public ModelCreationFactory(int objectType) {  
10         super();  
11         this.objectType = objectType;  
12     }  
13   
14     public Object getNewObject() {  
15         switch (objectType) {  
16         case ELEMENT_TYPE:  
17             return new ElementType();  
18         case ATTRIBUTE_TYPE:  
19             return new AttributeType();  
20         case ROOT_TYPE:  
21             return new RootType();  
22         default:  
23             break;  
24         }  
25         return null;  
26     }  
27   
28     public Object getObjectType() {  
29         return objectType;  
30     }  
31   
32 }  
getGraphicalViewer().addDropTargetListener(new CustomTemplateTransferDropTargetListener(  
                getGraphicalViewer()));  
 1 public class CustomTemplateTransferDropTargetListener extends  
 2         TemplateTransferDropTargetListener {  
 3   
 4     public CustomTemplateTransferDropTargetListener(EditPartViewer viewer) {  
 5         super(viewer);  
 6     }  
 7   
 8     @Override  
 9     protected CreationFactory getFactory(Object template) {  
10         return new ModelCreationFactory(((Integer)template).intValue());  
11     }  
12   
13 }  

 

我的程序中是这样的:

ps:Palette是一个树形结构,在这个结构中有四种主要的节点类型:PaletteRoot、PaletteGroup、PaletteDrawer、ToolEntry,其中,PaletteRoot、PaletteGroup、PaletteDrawer都继承自PaletteContainer,PaletteContainer和ToolEntry则都继承自PaletteEntry,这是Palette树结构的基类。getPaletteRoot就是要向系统返回PaletteRoot对象。

 1 class EditorDropListener extends TemplateTransferDropTargetListener {
 2         public EditorDropListener() {
 3             this(getGraphicalViewer());
 4         }
 5       
 6         public EditorDropListener(EditPartViewer viewer) {
 7             super(viewer);
 8             // TODO Auto-generated constructor stub
 9         }
10         //GraphicalViewer和EditPartViewer的区别??用了两个构造函数。查资料说GraphicalViewer  EditPartViewer 的子接口,可是为何要两个?????
11         @Override
12         protected CreationFactory getFactory(Object template) {
13             if (template instanceof CreationFactory) {
14                 return ((CreationFactory) template);
15             }
16             return null;
17         }
18 
19     };
  1 public class TestCasePalette {
  2     //首先创建PaletteRoot。
  3     public static PaletteRoot createPalette() {
  4         PaletteRoot palette = new PaletteRoot();
  5         palette.addAll(createCategories(palette));
  6         
  7         return palette;
  8     }
  9     
//根据PaletteRoot创建categories,在里面创建Drawer(放自己的工具)和Group(连接和选择工具) 10 public static List<PaletteContainer> createCategories(PaletteRoot root){ 11 List<PaletteContainer> categories = new ArrayList<PaletteContainer>(); 12 13 categories.add(createControlGroup(root));
14 categories.add(createStatementDrawer()); 15 categories.add(createTimerDrawer()); 16 categories.add(createConditionDrawer()); 17 categories.add(createMessageDrawer()); 18 categories.add(createInlineDrawer()); 19 categories.add(createInstanceDrawer()); 20 categories.add(createCommentDrawer()); 21 22 return categories; 23 } 24 //在Group里创建entry。SelectionToolEntry用于创建选择工具,MarqueeToolEntry则用于创建区域选择工具。 25 public static PaletteGroup createControlGroup(PaletteRoot root){ 26 PaletteGroup toolGroup = new PaletteGroup("tool"); 27 28 ToolEntry tool = new SelectionToolEntry(); 29 toolGroup.add(tool); 30 root.setDefaultEntry(tool); 31 tool = new MarqueeToolEntry(); 32 toolGroup.add(tool); 33 34 return toolGroup; 35 } 36 在Drawer里创建entry 37 public static PaletteDrawer createCommentDrawer(){ 38 PaletteDrawer commentDrawer = new PaletteDrawer("Comment"); 39 CombinedTemplateCreationEntry creationTool; 40 ImageDescriptor image; 41 42 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_EVENTCOMMENT);暂时未深究 43 creationTool = new CombinedTemplateCreationEntry( 44 "Event Comment","Event Comment", 45 new SimpleFactory(EventCommentModel.class), 46 image,image); 47 commentDrawer.add(creationTool); 48 49 return commentDrawer; 50 } 51 52 public static PaletteDrawer createTimerDrawer(){ 53 PaletteDrawer timerDrawer = new PaletteDrawer("Timer"); 54 CombinedTemplateCreationEntry creationTool; 55 ImageDescriptor image; 56 57 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_STARTTIMER); 58 creationTool = new CombinedTemplateCreationEntry( 59 "Start Timer","Start Timer", 60 new SimpleFactory(StartTimerModel.class), 61 image,image); 62 timerDrawer.add(creationTool); 63 64 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_STOPTIMER); 65 creationTool = new CombinedTemplateCreationEntry( 66 "Stop Timer","Stop Timer", 67 new SimpleFactory(StopTimerModel.class), 68 image,image); 69 timerDrawer.add(creationTool); 70 71 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_TIMEOUTTIMER); 72 creationTool = new CombinedTemplateCreationEntry( 73 "Time out Timer","Time out Timer", 74 new SimpleFactory(TimeoutTimerModel.class), 75 image,image); 76 timerDrawer.add(creationTool); 77 78 return timerDrawer; 79 } 80 81 public static PaletteDrawer createInstanceDrawer(){ 82 PaletteDrawer instanceDrawer = new PaletteDrawer("Instance"); 83 CombinedTemplateCreationEntry creationTool; 84 ImageDescriptor image; 85 86 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_STOPCOMPONENT); 87 creationTool = new CombinedTemplateCreationEntry( 88 "Stop Component","Stop Component", 89 new SimpleFactory(StopComponentModel.class), 90 image,image); 91 instanceDrawer.add(creationTool); 92 93 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_LABEL); 94 creationTool = new CombinedTemplateCreationEntry( 95 "Label","Label", 96 new SimpleFactory(LabelModel.class), 97 image,image); 98 instanceDrawer.add(creationTool); 99 100 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_PORT); 101 creationTool = new CombinedTemplateCreationEntry( 102 "Port Instance","Port Instance", 103 new SimpleFactory(PortInstanceModel.class), 104 image,image); 105 instanceDrawer.add(creationTool); 106 107 return instanceDrawer; 108 } 109 110 public static PaletteDrawer createStatementDrawer(){ 111 PaletteDrawer statementDrawer = new PaletteDrawer("Statement"); 112 CombinedTemplateCreationEntry creationTool; 113 ImageDescriptor image; 114 115 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_ACTION); 116 creationTool = new CombinedTemplateCreationEntry( 117 "Action","Action", 118 new SimpleFactory(ActionModel.class), 119 image,image); 120 statementDrawer.add(creationTool); 121 122 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_CREATE); 123 creationTool = new CombinedTemplateCreationEntry( 124 "Create","Create", 125 new SimpleFactory(CreateModel.class), 126 image,image); 127 statementDrawer.add(creationTool); 128 129 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_DEFAULT); 130 creationTool = new CombinedTemplateCreationEntry( 131 "Default","Default", 132 new SimpleFactory(DefaultModel.class), 133 image,image); 134 statementDrawer.add(creationTool); 135 136 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_EXECUTE); 137 creationTool = new CombinedTemplateCreationEntry( 138 "Execute","Execute", 139 new SimpleFactory(ExecuteModel.class), 140 image,image); 141 statementDrawer.add(creationTool); 142 143 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_REFERENCE); 144 creationTool = new CombinedTemplateCreationEntry( 145 "Reference","Reference", 146 new SimpleFactory(ReferenceModel.class), 147 image,image); 148 statementDrawer.add(creationTool); 149 150 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_START); 151 creationTool = new CombinedTemplateCreationEntry( 152 "Start","Start", 153 new SimpleFactory(StartModel.class), 154 image,image); 155 statementDrawer.add(creationTool); 156 157 return statementDrawer; 158 } 159 160 public static PaletteDrawer createConditionDrawer(){ 161 PaletteDrawer conditionDrawer = new PaletteDrawer("Condition"); 162 CombinedTemplateCreationEntry creationTool; 163 ImageDescriptor image; 164 165 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_VERDICTCONDITION); 166 creationTool = new CombinedTemplateCreationEntry( 167 "Setverdict","Setverdict", 168 new SimpleFactory(ConditionModel.class), 169 image,image); 170 conditionDrawer.add(creationTool); 171 172 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_DONECONDITION); 173 creationTool = new CombinedTemplateCreationEntry( 174 "Done Condition","Done Condition", 175 new SimpleFactory(DoneConditionModel.class), 176 image,image); 177 conditionDrawer.add(creationTool); 178 179 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_BOOLEANCONDITION); 180 creationTool = new CombinedTemplateCreationEntry( 181 "Boolean Condition","Boolean Condition", 182 new SimpleFactory(BooleanConditionModel.class), 183 image,image); 184 conditionDrawer.add(creationTool); 185 186 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_PORTOPERATION); 187 creationTool = new CombinedTemplateCreationEntry( 188 "Port Operation","Port Operation", 189 new SimpleFactory(PortOperationModel.class), 190 image,image); 191 conditionDrawer.add(creationTool); 192 193 return conditionDrawer; 194 } 195 196 public static PaletteDrawer createMessageDrawer(){ 197 PaletteDrawer messageDrawer = new PaletteDrawer("Message"); 198 CombinedTemplateCreationEntry creationTool; 199 ImageDescriptor image; 200 201 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_MESSAGETO); 202 creationTool = new CombinedTemplateCreationEntry( 203 "Message To","Message To", 204 new SimpleFactory(MessageToModel.class), 205 image,image); 206 messageDrawer.add(creationTool); 207 208 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_MESSAGEFROM); 209 creationTool = new CombinedTemplateCreationEntry( 210 "Message From","Message From", 211 new SimpleFactory(MessageFromModel.class), 212 image,image); 213 messageDrawer.add(creationTool); 214 215 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_MESSAGECHECK); 216 creationTool = new CombinedTemplateCreationEntry( 217 "Check Message","Check Message", 218 new SimpleFactory(MessageCheckModel.class), 219 image,image); 220 messageDrawer.add(creationTool); 221 222 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_FOUND); 223 creationTool = new CombinedTemplateCreationEntry( 224 "Found","Found", 225 new SimpleFactory(FoundModel.class), 226 image,image); 227 messageDrawer.add(creationTool); 228 229 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_FOUNDCHECK); 230 creationTool = new CombinedTemplateCreationEntry( 231 "Check Found","Check Found", 232 new SimpleFactory(FoundCheckModel.class), 233 image,image); 234 messageDrawer.add(creationTool); 235 236 return messageDrawer; 237 } 238 239 public static PaletteDrawer createInlineDrawer(){ 240 PaletteDrawer inlineDrawer = new PaletteDrawer("Inline"); 241 CombinedTemplateCreationEntry creationTool; 242 ImageDescriptor image; 243 244 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_ALT); 245 creationTool = new CombinedTemplateCreationEntry( 246 "Alt","Alt", 247 DiagramFactory.getAlternativeModelFactory(), 248 image,image); 249 inlineDrawer.add(creationTool); 250 251 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_CALL); 252 creationTool = new CombinedTemplateCreationEntry( 253 "Call","Call", 254 DiagramFactory.getCallReplyModelFactory(), 255 image,image); 256 inlineDrawer.add(creationTool); 257 258 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_INTERLEAVE); 259 creationTool = new CombinedTemplateCreationEntry( 260 "Interleave","Interleave", 261 DiagramFactory.getInterleaveModelFactory(), 262 image,image); 263 inlineDrawer.add(creationTool); 264 265 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_IF); 266 creationTool = new CombinedTemplateCreationEntry( 267 "If","If", 268 DiagramFactory.getIfModelFactory(), 269 image,image); 270 inlineDrawer.add(creationTool); 271 272 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_WHILE); 273 creationTool = new CombinedTemplateCreationEntry( 274 "While","While", 275 DiagramFactory.getWhileLoopModelFactory(), 276 image,image); 277 inlineDrawer.add(creationTool); 278 279 image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_FOR); 280 creationTool = new CombinedTemplateCreationEntry( 281 "For","For", 282 DiagramFactory.getForLoopModelFactory(), 283 image,image); 284 inlineDrawer.add(creationTool); 285 286 return inlineDrawer; 287 } 288 }
  1 public class ControlPalette {
  2     
  3     public static PaletteRoot createPalette() {
  4         PaletteRoot palette = new PaletteRoot();
  5         palette.addAll(createCategories(palette));
  6         
  7         return palette;
  8     }
  9     
 10     public static List<PaletteContainer> createCategories(PaletteRoot root){
 11         List<PaletteContainer> categories = new ArrayList<PaletteContainer>();
 12         
 13         categories.add(createControlGroup(root));
 14         categories.add(createStatementDrawer());
 15         categories.add(createTimerDrawer());
 16         categories.add(createInlineDrawer());    
 17         categories.add(createCommentDrawer());
 18         
 19         return categories;
 20     }
 21     
 22     public static PaletteGroup createControlGroup(PaletteRoot root){        
 23         PaletteGroup toolGroup = new PaletteGroup("tool");
 24         
 25         ToolEntry tool = new SelectionToolEntry();        
 26         toolGroup.add(tool);
 27         root.setDefaultEntry(tool);
 28         tool = new MarqueeToolEntry();
 29         toolGroup.add(tool);
 30         
 31         return toolGroup;
 32     }
 33     
 34     public static PaletteDrawer createCommentDrawer(){
 35         PaletteDrawer commentDrawer = new PaletteDrawer("Comment");        
 36         CombinedTemplateCreationEntry  creationTool;
 37         ImageDescriptor image;
 38         
 39         image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_EVENTCOMMENT);        
 40         creationTool = new CombinedTemplateCreationEntry(
 41                 "Event Comment","Event Comment",
 42                 new SimpleFactory(EventCommentModel.class),
 43                 image,image);
 44         commentDrawer.add(creationTool);
 45         
 46         image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_TEXT);
 47         creationTool = new CombinedTemplateCreationEntry(
 48                 "Text","Text",
 49                 new SimpleFactory(TextModel.class),
 50                 image,image);
 51         commentDrawer.add(creationTool);
 52         
 53         return commentDrawer;
 54     }
 55     
 56     public static PaletteDrawer createTimerDrawer(){
 57         PaletteDrawer timerDrawer = new PaletteDrawer("Timer");        
 58         CombinedTemplateCreationEntry  creationTool;
 59         ImageDescriptor image;
 60     
 61         image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_STARTTIMER);
 62         creationTool =  new CombinedTemplateCreationEntry(
 63                 "Start Timer","Start Timer",
 64                 new SimpleFactory(StartTimerModel.class),
 65                 image,image);
 66         timerDrawer.add(creationTool);    
 67         
 68         image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_STOPTIMER);
 69         creationTool =  new CombinedTemplateCreationEntry(
 70                 "Stop Timer","Stop Timer",
 71                 new SimpleFactory(StopTimerModel.class),
 72                 image,image);
 73         timerDrawer.add(creationTool);    
 74         
 75         image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_TIMEOUTTIMER);
 76         creationTool =  new CombinedTemplateCreationEntry(
 77                 "Time out Timer","Time out Timer",
 78                 new SimpleFactory(TimeoutTimerModel.class),
 79                 image,image);
 80         timerDrawer.add(creationTool);    
 81         
 82         return timerDrawer;
 83     }
 84     
 85     public static PaletteDrawer createStatementDrawer(){
 86         PaletteDrawer statementDrawer = new PaletteDrawer("Statement");        
 87         CombinedTemplateCreationEntry  creationTool;
 88         ImageDescriptor image;
 89         
 90         image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_ACTION);
 91         creationTool = new CombinedTemplateCreationEntry(
 92                 "Action","Action",
 93                 new SimpleFactory(ActionModel.class),
 94                 image,image);
 95         statementDrawer.add(creationTool);
 96         
 97         image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_EXECUTE);
 98         creationTool = new CombinedTemplateCreationEntry(
 99                 "Execute","Execute",
100                 new SimpleFactory(ExecuteModel.class),
101                 image,image);
102         statementDrawer.add(creationTool);
103         
104         image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_REFERENCE);
105         creationTool = new CombinedTemplateCreationEntry(
106                 "Reference","Reference",
107                 new SimpleFactory(ReferenceModel.class),
108                 image,image);
109         statementDrawer.add(creationTool);
110         
111         return statementDrawer;
112     }    
113     
114     public static PaletteDrawer createInlineDrawer(){
115         PaletteDrawer inlineDrawer = new PaletteDrawer("Inline");
116         CombinedTemplateCreationEntry creationTool;
117         ImageDescriptor image;
118         
119         image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_ALT);
120         creationTool = new CombinedTemplateCreationEntry(
121                 "Alt","Alt",
122                 DiagramFactory.getAlternativeModelFactory(),
123                 image,image);
124         inlineDrawer.add(creationTool);
125         
126         image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_INTERLEAVE);
127         creationTool = new CombinedTemplateCreationEntry(
128                 "Interleave","Interleave",
129                 DiagramFactory.getInterleaveModelFactory(),
130                 image,image);
131         inlineDrawer.add(creationTool);
132         
133         image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_IF);
134         creationTool = new CombinedTemplateCreationEntry(
135                 "If","If",
136                 DiagramFactory.getIfModelFactory(),
137                 image,image);
138         inlineDrawer.add(creationTool);    
139         
140         image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_WHILE);
141         creationTool = new CombinedTemplateCreationEntry(
142                 "While","While",
143                 DiagramFactory.getWhileLoopModelFactory(),
144                 image,image);
145         inlineDrawer.add(creationTool);
146         
147         image = AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKey.IIMAGE_FOR);
148         creationTool = new CombinedTemplateCreationEntry(
149                 "For","For",
150                 DiagramFactory.getForLoopModelFactory(),
151                 image,image);
152         inlineDrawer.add(creationTool);
153                 
154         return inlineDrawer;
155     }    
156 }
1 public class FunctionPalette {
2 
3     public static PaletteRoot createPalette() {        
4         return TestCasePalette.createPalette();
5     }    
6     
7 }
public class AltstepPalette {
    
    public static PaletteRoot createPalette() {        
        return TestCasePalette.createPalette();
    }
    
    
}
  1 public class DiagramFactory {
  2 
  3     public static CreationFactory getAlternativeModelFactory() {
  4         return new CreationFactory(){
  5             public Object getNewObject(){
//将模型对应到一个int上。
6 AlternativeModel alt = new AlternativeModel(); 7 ContainerSeperatorModel seperator; 8 int cell = alt.getPreferredCell(); cell=3
  9                 
 10                 for(int i=1;i<cell;i++){
 11                     seperator = new ContainerSeperatorModel();
 12                     alt.addChild(-1, seperator);  ???                  
 13                 }
 14                 return alt;
 15             }
 16             public Object getObjectType(){
 17                 return "AlternativeModel.class";
 18             }
 19         };
 20     }
 21     
 22     public static CreationFactory getCallReplyModelFactory() {
 23         return new CreationFactory(){
 24             public Object getNewObject(){
 25                 CallReplyModel callreply = new CallReplyModel();
 26                 ContainerSeperatorModel seperator;
 27                 SuspensionRegionModel suspension;
 28                 int cell = callreply.getPreferredCell();
 29                 
 30                 MessageToModel call = new MessageToModel();
 31                 callreply.addChild(-1, call);
 32                 callreply.setCall(call);
 33                 suspension = new SuspensionRegionModel();
 34                 callreply.addChild(-1, suspension);
 35                 suspension.setCall(call);
 36                 suspension.setHeadSuspension(true);
 37                 call.setSuspension(suspension);
 38                 
 39                 for(int i=1;i<cell;i++){
 40                     seperator = new ContainerSeperatorModel();
 41                     callreply.addChild(-1, seperator);                    
 42                     
 43                     suspension = new SuspensionRegionModel();
 44                     callreply.addChild(-1, suspension);
 45                 }
 46                 return callreply;
 47             }
 48             public Object getObjectType(){
 49                 return "CallReplyModel.class";
 50             }
 51         };
 52     }
 53     
 54     public static CreationFactory getIfModelFactory() {
 55         return new CreationFactory(){
 56             public Object getNewObject(){
 57                 IfModel ifm = new IfModel();
 58                 ContainerSeperatorModel seperator;
 59                 int cell =  ifm.getPreferredCell();
 60                 
 61                 for(int i=1;i<cell;i++){
 62                     seperator = new ContainerSeperatorModel();
 63                     ifm.addChild(-1, seperator);                    
 64                 }
 65                 return ifm;
 66             }
 67             public Object getObjectType(){
 68                 return "IfModel.class";
 69             }
 70         };
 71     }
 72     
 73     public static CreationFactory getWhileLoopModelFactory() {
 74         return new CreationFactory(){
 75             public Object getNewObject(){
 76                 WhileLoopModel loop = new WhileLoopModel();
 77                 ContainerSeperatorModel seperator;
 78                 int cell =  loop.getPreferredCell();
 79                 
 80                 for(int i=1;i<cell;i++){
 81                     seperator = new ContainerSeperatorModel();
 82                     loop.addChild(-1, seperator);
 83                 }
 84                 return loop;
 85             }
 86             public Object getObjectType(){
 87                 return "WhileLoopModel.class";
 88             }
 89         };
 90     }
 91     
 92     public static CreationFactory getForLoopModelFactory(){
 93         return new CreationFactory(){
 94             public Object getNewObject(){
 95                 ForLoopModel loop = new ForLoopModel();
 96                 ContainerSeperatorModel seperator;
 97                 int cell =  loop.getPreferredCell();
 98                 
 99                 for(int i=1;i<cell;i++){
100                     seperator = new ContainerSeperatorModel();
101                     loop.addChild(-1, seperator);
102                 }
103                 return loop;
104             }
105             public Object getObjectType(){
106                 return "ForLoopModel.class";
107             }
108         };
109     }
110     
111     public static CreationFactory getInterleaveModelFactory() {
112         return new CreationFactory(){
113             public Object getNewObject(){
114                 InterleaveModel interleave = new InterleaveModel();
115                 ContainerSeperatorModel seperator;
116                 int cell =  interleave.getPreferredCell();
117                 
118                 for(int i=1;i<cell;i++){
119                     seperator = new ContainerSeperatorModel();
120                     interleave.addChild(-1, seperator);
121                 }
122                 return interleave ;
123             }
124             public Object getObjectType(){
125                 return "InterleaveModel.class";
126             }
127         };
128     }
129     
130     
131     
132 }
protected void initializeGraphicalViewer() {
        // TODO Auto-generated method stub

        viewer.setContents(diagram);

        resourceChangeListener = new ResourceChangeListener(this);
        ResourcesPlugin.getWorkspace().addResourceChangeListener(
                resourceChangeListener);

        editorListener = new EditorPartListener(this);
        getSite().getWorkbenchWindow().getPartService().addPartListener(
                editorListener);

        viewer.addDropTargetListener(new EditorDropListener());

    }

最后的成果是:

图片对应的是ControlPalette。

问题:

DiagramFactory那里没看懂。还有什么时候需要用到这个?simpleFactory是啥。

posted @ 2014-06-12 16:30  behappylee  阅读(520)  评论(0编辑  收藏  举报