DefaultSizeNodeFigureWithFixedAnchors.java
Go to the documentation of this file.
00001 package org.ros.model.ros.diagram;
00002 
00003 import java.util.HashMap;
00004 import java.util.Iterator;
00005  
00006 import org.eclipse.draw2d.ConnectionAnchor;
00007 import org.eclipse.draw2d.geometry.Dimension;
00008 import org.eclipse.draw2d.geometry.Point;
00009 import org.eclipse.draw2d.geometry.PrecisionPoint;
00010 import org.eclipse.gmf.runtime.gef.ui.figures.DefaultSizeNodeFigure;
00011  
00012 public class DefaultSizeNodeFigureWithFixedAnchors extends
00013                 DefaultSizeNodeFigure {
00014  
00015         private HashMap<String, FixedConnectionAnchor> anchors = new HashMap<String, FixedConnectionAnchor>();
00016  
00017         public DefaultSizeNodeFigureWithFixedAnchors(Dimension defSize, HashMap<String, PrecisionPoint> anchorLocations) {
00018                 this(defSize.width, defSize.height, anchorLocations);
00019         }
00020  
00021         public DefaultSizeNodeFigureWithFixedAnchors(int width, int height, HashMap<String, PrecisionPoint> anchorLocations) {
00022                 super(width, height);
00023                 if (anchorLocations.size()==0)
00024                         throw new IllegalArgumentException("At least one fixed anchor must be specified");
00025                 Iterator<String> terminals = anchorLocations.keySet().iterator();
00026                 while (terminals.hasNext()) {
00027                         String terminal = terminals.next();
00028                         PrecisionPoint anchorLocation = anchorLocations.get(terminal);
00029                         anchors.put(terminal, new FixedConnectionAnchor(this, anchorLocation));
00030                 }
00031         }
00032  
00033         @Override
00034         public ConnectionAnchor getSourceConnectionAnchorAt(Point p) {
00035                 return findNearestAnchorFrom(p);
00036         }
00037  
00038         @Override
00039         public ConnectionAnchor getTargetConnectionAnchorAt(Point p) {
00040                 return findNearestAnchorFrom(p);
00041         }
00042  
00043         @Override
00044         public ConnectionAnchor getConnectionAnchor(String terminal) {
00045                 return anchors.get(terminal);
00046         }
00047  
00048         @Override
00049         public String getConnectionAnchorTerminal(ConnectionAnchor c) {
00050                 String selectedTerminal = null;
00051                 Iterator<String> terminals = anchors.keySet().iterator();
00052                 while (terminals.hasNext() && selectedTerminal==null) {
00053                         String terminal = terminals.next();
00054                         FixedConnectionAnchor anchor = anchors.get(terminal);
00055                         if (anchor == c) {
00056                                 selectedTerminal = terminal;
00057                         }
00058                 }
00059                 return selectedTerminal;
00060         }
00061  
00062         private ConnectionAnchor findNearestAnchorFrom(Point point) {
00063                 ConnectionAnchor result = null;
00064                 if (point == null || anchors.size()==1) {
00065                         result = anchors.values().iterator().next();
00066                 }
00067                 else {
00068                         double minDistance = Double.MAX_VALUE;
00069                         String nearestTerminal = null;
00070                         Iterator<String> terminals = anchors.keySet().iterator();
00071                         while (terminals.hasNext()) {
00072                                 String terminal = terminals.next();
00073                                 FixedConnectionAnchor anchor = anchors.get(terminal);
00074                                 Point anchorPosition = anchor.getLocation();
00075                                 double distance = point.getDistance(anchorPosition);
00076                                 if (distance < minDistance) {
00077                                         minDistance = distance;
00078                                         nearestTerminal = terminal;
00079                                 }
00080                         }
00081                         result = anchors.get(nearestTerminal);
00082                 }
00083                 return result;
00084         }
00085  
00086 }


bride_plugin_source
Author(s): Alexander Bubeck
autogenerated on Sun Oct 5 2014 22:38:34