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