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
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package fi.vtt.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.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
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
40 public class InsertNozzleAction extends WriteAction implements DropListener {
\r
42 Resource equipmentResource = null;
\r
43 Resource nozzleType = null;
\r
45 public InsertNozzleAction(ThreeDimensionalEditorBase parent) {
\r
46 super(parent,false);
\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
56 Resource type = dialog.getComboValue();
\r
58 equipmentResource = null;
\r
66 public GraphRequestStatus doChanges(Graph graph) throws Exception {
\r
67 createNozzle(graph);
\r
68 return GraphRequestStatus.transactionComplete();
\r
71 private void createNozzle(Graph graph) {
\r
72 assert(nozzleType != null);
\r
73 assert(equipmentResource != null);
\r
75 Nozzle n = PipingTools2.instantiateNozzle(graph, nozzleType);
\r
77 Equipment equipment = new Equipment(graph, equipmentResource);
\r
79 G3DAPI.addNodeLocal(equipment,ProcessResource.plant3Dresource.HasNozzle, n);
\r
80 //parent.getSelectionAdapter().updateSelection(new StructuredResourceSelection(n.getResource()));
\r
84 public boolean usable(Graph graph, List<Resource> resources) {
\r
85 if (resources.size() != 1) {
\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
98 public void init() {
\r
99 setText("Insert Nozzle");
\r
100 this.setImageDescriptor(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/Nozzle.png"));
\r
103 public boolean acceptDrop(StructuredResourceSelection s, Resource[] ids) {
\r
108 if (ids.length != 1)
\r
110 final Resource r = ids[0];
\r
111 final Resource selectedResource = s.iterator().next();
\r
112 GraphRequestWithResult<Boolean> query = new GraphRequestWithResult<Boolean>() {
\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
118 t = EntityFactory.create(g,selectedResource);
\r
119 if (t.isInstanceOf(ProcessResource.plant3Dresource.Equipment)) {
\r
125 parent.getSession().syncRead(query);
\r
126 return query.getResult();
\r
129 public void doDrop(StructuredResourceSelection s, Resource[] ids) {
\r
130 equipmentResource = s.iterator().next();
\r
131 parent.getSession().asyncWrite(new GraphRequestAdapter() {
\r
133 public GraphRequestStatus perform(Graph g) throws Exception {
\r
135 return GraphRequestStatus.transactionComplete();
\r