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.scenegraph.NodeMap;
12 import org.simantics.g3d.scenegraph.base.INode;
13 import org.simantics.g3d.vtk.swt.InteractiveVtkComposite;
14 import org.simantics.g3d.vtk.swt.vtkSwtAction;
15 import org.simantics.plant3d.Activator;
16 import org.simantics.plant3d.dialog.ComponentSelectionDialog;
17 import org.simantics.plant3d.gizmo.TerminalSelectionGizmo;
18 import org.simantics.plant3d.scenegraph.InlineComponent;
19 import org.simantics.plant3d.scenegraph.Nozzle;
20 import org.simantics.plant3d.scenegraph.P3DRootNode;
21 import org.simantics.plant3d.scenegraph.PipelineComponent;
22 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.PositionType;
23 import org.simantics.plant3d.utils.ComponentUtils;
24 import org.simantics.plant3d.utils.ComponentUtils.InsertInstruction;
25 import org.simantics.plant3d.utils.Item;
26 import org.simantics.utils.threads.ThreadUtils;
27 import org.simantics.utils.ui.ExceptionUtils;
31 public class AddComponentAction extends vtkSwtAction {
34 private P3DRootNode root;
35 private PipelineComponent component;
36 private NodeMap<Resource,vtkProp,INode> nodeMap;
38 private TerminalSelectionGizmo gizmo;
40 private Set<PositionType> allowed = new HashSet<PositionType>();
42 private Item toAdd = null;
43 private PositionType insertPosition;
44 private boolean insertAdjustable;
45 private boolean lengthAdjustable;
47 private String libUri;
49 public AddComponentAction(InteractiveVtkComposite panel, P3DRootNode root, String libUri) {
52 setText("Add Component");
53 setImageDescriptor(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/Component.png"));
54 nodeMap = root.getNodeMap();
55 gizmo = new TerminalSelectionGizmo(panel);
59 public void setComponent(PipelineComponent component) {
60 this.component = component;
63 if (component instanceof Nozzle) {
64 if (component.getNext() == null && component.getPrevious() == null) {
65 allowed.add(PositionType.NEXT);
68 if (component.getNext() == null) {
69 allowed.add(PositionType.NEXT);
71 if (component.getPrevious() == null) {
72 allowed.add(PositionType.PREVIOUS);
74 if (component instanceof InlineComponent && !component.getControlPoint().isFixed()){
75 allowed.add(PositionType.SPLIT);
78 setEnabled(allowed.size() > 0);
81 private Double length;
83 private Double diameter;
84 private Double turnRadius;
89 ComponentSelectionDialog dialog = new ComponentSelectionDialog(Display.getCurrent().getActiveShell(), allowed, component, libUri);
90 if (dialog.open() == ComponentSelectionDialog.CANCEL)
92 toAdd = dialog.getSelected();
95 this.insertPosition = dialog.getInsertPosition();
96 this.insertAdjustable = dialog.isInsertAdjustable();
97 this.lengthAdjustable = dialog.isLenghtAdjustable();
98 this.length = dialog.getLength();
99 this.angle = dialog.getAngle();
100 this.diameter = dialog.getDiameter();
101 this.turnRadius = dialog.getTurnRadius();
102 allowed = dialog.filterAllowed();
103 gizmo.setComponent(component, allowed);
109 public boolean keyPressed(KeyEvent e) {
110 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
111 panel.useDefaultAction();
116 public void attach() {
117 if (component == null)
121 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
129 public void deattach() {
132 nodeMap.commit("Add component");
138 private void attachUI() {
139 //panel.setCursor(activeCursor);
143 private void deattachUI() {
144 //panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
149 public boolean mouseMoved(MouseEvent e) {
150 return getDefaultAction().mouseMoved(e);
154 public boolean mousePressed(MouseEvent e) {
155 return getDefaultAction().mousePressed(e);
159 public boolean mouseReleased(MouseEvent e) {
160 return getDefaultAction().mouseReleased(e);
164 public boolean mouseDragged(MouseEvent e) {
165 return getDefaultAction().mouseDragged(e);
168 public void doInsert(PositionType position) {
170 InsertInstruction inst = new InsertInstruction();
171 inst.typeUri = toAdd.getUri();
173 inst.diameter = diameter;
174 inst.length = length;
175 inst.turnRadius = turnRadius;
176 inst.insertPosition = insertPosition;
177 inst.position = position;
178 ComponentUtils.addComponent(root, component, inst);
179 } catch (Exception e) {
180 ExceptionUtils.logAndShowError("Cannot add component", e);
184 public boolean mouseClicked(MouseEvent e) {
185 if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) {
186 int type = panel.getPickType();
187 //panel.setPickType(0);
188 panel.setPickType(5);
189 vtkProp[] picked = panel.pick(e.getX(), e.getY());
190 panel.setPickType(type);
191 PositionType position = gizmo.getPickedPosition(picked);
193 if (position != null) {
195 panel.useDefaultAction();
199 return getDefaultAction().mouseClicked(e);
203 public boolean mouseWheelMoved(MouseWheelEvent e) {
204 return getDefaultAction().mouseWheelMoved(e);