1 package org.simantics.plant3d.actions;
3 import java.awt.event.KeyEvent;
4 import java.awt.event.MouseEvent;
5 import java.awt.event.MouseWheelEvent;
6 import java.util.HashSet;
9 import javax.vecmath.Vector3d;
11 import org.eclipse.swt.widgets.Display;
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.PipeRun;
23 import org.simantics.plant3d.scenegraph.PipelineComponent;
24 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
25 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.Direction;
26 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.PositionType;
27 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
28 import org.simantics.plant3d.utils.ComponentUtils;
29 import org.simantics.plant3d.utils.Item;
30 import org.simantics.plant3d.utils.ComponentUtils.InsertInstruction;
31 import org.simantics.plant3d.utils.Item.Type;
32 import org.simantics.utils.threads.ThreadUtils;
33 import org.simantics.utils.ui.ExceptionUtils;
37 public class AddComponentAction extends vtkSwtAction {
40 private P3DRootNode root;
41 private PipelineComponent component;
42 private NodeMap<vtkProp,INode> nodeMap;
44 private TerminalSelectionGizmo gizmo;
46 private Set<PositionType> allowed = new HashSet<PositionType>();
48 private Item toAdd = null;
49 private PositionType insertPosition;
50 private boolean insertAdjustable;
51 private boolean lengthAdjustable;
53 public AddComponentAction(InteractiveVtkComposite panel, P3DRootNode root) {
56 setText("Add Component");
57 setImageDescriptor(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/Component.png"));
58 nodeMap = root.getNodeMap();
59 gizmo = new TerminalSelectionGizmo(panel);
62 public void setComponent(PipelineComponent component) {
63 this.component = component;
66 if (component instanceof Nozzle) {
67 if (component.getNext() == null && component.getPrevious() == null) {
68 allowed.add(PositionType.NEXT);
71 if (component.getNext() == null) {
72 allowed.add(PositionType.NEXT);
74 if (component.getPrevious() == null) {
75 allowed.add(PositionType.PREVIOUS);
77 if (component instanceof InlineComponent && !component.getControlPoint().isFixed()){
78 allowed.add(PositionType.SPLIT);
81 setEnabled(allowed.size() > 0);
84 private Double length;
86 private Double diameter;
87 private Double turnRadius;
92 ComponentSelectionDialog dialog = new ComponentSelectionDialog(Display.getCurrent().getActiveShell(), allowed, component);
93 if (dialog.open() == ComponentSelectionDialog.CANCEL)
95 toAdd = dialog.getSelected();
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);
112 public boolean keyPressed(KeyEvent e) {
113 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
114 panel.useDefaultAction();
119 public void attach() {
120 if (component == null)
124 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
132 public void deattach() {
135 nodeMap.commit("Add component");
141 private void attachUI() {
142 //panel.setCursor(activeCursor);
146 private void deattachUI() {
147 //panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
152 public boolean mouseMoved(MouseEvent e) {
153 return getDefaultAction().mouseMoved(e);
157 public boolean mousePressed(MouseEvent e) {
158 return getDefaultAction().mousePressed(e);
162 public boolean mouseReleased(MouseEvent e) {
163 return getDefaultAction().mouseReleased(e);
167 public boolean mouseDragged(MouseEvent e) {
168 return getDefaultAction().mouseDragged(e);
171 public void doInsert(PositionType position) {
173 InsertInstruction inst = new InsertInstruction();
174 inst.typeUri = toAdd.getUri();
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);
186 // PipelineComponent newComponent = ComponentUtils.createComponent(root,toAdd.getUri());
187 // PipeControlPoint newPcp = newComponent.getControlPoint();
189 // PipeControlPoint toPcp = component.getControlPoint();
190 // PipeRun pipeRun = toPcp.getPipeRun();
192 // Vector3d dir = null;
193 // Vector3d pos = null;
196 // if (toPcp.isInline()) {
197 // switch (position) {
199 // if (toPcp.isDualInline())
200 // toPcp = toPcp.getSubPoint().get(0);
204 // if (toPcp.isDualSub())
205 // toPcp = toPcp.parent;
207 // Vector3d start = new Vector3d();
208 // Vector3d end = new Vector3d();
209 // dir = new Vector3d();
210 // toPcp.getInlineControlPointEnds(start, end, dir);
212 // switch (position) {
214 // pos = new Vector3d(end);
217 // pos = new Vector3d(start);
220 // pos = new Vector3d(toPcp.getWorldPosition());
224 // } else if (toPcp.isDirected()) {
225 // dir = new Vector3d(toPcp.getDirection(Direction.NEXT));
226 // pos = new Vector3d(toPcp.getWorldPosition());
227 // } else if (toPcp.isTurn() && toPcp.isFixed()) {
228 // dir = new Vector3d(toPcp.getDirection(position == PositionType.NEXT ? Direction.NEXT : Direction.PREVIOUS));
229 // pos = new Vector3d(toPcp.getWorldPosition());
230 // if (!lengthAdjustable) {
231 // Vector3d v = new Vector3d(dir);
232 // v.scale(toPcp.getInlineLength());
235 // if (insertPosition == PositionType.NEXT) {
236 // Vector3d v = new Vector3d(dir);
237 // v.scale(toPcp.getInlineLength());
239 // } else if (insertPosition == PositionType.SPLIT) {
240 // // scale 0.5*length so that we don't remove the length twice from the new component
241 // Vector3d v = new Vector3d(dir);
242 // v.scale(toPcp.getInlineLength()*0.5);
249 // if (!toAdd.isSizeChange()) {
250 // String name = component.getPipeRun().getUniqueName(toAdd.getName());
251 // newComponent.setName(name);
253 // pipeRun.addChild(newComponent);
254 // if (toAdd.isVariable()) {
255 // // TODO: these options are not stored into DB. Should they?!
256 // if (toAdd.getType() == Type.INLINE) {
257 // newPcp.setLength(length);
258 //// newPcp.setFixed(true);
259 // } else if (toAdd.getType() == Type.TURN) {
260 // newPcp.setTurnAngle(angle);
261 //// newPcp.setFixed(true);
264 // newComponent.updateParameters();
266 // Vector3d v = new Vector3d(dir);
267 // if (insertAdjustable) {
268 // if (insertPosition == PositionType.NEXT)
269 // v.scale(newComponent.getControlPoint().getInlineLength());
270 // else if (insertPosition == PositionType.SPLIT)
272 // else if (insertPosition == PositionType.PREVIOUS)
273 // v.scale(-newComponent.getControlPoint().getInlineLength());
275 // v.scale(newComponent.getControlPoint().getInlineLength());
277 // switch (position) {
288 // switch (position) {
290 // if (toPcp.isDualInline())
291 // toPcp = toPcp.getSubPoint().get(0);
292 // newPcp.insert(toPcp, Direction.NEXT);
293 // newPcp.setWorldPosition(pos);
296 // if (toPcp.isDualSub())
297 // toPcp = toPcp.parent;
298 // newPcp.insert(toPcp, Direction.PREVIOUS);
299 // newPcp.setWorldPosition(pos);
302 // PipingRules.splitVariableLengthComponent(newComponent, (InlineComponent)component, true);
305 // PipeRun other = new PipeRun();
306 // String n = root.getUniqueName("PipeRun");
308 // other.setPipeDiameter(diameter);
309 // other.setTurnRadius(turnRadius);
310 // root.addChild(other);
313 // if (position == PositionType.NEXT) {
314 // PipingRules.addSizeChange(false, pipeRun, other, (InlineComponent)newComponent, toPcp, null);
315 // } else if (position == PositionType.PREVIOUS){
316 // PipingRules.addSizeChange(true, pipeRun, other, (InlineComponent)newComponent, toPcp, null);
318 // newPcp.setWorldPosition(pos);
319 // // TODO : chicken-egg problem
320 // newComponent.updateParameters();
321 // Vector3d v = new Vector3d(dir);
322 // v.scale(newComponent.getControlPoint().getLength()*0.5);
323 // switch (position) {
333 // newPcp.setWorldPosition(pos);
338 // } catch (Exception e) {
339 // ExceptionUtils.logAndShowError("Cannot add component", e);
343 public boolean mouseClicked(MouseEvent e) {
344 if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) {
345 int type = panel.getPickType();
346 //panel.setPickType(0);
347 panel.setPickType(5);
348 vtkProp[] picked = panel.pick(e.getX(), e.getY());
349 panel.setPickType(type);
350 PositionType position = gizmo.getPickedPosition(picked);
352 if (position != null) {
354 panel.useDefaultAction();
358 return getDefaultAction().mouseClicked(e);
362 public boolean mouseWheelMoved(MouseWheelEvent e) {
363 return getDefaultAction().mouseWheelMoved(e);