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