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 private String libUri;
55 public AddComponentAction(InteractiveVtkComposite panel, P3DRootNode root, String libUri) {
58 setText("Add Component");
59 setImageDescriptor(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/Component.png"));
60 nodeMap = root.getNodeMap();
61 gizmo = new TerminalSelectionGizmo(panel);
65 public void setComponent(PipelineComponent component) {
66 this.component = component;
69 if (component instanceof Nozzle) {
70 if (component.getNext() == null && component.getPrevious() == null) {
71 allowed.add(PositionType.NEXT);
74 if (component.getNext() == null) {
75 allowed.add(PositionType.NEXT);
77 if (component.getPrevious() == null) {
78 allowed.add(PositionType.PREVIOUS);
80 if (component instanceof InlineComponent && !component.getControlPoint().isFixed()){
81 allowed.add(PositionType.SPLIT);
84 setEnabled(allowed.size() > 0);
87 private Double length;
89 private Double diameter;
90 private Double turnRadius;
95 ComponentSelectionDialog dialog = new ComponentSelectionDialog(Display.getCurrent().getActiveShell(), allowed, component, libUri);
96 if (dialog.open() == ComponentSelectionDialog.CANCEL)
98 toAdd = dialog.getSelected();
101 this.insertPosition = dialog.getInsertPosition();
102 this.insertAdjustable = dialog.isInsertAdjustable();
103 this.lengthAdjustable = dialog.isLenghtAdjustable();
104 this.length = dialog.getLength();
105 this.angle = dialog.getAngle();
106 this.diameter = dialog.getDiameter();
107 this.turnRadius = dialog.getTurnRadius();
108 allowed = dialog.filterAllowed();
109 gizmo.setComponent(component, allowed);
115 public boolean keyPressed(KeyEvent e) {
116 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
117 panel.useDefaultAction();
122 public void attach() {
123 if (component == null)
127 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
135 public void deattach() {
138 nodeMap.commit("Add component");
144 private void attachUI() {
145 //panel.setCursor(activeCursor);
149 private void deattachUI() {
150 //panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
155 public boolean mouseMoved(MouseEvent e) {
156 return getDefaultAction().mouseMoved(e);
160 public boolean mousePressed(MouseEvent e) {
161 return getDefaultAction().mousePressed(e);
165 public boolean mouseReleased(MouseEvent e) {
166 return getDefaultAction().mouseReleased(e);
170 public boolean mouseDragged(MouseEvent e) {
171 return getDefaultAction().mouseDragged(e);
174 public void doInsert(PositionType position) {
176 InsertInstruction inst = new InsertInstruction();
177 inst.typeUri = toAdd.getUri();
179 inst.diameter = diameter;
180 inst.length = length;
181 inst.turnRadius = turnRadius;
182 inst.insertPosition = insertPosition;
183 inst.position = position;
184 ComponentUtils.addComponent(root, component, inst);
185 } catch (Exception e) {
186 ExceptionUtils.logAndShowError("Cannot add component", e);
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);
199 if (position != null) {
201 panel.useDefaultAction();
205 return getDefaultAction().mouseClicked(e);
209 public boolean mouseWheelMoved(MouseWheelEvent e) {
210 return getDefaultAction().mouseWheelMoved(e);