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.jface.resource.ResourceLocator;
10 import org.eclipse.swt.widgets.Display;
11 import org.simantics.db.Resource;
12 import org.simantics.g3d.math.MathTools;
13 import org.simantics.g3d.scenegraph.NodeMap;
14 import org.simantics.g3d.scenegraph.base.INode;
15 import org.simantics.g3d.vtk.swt.InteractiveVtkComposite;
16 import org.simantics.g3d.vtk.swt.vtkSwtAction;
17 import org.simantics.plant3d.Activator;
18 import org.simantics.plant3d.dialog.ComponentSelectionDialog;
19 import org.simantics.plant3d.gizmo.TerminalSelectionGizmo;
20 import org.simantics.plant3d.scenegraph.InlineComponent;
21 import org.simantics.plant3d.scenegraph.Nozzle;
22 import org.simantics.plant3d.scenegraph.P3DRootNode;
23 import org.simantics.plant3d.scenegraph.PipelineComponent;
24 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.PositionType;
25 import org.simantics.plant3d.utils.ComponentUtils;
26 import org.simantics.plant3d.utils.ComponentUtils.InsertInstruction;
27 import org.simantics.plant3d.utils.Item;
28 import org.simantics.utils.threads.ThreadUtils;
29 import org.simantics.utils.ui.ExceptionUtils;
33 public class AddComponentAction extends vtkSwtAction {
36 private P3DRootNode root;
37 private PipelineComponent component;
38 private NodeMap<Resource,vtkProp,INode> nodeMap;
40 private TerminalSelectionGizmo gizmo;
42 private Set<PositionType> allowed = new HashSet<PositionType>();
44 private Item toAdd = null;
45 private PositionType insertPosition;
46 @SuppressWarnings("unused")
47 private boolean insertAdjustable;
48 @SuppressWarnings("unused")
49 private boolean lengthAdjustable;
51 private String libUri;
53 private double lengthFactor = 1.0;
55 public AddComponentAction(InteractiveVtkComposite panel, P3DRootNode root, String libUri) {
58 setText("Add Component");
59 setImageDescriptor(ResourceLocator.imageDescriptorFromBundle(Activator.PLUGIN_ID, "icons/Component.png").get());
60 nodeMap = root.getNodeMap();
61 gizmo = new TerminalSelectionGizmo(panel);
65 public void setLengthFactor(double lengthFactor) {
66 this.lengthFactor = lengthFactor;
69 public void setComponent(PipelineComponent component) {
70 this.component = component;
73 if (component instanceof Nozzle) {
74 if (component.getNext() == null && component.getPrevious() == null) {
75 allowed.add(PositionType.NEXT);
78 if (component.getNext() == null) {
79 allowed.add(PositionType.NEXT);
81 if (component.getPrevious() == null) {
82 allowed.add(PositionType.PREVIOUS);
84 if (component instanceof InlineComponent && !component.getControlPoint().isFixedLength()){
85 allowed.add(PositionType.SPLIT);
88 setEnabled(allowed.size() > 0);
93 private Double length;
95 private Double rotationAngle;
96 private Double diameter;
97 private Double turnRadius;
102 ComponentSelectionDialog dialog = new ComponentSelectionDialog(Display.getCurrent().getActiveShell(), allowed, component, libUri);
104 // Set list of already reserved component names
105 dialog.addUsedNames(ComponentUtils.getPipelineComponentNames(root));
106 dialog.setLengthFactor(lengthFactor);
108 if (dialog.open() == ComponentSelectionDialog.CANCEL)
110 toAdd = dialog.getSelected();
113 this.name = dialog.getName();
114 this.insertPosition = dialog.getInsertPosition();
115 this.insertAdjustable = dialog.isInsertAdjustable();
116 this.lengthAdjustable = dialog.isLenghtAdjustable();
117 this.length = dialog.getLength();
118 this.angle = dialog.getAngle();
119 this.rotationAngle = dialog.getRotationAngle();
120 this.diameter = dialog.getDiameter();
121 this.turnRadius = dialog.getTurnRadius();
122 allowed = dialog.filterAllowed();
123 gizmo.setComponent(component, allowed);
129 public boolean keyPressed(KeyEvent e) {
130 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
131 panel.useDefaultAction();
136 public void attach() {
137 if (component == null)
141 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
149 public void deattach() {
152 nodeMap.commit("Add component");
158 private void attachUI() {
159 //panel.setCursor(activeCursor);
163 private void deattachUI() {
164 //panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
169 public boolean mouseMoved(MouseEvent e) {
170 return getDefaultAction().mouseMoved(e);
174 public boolean mousePressed(MouseEvent e) {
175 return getDefaultAction().mousePressed(e);
179 public boolean mouseReleased(MouseEvent e) {
180 return getDefaultAction().mouseReleased(e);
184 public boolean mouseDragged(MouseEvent e) {
185 return getDefaultAction().mouseDragged(e);
188 public void doInsert(PositionType position) {
190 InsertInstruction inst = new InsertInstruction();
191 inst.typeUri = toAdd.getUri();
193 inst.angle = angle != null ? MathTools.degToRad(angle) : null;
194 inst.diameter = diameter;
195 inst.length = length;
196 inst.turnRadius = turnRadius;
197 inst.insertPosition = insertPosition;
198 inst.rotationAngle = rotationAngle;
199 inst.position = position;
200 ComponentUtils.addComponent(root, component, inst);
201 } catch (Exception e) {
202 ExceptionUtils.logAndShowError("Cannot add component", e);
206 public boolean mouseClicked(MouseEvent e) {
207 if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) {
208 int type = panel.getPickType();
209 //panel.setPickType(0);
210 panel.setPickType(5);
211 vtkProp[] picked = panel.pick(e.getX(), e.getY());
212 panel.setPickType(type);
213 PositionType position = gizmo.getPickedPosition(picked);
215 if (position != null) {
217 panel.useDefaultAction();
221 return getDefaultAction().mouseClicked(e);
225 public boolean mouseWheelMoved(MouseWheelEvent e) {
226 return getDefaultAction().mouseWheelMoved(e);