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