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;
55 private String lengthUnit = "m";
57 public AddComponentAction(InteractiveVtkComposite panel, P3DRootNode root, String libUri) {
60 setText("Add Component");
61 setImageDescriptor(ResourceLocator.imageDescriptorFromBundle(Activator.PLUGIN_ID, "icons/Component.png").get());
62 nodeMap = root.getNodeMap();
63 gizmo = new TerminalSelectionGizmo(panel);
67 public void setLengthFactor(double lengthFactor) {
68 this.lengthFactor = lengthFactor;
71 public void setLengthUnit(String lengthUnit) {
72 this.lengthUnit = lengthUnit;
75 public void setComponent(PipelineComponent component) {
76 this.component = component;
79 if (component instanceof Nozzle) {
80 if (component.getNext() == null && component.getPrevious() == null) {
81 allowed.add(PositionType.NEXT);
84 if (component.getNext() == null || component.getControlPoint().isVariableLength()) {
85 allowed.add(PositionType.NEXT);
87 if (component.getPrevious() == null || component.getControlPoint().isVariableLength()) {
88 allowed.add(PositionType.PREVIOUS);
90 if (component instanceof InlineComponent && component.getControlPoint().isVariableLength()){
91 allowed.add(PositionType.SPLIT);
94 setEnabled(allowed.size() > 0);
99 private Double length;
100 private Double angle;
101 private Double rotationAngle;
102 private Double diameter;
103 private Double thickness;
104 private Double turnRadius;
109 ComponentSelectionDialog dialog = new ComponentSelectionDialog(Display.getCurrent().getActiveShell(), allowed, component, libUri);
111 // Set list of already reserved component names
112 dialog.addUsedNames(ComponentUtils.getPipelineComponentNames(root));
113 dialog.setLengthFactor(lengthFactor);
114 dialog.setLengthUnit(lengthUnit);
116 if (dialog.open() == ComponentSelectionDialog.CANCEL)
118 toAdd = dialog.getSelected();
121 this.name = dialog.getName();
122 this.insertPosition = dialog.getInsertPosition();
123 this.insertAdjustable = dialog.isInsertAdjustable();
124 this.lengthAdjustable = dialog.isLenghtAdjustable();
125 this.length = dialog.getLength();
126 this.angle = dialog.getAngle();
127 this.rotationAngle = dialog.getRotationAngle();
128 this.diameter = dialog.getDiameter();
129 this.thickness = dialog.getThickness();
130 this.turnRadius = dialog.getTurnRadius();
131 allowed = dialog.filterAllowed();
132 gizmo.setComponent(component, allowed);
138 public boolean keyPressed(KeyEvent e) {
139 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
140 panel.useDefaultAction();
145 public void attach() {
146 if (component == null)
150 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
158 public void deattach() {
161 nodeMap.commit("Add component");
167 private void attachUI() {
168 //panel.setCursor(activeCursor);
172 private void deattachUI() {
173 //panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
178 public boolean mouseMoved(MouseEvent e) {
179 return getDefaultAction().mouseMoved(e);
183 public boolean mousePressed(MouseEvent e) {
184 return getDefaultAction().mousePressed(e);
188 public boolean mouseReleased(MouseEvent e) {
189 return getDefaultAction().mouseReleased(e);
193 public boolean mouseDragged(MouseEvent e) {
194 return getDefaultAction().mouseDragged(e);
197 public void doInsert(PositionType position) {
199 if (position == PositionType.SPLIT && length != null && length > component.getControlPoint().getLength()) {
200 MessageDialog.openError(panel.getComponent().getShell(), "Error", "There is no room for a component of length " + length * lengthFactor + " units");
204 InsertInstruction inst = new InsertInstruction();
205 inst.typeUri = toAdd.getUri();
207 inst.angle = angle != null ? MathTools.degToRad(angle) : null;
208 inst.diameter = diameter;
209 inst.thickness = thickness;
210 inst.length = length;
211 inst.turnRadius = turnRadius;
212 inst.insertPosition = insertPosition;
213 inst.rotationAngle = rotationAngle;
214 inst.position = position;
215 PipelineComponent newComponent = ComponentUtils.addComponent(root, component, inst);
216 componentAdded(newComponent);
217 } catch (Exception e) {
218 ExceptionUtils.logAndShowError("Cannot add component", e);
223 * This method does nothing, but can be overridden by a subclass to do additional
224 * post-processing for a newly added component.
226 * @param newComponent A newly added pipeline component
228 protected void componentAdded(PipelineComponent newComponent) {
229 // Nothing to do here
232 public boolean mouseClicked(MouseEvent e) {
233 if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) {
234 int type = panel.getPickType();
235 //panel.setPickType(0);
236 panel.setPickType(5);
237 vtkProp[] picked = panel.pick(e.getX(), e.getY());
238 panel.setPickType(type);
239 PositionType position = gizmo.getPickedPosition(picked);
241 if (position != null) {
243 panel.useDefaultAction();
247 return getDefaultAction().mouseClicked(e);
251 public boolean mouseWheelMoved(MouseWheelEvent e) {
252 return getDefaultAction().mouseWheelMoved(e);