]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.plant3d/src/org/simantics/plant3d/actions/RoutePipeAction.java
White space clean-up
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / actions / RoutePipeAction.java
index 3c320434cd6af985911e24c0d3b49eccfd0eceee..24b6a14bdfcef66fd5c1df8876fa6c14ac2fe0ff 100644 (file)
@@ -79,13 +79,13 @@ public class RoutePipeAction extends vtkSwtAction {
        boolean step = false;
        
        PipelineComponent endTo = null;
-    PositionType endType = null;
-    PipeControlPoint endPort = null;
-    
-    boolean reversed = false;
-    
-    private Set<PositionType> allowed = new HashSet<PositionType>();
-    
+       PositionType endType = null;
+       PipeControlPoint endPort = null;
+       
+       boolean reversed = false;
+       
+       private Set<PositionType> allowed = new HashSet<PositionType>();
+       
        
        public RoutePipeAction(InteractiveVtkComposite panel, P3DRootNode root) {
                super(panel);
@@ -234,19 +234,19 @@ public class RoutePipeAction extends vtkSwtAction {
        }
        private void update(double x, double y) {
                switch (state) {
-       case NOT_ACTIVE:
-               return; // TODO : throw Exception?
-       case INITIALIZING:
-               return;
-       case SELECTING_POSITION:
-               return;
-       case SELECTING_SPLIT:
-               return;
-       case ROUTING:
-               updateRouting(x,y);
-               break;
-       }
-       return;
+               case NOT_ACTIVE:
+                       return; // TODO : throw Exception?
+               case INITIALIZING:
+                       return;
+               case SELECTING_POSITION:
+                       return;
+               case SELECTING_SPLIT:
+                       return;
+               case ROUTING:
+                       updateRouting(x,y);
+                       break;
+               }
+               return;
        }
        
        boolean startRemovable = false;
@@ -494,10 +494,10 @@ public class RoutePipeAction extends vtkSwtAction {
        
        @Override
        public boolean mouseWheelMoved(MouseWheelEvent e) {
-           if (useDefault) {
-            getDefaultAction().mouseWheelMoved(e);
-        }
-        return true;
+               if (useDefault) {
+                       getDefaultAction().mouseWheelMoved(e);
+               }
+               return true;
        }
        
        @Override
@@ -509,31 +509,31 @@ public class RoutePipeAction extends vtkSwtAction {
                if (state == ToolState.ROUTING) {
                        try {
                                if (e.getClickCount() == 1) {
-                           if (e.getButton() == MouseEvent.BUTTON1) {
-                               if (this.added.size() > 0) {
-                                 
-                                   setLockType(LockType.NONE,true);
-                                   if (endTo != null) {
-                                       
-                                       endPiping();
-                                   } else {
-                                       addPoint();
-                                   }
-                               } else {
-                                       throw new RuntimeException("kjf");
+                                       if (e.getButton() == MouseEvent.BUTTON1) {
+                                               if (this.added.size() > 0) {
+                                                       
+                                                       setLockType(LockType.NONE,true);
+                                                       if (endTo != null) {
+                                                               
+                                                               endPiping();
+                                                       } else {
+                                                               addPoint();
+                                                       }
+                                               } else {
+                                                       throw new RuntimeException("kjf");
                //                      // user was selecting position of branch
                //                    lastPoint.set(startPoint);
                //                    controlPoints.add(new Point3d(startPoint));
                //                    if (selectionLine != null)
                //                      selectionLine.removeFromParent();
                //                    selectionLine = null;
-                               }
-                           } else if (e.getButton() ==MouseEvent.BUTTON2){
+                                               }
+                                       } else if (e.getButton() ==MouseEvent.BUTTON2){
                //                detector.updateConstraintReference();
-                           } else if (e.getButton() == MouseEvent.BUTTON3){      
-                               endPiping();
-                           }
-                       }
+                                       } else if (e.getButton() == MouseEvent.BUTTON3){      
+                                               endPiping();
+                                       }
+                               }
                        } catch(Exception err) {
                                err.printStackTrace();
                        }
@@ -627,513 +627,513 @@ public class RoutePipeAction extends vtkSwtAction {
                return nodes;
        }
        
-        
-     
-        private void updateRouting(double x, double y) {
-//             if(input.keyPressed(KeyEvent.VK_ESCAPE)) {
-//                 controlPoints.clear();
-//                 end();
-//                 return;
-//             }
-//             if (input.keyPressed(KeyEvent.VK_C)) {
-//                     useCamera = !useCamera;
-//                     cameraAction.setChecked(useCamera);
-//             }
-               if (useDefault) {
-                       //panel.getDefaultAction().update();
-                       return;
-               }
-               
-               endTo = null;
-               endType = null;
-               endPort = null;
 
-               Ray ray = vtkUtil.createMouseRay(panel.getRenderer(),x, y);
-               Vector3d o = new Vector3d(ray.pos);
-               Vector3d d = ray.dir;
-               
-               
-               if (!updateCurrentPoint(o, d))
-                   return;
-               //Point3d startPoint = new Point3d();
-               double mu[] = new double[2];
-               
-               
-               
-               INode hoverObject = null;
-               
-               List<INode> hover = isOverNode((int)x,(int)y);
-               if (hover.size() > 0) {
-                       hoverObject = hover.get(0);
-               } 
+
+       private void updateRouting(double x, double y) {
+//             if(input.keyPressed(KeyEvent.VK_ESCAPE)) {
+//                     controlPoints.clear();
+//                     end();
+//                     return;
+//             }
+//             if (input.keyPressed(KeyEvent.VK_C)) {
+//                     useCamera = !useCamera;
+//                     cameraAction.setChecked(useCamera);
+//             }
+               if (useDefault) {
+                       //panel.getDefaultAction().update();
+                       return;
+               }
+               
+               endTo = null;
+               endType = null;
+               endPort = null;
+
+               Ray ray = vtkUtil.createMouseRay(panel.getRenderer(),x, y);
+               Vector3d o = new Vector3d(ray.pos);
+               Vector3d d = ray.dir;
+               
+               
+               if (!updateCurrentPoint(o, d))
+                       return;
+               //Point3d startPoint = new Point3d();
+               double mu[] = new double[2];
+               
+               
+               
+               INode hoverObject = null;
+               
+               List<INode> hover = isOverNode((int)x,(int)y);
+               if (hover.size() > 0) {
+                       hoverObject = hover.get(0);
+               } 
 //             System.out.println(hoverObject + " " + getLast());
-               if (hoverObject != null) {
-                       if (hoverObject.equals(getLast()) ) {
-                               boolean set = false;
-                               for (int i = 1; i < hover.size(); i++) {
-                                       hoverObject = hover.get(i);
-                                       if (!getLast().equals(hoverObject)) {
-                                               set = true;
-                                               break;
-                                       }
-                               }
-                               if (!set)
-                                       hoverObject = null;
-                       }
-               }
+               if (hoverObject != null) {
+                       if (hoverObject.equals(getLast()) ) {
+                               boolean set = false;
+                               for (int i = 1; i < hover.size(); i++) {
+                                       hoverObject = hover.get(i);
+                                       if (!getLast().equals(hoverObject)) {
+                                               set = true;
+                                               break;
+                                       }
+                               }
+                               if (!set)
+                                       hoverObject = null;
+                       }
+               }
 //             System.out.println(hoverObject);
-                 if (hoverObject != null) {
-                            
-                         if (lock == LockType.NONE) {
-                                 if (hoverObject instanceof Nozzle && endingToNozzle(hoverObject,o,d)) {
-                                         endTo = (Nozzle)hoverObject;
-                                 } else if (hoverObject instanceof InlineComponent && ((InlineComponent)hoverObject).isVariableLength()) {
-                                         endTo = (InlineComponent)hoverObject;
-                                         endType = endingToStraight(endTo,mu,o,d);     
-                                 } else if (hoverObject instanceof PipelineComponent && (endPort = endingToComponent(hoverObject,o,d)) != null) {
-                                         endTo = (PipelineComponent)hoverObject;
-                                 } else {
-                                         updateRoute(o,d); 
-                                 }
-                         } else {  
-                                 if (hoverObject instanceof InlineComponent && ((InlineComponent)hoverObject).isVariableLength() && (endType = endingLockToStraight(hoverObject,mu)) != null) {
-                                         endTo = (InlineComponent)hoverObject;
-                                 } else if (hoverObject instanceof Nozzle && endingLockToNozzle(hoverObject)) {
-                                         endTo = (Nozzle)hoverObject;
-                                 } else if ((hoverObject instanceof PipelineComponent) &&  ((endPort = endingLockToComponent(hoverObject)) != null)) {
-                                         endTo = (PipelineComponent)hoverObject;
-                                 } else {
-                                         updateRoute(o,d);
-                                 }
-                         }
-                         if (added.contains(endTo))
-                                 endTo = null;
-                     
-               } else {
-                   updateRoute(o,d);
-               }
-                 
-               panel.refresh();
-           }
-           
-           private boolean updateCurrentPoint(Vector3d o, Vector3d d) {
+               if (hoverObject != null) {
+
+                       if (lock == LockType.NONE) {
+                               if (hoverObject instanceof Nozzle && endingToNozzle(hoverObject,o,d)) {
+                                       endTo = (Nozzle)hoverObject;
+                               } else if (hoverObject instanceof InlineComponent && ((InlineComponent)hoverObject).isVariableLength()) {
+                                       endTo = (InlineComponent)hoverObject;
+                                       endType = endingToStraight(endTo,mu,o,d);     
+                               } else if (hoverObject instanceof PipelineComponent && (endPort = endingToComponent(hoverObject,o,d)) != null) {
+                                       endTo = (PipelineComponent)hoverObject;
+                               } else {
+                                       updateRoute(o,d); 
+                               }
+                       } else {  
+                               if (hoverObject instanceof InlineComponent && ((InlineComponent)hoverObject).isVariableLength() && (endType = endingLockToStraight(hoverObject,mu)) != null) {
+                                       endTo = (InlineComponent)hoverObject;
+                               } else if (hoverObject instanceof Nozzle && endingLockToNozzle(hoverObject)) {
+                                       endTo = (Nozzle)hoverObject;
+                               } else if ((hoverObject instanceof PipelineComponent) &&  ((endPort = endingLockToComponent(hoverObject)) != null)) {
+                                       endTo = (PipelineComponent)hoverObject;
+                               } else {
+                                       updateRoute(o,d);
+                               }
+                       }
+                       if (added.contains(endTo))
+                               endTo = null;
 
-               Vector3d point = new Vector3d(this.previousPosition);
-               
-               switch(lock) {
-               case X:
-                   MathTools.intersectStraightStraight(point, new Vector3d(1.0,0.0,0.0), o,d, currentPosition, new Vector3d());
-                   if (step) {
-                       currentPosition.x = Math.round(istep * currentPosition.x) / istep;
-                       BigDecimal bx = new BigDecimal(currentPosition.x);
-                       bx.setScale(decimals, BigDecimal.ROUND_HALF_UP);
-                       currentPosition.x = bx.doubleValue();
-                   }
-                   break;
-               case Y:
-                   MathTools.intersectStraightStraight(point, new Vector3d(0.0,1.0,0.0), o,d, currentPosition, new Vector3d());
-                   if (step) {
-                       currentPosition.y = Math.round(istep * currentPosition.y) / istep;
-                       BigDecimal bx = new BigDecimal(currentPosition.y);
-                       bx.setScale(decimals, BigDecimal.ROUND_HALF_UP);
-                       currentPosition.y = bx.doubleValue();
-                   }
-                   break;
-               case Z:
-                   MathTools.intersectStraightStraight(point, new Vector3d(0.0,0.0,1.0), o,d, currentPosition, new Vector3d());
-                   if (step) {
-                       currentPosition.z = Math.round(istep * currentPosition.z) / istep;
-                       BigDecimal bx = new BigDecimal(currentPosition.z);
-                       bx.setScale(decimals, BigDecimal.ROUND_HALF_UP);
-                       currentPosition.z = bx.doubleValue();
-                   }break;
-               case XY:
-                   MathTools.intersectStraightPlane(o, d, point, new Vector3d(0.0,0.0,1.0), currentPosition);
-                   break;
-               case XZ:
-                   MathTools.intersectStraightPlane(o, d, point, new Vector3d(0.0,1.0,0.0), currentPosition);
-                   break;
-               case YZ:
-                   MathTools.intersectStraightPlane(o, d, point, new Vector3d(1.0,0.0,0.0), currentPosition);
-                   break;
-               case NONE:
-                       Vector3d normal = new Vector3d(panel.getRenderer().GetActiveCamera().GetDirectionOfProjection());
-                   normal.normalize();
-                   
-                   MathTools.intersectStraightPlane(o, d, point, normal, currentPosition);
-                   break;
-               case CUSTOM:
-                       MathTools.intersectStraightStraight(point, new Vector3d(direction), o,d, currentPosition, new Vector3d());
-                   double dist = MathTools.distanceFromPlane(new Vector3d(currentPosition), direction, previousPosition);
-                       if (dist < 0.0)
-                               currentPosition.set(previousPosition);
-                   break;
-               default:
-                   return false;
-               }
-               return true;
-           }
-           
-           private Vector3d getLockDir() {
-               switch (lock) {
-               case CUSTOM:
-                       return direction;
-               case X:
-                       return new Vector3d(1,0,0);
-               case Y:
-                       return new Vector3d(0,1,0);
-               case Z:
-                       return new Vector3d(0,0,1);
-               }
-               return null;
-           }
-           
-           private void updateRoute(Vector3d o, Vector3d d) {
-               detector.clearConstraintHighlights();
-               Point3d previousPipePoint = new Point3d(previousPosition);
-               String s = "";
-               if (lock == LockType.NONE) {
-                   Point3d p = detector.getSnappedPoint(o, d, new Vector3d(previousPipePoint));
-                   if (p != null)
-                       currentPosition = new Vector3d(p);
-                   s += detector.getSnapString();
+               } else {
+                       updateRoute(o,d);
+               }
+               
+               panel.refresh();
+       }
+       
+       private boolean updateCurrentPoint(Vector3d o, Vector3d d) {
 
-               } else {
-                   Vector3d dir = new Vector3d(currentPosition);
-                   dir.sub(previousPipePoint);
-                   Point3d p = detector.getPointSnap(new Vector3d(previousPipePoint), dir);
-                   if (p != null)
-                       currentPosition = new Vector3d(p);
-                   s += detector.getSnapString();
+               Vector3d point = new Vector3d(this.previousPosition);
+               
+               switch(lock) {
+               case X:
+                       MathTools.intersectStraightStraight(point, new Vector3d(1.0,0.0,0.0), o,d, currentPosition, new Vector3d());
+                       if (step) {
+                               currentPosition.x = Math.round(istep * currentPosition.x) / istep;
+                               BigDecimal bx = new BigDecimal(currentPosition.x);
+                               bx.setScale(decimals, BigDecimal.ROUND_HALF_UP);
+                               currentPosition.x = bx.doubleValue();
+                       }
+                       break;
+               case Y:
+                       MathTools.intersectStraightStraight(point, new Vector3d(0.0,1.0,0.0), o,d, currentPosition, new Vector3d());
+                       if (step) {
+                               currentPosition.y = Math.round(istep * currentPosition.y) / istep;
+                               BigDecimal bx = new BigDecimal(currentPosition.y);
+                               bx.setScale(decimals, BigDecimal.ROUND_HALF_UP);
+                               currentPosition.y = bx.doubleValue();
+                       }
+                       break;
+               case Z:
+                       MathTools.intersectStraightStraight(point, new Vector3d(0.0,0.0,1.0), o,d, currentPosition, new Vector3d());
+                       if (step) {
+                               currentPosition.z = Math.round(istep * currentPosition.z) / istep;
+                               BigDecimal bx = new BigDecimal(currentPosition.z);
+                               bx.setScale(decimals, BigDecimal.ROUND_HALF_UP);
+                               currentPosition.z = bx.doubleValue();
+                       }break;
+               case XY:
+                       MathTools.intersectStraightPlane(o, d, point, new Vector3d(0.0,0.0,1.0), currentPosition);
+                       break;
+               case XZ:
+                       MathTools.intersectStraightPlane(o, d, point, new Vector3d(0.0,1.0,0.0), currentPosition);
+                       break;
+               case YZ:
+                       MathTools.intersectStraightPlane(o, d, point, new Vector3d(1.0,0.0,0.0), currentPosition);
+                       break;
+               case NONE:
+                       Vector3d normal = new Vector3d(panel.getRenderer().GetActiveCamera().GetDirectionOfProjection());
+                       normal.normalize();
+                       
+                       MathTools.intersectStraightPlane(o, d, point, normal, currentPosition);
+                       break;
+               case CUSTOM:
+                       MathTools.intersectStraightStraight(point, new Vector3d(direction), o,d, currentPosition, new Vector3d());
+                       double dist = MathTools.distanceFromPlane(new Vector3d(currentPosition), direction, previousPosition);
+                       if (dist < 0.0)
+                               currentPosition.set(previousPosition);
+                       break;
+               default:
+                       return false;
+               }
+               return true;
+       }
+       
+       private Vector3d getLockDir() {
+               switch (lock) {
+               case CUSTOM:
+                       return direction;
+               case X:
+                       return new Vector3d(1,0,0);
+               case Y:
+                       return new Vector3d(0,1,0);
+               case Z:
+                       return new Vector3d(0,0,1);
+               }
+               return null;
+       }
+       
+       private void updateRoute(Vector3d o, Vector3d d) {
+               detector.clearConstraintHighlights();
+               Point3d previousPipePoint = new Point3d(previousPosition);
+               String s = "";
+               if (lock == LockType.NONE) {
+                       Point3d p = detector.getSnappedPoint(o, d, new Vector3d(previousPipePoint));
+                       if (p != null)
+                               currentPosition = new Vector3d(p);
+                       s += detector.getSnapString();
 
-               }
-               //System.out.println(previousPosition + " -> " + currentPosition);
-//             double dist = MathTools.distance(previousPosition, currentPosition);
-//             if (dist < pipeRun.getTurnRadius()) {
-//                 s += "Too close";
-//                 Vector3d v = new Vector3d(currentPosition);
-//                 v.sub(previousPosition);
-//                 double vl = v.length();
-//                 if (vl > MathTools.NEAR_ZERO) {
-//                     v.scale(1.0/vl);
-//                 } else {
-//                     
-//                     return;
-//                 }
-//                 v.scale(pipeRun.getTurnRadius());
-//                 v.add(previousPosition);
-//                 currentPosition.set(v);
-//             }
-               
-               updateCurrentPoint();
-               s += currentPosition.toString();
-               setInfoText(s);
-           }
-           
-           vtkTextActor infoActor;
-           
-           private void setInfoText(String text) {
-               //System.out.println(text);
-               if (infoActor == null) {
-                       infoActor = new vtkTextActor();
-                       infoActor.GetTextProperty().SetColor(0.0, 0.0, 0.0);
-                       infoActor.GetTextProperty().ShadowOff();
-                       infoActor.GetTextProperty().ItalicOff();
-                       infoActor.GetTextProperty().BoldOff();
-                       infoActor.GetTextProperty().SetFontSize(18);
-                       infoActor.GetTextProperty().Delete();
-                       infoActor.GetProperty().SetColor(0.0, 0.0, 0.0);
-                       infoActor.GetProperty().Delete();
+               } else {
+                       Vector3d dir = new Vector3d(currentPosition);
+                       dir.sub(previousPipePoint);
+                       Point3d p = detector.getPointSnap(new Vector3d(previousPipePoint), dir);
+                       if (p != null)
+                               currentPosition = new Vector3d(p);
+                       s += detector.getSnapString();
 
-                               
-                       infoActor.SetPosition(10,10);
-                               panel.getRenderer().AddActor(infoActor);
-               }
-               infoActor.SetInput(text);
-           }
-           
-           private boolean endingToNozzle(INode nozzleNode,Vector3d o, Vector3d d) {
-               Nozzle nozzle = (Nozzle)nozzleNode;
-               PipeControlPoint pcp  =nozzle.getControlPoint();
-               if (pcp != null && (pcp.getNext() != null ||
-                                           pcp.getPrevious() != null))
-                       return false; // nozzle is already connected to pipe
-               currentPosition = pcp.getWorldPosition();
-               Point3d previousPipePoint = new Point3d(previousPosition);
-               Point3d p = detector.getSnappedPoint(o, d, new Vector3d(previousPipePoint));
-               if (p != null) {
-                   if (MathTools.distance(p, currentPosition) > NOZZLE_SNAP_DISTANCE) {
-                       return false;
-                   }
-               } 
-               
-               updateCurrentPoint();
-               
-               setInfoText("Connect to nozzle " + currentPosition);
-               return true;
-           
-           }
-           
-           private PositionType endingToStraight(INode straightNode, double mu[], Vector3d o, Vector3d d) {
-               InlineComponent s = (InlineComponent)straightNode;
-               String info = "";
-               Point3d sStart = new Point3d();
-               Point3d sEnd = new Point3d();
-               s.getEnds(sStart, sEnd);
-               //detector.clearConstraintHighlights();
-               
-               Point3d previousPipePoint = new Point3d(previousPosition);
-               //String st = "";
-               if (lock == LockType.NONE) {
-                   Point3d p = detector.getSnappedPoint(o, d, new Vector3d(previousPipePoint));
-                   if (p != null) {
-                       currentPosition = new Vector3d(p);
-                       // snapping is detected, check if snapped point can create branch with straight
-                       PositionType t = endingLockToStraight(s, mu);
-                       if (t != null)
-                           return t;
-                       // if not, we'll have to remove highlight that was added when snapped point was detected
-                       detector.clearConstraintHighlights();
-                   } 
-                     
+               }
+//             System.out.println(previousPosition + " -> " + currentPosition);
+//             double dist = MathTools.distance(previousPosition, currentPosition);
+//             if (dist < pipeRun.getTurnRadius()) {
+//                     s += "Too close";
+//                     Vector3d v = new Vector3d(currentPosition);
+//                     v.sub(previousPosition);
+//                     double vl = v.length();
+//                     if (vl > MathTools.NEAR_ZERO) {
+//                             v.scale(1.0/vl);
+//                     } else {
+//
+//                             return;
+//                     }
+//                     v.scale(pipeRun.getTurnRadius());
+//                     v.add(previousPosition);
+//                     currentPosition.set(v);
+//             }
+               
+               updateCurrentPoint();
+               s += currentPosition.toString();
+               setInfoText(s);
+       }
+       
+       vtkTextActor infoActor;
+       
+       private void setInfoText(String text) {
+               //System.out.println(text);
+               if (infoActor == null) {
+                       infoActor = new vtkTextActor();
+                       infoActor.GetTextProperty().SetColor(0.0, 0.0, 0.0);
+                       infoActor.GetTextProperty().ShadowOff();
+                       infoActor.GetTextProperty().ItalicOff();
+                       infoActor.GetTextProperty().BoldOff();
+                       infoActor.GetTextProperty().SetFontSize(18);
+                       infoActor.GetTextProperty().Delete();
+                       infoActor.GetProperty().SetColor(0.0, 0.0, 0.0);
+                       infoActor.GetProperty().Delete();
 
-                   Vector3d sDir = new Vector3d(sEnd);
-                   sDir.sub(sStart);
-                   MathTools.intersectStraightStraight(sStart, sDir, o, d, currentPosition, new Point3d(), mu);
-                   
+                       
+                       infoActor.SetPosition(10,10);
+                       panel.getRenderer().AddActor(infoActor);
+               }
+               infoActor.SetInput(text);
+       }
+       
+       private boolean endingToNozzle(INode nozzleNode,Vector3d o, Vector3d d) {
+               Nozzle nozzle = (Nozzle)nozzleNode;
+               PipeControlPoint pcp  =nozzle.getControlPoint();
+               if (pcp != null && (pcp.getNext() != null ||
+                                                       pcp.getPrevious() != null))
+                       return false; // nozzle is already connected to pipe
+               currentPosition = pcp.getWorldPosition();
+               Point3d previousPipePoint = new Point3d(previousPosition);
+               Point3d p = detector.getSnappedPoint(o, d, new Vector3d(previousPipePoint));
+               if (p != null) {
+                       if (MathTools.distance(p, currentPosition) > NOZZLE_SNAP_DISTANCE) {
+                               return false;
+                       }
+               } 
+               
+               updateCurrentPoint();
+               
+               setInfoText("Connect to nozzle " + currentPosition);
+               return true;
+       
+       }
+       
+       private PositionType endingToStraight(INode straightNode, double mu[], Vector3d o, Vector3d d) {
+               InlineComponent s = (InlineComponent)straightNode;
+               String info = "";
+               Point3d sStart = new Point3d();
+               Point3d sEnd = new Point3d();
+               s.getEnds(sStart, sEnd);
+               //detector.clearConstraintHighlights();
+               
+               Point3d previousPipePoint = new Point3d(previousPosition);
+               //String st = "";
+               if (lock == LockType.NONE) {
+                       Point3d p = detector.getSnappedPoint(o, d, new Vector3d(previousPipePoint));
+                       if (p != null) {
+                               currentPosition = new Vector3d(p);
+                               // snapping is detected, check if snapped point can create branch with straight
+                               PositionType t = endingLockToStraight(s, mu);
+                               if (t != null)
+                                       return t;
+                               // if not, we'll have to remove highlight that was added when snapped point was detected
+                               detector.clearConstraintHighlights();
+                       } 
+                       
 
-               } else {
-                   throw new RuntimeException("Lock shouldn't be on");
+                       Vector3d sDir = new Vector3d(sEnd);
+                       sDir.sub(sStart);
+                       MathTools.intersectStraightStraight(sStart, sDir, o, d, currentPosition, new Point3d(), mu);
+                       
 
-               }
-               
-               updateCurrentPoint();
-               
-               // branch point must lie between straight's ends. If connection point is exactly
-               // on straight end user may want to connect pipes to each other
-               // TODO : take account sizes of inline components)
-               // TODO : actually make connection if its detected
-               boolean connectPrev = false;
-               boolean connectNext = false;
-               
-               if (mu[0] < 0.0) {
-                   currentPosition.set(sStart);
-                   connectPrev = true;
-               }
-               else if (mu[0] > 1.0) {
-                   currentPosition.set(sEnd);
-                   connectNext = true;
-               }
-               boolean connect = false;
-               if (connectPrev) {
-                   PipeControlPoint pcp = s.getControlPoint();
-                   if (pcp.getPrevious() == null)
-                       connect = true;
-               } else if (connectNext) {
-                       PipeControlPoint pcp = s.getControlPoint();
-                   if (pcp.getNext() == null)
-                       connect = true;
-               }
-               
-               updateCurrentPoint();
-               
-               if (connect)
-                   info += "Connect pipes :";
-               else
-                   info += "Make Branch :";
-               
-               setInfoText(info + currentPosition + " " + Math.max(0.0, Math.min(mu[0], 1.0)));
-               if (connect) {
-                       if (connectNext) {
-                               return PositionType.NEXT;
-                       } else {
-                               return PositionType.PREVIOUS;
-                       }
-                               
-               }
-               return PositionType.SPLIT;
-                       
-           }
-           
-           private PipeControlPoint endingToComponent(INode componentNode, Vector3d o, Vector3d d) {
-               PipelineComponent component = (PipelineComponent)componentNode;
-               PipeControlPoint pcp = component.getControlPoint();
-               if (component instanceof EndComponent) {
-                       if (pcp.getNext() != null || pcp.getPrevious() != null)
-                               return null;
-                       return pcp;
-               } else if (component instanceof TurnComponent) {
-                       if (pcp.getNext() == null || pcp.getPrevious() == null)
-                               return pcp;
-                       return null;
-               } else if (component instanceof InlineComponent) {
-                       // TODO : scan all empty pcps of the component and select closest one.
-                       if (pcp.getNext() == null || pcp.getPrevious() == null)
-                               return pcp;
-                       return null;
-               }
+               } else {
+                       throw new RuntimeException("Lock shouldn't be on");
 
-               return null;
-           }
-           
-           private PositionType endingLockToStraight(INode straightNode, double mu[]) {
-               InlineComponent s = (InlineComponent)straightNode;
-               Point3d sStart = new Point3d();
-               Point3d sEnd = new Point3d(); 
-               s.getControlPoint().getInlineControlPointEnds(sStart, sEnd);  
-               Vector3d sDir = new Vector3d(sEnd);
-               sDir.sub(sStart);
-               Vector3d dir = new Vector3d(currentPosition);
-               Point3d prev = new Point3d(previousPosition);
-               dir.sub(prev);
-               // intersection point in pipe where branch would be inserted to
-               Vector3d branchPoint = new Vector3d();
-               // intersection point in straight pipe that is currently routed
-               Vector3d routePoint = new Vector3d();
-               MathTools.intersectStraightStraight(sStart, sDir, new Vector3d(prev), dir, branchPoint, routePoint, mu);
-               routePoint.sub(branchPoint);
-               // startPoint of branch must be between pipe ends
-               // TODO : take account sizes of elbows (or other components)
-               // branch point must be between pipe ends and intersection points must be quite close to each other
-               if (mu[0] > 0.0 && mu[0] < 1.0 && routePoint.lengthSquared() < BRANCH_SNAP_DISTANCE) {
-                   currentPosition.set(branchPoint);
-                   
-                   updateCurrentPoint();
-                   
-                   setInfoText("Make branch (l) :" + currentPosition + " " + Math.max(0.0, Math.min(mu[0], 1.0)) + " " + routePoint.lengthSquared());
-                   return PositionType.SPLIT;
-               }
-               return null;
-           }
-           
-           private boolean endingLockToNozzle(INode nozzleNode) {
-               Nozzle nozzle = (Nozzle)nozzleNode;
-                Vector3d dir = new Vector3d(currentPosition);
-                Point3d prev = new Point3d(previousPosition);
-                dir.sub(prev);
-                Vector3d nozzleLoc = nozzle.getWorldPosition();
-                double u[] = new double[1];
-                Vector3d closest = MathTools.closestPointOnStraight(new Point3d(nozzleLoc), new Point3d(prev), new Vector3d(dir), u);
-                double dist = MathTools.distanceSquared(nozzleLoc,closest);
-                if (dist < BRANCH_SNAP_DISTANCE) {
-                        // FIXME : directions should be checked (insert an elbow)
-                        currentPosition.set(nozzleLoc);
-                        updateCurrentPoint();
-                        setInfoText("Connect to nozzle (l) :" + currentPosition);
-                        return true;
-                } 
-                //System.out.println(u[0]);
-               return false;
-           }
-           
-           private PipeControlPoint endingLockToComponent(INode componentNode) {
-               // we'll must scan all free pcp's and their direction to accept the connection.
-               return null;
-           }
-           
-           private void addPoint() throws Exception {
-               InlineComponent previous = (InlineComponent)getLast();
-               PipeControlPoint previousCP = previous.getControlPoint();
-               TurnComponent turn = ComponentUtils.createTurn(root);
-               InlineComponent straight = ComponentUtils.createStraight(root);
-               PipeControlPoint turnCP = turn.getControlPoint();
-               PipeControlPoint straightCP = straight.getControlPoint();
-               straight.setName(pipeRun.getUniqueName("Pipe"));
-               turn.setName(pipeRun.getUniqueName("Elbow"));
-               pipeRun.addChild(turn);
-               pipeRun.addChild(straight);
-               added.add(turn);
-               added.add(straight);
-               
-               turnCP.setDeletable(false); // mark turnCP nonDeletable so that PipingRules won't delete it immediately.
-               
-               if (!reversed) {
-                       previousCP.setNext(turnCP);
-                       turnCP.setPrevious(previousCP);
-                       turnCP.setNext(straightCP);
-                       straightCP.setPrevious(turnCP);
-               } else {
-                       previousCP.setPrevious(turnCP);
-                       turnCP.setNext(previousCP);
-                       turnCP.setPrevious(straightCP);
-                       straightCP.setNext(turnCP);
-               }
-               
-               turnCP.setWorldPosition(currentPosition);
-               turnCP.setTurnAngle(0.0);
-               turnCP.setLength(0.0);
-               straightCP.setWorldPosition(currentPosition);
-               straightCP.setLength(0.0);
-               
-               setPreviousPosition(currentPosition);
-               updateCurrentPoint();
-               
-               
-               
-           }
-           
-           /**
-            * Updates tool graphics for current point 
-            */
-           private void updateCurrentPoint() {
-               InlineComponent straight = (InlineComponent)added.get(added.size()-1);
-               // TODO: the inline length is from previous update step.
-               double l;
-               if (!reversed)
-                   l = straight.getPrevious().getControlPoint().getInlineLength();
-               else
-                   l = straight.getNext().getControlPoint().getInlineLength();
-               Vector3d v = new Vector3d();
-               v.sub(currentPosition, previousPosition);
-               double length = v.length();
-               if (length > MathTools.NEAR_ZERO) {
-               v.scale(1.0/length);
-               v.scale(0.5*(length+l));
-               v.add(previousPosition);
-               straight.getControlPoint().setWorldPosition(v);
-               straight.getControlPoint().setLength(length);
-               }
-               try {
-                               PipingRules.positionUpdate(straight.getControlPoint(),false);
-                       } catch (Exception e) {
-                               // TODO Auto-generated catch block
-                               e.printStackTrace();
+               }
+               
+               updateCurrentPoint();
+               
+               // branch point must lie between straight's ends. If connection point is exactly
+               // on straight end user may want to connect pipes to each other
+               // TODO : take account sizes of inline components)
+               // TODO : actually make connection if its detected
+               boolean connectPrev = false;
+               boolean connectNext = false;
+               
+               if (mu[0] < 0.0) {
+                       currentPosition.set(sStart);
+                       connectPrev = true;
+               }
+               else if (mu[0] > 1.0) {
+                       currentPosition.set(sEnd);
+                       connectNext = true;
+               }
+               boolean connect = false;
+               if (connectPrev) {
+                       PipeControlPoint pcp = s.getControlPoint();
+                       if (pcp.getPrevious() == null)
+                               connect = true;
+               } else if (connectNext) {
+                       PipeControlPoint pcp = s.getControlPoint();
+                       if (pcp.getNext() == null)
+                               connect = true;
+               }
+               
+               updateCurrentPoint();
+               
+               if (connect)
+                       info += "Connect pipes :";
+               else
+                       info += "Make Branch :";
+               
+               setInfoText(info + currentPosition + " " + Math.max(0.0, Math.min(mu[0], 1.0)));
+               if (connect) {
+                       if (connectNext) {
+                               return PositionType.NEXT;
+                       } else {
+                               return PositionType.PREVIOUS;
                        }
-           }
-           
-           private PipelineComponent getLast() {
-               if (added.size() == 0)
-                       return startComponent;
-               return added.get(added.size()-1);
-           }
-          
-           
-           /**
-            * Removes last point from pipeline
-            */
-           public void removePoint() {
-               if (added.size() < 3)
-                       return;
-               InlineComponent straight = (InlineComponent)added.remove(added.size()-1);
-               TurnComponent turn = (TurnComponent)added.remove(added.size()-1);
-               straight.getControlPoint().remove();
-               turn.getControlPoint().remove();
-               if (added.size() > 1) {
-                       setPreviousPosition(added.get(added.size()-2).getWorldPosition());
-               } else {
-                       setPreviousPosition(startComponent.getWorldPosition());
-                       if (direction != null)
-                               setLockType(LockType.CUSTOM, true);
-               }
-               
-           }
-           
-           private void endPiping() throws Exception {
-               state = ToolState.NOT_ACTIVE;
-                
-               if (endTo != null) {
-                   ComponentUtils.connect(getLast(), endTo, endType, currentPosition);
-               }
-               panel.useDefaultAction();
-           }
+                               
+               }
+               return PositionType.SPLIT;
+                       
+       }
+       
+       private PipeControlPoint endingToComponent(INode componentNode, Vector3d o, Vector3d d) {
+               PipelineComponent component = (PipelineComponent)componentNode;
+               PipeControlPoint pcp = component.getControlPoint();
+               if (component instanceof EndComponent) {
+                       if (pcp.getNext() != null || pcp.getPrevious() != null)
+                               return null;
+                       return pcp;
+               } else if (component instanceof TurnComponent) {
+                       if (pcp.getNext() == null || pcp.getPrevious() == null)
+                               return pcp;
+                       return null;
+               } else if (component instanceof InlineComponent) {
+                       // TODO : scan all empty pcps of the component and select closest one.
+                       if (pcp.getNext() == null || pcp.getPrevious() == null)
+                               return pcp;
+                       return null;
+               }
+
+               return null;
+       }
+       
+       private PositionType endingLockToStraight(INode straightNode, double mu[]) {
+               InlineComponent s = (InlineComponent)straightNode;
+               Point3d sStart = new Point3d();
+               Point3d sEnd = new Point3d(); 
+               s.getControlPoint().getInlineControlPointEnds(sStart, sEnd);  
+               Vector3d sDir = new Vector3d(sEnd);
+               sDir.sub(sStart);
+               Vector3d dir = new Vector3d(currentPosition);
+               Point3d prev = new Point3d(previousPosition);
+               dir.sub(prev);
+               // intersection point in pipe where branch would be inserted to
+               Vector3d branchPoint = new Vector3d();
+               // intersection point in straight pipe that is currently routed
+               Vector3d routePoint = new Vector3d();
+               MathTools.intersectStraightStraight(sStart, sDir, new Vector3d(prev), dir, branchPoint, routePoint, mu);
+               routePoint.sub(branchPoint);
+               // startPoint of branch must be between pipe ends
+               // TODO : take account sizes of elbows (or other components)
+               // branch point must be between pipe ends and intersection points must be quite close to each other
+               if (mu[0] > 0.0 && mu[0] < 1.0 && routePoint.lengthSquared() < BRANCH_SNAP_DISTANCE) {
+                       currentPosition.set(branchPoint);
+                       
+                       updateCurrentPoint();
+                       
+                       setInfoText("Make branch (l) :" + currentPosition + " " + Math.max(0.0, Math.min(mu[0], 1.0)) + " " + routePoint.lengthSquared());
+                       return PositionType.SPLIT;
+               }
+               return null;
+       }
+       
+       private boolean endingLockToNozzle(INode nozzleNode) {
+               Nozzle nozzle = (Nozzle)nozzleNode;
+               Vector3d dir = new Vector3d(currentPosition);
+               Point3d prev = new Point3d(previousPosition);
+               dir.sub(prev);
+               Vector3d nozzleLoc = nozzle.getWorldPosition();
+               double u[] = new double[1];
+               Vector3d closest = MathTools.closestPointOnStraight(new Point3d(nozzleLoc), new Point3d(prev), new Vector3d(dir), u);
+               double dist = MathTools.distanceSquared(nozzleLoc,closest);
+               if (dist < BRANCH_SNAP_DISTANCE) {
+                       // FIXME : directions should be checked (insert an elbow)
+                       currentPosition.set(nozzleLoc);
+                       updateCurrentPoint();
+                       setInfoText("Connect to nozzle (l) :" + currentPosition);
+                       return true;
+               } 
+               //System.out.println(u[0]);
+               return false;
+       }
+       
+       private PipeControlPoint endingLockToComponent(INode componentNode) {
+               // we'll must scan all free pcp's and their direction to accept the connection.
+               return null;
+       }
+       
+       private void addPoint() throws Exception {
+               InlineComponent previous = (InlineComponent)getLast();
+               PipeControlPoint previousCP = previous.getControlPoint();
+               TurnComponent turn = ComponentUtils.createTurn(root);
+               InlineComponent straight = ComponentUtils.createStraight(root);
+               PipeControlPoint turnCP = turn.getControlPoint();
+               PipeControlPoint straightCP = straight.getControlPoint();
+               straight.setName(pipeRun.getUniqueName("Pipe"));
+               turn.setName(pipeRun.getUniqueName("Elbow"));
+               pipeRun.addChild(turn);
+               pipeRun.addChild(straight);
+               added.add(turn);
+               added.add(straight);
+               
+               turnCP.setDeletable(false); // mark turnCP nonDeletable so that PipingRules won't delete it immediately.
+               
+               if (!reversed) {
+                       previousCP.setNext(turnCP);
+                       turnCP.setPrevious(previousCP);
+                       turnCP.setNext(straightCP);
+                       straightCP.setPrevious(turnCP);
+               } else {
+                       previousCP.setPrevious(turnCP);
+                       turnCP.setNext(previousCP);
+                       turnCP.setPrevious(straightCP);
+                       straightCP.setNext(turnCP);
+               }
+               
+               turnCP.setWorldPosition(currentPosition);
+               turnCP.setTurnAngle(0.0);
+               turnCP.setLength(0.0);
+               straightCP.setWorldPosition(currentPosition);
+               straightCP.setLength(0.0);
+               
+               setPreviousPosition(currentPosition);
+               updateCurrentPoint();
+               
+               
+               
+       }
+       
+       /**
+        * Updates tool graphics for current point 
+        */
+       private void updateCurrentPoint() {
+               InlineComponent straight = (InlineComponent)added.get(added.size()-1);
+               // TODO: the inline length is from previous update step.
+               double l;
+               if (!reversed)
+                       l = straight.getPrevious().getControlPoint().getInlineLength();
+               else
+                       l = straight.getNext().getControlPoint().getInlineLength();
+               Vector3d v = new Vector3d();
+               v.sub(currentPosition, previousPosition);
+               double length = v.length();
+               if (length > MathTools.NEAR_ZERO) {
+                       v.scale(1.0/length);
+                       v.scale(0.5*(length+l));
+                       v.add(previousPosition);
+                       straight.getControlPoint().setWorldPosition(v);
+                       straight.getControlPoint().setLength(length);
+               }
+               try {
+                       PipingRules.positionUpdate(straight.getControlPoint(),false);
+               } catch (Exception e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               }
+       }
+       
+       private PipelineComponent getLast() {
+               if (added.size() == 0)
+                       return startComponent;
+               return added.get(added.size()-1);
+       }
+       
+       
+       /**
+        * Removes last point from pipeline
+        */
+       public void removePoint() {
+               if (added.size() < 3)
+                       return;
+               InlineComponent straight = (InlineComponent)added.remove(added.size()-1);
+               TurnComponent turn = (TurnComponent)added.remove(added.size()-1);
+               straight.getControlPoint().remove();
+               turn.getControlPoint().remove();
+               if (added.size() > 1) {
+                       setPreviousPosition(added.get(added.size()-2).getWorldPosition());
+               } else {
+                       setPreviousPosition(startComponent.getWorldPosition());
+                       if (direction != null)
+                               setLockType(LockType.CUSTOM, true);
+               }
+               
+       }
+       
+       private void endPiping() throws Exception {
+               state = ToolState.NOT_ACTIVE;
+               
+               if (endTo != null) {
+                       ComponentUtils.connect(getLast(), endTo, endType, currentPosition);
+               }
+               panel.useDefaultAction();
+       }
 }