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 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;
32 public class AddComponentAction extends vtkSwtAction {
35 private P3DRootNode root;
36 private PipelineComponent component;
37 private NodeMap<Resource,vtkProp,INode> nodeMap;
39 private TerminalSelectionGizmo gizmo;
41 private Set<PositionType> allowed = new HashSet<PositionType>();
43 private Item toAdd = null;
44 private PositionType insertPosition;
45 private boolean insertAdjustable;
46 private boolean lengthAdjustable;
48 private String libUri;
50 public AddComponentAction(InteractiveVtkComposite panel, P3DRootNode root, String libUri) {
53 setText("Add Component");
54 setImageDescriptor(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/Component.png"));
55 nodeMap = root.getNodeMap();
56 gizmo = new TerminalSelectionGizmo(panel);
60 public void setComponent(PipelineComponent component) {
61 this.component = component;
64 if (component instanceof Nozzle) {
65 if (component.getNext() == null && component.getPrevious() == null) {
66 allowed.add(PositionType.NEXT);
69 if (component.getNext() == null) {
70 allowed.add(PositionType.NEXT);
72 if (component.getPrevious() == null) {
73 allowed.add(PositionType.PREVIOUS);
75 if (component instanceof InlineComponent && !component.getControlPoint().isFixedLength()){
76 allowed.add(PositionType.SPLIT);
79 setEnabled(allowed.size() > 0);
82 private Double length;
84 private Double diameter;
85 private Double turnRadius;
90 ComponentSelectionDialog dialog = new ComponentSelectionDialog(Display.getCurrent().getActiveShell(), allowed, component, libUri);
91 if (dialog.open() == ComponentSelectionDialog.CANCEL)
93 toAdd = dialog.getSelected();
96 this.insertPosition = dialog.getInsertPosition();
97 this.insertAdjustable = dialog.isInsertAdjustable();
98 this.lengthAdjustable = dialog.isLenghtAdjustable();
99 this.length = dialog.getLength();
100 this.angle = dialog.getAngle();
101 this.diameter = dialog.getDiameter();
102 this.turnRadius = dialog.getTurnRadius();
103 allowed = dialog.filterAllowed();
104 gizmo.setComponent(component, allowed);
110 public boolean keyPressed(KeyEvent e) {
111 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
112 panel.useDefaultAction();
117 public void attach() {
118 if (component == null)
122 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
130 public void deattach() {
133 nodeMap.commit("Add component");
139 private void attachUI() {
140 //panel.setCursor(activeCursor);
144 private void deattachUI() {
145 //panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
150 public boolean mouseMoved(MouseEvent e) {
151 return getDefaultAction().mouseMoved(e);
155 public boolean mousePressed(MouseEvent e) {
156 return getDefaultAction().mousePressed(e);
160 public boolean mouseReleased(MouseEvent e) {
161 return getDefaultAction().mouseReleased(e);
165 public boolean mouseDragged(MouseEvent e) {
166 return getDefaultAction().mouseDragged(e);
169 public void doInsert(PositionType position) {
171 InsertInstruction inst = new InsertInstruction();
172 inst.typeUri = toAdd.getUri();
173 inst.angle = angle != null ? MathTools.degToRad(angle) : null;
174 inst.diameter = diameter;
175 inst.length = length;
176 inst.turnRadius = turnRadius;
177 inst.insertPosition = insertPosition;
178 inst.position = position;
179 ComponentUtils.addComponent(root, component, inst);
180 } catch (Exception e) {
181 ExceptionUtils.logAndShowError("Cannot add component", e);
185 public boolean mouseClicked(MouseEvent e) {
186 if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) {
187 int type = panel.getPickType();
188 //panel.setPickType(0);
189 panel.setPickType(5);
190 vtkProp[] picked = panel.pick(e.getX(), e.getY());
191 panel.setPickType(type);
192 PositionType position = gizmo.getPickedPosition(picked);
194 if (position != null) {
196 panel.useDefaultAction();
200 return getDefaultAction().mouseClicked(e);
204 public boolean mouseWheelMoved(MouseWheelEvent e) {
205 return getDefaultAction().mouseWheelMoved(e);