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 public AddComponentAction(InteractiveVtkComposite panel, P3DRootNode root, String libUri) {
56 setText("Add Component");
57 setImageDescriptor(ResourceLocator.imageDescriptorFromBundle(Activator.PLUGIN_ID, "icons/Component.png").get());
58 nodeMap = root.getNodeMap();
59 gizmo = new TerminalSelectionGizmo(panel);
63 public void setComponent(PipelineComponent component) {
64 this.component = component;
67 if (component instanceof Nozzle) {
68 if (component.getNext() == null && component.getPrevious() == null) {
69 allowed.add(PositionType.NEXT);
72 if (component.getNext() == null) {
73 allowed.add(PositionType.NEXT);
75 if (component.getPrevious() == null) {
76 allowed.add(PositionType.PREVIOUS);
78 if (component instanceof InlineComponent && !component.getControlPoint().isFixedLength()){
79 allowed.add(PositionType.SPLIT);
82 setEnabled(allowed.size() > 0);
87 private Double length;
89 private Double rotationAngle;
90 private Double diameter;
91 private Double turnRadius;
96 ComponentSelectionDialog dialog = new ComponentSelectionDialog(Display.getCurrent().getActiveShell(), allowed, component, libUri);
98 // Set list of already reserved component names
99 dialog.addUsedNames(ComponentUtils.getPipelineComponentNames(root));
101 if (dialog.open() == ComponentSelectionDialog.CANCEL)
103 toAdd = dialog.getSelected();
106 this.name = dialog.getName();
107 this.insertPosition = dialog.getInsertPosition();
108 this.insertAdjustable = dialog.isInsertAdjustable();
109 this.lengthAdjustable = dialog.isLenghtAdjustable();
110 this.length = dialog.getLength();
111 this.angle = dialog.getAngle();
112 this.rotationAngle = dialog.getRotationAngle();
113 this.diameter = dialog.getDiameter();
114 this.turnRadius = dialog.getTurnRadius();
115 allowed = dialog.filterAllowed();
116 gizmo.setComponent(component, allowed);
122 public boolean keyPressed(KeyEvent e) {
123 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
124 panel.useDefaultAction();
129 public void attach() {
130 if (component == null)
134 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
142 public void deattach() {
145 nodeMap.commit("Add component");
151 private void attachUI() {
152 //panel.setCursor(activeCursor);
156 private void deattachUI() {
157 //panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
162 public boolean mouseMoved(MouseEvent e) {
163 return getDefaultAction().mouseMoved(e);
167 public boolean mousePressed(MouseEvent e) {
168 return getDefaultAction().mousePressed(e);
172 public boolean mouseReleased(MouseEvent e) {
173 return getDefaultAction().mouseReleased(e);
177 public boolean mouseDragged(MouseEvent e) {
178 return getDefaultAction().mouseDragged(e);
181 public void doInsert(PositionType position) {
183 InsertInstruction inst = new InsertInstruction();
184 inst.typeUri = toAdd.getUri();
186 inst.angle = angle != null ? MathTools.degToRad(angle) : null;
187 inst.diameter = diameter;
188 inst.length = length;
189 inst.turnRadius = turnRadius;
190 inst.insertPosition = insertPosition;
191 inst.rotationAngle = rotationAngle;
192 inst.position = position;
193 ComponentUtils.addComponent(root, component, inst);
194 } catch (Exception e) {
195 ExceptionUtils.logAndShowError("Cannot add component", e);
199 public boolean mouseClicked(MouseEvent e) {
200 if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) {
201 int type = panel.getPickType();
202 //panel.setPickType(0);
203 panel.setPickType(5);
204 vtkProp[] picked = panel.pick(e.getX(), e.getY());
205 panel.setPickType(type);
206 PositionType position = gizmo.getPickedPosition(picked);
208 if (position != null) {
210 panel.useDefaultAction();
214 return getDefaultAction().mouseClicked(e);
218 public boolean mouseWheelMoved(MouseWheelEvent e) {
219 return getDefaultAction().mouseWheelMoved(e);