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