]> gerrit.simantics Code Review - simantics/3d.git/blob
91733ab0b4c1408acad4d0503828ff9d85eb9898
[simantics/3d.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package fi.vtt.simantics.processeditor.actions;\r
12 \r
13 import java.util.List;\r
14 \r
15 import org.simantics.db.Graph;\r
16 import org.simantics.db.GraphRequestAdapter;\r
17 import org.simantics.db.GraphRequestStatus;\r
18 import org.simantics.db.GraphRequestWithResult;\r
19 import org.simantics.db.Resource;\r
20 import org.simantics.layer0.utils.EntityFactory;\r
21 import org.simantics.layer0.utils.IEntity;\r
22 import org.simantics.layer0.utils.instantiation.Instance;\r
23 import org.simantics.layer0.utils.instantiation.InstanceFactory;\r
24 import org.simantics.proconf.g3d.actions.WriteAction;\r
25 import org.simantics.proconf.g3d.base.G3DAPI;\r
26 import org.simantics.proconf.g3d.base.G3DTools;\r
27 import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;\r
28 import org.simantics.proconf.g3d.common.StructuredResourceSelection;\r
29 import org.simantics.proconf.g3d.dnd.DropListener;\r
30 \r
31 import fi.vtt.simantics.processeditor.Activator;\r
32 import fi.vtt.simantics.processeditor.ProcessResource;\r
33 import fi.vtt.simantics.processeditor.common.PipingTools2;\r
34 import fi.vtt.simantics.processeditor.dialogs.NozzleDialog;\r
35 import fi.vtt.simantics.processeditor.stubs.Equipment;\r
36 import fi.vtt.simantics.processeditor.stubs.Nozzle;\r
37 \r
38 \r
39 \r
40 public class InsertNozzleAction extends WriteAction implements DropListener {\r
41 \r
42     Resource equipmentResource = null;\r
43     Resource nozzleType = null;\r
44     \r
45     public InsertNozzleAction(ThreeDimensionalEditorBase parent) {\r
46         super(parent,false);\r
47     }\r
48 \r
49     @Override\r
50     public boolean canActivate() {\r
51         NozzleDialog dialog = new NozzleDialog(parent.getRenderingComposite().getShell(),"Select a nozzle", parent.getSession());\r
52         if (dialog.open() == NozzleDialog.CANCEL) {\r
53              equipmentResource = null;\r
54              return false;\r
55         }\r
56         Resource type = dialog.getComboValue();\r
57         if (type == null) {\r
58                 equipmentResource = null;\r
59                 return false;\r
60         }\r
61         nozzleType = type;      \r
62         return true;\r
63     }\r
64     \r
65     @Override\r
66     public GraphRequestStatus doChanges(Graph graph) throws Exception {\r
67         createNozzle(graph);\r
68         return GraphRequestStatus.transactionComplete();\r
69     }\r
70     \r
71     private void createNozzle(Graph graph) {\r
72         assert(nozzleType != null);\r
73         assert(equipmentResource != null);\r
74 \r
75         Nozzle n = PipingTools2.instantiateNozzle(graph, nozzleType);\r
76         \r
77         Equipment equipment = new Equipment(graph, equipmentResource);\r
78         \r
79         G3DAPI.addNodeLocal(equipment,ProcessResource.plant3Dresource.HasNozzle, n);\r
80         //parent.getSelectionAdapter().updateSelection(new StructuredResourceSelection(n.getResource()));        \r
81         equipment = null;\r
82     }\r
83     \r
84     public boolean usable(Graph graph, List<Resource> resources) {\r
85         if (resources.size() != 1) {\r
86             return false;\r
87         }\r
88         Resource r = resources.iterator().next();\r
89         IEntity t = EntityFactory.create(graph,r);\r
90         if (t.isInstanceOf(ProcessResource.plant3Dresource.Equipment)) {\r
91             equipmentResource = r;\r
92             return true;\r
93         }\r
94         return false;\r
95     }\r
96     \r
97     @Override\r
98     public void init() {\r
99         setText("Insert Nozzle");\r
100         this.setImageDescriptor(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/Nozzle.png"));\r
101     }\r
102     \r
103     public boolean acceptDrop(StructuredResourceSelection s, Resource[] ids) {\r
104                 if(s.size() != 1)\r
105                         return false;\r
106                 if (ids == null)\r
107                         return false;\r
108                 if (ids.length != 1)\r
109                         return false;\r
110                 final Resource r = ids[0];\r
111                 final Resource selectedResource = s.iterator().next();\r
112                 GraphRequestWithResult<Boolean> query = new GraphRequestWithResult<Boolean>() {\r
113                         @Override\r
114                         public Boolean performWithResult(Graph g) throws Exception {\r
115                                 IEntity t = EntityFactory.create(g,r);\r
116                                 if (!t.isInheritedFrom(ProcessResource.plant3Dresource.Nozzle))\r
117                                         return false;\r
118                                 t = EntityFactory.create(g,selectedResource);\r
119                                 if (t.isInstanceOf(ProcessResource.plant3Dresource.Equipment)) {\r
120                                         return true;\r
121                                 }\r
122                                 return false;\r
123                         }\r
124                 };\r
125                 parent.getSession().syncRead(query);\r
126                 return query.getResult();\r
127         }\r
128     \r
129     public void doDrop(StructuredResourceSelection s, Resource[] ids) {\r
130         equipmentResource = s.iterator().next();\r
131         parent.getSession().asyncWrite(new GraphRequestAdapter() {\r
132                 @Override\r
133                 public GraphRequestStatus perform(Graph g) throws Exception {\r
134                         createNozzle(g);\r
135                         return GraphRequestStatus.transactionComplete();\r
136                 }\r
137         });\r
138     }\r
139 }\r