1 /*******************************************************************************
\r
2 * Copyright (c) 2007- VTT Technical Research Centre of Finland.
\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
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.simantics.processeditor.actions;
\r
13 import java.util.List;
\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
36 public class InsertEquipmentAction extends ContextAction implements DropListener{
\r
39 public InsertEquipmentAction(ThreeDimensionalEditorBase parent) {
\r
41 this.setText("Insert Equipment");
\r
46 EquipmentDialog dialog = new EquipmentDialog(parent.getRenderingComposite().getShell(),"Insert Equipment",parent.getSession());
\r
47 if (dialog.open() == EquipmentDialog.CANCEL)
\r
49 final Resource typeResource = dialog.getComboValue();
\r
50 parent.getSession().asyncWrite(new GraphRequestAdapter() {
\r
52 public GraphRequestStatus perform(Graph g) throws Exception {
\r
53 createEquipment(g,typeResource);
\r
54 return GraphRequestStatus.transactionComplete();
\r
62 private void createEquipment(Graph graph,Resource typeResource) {
\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
68 G3DAPI.addNodeWorld(plant, instanceNode);
\r
70 //parent.getSelectionAdapter().updateSelection(new StructuredResourceSelection(instance)));
\r
73 public boolean usable(Graph graph,List<Resource> resources) {
\r
74 return (resources.size() == 0);
\r
79 public boolean acceptDrop(StructuredResourceSelection s, Resource[] ids) {
\r
84 if (ids.length != 1)
\r
86 final Resource r = ids[0];
\r
87 GraphRequestWithResult<Boolean> query = new GraphRequestWithResult<Boolean>() {
\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
93 if (t.getRelatedObjects(ProcessResource.plant3Dresource.HasGraphics).size() == 1)
\r
99 parent.getSession().syncRead(query);
\r
100 return query.getResult();
\r
103 public void doDrop(StructuredResourceSelection s, Resource[] ids) {
\r
104 final Resource typeResource = ids[0];
\r
105 parent.getSession().asyncWrite(new GraphRequestAdapter() {
\r
107 public GraphRequestStatus perform(Graph g) throws Exception {
\r
108 createEquipment(g,typeResource);
\r
109 return GraphRequestStatus.transactionComplete();
\r