]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/actions/AddComponentAction.java
5aaa9c71d646c1e5d4499e6ae71b323407edb3ca
[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.util.HashSet;
6 import java.util.Set;
7
8 import javax.vecmath.Vector3d;
9
10 import org.eclipse.swt.widgets.Display;
11 import org.simantics.g3d.scenegraph.NodeMap;
12 import org.simantics.g3d.scenegraph.base.INode;
13 import org.simantics.g3d.vtk.swt.InteractiveVtkComposite;
14 import org.simantics.g3d.vtk.swt.vtkSwtAction;
15 import org.simantics.plant3d.Activator;
16 import org.simantics.plant3d.dialog.ComponentSelectionDialog;
17 import org.simantics.plant3d.gizmo.TerminalSelectionGizmo;
18 import org.simantics.plant3d.scenegraph.InlineComponent;
19 import org.simantics.plant3d.scenegraph.P3DRootNode;
20 import org.simantics.plant3d.scenegraph.PipeRun;
21 import org.simantics.plant3d.scenegraph.PipelineComponent;
22 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
23 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.Direction;
24 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.PositionType;
25 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
26 import org.simantics.plant3d.utils.ComponentUtils;
27 import org.simantics.plant3d.utils.Item;
28 import org.simantics.plant3d.utils.Item.Type;
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<vtkProp,INode> nodeMap;
40         
41         private TerminalSelectionGizmo gizmo;
42         
43         private Set<PositionType> allowed = new HashSet<PositionType>();
44         
45         private Item toAdd = null;
46         
47         public AddComponentAction(InteractiveVtkComposite panel, P3DRootNode root) {
48                 super(panel);
49                 this.root = root;
50                 setText("Add Component");
51                 setImageDescriptor(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/Component.png"));
52                 nodeMap = root.getNodeMap();
53                 gizmo = new TerminalSelectionGizmo(panel);
54         }
55         
56         public void setComponent(PipelineComponent component) {
57                 this.component = component;
58                 
59                 allowed.clear();
60                 if (component.getNext() == null) {
61                         allowed.add(PositionType.NEXT);
62                 }
63                 if (component.getPrevious() == null) {
64                         allowed.add(PositionType.PREVIOUS);
65                 }
66                 if (component instanceof InlineComponent && !component.getControlPoint().isFixed()){
67                         allowed.add(PositionType.SPLIT);
68                 }
69                 setEnabled(allowed.size() > 0);
70         }
71         
72         private Double length;
73         private Double angle;
74         private Double diameter;
75         private Double turnRadius;
76         
77         @Override
78         public void run() {
79                 ComponentSelectionDialog dialog = new ComponentSelectionDialog(Display.getCurrent().getActiveShell(), allowed);
80                 if (dialog.open() == ComponentSelectionDialog.CANCEL)
81                         return;
82                 toAdd = dialog.getSelected();
83                 if (toAdd == null)
84                         return;
85                 this.length = dialog.getLength();
86                 this.angle = dialog.getAngle();
87                 this.diameter = dialog.getDiameter();
88                 this.turnRadius = dialog.getTurnRadius();
89                 allowed = dialog.filterAllowed();
90                 gizmo.setComponent(component, allowed);
91                 super.run();
92                 panel.refresh();
93         }
94         
95         @Override
96         public boolean keyPressed(KeyEvent e) {
97                 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
98                         panel.useDefaultAction();
99                 return true;
100                 
101         }
102         
103         public void attach() {
104                 if (component == null)
105                         return;
106                 
107                 super.attach();
108                 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
109                         public void run() {
110                                 attachUI();
111                         }
112                 });
113                 
114         }
115         
116         public void deattach() {
117 //              deactivate();
118                 component = null;
119                 nodeMap.commit();
120                 deattachUI();
121                 super.deattach();
122                 panel.refresh();
123         }
124         
125         private void attachUI() {
126                 //panel.setCursor(activeCursor);
127                 gizmo.attach(panel);
128         }
129         
130         private void deattachUI() {
131                 //panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
132                 gizmo.deattach();
133         }
134         
135         @Override
136         public boolean mouseMoved(MouseEvent e) {
137                 return getDefaultAction().mouseMoved(e);
138         }
139         
140         @Override
141         public boolean mousePressed(MouseEvent e) {
142                 return getDefaultAction().mousePressed(e);
143         }
144
145         @Override
146         public boolean mouseReleased(MouseEvent e) {
147                 return getDefaultAction().mouseReleased(e);
148         }
149         
150         @Override
151         public boolean mouseDragged(MouseEvent e) {
152                 return getDefaultAction().mouseDragged(e);
153         }
154         
155         public void doInsert(PositionType position) {
156                 try  {
157                         PipelineComponent newComponent = ComponentUtils.createComponent(root,toAdd.getUri());
158                         PipeControlPoint newPcp = newComponent.getControlPoint();
159                         
160                         PipeControlPoint toPcp = component.getControlPoint();
161                         
162                         
163                         switch (position) {
164                         case NEXT: 
165                                 if (toPcp.isDualInline())
166                                         toPcp = toPcp.getSubPoint().get(0);
167                                 
168                                 break;
169                         case PREVIOUS:
170                                 if (toPcp.isDualSub())
171                                         toPcp = toPcp.parent;
172                         }
173                         
174                         Vector3d start = new Vector3d();
175                         Vector3d end = new Vector3d();
176                         Vector3d dir = new Vector3d();
177                         toPcp.getInlineControlPointEnds(start, end, dir);
178                         dir.normalize();
179                         
180                         PipeRun pipeRun = toPcp.getPipeRun();
181                         
182                         if (!toAdd.isSizeChange()) {
183                                 String name = component.getPipeRun().getUniqueName(toAdd.getName());
184                                 newComponent.setName(name);
185
186                                 pipeRun.addChild(newComponent);
187                                 if (toAdd.isVariable()) {
188                                         // TODO: these options are not stored into DB. Should they?!
189                                         if (toAdd.getType() == Type.INLINE) {
190                                                 newPcp.setLength(length);
191 //                                              newPcp.setFixed(true);
192                                         } else if (toAdd.getType() == Type.TURN) {
193                                                 newPcp.setTurnAngle(angle);
194 //                                              newPcp.setFixed(true);
195                                         }
196                                 }
197                                 newComponent.updateParameters();
198
199                                 dir.scale(newComponent.getControlPoint().getLength()*0.5);
200                                 start.sub(dir);
201                                 end.add(dir);
202                                 switch (position) {
203                                 case NEXT: 
204                                         if (toPcp.isDualInline())
205                                                 toPcp = toPcp.getSubPoint().get(0);
206                                         newPcp.insert(toPcp, Direction.NEXT);
207                                         newPcp.setWorldPosition(end);
208                                         break;
209                                 case PREVIOUS:
210                                         if (toPcp.isDualSub())
211                                                 toPcp = toPcp.parent;
212                                         newPcp.insert(toPcp, Direction.PREVIOUS);
213                                         newPcp.setWorldPosition(start);
214                                         break;
215                                 case SPLIT:
216                                         PipingRules.splitVariableLengthComponent(newComponent, (InlineComponent)component, true);
217                                 }
218                         } else {
219                                 
220                                 
221                                 PipeRun other = new PipeRun();
222                                 String n = root.getUniqueName("PipeRun");
223                                 other.setName(n);
224                                 other.setPipeDiameter(diameter);
225                                 other.setTurnRadius(turnRadius);
226                                 root.addChild(other);
227                                 
228                                 
229                                 
230                                 if (position == PositionType.NEXT) {
231                                         PipingRules.addSizeChange(false, pipeRun, other, (InlineComponent)newComponent, toPcp, null);
232                                         newPcp.setWorldPosition(end);
233                         } else if (position == PositionType.PREVIOUS){
234                                 PipingRules.addSizeChange(true, pipeRun, other, (InlineComponent)newComponent, toPcp, null);
235                                 newPcp.setWorldPosition(start);
236                         }
237                                 // TODO : chicken-egg problem
238                                 newComponent.updateParameters();
239                                 dir.scale(newComponent.getControlPoint().getLength()*0.5);
240                                 start.sub(dir);
241                                 end.add(dir);
242                                 if (position == PositionType.NEXT) {
243                                         newPcp.setWorldPosition(end);
244                                 } else if (position == PositionType.PREVIOUS){
245                                         newPcp.setWorldPosition(start);
246                                 }
247                         }
248                         
249                         
250                 } catch (Exception e) {
251                         ExceptionUtils.logAndShowError("Cannot add component", e);
252                 }
253         }
254         
255         public boolean mouseClicked(MouseEvent e) {
256                 if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) {
257                         int type = panel.getPickType();
258                         //panel.setPickType(0);
259                         panel.setPickType(5);
260                         vtkProp[] picked = panel.pick(e.getX(), e.getY());
261                         panel.setPickType(type);
262                         PositionType position = gizmo.getPickedPosition(picked);
263
264                         if (position != null) {
265                                 doInsert(position);
266                                 panel.useDefaultAction();
267                                 return true;
268                         }
269                 } 
270                 return getDefaultAction().mouseClicked(e);
271         }
272 }