]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/actions/AddComponentAction.java
Merge "Allow user to specify component name in ComponentSelectionDialog"
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / actions / AddComponentAction.java
1 package org.simantics.plant3d.actions;
2
3 import java.awt.event.KeyEvent;
4 import java.awt.event.MouseEvent;
5 import java.awt.event.MouseWheelEvent;
6 import java.util.HashSet;
7 import java.util.Set;
8
9 import org.eclipse.jface.resource.ResourceLocator;
10 import org.eclipse.swt.widgets.Display;
11 import org.simantics.db.Resource;
12 import org.simantics.g3d.math.MathTools;
13 import org.simantics.g3d.scenegraph.NodeMap;
14 import org.simantics.g3d.scenegraph.base.INode;
15 import org.simantics.g3d.vtk.swt.InteractiveVtkComposite;
16 import org.simantics.g3d.vtk.swt.vtkSwtAction;
17 import org.simantics.plant3d.Activator;
18 import org.simantics.plant3d.dialog.ComponentSelectionDialog;
19 import org.simantics.plant3d.gizmo.TerminalSelectionGizmo;
20 import org.simantics.plant3d.scenegraph.InlineComponent;
21 import org.simantics.plant3d.scenegraph.Nozzle;
22 import org.simantics.plant3d.scenegraph.P3DRootNode;
23 import org.simantics.plant3d.scenegraph.PipelineComponent;
24 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.PositionType;
25 import org.simantics.plant3d.utils.ComponentUtils;
26 import org.simantics.plant3d.utils.ComponentUtils.InsertInstruction;
27 import org.simantics.plant3d.utils.Item;
28 import org.simantics.utils.threads.ThreadUtils;
29 import org.simantics.utils.ui.ExceptionUtils;
30
31 import vtk.vtkProp;
32
33 public class AddComponentAction extends vtkSwtAction {
34         
35
36         private P3DRootNode root;
37         private PipelineComponent component;
38         private NodeMap<Resource,vtkProp,INode> nodeMap;
39         
40         private TerminalSelectionGizmo gizmo;
41         
42         private Set<PositionType> allowed = new HashSet<PositionType>();
43         
44         private Item toAdd = null;
45         private PositionType insertPosition;
46         @SuppressWarnings("unused")
47         private boolean insertAdjustable;
48         @SuppressWarnings("unused")
49         private boolean lengthAdjustable;
50         
51         private String libUri;
52         
53         public AddComponentAction(InteractiveVtkComposite panel, P3DRootNode root, String libUri) {
54                 super(panel);
55                 this.root = root;
56                 setText("Add Component");
57                 setImageDescriptor(ResourceLocator.imageDescriptorFromBundle(Activator.PLUGIN_ID, "icons/Component.png").get());
58                 nodeMap = root.getNodeMap();
59                 gizmo = new TerminalSelectionGizmo(panel);
60                 this.libUri = libUri;
61         }
62         
63         public void setComponent(PipelineComponent component) {
64                 this.component = component;
65                 
66                 allowed.clear();
67                 if (component instanceof Nozzle) {
68                         if (component.getNext() == null && component.getPrevious() == null) {
69                                 allowed.add(PositionType.NEXT);
70                         }  
71                 } else {
72                         if (component.getNext() == null) {
73                                 allowed.add(PositionType.NEXT);
74                         }
75                         if (component.getPrevious() == null) {
76                                 allowed.add(PositionType.PREVIOUS);
77                         }
78                         if (component instanceof InlineComponent && !component.getControlPoint().isFixedLength()){
79                                 allowed.add(PositionType.SPLIT);
80                         }
81                 }
82                 setEnabled(allowed.size() > 0);
83         }
84         
85         private String name;
86         
87         private Double length;
88         private Double angle;
89         private Double rotationAngle;
90         private Double diameter;
91         private Double turnRadius;
92         
93         @Override
94         public void run() {
95                 
96                 ComponentSelectionDialog dialog = new ComponentSelectionDialog(Display.getCurrent().getActiveShell(), allowed, component, libUri);
97                 
98                 // Set list of already reserved component names
99                 dialog.addUsedNames(ComponentUtils.getPipelineComponentNames(root));
100                 
101                 if (dialog.open() == ComponentSelectionDialog.CANCEL)
102                         return;
103                 toAdd = dialog.getSelected();
104                 if (toAdd == null)
105                         return;
106                 this.name = dialog.getName();
107                 this.insertPosition = dialog.getInsertPosition();
108                 this.insertAdjustable = dialog.isInsertAdjustable();
109                 this.lengthAdjustable = dialog.isLenghtAdjustable();
110                 this.length = dialog.getLength();
111                 this.angle = dialog.getAngle();
112                 this.rotationAngle = dialog.getRotationAngle();
113                 this.diameter = dialog.getDiameter();
114                 this.turnRadius = dialog.getTurnRadius();
115                 allowed = dialog.filterAllowed();
116                 gizmo.setComponent(component, allowed);
117                 super.run();
118                 panel.refresh();
119         }
120         
121         @Override
122         public boolean keyPressed(KeyEvent e) {
123                 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
124                         panel.useDefaultAction();
125                 return true;
126                 
127         }
128         
129         public void attach() {
130                 if (component == null)
131                         return;
132                 
133                 super.attach();
134                 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
135                         public void run() {
136                                 attachUI();
137                         }
138                 });
139                 
140         }
141         
142         public void deattach() {
143 //              deactivate();
144                 component = null;
145                 nodeMap.commit("Add component");
146                 deattachUI();
147                 super.deattach();
148                 panel.refresh();
149         }
150         
151         private void attachUI() {
152                 //panel.setCursor(activeCursor);
153                 gizmo.attach(panel);
154         }
155         
156         private void deattachUI() {
157                 //panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
158                 gizmo.deattach();
159         }
160         
161         @Override
162         public boolean mouseMoved(MouseEvent e) {
163                 return getDefaultAction().mouseMoved(e);
164         }
165         
166         @Override
167         public boolean mousePressed(MouseEvent e) {
168                 return getDefaultAction().mousePressed(e);
169         }
170
171         @Override
172         public boolean mouseReleased(MouseEvent e) {
173                 return getDefaultAction().mouseReleased(e);
174         }
175         
176         @Override
177         public boolean mouseDragged(MouseEvent e) {
178                 return getDefaultAction().mouseDragged(e);
179         }
180         
181         public void doInsert(PositionType position) {
182                 try  {
183                         InsertInstruction inst = new InsertInstruction();
184                         inst.typeUri = toAdd.getUri();
185                         inst.name = name;
186                         inst.angle = angle != null ? MathTools.degToRad(angle) : null;
187                         inst.diameter = diameter;
188                         inst.length = length;
189                         inst.turnRadius = turnRadius;
190                         inst.insertPosition = insertPosition;
191                         inst.rotationAngle = rotationAngle;
192                         inst.position = position;
193                         ComponentUtils.addComponent(root, component, inst);
194                 } catch (Exception e) {
195                         ExceptionUtils.logAndShowError("Cannot add component", e);
196                 }
197         }
198         
199         public boolean mouseClicked(MouseEvent e) {
200                 if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) {
201                         int type = panel.getPickType();
202                         //panel.setPickType(0);
203                         panel.setPickType(5);
204                         vtkProp[] picked = panel.pick(e.getX(), e.getY());
205                         panel.setPickType(type);
206                         PositionType position = gizmo.getPickedPosition(picked);
207
208                         if (position != null) {
209                                 doInsert(position);
210                                 panel.useDefaultAction();
211                                 return true;
212                         }
213                 } 
214                 return getDefaultAction().mouseClicked(e);
215         }
216         
217         @Override
218         public boolean mouseWheelMoved(MouseWheelEvent e) {
219                 return getDefaultAction().mouseWheelMoved(e);
220         }
221 }