1 package org.simantics.plant3d.actions;
3 import java.awt.event.KeyEvent;
4 import java.awt.event.MouseEvent;
5 import java.util.HashSet;
8 import javax.vecmath.Vector3d;
10 import org.eclipse.swt.widgets.Display;
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.P3DRootNode;
20 import org.simantics.plant3d.scenegraph.PipeRun;
21 import org.simantics.plant3d.scenegraph.PipelineComponent;
22 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
23 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.Direction;
24 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.PositionType;
25 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
26 import org.simantics.plant3d.utils.ComponentUtils;
27 import org.simantics.plant3d.utils.Item;
28 import org.simantics.plant3d.utils.Item.Type;
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<vtkProp,INode> nodeMap;
41 private TerminalSelectionGizmo gizmo;
43 private Set<PositionType> allowed = new HashSet<PositionType>();
45 private Item toAdd = null;
47 public AddComponentAction(InteractiveVtkComposite panel, P3DRootNode root) {
50 setText("Add Component");
51 setImageDescriptor(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/Component.png"));
52 nodeMap = root.getNodeMap();
53 gizmo = new TerminalSelectionGizmo(panel);
56 public void setComponent(PipelineComponent component) {
57 this.component = component;
60 if (component.getNext() == null) {
61 allowed.add(PositionType.NEXT);
63 if (component.getPrevious() == null) {
64 allowed.add(PositionType.PREVIOUS);
66 if (component instanceof InlineComponent && !component.getControlPoint().isFixed()){
67 allowed.add(PositionType.SPLIT);
69 setEnabled(allowed.size() > 0);
72 private Double length;
74 private Double diameter;
75 private Double turnRadius;
79 ComponentSelectionDialog dialog = new ComponentSelectionDialog(Display.getCurrent().getActiveShell(), allowed);
80 if (dialog.open() == ComponentSelectionDialog.CANCEL)
82 toAdd = dialog.getSelected();
85 this.length = dialog.getLength();
86 this.angle = dialog.getAngle();
87 this.diameter = dialog.getDiameter();
88 this.turnRadius = dialog.getTurnRadius();
89 allowed = dialog.filterAllowed();
90 gizmo.setComponent(component, allowed);
96 public boolean keyPressed(KeyEvent e) {
97 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
98 panel.useDefaultAction();
103 public void attach() {
104 if (component == null)
108 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
116 public void deattach() {
125 private void attachUI() {
126 //panel.setCursor(activeCursor);
130 private void deattachUI() {
131 //panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
136 public boolean mouseMoved(MouseEvent e) {
137 return getDefaultAction().mouseMoved(e);
141 public boolean mousePressed(MouseEvent e) {
142 return getDefaultAction().mousePressed(e);
146 public boolean mouseReleased(MouseEvent e) {
147 return getDefaultAction().mouseReleased(e);
151 public boolean mouseDragged(MouseEvent e) {
152 return getDefaultAction().mouseDragged(e);
155 public void doInsert(PositionType position) {
157 PipelineComponent newComponent = ComponentUtils.createComponent(root,toAdd.getUri());
158 PipeControlPoint newPcp = newComponent.getControlPoint();
160 PipeControlPoint toPcp = component.getControlPoint();
165 if (toPcp.isDualInline())
166 toPcp = toPcp.getSubPoint().get(0);
170 if (toPcp.isDualSub())
171 toPcp = toPcp.parent;
174 Vector3d start = new Vector3d();
175 Vector3d end = new Vector3d();
176 Vector3d dir = new Vector3d();
177 toPcp.getInlineControlPointEnds(start, end, dir);
180 PipeRun pipeRun = toPcp.getPipeRun();
182 if (!toAdd.isSizeChange()) {
183 String name = component.getPipeRun().getUniqueName(toAdd.getName());
184 newComponent.setName(name);
186 pipeRun.addChild(newComponent);
187 if (toAdd.isVariable()) {
188 // TODO: these options are not stored into DB. Should they?!
189 if (toAdd.getType() == Type.INLINE) {
190 newPcp.setLength(length);
191 // newPcp.setFixed(true);
192 } else if (toAdd.getType() == Type.TURN) {
193 newPcp.setTurnAngle(angle);
194 // newPcp.setFixed(true);
197 newComponent.updateParameters();
199 dir.scale(newComponent.getControlPoint().getLength()*0.5);
204 if (toPcp.isDualInline())
205 toPcp = toPcp.getSubPoint().get(0);
206 newPcp.insert(toPcp, Direction.NEXT);
207 newPcp.setWorldPosition(end);
210 if (toPcp.isDualSub())
211 toPcp = toPcp.parent;
212 newPcp.insert(toPcp, Direction.PREVIOUS);
213 newPcp.setWorldPosition(start);
216 PipingRules.splitVariableLengthComponent(newComponent, (InlineComponent)component, true);
221 PipeRun other = new PipeRun();
222 String n = root.getUniqueName("PipeRun");
224 other.setPipeDiameter(diameter);
225 other.setTurnRadius(turnRadius);
226 root.addChild(other);
230 if (position == PositionType.NEXT) {
231 PipingRules.addSizeChange(false, pipeRun, other, (InlineComponent)newComponent, toPcp, null);
232 newPcp.setWorldPosition(end);
233 } else if (position == PositionType.PREVIOUS){
234 PipingRules.addSizeChange(true, pipeRun, other, (InlineComponent)newComponent, toPcp, null);
235 newPcp.setWorldPosition(start);
237 // TODO : chicken-egg problem
238 newComponent.updateParameters();
239 dir.scale(newComponent.getControlPoint().getLength()*0.5);
242 if (position == PositionType.NEXT) {
243 newPcp.setWorldPosition(end);
244 } else if (position == PositionType.PREVIOUS){
245 newPcp.setWorldPosition(start);
250 } catch (Exception e) {
251 ExceptionUtils.logAndShowError("Cannot add component", e);
255 public boolean mouseClicked(MouseEvent e) {
256 if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) {
257 int type = panel.getPickType();
258 //panel.setPickType(0);
259 panel.setPickType(5);
260 vtkProp[] picked = panel.pick(e.getX(), e.getY());
261 panel.setPickType(type);
262 PositionType position = gizmo.getPickedPosition(picked);
264 if (position != null) {
266 panel.useDefaultAction();
270 return getDefaultAction().mouseClicked(e);