]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/actions/AddComponentAction.java
Fixed variable angle turns when they are not connected
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / actions / AddComponentAction.java
1 package org.simantics.plant3d.actions;
2
3 import java.awt.event.KeyEvent;
4 import java.awt.event.MouseEvent;
5 import java.awt.event.MouseWheelEvent;
6 import java.util.HashSet;
7 import java.util.Set;
8
9 import org.eclipse.swt.widgets.Display;
10 import org.simantics.db.Resource;
11 import org.simantics.g3d.math.MathTools;
12 import org.simantics.g3d.scenegraph.NodeMap;
13 import org.simantics.g3d.scenegraph.base.INode;
14 import org.simantics.g3d.vtk.swt.InteractiveVtkComposite;
15 import org.simantics.g3d.vtk.swt.vtkSwtAction;
16 import org.simantics.plant3d.Activator;
17 import org.simantics.plant3d.dialog.ComponentSelectionDialog;
18 import org.simantics.plant3d.gizmo.TerminalSelectionGizmo;
19 import org.simantics.plant3d.scenegraph.InlineComponent;
20 import org.simantics.plant3d.scenegraph.Nozzle;
21 import org.simantics.plant3d.scenegraph.P3DRootNode;
22 import org.simantics.plant3d.scenegraph.PipelineComponent;
23 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.PositionType;
24 import org.simantics.plant3d.utils.ComponentUtils;
25 import org.simantics.plant3d.utils.ComponentUtils.InsertInstruction;
26 import org.simantics.plant3d.utils.Item;
27 import org.simantics.utils.threads.ThreadUtils;
28 import org.simantics.utils.ui.ExceptionUtils;
29
30 import vtk.vtkProp;
31
32 public class AddComponentAction extends vtkSwtAction {
33         
34
35         private P3DRootNode root;
36         private PipelineComponent component;
37         private NodeMap<Resource,vtkProp,INode> nodeMap;
38         
39         private TerminalSelectionGizmo gizmo;
40         
41         private Set<PositionType> allowed = new HashSet<PositionType>();
42         
43         private Item toAdd = null;
44         private PositionType insertPosition;
45         private boolean insertAdjustable;
46         private boolean lengthAdjustable;
47         
48         private String libUri;
49         
50         public AddComponentAction(InteractiveVtkComposite panel, P3DRootNode root, String libUri) {
51                 super(panel);
52                 this.root = root;
53                 setText("Add Component");
54                 setImageDescriptor(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/Component.png"));
55                 nodeMap = root.getNodeMap();
56                 gizmo = new TerminalSelectionGizmo(panel);
57                 this.libUri = libUri;
58         }
59         
60         public void setComponent(PipelineComponent component) {
61                 this.component = component;
62                 
63                 allowed.clear();
64                 if (component instanceof Nozzle) {
65                         if (component.getNext() == null && component.getPrevious() == null) {
66                                 allowed.add(PositionType.NEXT);
67                         }  
68                 } else {
69                         if (component.getNext() == null) {
70                                 allowed.add(PositionType.NEXT);
71                         }
72                         if (component.getPrevious() == null) {
73                                 allowed.add(PositionType.PREVIOUS);
74                         }
75                         if (component instanceof InlineComponent && !component.getControlPoint().isFixedLength()){
76                                 allowed.add(PositionType.SPLIT);
77                         }
78                 }
79                 setEnabled(allowed.size() > 0);
80         }
81         
82         private Double length;
83         private Double angle;
84         private Double diameter;
85         private Double turnRadius;
86         
87         @Override
88         public void run() {
89                 
90                 ComponentSelectionDialog dialog = new ComponentSelectionDialog(Display.getCurrent().getActiveShell(), allowed, component, libUri);
91                 if (dialog.open() == ComponentSelectionDialog.CANCEL)
92                         return;
93                 toAdd = dialog.getSelected();
94                 if (toAdd == null)
95                         return;
96                 this.insertPosition = dialog.getInsertPosition();
97                 this.insertAdjustable = dialog.isInsertAdjustable();
98                 this.lengthAdjustable = dialog.isLenghtAdjustable();
99                 this.length = dialog.getLength();
100                 this.angle = dialog.getAngle();
101                 this.diameter = dialog.getDiameter();
102                 this.turnRadius = dialog.getTurnRadius();
103                 allowed = dialog.filterAllowed();
104                 gizmo.setComponent(component, allowed);
105                 super.run();
106                 panel.refresh();
107         }
108         
109         @Override
110         public boolean keyPressed(KeyEvent e) {
111                 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
112                         panel.useDefaultAction();
113                 return true;
114                 
115         }
116         
117         public void attach() {
118                 if (component == null)
119                         return;
120                 
121                 super.attach();
122                 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
123                         public void run() {
124                                 attachUI();
125                         }
126                 });
127                 
128         }
129         
130         public void deattach() {
131 //              deactivate();
132                 component = null;
133                 nodeMap.commit("Add component");
134                 deattachUI();
135                 super.deattach();
136                 panel.refresh();
137         }
138         
139         private void attachUI() {
140                 //panel.setCursor(activeCursor);
141                 gizmo.attach(panel);
142         }
143         
144         private void deattachUI() {
145                 //panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
146                 gizmo.deattach();
147         }
148         
149         @Override
150         public boolean mouseMoved(MouseEvent e) {
151                 return getDefaultAction().mouseMoved(e);
152         }
153         
154         @Override
155         public boolean mousePressed(MouseEvent e) {
156                 return getDefaultAction().mousePressed(e);
157         }
158
159         @Override
160         public boolean mouseReleased(MouseEvent e) {
161                 return getDefaultAction().mouseReleased(e);
162         }
163         
164         @Override
165         public boolean mouseDragged(MouseEvent e) {
166                 return getDefaultAction().mouseDragged(e);
167         }
168         
169         public void doInsert(PositionType position) {
170                 try  {
171                         InsertInstruction inst = new InsertInstruction();
172                         inst.typeUri = toAdd.getUri();
173                         inst.angle = angle != null ? MathTools.degToRad(angle) : null;
174                         inst.diameter = diameter;
175                         inst.length = length;
176                         inst.turnRadius = turnRadius;
177                         inst.insertPosition = insertPosition;
178                         inst.position = position;
179                         ComponentUtils.addComponent(root, component, inst);
180                 } catch (Exception e) {
181                         ExceptionUtils.logAndShowError("Cannot add component", e);
182                 }
183         }
184         
185         public boolean mouseClicked(MouseEvent e) {
186                 if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) {
187                         int type = panel.getPickType();
188                         //panel.setPickType(0);
189                         panel.setPickType(5);
190                         vtkProp[] picked = panel.pick(e.getX(), e.getY());
191                         panel.setPickType(type);
192                         PositionType position = gizmo.getPickedPosition(picked);
193
194                         if (position != null) {
195                                 doInsert(position);
196                                 panel.useDefaultAction();
197                                 return true;
198                         }
199                 } 
200                 return getDefaultAction().mouseClicked(e);
201         }
202         
203         @Override
204         public boolean mouseWheelMoved(MouseWheelEvent e) {
205                 return getDefaultAction().mouseWheelMoved(e);
206         }
207 }