]> gerrit.simantics Code Review - simantics/3d.git/blob
d1729c81c524c3580f5792acd425241da46911c7
[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 org.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.processeditor.ProcessResource;\r
23 import org.simantics.processeditor.common.PipingTools2;\r
24 import org.simantics.processeditor.dialogs.EquipmentDialog;\r
25 import org.simantics.processeditor.stubs.Equipment;\r
26 import org.simantics.proconf.g3d.actions.ContextAction;\r
27 import org.simantics.proconf.g3d.base.G3DAPI;\r
28 import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;\r
29 import org.simantics.proconf.g3d.common.StructuredResourceSelection;\r
30 import org.simantics.proconf.g3d.dnd.DropListener;\r
31 import org.simantics.proconf.g3d.stubs.G3DNode;\r
32 \r
33 \r
34 \r
35 \r
36 public class InsertEquipmentAction extends ContextAction implements DropListener{\r
37 \r
38         \r
39     public InsertEquipmentAction(ThreeDimensionalEditorBase parent) {\r
40         super(parent);\r
41         this.setText("Insert Equipment");\r
42     }\r
43     \r
44     @Override\r
45     public void run() {\r
46         EquipmentDialog dialog = new EquipmentDialog(parent.getRenderingComposite().getShell(),"Insert Equipment",parent.getSession());\r
47         if (dialog.open() == EquipmentDialog.CANCEL)\r
48                 return;\r
49         final Resource typeResource = dialog.getComboValue();\r
50         parent.getSession().asyncWrite(new GraphRequestAdapter() {\r
51                 @Override\r
52                 public GraphRequestStatus perform(Graph g) throws Exception {\r
53                         createEquipment(g,typeResource);\r
54                     return GraphRequestStatus.transactionComplete();\r
55                 }\r
56         });\r
57     }\r
58     \r
59   \r
60 \r
61     \r
62     private void createEquipment(Graph graph,Resource typeResource) {\r
63         \r
64         //G3DNode instanceNode = new G3DNode(graph, instance);\r
65         Equipment instanceNode = PipingTools2.instantiateEquipment(graph, typeResource);\r
66         G3DNode plant = parent.getScenegraphAdapter().getRootNode().getG3DNode(graph); \r
67         \r
68         G3DAPI.addNodeWorld(plant, instanceNode);\r
69 \r
70         //parent.getSelectionAdapter().updateSelection(new StructuredResourceSelection(instance)));\r
71     }\r
72     \r
73     public boolean usable(Graph graph,List<Resource> resources) {\r
74         return (resources.size() == 0);\r
75     }\r
76     \r
77 \r
78     \r
79     public boolean acceptDrop(StructuredResourceSelection s, Resource[] ids) {\r
80                 if(!s.isEmpty())\r
81                         return false;\r
82                 if (ids == null)\r
83                         return false;\r
84                 if (ids.length != 1)\r
85                         return false;\r
86                 final Resource r = ids[0];\r
87                 GraphRequestWithResult<Boolean> query = new GraphRequestWithResult<Boolean>() {\r
88                         @Override\r
89                         public Boolean performWithResult(Graph g) throws Exception {\r
90                                 IEntity t = EntityFactory.create(g, r);\r
91                                 if(!t.isInheritedFrom(ProcessResource.plant3Dresource.Equipment))\r
92                                         return false;\r
93                                 if (t.getRelatedObjects(ProcessResource.plant3Dresource.HasGraphics).size() == 1)\r
94                                         return true;\r
95                                 return false;\r
96                         }\r
97                 };\r
98 \r
99                 parent.getSession().syncRead(query);\r
100                 return query.getResult();\r
101         }\r
102     \r
103     public void doDrop(StructuredResourceSelection s, Resource[] ids) {\r
104                 final Resource typeResource = ids[0];\r
105                 parent.getSession().asyncWrite(new GraphRequestAdapter() {\r
106                         @Override\r
107                         public GraphRequestStatus perform(Graph g) throws Exception {\r
108                                 createEquipment(g,typeResource);\r
109                                 return GraphRequestStatus.transactionComplete();\r
110                         }\r
111                 });\r
112                 \r
113                 \r
114         }\r
115 }\r