]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/actions/TranslateFreeVariableLengthAction.java
Compiler warning elimination
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / actions / TranslateFreeVariableLengthAction.java
1 package org.simantics.plant3d.actions;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.vecmath.Vector3d;
7
8 import org.simantics.g3d.math.Ray;
9 import org.simantics.g3d.scenegraph.IG3DNode;
10 import org.simantics.g3d.vtk.swt.InteractiveVtkComposite;
11 import org.simantics.g3d.vtk.utils.vtkUtil;
12 import org.simantics.plant3d.Activator;
13 import org.simantics.plant3d.scenegraph.EndComponent;
14 import org.simantics.plant3d.scenegraph.InlineComponent;
15 import org.simantics.plant3d.scenegraph.Nozzle;
16 import org.simantics.plant3d.scenegraph.P3DRootNode;
17 import org.simantics.plant3d.scenegraph.PipelineComponent;
18 import org.simantics.plant3d.scenegraph.TurnComponent;
19 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
20 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.Direction;
21
22 public class TranslateFreeVariableLengthAction extends RoutePipeAction{
23
24     private InlineComponent component;
25     
26     public TranslateFreeVariableLengthAction(InteractiveVtkComposite panel, P3DRootNode root) {
27         super(panel, root, false);
28         setText("Translate");
29         setImageDescriptor(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/arrow_refresh.png"));
30     }
31     
32     
33     public void setNode(IG3DNode node) {
34         if (node instanceof InlineComponent) {
35             component = (InlineComponent)node;
36             if (component.isVariableLength() && (component.getNext() == null || component.getPrevious() == null)) {
37                 PipelineComponent pathLegStart;
38                 if (component.getNext() == null) {
39                     pathLegStart = component.getControlPoint().findPreviousEnd().getPipelineComponent();
40                 } else {
41                     pathLegStart = component.getControlPoint().findNextEnd().getPipelineComponent();
42                 }
43                 setComponent(pathLegStart);
44                 
45                 setEnabled(true);
46             } else {
47                 setEnabled(false);
48             }
49         } else {
50             component = null;
51             setEnabled(false);
52         }
53     }
54     
55     @Override
56     protected void activate() throws Exception {
57         state = ToolState.INITIALIZING;
58         added.clear();
59         pipeRun = startComponent.getPipeRun();
60         PipeControlPoint start = startComponent.getControlPoint();
61         activateNextPrev(start);
62     }
63     
64     @Override
65     protected void activateNextPrev(PipeControlPoint start) throws Exception {
66         if (!reversed && start.isDualInline())
67             start = start.getSubPoint().get(0);
68         else if (reversed && start.isDualSub())
69             start = start.parent;
70         
71         pipeRun = component.getPipeRun();
72         
73         List<PipeControlPoint> cps = new ArrayList<PipeControlPoint>();
74         PipeControlPoint s = null;
75         if (component.getNext() == null) {
76             s = component.getControlPoint().findPreviousEnd(cps);
77         } else {
78             s = component.getControlPoint().findNextEnd(cps);
79         }
80         if (s.getPipelineComponent() != startComponent) {
81             throw new Exception("Path leg start mismatch");
82         }
83         
84         
85         
86         setPreviousPosition(start.getWorldPosition());
87         currentPosition = component.getPosition();
88         
89         if (startComponent instanceof Nozzle) {
90             direction = startComponent.getControlPoint().getDirectedControlPointDirection();
91             lock = LockType.CUSTOM;
92         } else if (startComponent instanceof PipelineComponent){
93             if (startComponent instanceof InlineComponent) {
94                 direction = startComponent.getControlPoint().getPathLegDirection(reversed ? Direction.PREVIOUS : Direction.NEXT);
95                 lock = LockType.CUSTOM;
96                 if (((InlineComponent) startComponent).isVariableLength()) {
97                     direction = null;
98                     lock = LockType.NONE;
99                 } 
100                 Vector3d v = new Vector3d();
101                 if (!reversed) {
102                     start.getControlPointEnds(v, previousPosition);
103                 } else {
104                     start.getControlPointEnds(previousPosition,v);
105                 }
106             } else if (startComponent instanceof TurnComponent) {
107                 if (start.asFixedAngle()) {
108                     direction = startComponent.getControlPoint().getPathLegDirection(reversed ? Direction.PREVIOUS : Direction.NEXT);
109                     lock = LockType.CUSTOM;
110                 } else {
111                     direction = null;
112                     lock = LockType.NONE;
113                 }
114             } else if (startComponent instanceof EndComponent) {
115                 throw new Exception("Not supported");
116             }
117             
118         } else {
119             throw new Exception("Not supported");
120         }
121         currentPosition = new Vector3d(previousPosition);
122         state = ToolState.ROUTING;
123         if (direction != null) {
124             direction.normalize();
125             
126         } 
127         startRemovable = start.isDeletable();
128         start.setDeletable(false);
129         for (PipeControlPoint pcp : cps) {
130             added.add(pcp.getPipelineComponent());
131             pcp.setDeletable(false);
132         }
133         added.add(component);
134         
135         
136         translateAxisGizmo.attach(panel);
137         setPreviousPosition(previousPosition);
138         updateCurrentPoint();
139     }
140     
141     @Override
142     public void deactivate() {
143         // TODO Auto-generated method stub
144         super.deactivate();
145     }
146     
147     protected void addPoint() throws Exception {
148         // Translate action is not supposed to add points
149         return;
150     }
151     
152     @Override
153     protected void endPiping() throws Exception {
154         state = ToolState.NOT_ACTIVE;
155         panel.useDefaultAction();
156     }
157     
158     protected void updateRouting(double x, double y) {
159         if (useDefault) {
160             //panel.getDefaultAction().update();
161             return;
162         }
163         
164         endTo = null;
165         endType = null;
166         endPort = null;
167
168         Ray ray = vtkUtil.createMouseRay(panel.getRenderer(),x, y);
169         Vector3d o = new Vector3d(ray.pos);
170         Vector3d d = ray.dir;
171         
172         
173         updateCurrentPoint(o, d);
174         updateRoute(o,d); 
175         panel.refresh();
176     }
177     
178
179 }