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.dialogs.MessageDialog;
10 import org.eclipse.jface.resource.ResourceLocator;
11 import org.eclipse.swt.widgets.Display;
12 import org.simantics.db.Resource;
13 import org.simantics.g3d.math.MathTools;
14 import org.simantics.g3d.scenegraph.NodeMap;
15 import org.simantics.g3d.scenegraph.base.INode;
16 import org.simantics.g3d.vtk.swt.InteractiveVtkComposite;
17 import org.simantics.g3d.vtk.swt.vtkSwtAction;
18 import org.simantics.plant3d.Activator;
19 import org.simantics.plant3d.dialog.ComponentSelectionDialog;
20 import org.simantics.plant3d.gizmo.TerminalSelectionGizmo;
21 import org.simantics.plant3d.scenegraph.InlineComponent;
22 import org.simantics.plant3d.scenegraph.Nozzle;
23 import org.simantics.plant3d.scenegraph.P3DRootNode;
24 import org.simantics.plant3d.scenegraph.PipelineComponent;
25 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.PositionType;
26 import org.simantics.plant3d.utils.ComponentUtils;
27 import org.simantics.plant3d.utils.ComponentUtils.InsertInstruction;
28 import org.simantics.plant3d.utils.Item;
29 import org.simantics.utils.threads.ThreadUtils;
30 import org.simantics.utils.ui.ExceptionUtils;
34 public class AddComponentAction extends vtkSwtAction {
37 private P3DRootNode root;
38 private PipelineComponent component;
39 private NodeMap<Resource,vtkProp,INode> nodeMap;
41 private TerminalSelectionGizmo gizmo;
43 private Set<PositionType> allowed = new HashSet<PositionType>();
45 private Item toAdd = null;
46 private PositionType insertPosition;
47 @SuppressWarnings("unused")
48 private boolean insertAdjustable;
49 @SuppressWarnings("unused")
50 private boolean lengthAdjustable;
52 private String libUri;
54 private double lengthFactor = 1.0;
56 public AddComponentAction(InteractiveVtkComposite panel, P3DRootNode root, String libUri) {
59 setText("Add Component");
60 setImageDescriptor(ResourceLocator.imageDescriptorFromBundle(Activator.PLUGIN_ID, "icons/Component.png").get());
61 nodeMap = root.getNodeMap();
62 gizmo = new TerminalSelectionGizmo(panel);
66 public void setLengthFactor(double lengthFactor) {
67 this.lengthFactor = lengthFactor;
70 public void setComponent(PipelineComponent component) {
71 this.component = component;
74 if (component instanceof Nozzle) {
75 if (component.getNext() == null && component.getPrevious() == null) {
76 allowed.add(PositionType.NEXT);
79 if (component.getNext() == null) {
80 allowed.add(PositionType.NEXT);
82 if (component.getPrevious() == null) {
83 allowed.add(PositionType.PREVIOUS);
85 if (component instanceof InlineComponent && !component.getControlPoint().isFixedLength()){
86 allowed.add(PositionType.SPLIT);
89 setEnabled(allowed.size() > 0);
94 private Double length;
96 private Double rotationAngle;
97 private Double diameter;
98 private Double thickness;
99 private Double turnRadius;
104 ComponentSelectionDialog dialog = new ComponentSelectionDialog(Display.getCurrent().getActiveShell(), allowed, component, libUri);
106 // Set list of already reserved component names
107 dialog.addUsedNames(ComponentUtils.getPipelineComponentNames(root));
108 dialog.setLengthFactor(lengthFactor);
110 if (dialog.open() == ComponentSelectionDialog.CANCEL)
112 toAdd = dialog.getSelected();
115 this.name = dialog.getName();
116 this.insertPosition = dialog.getInsertPosition();
117 this.insertAdjustable = dialog.isInsertAdjustable();
118 this.lengthAdjustable = dialog.isLenghtAdjustable();
119 this.length = dialog.getLength();
120 this.angle = dialog.getAngle();
121 this.rotationAngle = dialog.getRotationAngle();
122 this.diameter = dialog.getDiameter();
123 this.thickness = dialog.getThickness();
124 this.turnRadius = dialog.getTurnRadius();
125 allowed = dialog.filterAllowed();
126 gizmo.setComponent(component, allowed);
132 public boolean keyPressed(KeyEvent e) {
133 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
134 panel.useDefaultAction();
139 public void attach() {
140 if (component == null)
144 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
152 public void deattach() {
155 nodeMap.commit("Add component");
161 private void attachUI() {
162 //panel.setCursor(activeCursor);
166 private void deattachUI() {
167 //panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
172 public boolean mouseMoved(MouseEvent e) {
173 return getDefaultAction().mouseMoved(e);
177 public boolean mousePressed(MouseEvent e) {
178 return getDefaultAction().mousePressed(e);
182 public boolean mouseReleased(MouseEvent e) {
183 return getDefaultAction().mouseReleased(e);
187 public boolean mouseDragged(MouseEvent e) {
188 return getDefaultAction().mouseDragged(e);
191 public void doInsert(PositionType position) {
193 if (position == PositionType.SPLIT && length != null && length > component.getControlPoint().getLength()) {
194 MessageDialog.openError(panel.getComponent().getShell(), "Error", "There is no room for a component of length " + length * lengthFactor + " units");
198 InsertInstruction inst = new InsertInstruction();
199 inst.typeUri = toAdd.getUri();
201 inst.angle = angle != null ? MathTools.degToRad(angle) : null;
202 inst.diameter = diameter;
203 inst.thickness = thickness;
204 inst.length = length;
205 inst.turnRadius = turnRadius;
206 inst.insertPosition = insertPosition;
207 inst.rotationAngle = rotationAngle;
208 inst.position = position;
209 ComponentUtils.addComponent(root, component, inst);
210 } catch (Exception e) {
211 ExceptionUtils.logAndShowError("Cannot add component", e);
215 public boolean mouseClicked(MouseEvent e) {
216 if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) {
217 int type = panel.getPickType();
218 //panel.setPickType(0);
219 panel.setPickType(5);
220 vtkProp[] picked = panel.pick(e.getX(), e.getY());
221 panel.setPickType(type);
222 PositionType position = gizmo.getPickedPosition(picked);
224 if (position != null) {
226 panel.useDefaultAction();
230 return getDefaultAction().mouseClicked(e);
234 public boolean mouseWheelMoved(MouseWheelEvent e) {
235 return getDefaultAction().mouseWheelMoved(e);