]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/actions/AddComponentAction.java
56b0443eb203b71a16300387d7e8c7e292e3c53d
[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 diameter;
87         private Double turnRadius;
88         
89         @Override
90         public void run() {
91                 
92                 ComponentSelectionDialog dialog = new ComponentSelectionDialog(Display.getCurrent().getActiveShell(), allowed, component, libUri);
93                 if (dialog.open() == ComponentSelectionDialog.CANCEL)
94                         return;
95                 toAdd = dialog.getSelected();
96                 if (toAdd == null)
97                         return;
98                 this.insertPosition = dialog.getInsertPosition();
99                 this.insertAdjustable = dialog.isInsertAdjustable();
100                 this.lengthAdjustable = dialog.isLenghtAdjustable();
101                 this.length = dialog.getLength();
102                 this.angle = dialog.getAngle();
103                 this.diameter = dialog.getDiameter();
104                 this.turnRadius = dialog.getTurnRadius();
105                 allowed = dialog.filterAllowed();
106                 gizmo.setComponent(component, allowed);
107                 super.run();
108                 panel.refresh();
109         }
110         
111         @Override
112         public boolean keyPressed(KeyEvent e) {
113                 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
114                         panel.useDefaultAction();
115                 return true;
116                 
117         }
118         
119         public void attach() {
120                 if (component == null)
121                         return;
122                 
123                 super.attach();
124                 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
125                         public void run() {
126                                 attachUI();
127                         }
128                 });
129                 
130         }
131         
132         public void deattach() {
133 //              deactivate();
134                 component = null;
135                 nodeMap.commit("Add component");
136                 deattachUI();
137                 super.deattach();
138                 panel.refresh();
139         }
140         
141         private void attachUI() {
142                 //panel.setCursor(activeCursor);
143                 gizmo.attach(panel);
144         }
145         
146         private void deattachUI() {
147                 //panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
148                 gizmo.deattach();
149         }
150         
151         @Override
152         public boolean mouseMoved(MouseEvent e) {
153                 return getDefaultAction().mouseMoved(e);
154         }
155         
156         @Override
157         public boolean mousePressed(MouseEvent e) {
158                 return getDefaultAction().mousePressed(e);
159         }
160
161         @Override
162         public boolean mouseReleased(MouseEvent e) {
163                 return getDefaultAction().mouseReleased(e);
164         }
165         
166         @Override
167         public boolean mouseDragged(MouseEvent e) {
168                 return getDefaultAction().mouseDragged(e);
169         }
170         
171         public void doInsert(PositionType position) {
172                 try  {
173                         InsertInstruction inst = new InsertInstruction();
174                         inst.typeUri = toAdd.getUri();
175                         inst.angle = angle != null ? MathTools.degToRad(angle) : null;
176                         inst.diameter = diameter;
177                         inst.length = length;
178                         inst.turnRadius = turnRadius;
179                         inst.insertPosition = insertPosition;
180                         inst.position = position;
181                         ComponentUtils.addComponent(root, component, inst);
182                 } catch (Exception e) {
183                         ExceptionUtils.logAndShowError("Cannot add component", e);
184                 }
185         }
186         
187         public boolean mouseClicked(MouseEvent e) {
188                 if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) {
189                         int type = panel.getPickType();
190                         //panel.setPickType(0);
191                         panel.setPickType(5);
192                         vtkProp[] picked = panel.pick(e.getX(), e.getY());
193                         panel.setPickType(type);
194                         PositionType position = gizmo.getPickedPosition(picked);
195
196                         if (position != null) {
197                                 doInsert(position);
198                                 panel.useDefaultAction();
199                                 return true;
200                         }
201                 } 
202                 return getDefaultAction().mouseClicked(e);
203         }
204         
205         @Override
206         public boolean mouseWheelMoved(MouseWheelEvent e) {
207                 return getDefaultAction().mouseWheelMoved(e);
208         }
209 }