GMF的Logic照猫画虎之一:在工具箱palette中添加新的工具

在help里面,Logic的例子提到,添加palette中的控件可以是以下几步:

1.添加扩展点:

org.eclipse.gmf.runtime.diagram.ui.paletteProviders

2.我们在plugin.xml中添加下面的代码(在最后的entry中,我加入了Test这个控件):

View Code
 1   <!-- Palette Provider -->
2 <extension
3 id="logicPaletteProvider"
4 name="%ext.logicPaletteProvider"
5 point="org.eclipse.gmf.runtime.diagram.ui.paletteProviders">
6 <paletteProvider class="org.eclipse.gmf.runtime.diagram.ui.providers.DefaultPaletteProvider">
7 <Priority name="Highest">
8 </Priority>
9 <editor id="LogicEditor">
10 </editor>
11 <!-- class=&quot;org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart&quot;-->
12 <contribution factoryClass="org.eclipse.gmf.examples.runtime.diagram.logic.internal.providers.LogicPaletteFactory">
13 <entry label="%LogicDrawer.Label" kind="drawer" description="%LogicDrawer.Description" path="/" small_icon="icons/comp.gif" id="logicDrawer">
14 <expand>
15 <content>
16 <!-- expand by default for LogicEditors -->
17 </content>
18 </expand>
19 </entry>
20 <entry label="%LogicFlowTool.Label" kind="tool" description="%LogicFlowTool.Description" large_icon="icons/logicflow24.gif" path="/logicDrawer/" small_icon="icons/logicflow16.gif" id="FlowContainer">
21 </entry>
22 <entry label="%CircuitTool.Label" kind="tool" description="%CircuitTool.Description" large_icon="icons/circuit24.gif" path="/logicDrawer/" small_icon="icons/circuit16.gif" id="circuit">
23 </entry>
24 <entry label="%LEDTool.Label" kind="tool" description="%LEDTool.Description" large_icon="icons/ledicon24.gif" path="/logicDrawer/" small_icon="icons/ledicon16.gif" id="LED">
25 </entry>
26 <entry label="%GateStack.Label" kind="stack" description="%GateStack.Description" small_icon="icons/or24.gif" path="/logicDrawer/" id="noteStack">
27 </entry>
28 <entry label="%OrGateTool.Label" kind="tool" description="%OrGateTool.Description" large_icon="icons/or24.gif" path="/logicDrawer/noteStack/" small_icon="icons/or16.gif" id="OrGate">
29 </entry>
30 <entry label="%AndGateTool.Label" kind="tool" description="%AndGateTool.Description" large_icon="icons/and24.gif" path="/logicDrawer/noteStack/" small_icon="icons/and16.gif" id="AndGate">
31 </entry>
32 <entry label="%XORGateTool.Label" kind="tool" description="%XORGateTool.Description" large_icon="icons/xor24.gif" path="/logicDrawer/noteStack/" small_icon="icons/xor16.gif" id="XORGate">
33 </entry>
34 <entry label="%ConnectionTool.Label" kind="tool" description="%ConnectionTool.Description" path="/logicDrawer/" small_icon="icons/connection16.gif" large_icon="icons/connection24.gif" id="wire">
35 </entry>
36 <entry label="%LogicTestTool.Label" kind="tool" description="%LogicTestTool.Desrciption" path="/logicDrawer/" small_icon="icons/connection16.gif" large_icon="icons/xor24.gif" id="Test">
37 </entry>
38 </contribution>
39 <contribution>
40 <predefinedEntry
41 id="geoshapeDrawer"
42 path="/">
43 <expand force="true"/>
44 </predefinedEntry>
45 </contribution>
46 </paletteProvider>
47 </extension>

即可发现,在编辑器中出现了我实验的结果

3.工厂模式生成控件,PaletteFactory方法是GMF的service infrastructure中所提供一个方法

工厂方法LogicPaletteFactory是在plugin.xml中的 <contribution factoryClass="org.eclipse.gmf.examples.runtime.diagram.logic.internal.providers.LogicPaletteFactory">指定的


View Code
 1 package org.eclipse.gmf.examples.runtime.diagram.logic.internal.providers;
2
3 import org.eclipse.gef.Tool;
4 import org.eclipse.gmf.examples.runtime.diagram.logic.semantic.util.LogicSemanticType;
5 import org.eclipse.gmf.runtime.diagram.ui.services.palette.PaletteFactory;
6 import org.eclipse.gmf.runtime.diagram.ui.tools.ConnectionCreationTool;
7 import org.eclipse.gmf.runtime.diagram.ui.tools.CreationTool;
8
9
10 public class LogicPaletteFactory extends PaletteFactory.Adapter
11 {
12
13 /*
14 * Create the tool according to type
15 */
16 public Tool createTool(String toolId)
17 {
18 if (toolId.equals(LogicConstants.TOOL_LED))
19 {
20 return new CreationTool(LogicSemanticType.LED);
21 } else if (toolId.equals(LogicConstants.TOOL_CIRCUIT))
22 {
23 return new CreationTool(LogicSemanticType.CIRCUIT);
24 } else if (toolId.equals(LogicConstants.TOOL_ORGATE))
25 {
26 return new CreationTool(LogicSemanticType.ORGATE);
27 } else if (toolId.equals(LogicConstants.TOOL_ANDGATE))
28 {
29 return new CreationTool(LogicSemanticType.ANDGATE);
30 } else if (toolId.equals(LogicConstants.TOOL_XORGATE))
31 {
32 return new CreationTool(LogicSemanticType.XORGATE);
33 } else if (toolId.equals(LogicConstants.TOOL_FLOWCONTAINER))
34 {
35 return new CreationTool(LogicSemanticType.FLOWCONTAINER);
36
37 } else if (toolId.equals(LogicConstants.TOOL_CONNECTION))
38 {
39 return new ConnectionCreationTool(LogicSemanticType.WIRE);
40 } else if (toolId.equals(LogicConstants.LOGIC_TEST))
41 {
42 return new CreationTool(LogicSemanticType.LogicTest);
43 }
44 return null;
45 }
46 }

 

在添加完以后,可以看到鼠标点击Test后在画布上已经是一个创建工具的标志了,但是并无法创建工具,这是因为我们仍没有为Test添加MVC的三个部分:model、view、part。

 

posted @ 2011-11-15 14:48  Sonald  阅读(430)  评论(0编辑  收藏  举报