]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/actions/AddComponentAction.java
9b3d90835e14a3961f5c9af0c48fd7ead86417dc
[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) {
80                                 allowed.add(PositionType.NEXT);
81                         }
82                         if (component.getPrevious() == null) {
83                                 allowed.add(PositionType.PREVIOUS);
84                         }
85                         if (component instanceof InlineComponent && !component.getControlPoint().isFixedLength()){
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 turnRadius;
99         
100         @Override
101         public void run() {
102                 
103                 ComponentSelectionDialog dialog = new ComponentSelectionDialog(Display.getCurrent().getActiveShell(), allowed, component, libUri);
104                 
105                 // Set list of already reserved component names
106                 dialog.addUsedNames(ComponentUtils.getPipelineComponentNames(root));
107                 dialog.setLengthFactor(lengthFactor);
108                 
109                 if (dialog.open() == ComponentSelectionDialog.CANCEL)
110                         return;
111                 toAdd = dialog.getSelected();
112                 if (toAdd == null)
113                         return;
114                 this.name = dialog.getName();
115                 this.insertPosition = dialog.getInsertPosition();
116                 this.insertAdjustable = dialog.isInsertAdjustable();
117                 this.lengthAdjustable = dialog.isLenghtAdjustable();
118                 this.length = dialog.getLength();
119                 this.angle = dialog.getAngle();
120                 this.rotationAngle = dialog.getRotationAngle();
121                 this.diameter = dialog.getDiameter();
122                 this.turnRadius = dialog.getTurnRadius();
123                 allowed = dialog.filterAllowed();
124                 gizmo.setComponent(component, allowed);
125                 super.run();
126                 panel.refresh();
127         }
128         
129         @Override
130         public boolean keyPressed(KeyEvent e) {
131                 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
132                         panel.useDefaultAction();
133                 return true;
134                 
135         }
136         
137         public void attach() {
138                 if (component == null)
139                         return;
140                 
141                 super.attach();
142                 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
143                         public void run() {
144                                 attachUI();
145                         }
146                 });
147                 
148         }
149         
150         public void deattach() {
151 //              deactivate();
152                 component = null;
153                 nodeMap.commit("Add component");
154                 deattachUI();
155                 super.deattach();
156                 panel.refresh();
157         }
158         
159         private void attachUI() {
160                 //panel.setCursor(activeCursor);
161                 gizmo.attach(panel);
162         }
163         
164         private void deattachUI() {
165                 //panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
166                 gizmo.deattach();
167         }
168         
169         @Override
170         public boolean mouseMoved(MouseEvent e) {
171                 return getDefaultAction().mouseMoved(e);
172         }
173         
174         @Override
175         public boolean mousePressed(MouseEvent e) {
176                 return getDefaultAction().mousePressed(e);
177         }
178
179         @Override
180         public boolean mouseReleased(MouseEvent e) {
181                 return getDefaultAction().mouseReleased(e);
182         }
183         
184         @Override
185         public boolean mouseDragged(MouseEvent e) {
186                 return getDefaultAction().mouseDragged(e);
187         }
188         
189         public void doInsert(PositionType position) {
190                 try  {
191                         if (position == PositionType.SPLIT && length != null && length > component.getControlPoint().getLength()) {
192                                 MessageDialog.openError(panel.getComponent().getShell(), "Error", "There is no room for a component of length " + length * lengthFactor + " units");
193                                 return;
194                         }
195                         
196                         InsertInstruction inst = new InsertInstruction();
197                         inst.typeUri = toAdd.getUri();
198                         inst.name = name;
199                         inst.angle = angle != null ? MathTools.degToRad(angle) : null;
200                         inst.diameter = diameter;
201                         inst.length = length;
202                         inst.turnRadius = turnRadius;
203                         inst.insertPosition = insertPosition;
204                         inst.rotationAngle = rotationAngle;
205                         inst.position = position;
206                         ComponentUtils.addComponent(root, component, inst);
207                 } catch (Exception e) {
208                         ExceptionUtils.logAndShowError("Cannot add component", e);
209                 }
210         }
211         
212         public boolean mouseClicked(MouseEvent e) {
213                 if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) {
214                         int type = panel.getPickType();
215                         //panel.setPickType(0);
216                         panel.setPickType(5);
217                         vtkProp[] picked = panel.pick(e.getX(), e.getY());
218                         panel.setPickType(type);
219                         PositionType position = gizmo.getPickedPosition(picked);
220
221                         if (position != null) {
222                                 doInsert(position);
223                                 panel.useDefaultAction();
224                                 return true;
225                         }
226                 } 
227                 return getDefaultAction().mouseClicked(e);
228         }
229         
230         @Override
231         public boolean mouseWheelMoved(MouseWheelEvent e) {
232                 return getDefaultAction().mouseWheelMoved(e);
233         }
234 }