Back to index

moin  1.9.0~rc2
ConnectionHandle.java
Go to the documentation of this file.
00001 /*
00002  * @(#)ConnectionHandle.java 5.1
00003  *
00004  */
00005 
00006 package CH.ifa.draw.standard;
00007 
00008 import java.awt.*;
00009 
00010 import CH.ifa.draw.framework.*;
00011 import CH.ifa.draw.util.Geom;
00012 
00027 public  class ConnectionHandle extends LocatorHandle {
00028 
00032     private ConnectionFigure fConnection;
00033 
00037     private ConnectionFigure fPrototype;
00038 
00042     private Figure fTarget = null;
00043 
00047     public ConnectionHandle(Figure owner, Locator l, ConnectionFigure prototype) {
00048         super(owner, l);
00049         fPrototype = prototype;
00050     }
00051 
00055     public void invokeStart(int  x, int  y, DrawingView view) {
00056         fConnection = createConnection();
00057         Point p = locate();
00058         fConnection.startPoint(p.x, p.y);
00059         fConnection.endPoint(p.x, p.y);
00060         view.drawing().add(fConnection);
00061     }
00062 
00066     public void invokeStep (int x, int y, int anchorX, int anchorY, DrawingView view) {
00067         Point p = new Point(x,y);
00068         Figure f = findConnectableFigure(x, y, view.drawing());
00069         // track the figure containing the mouse
00070         if (f != fTarget) {
00071             if (fTarget != null)
00072                 fTarget.connectorVisibility(false);
00073             fTarget = f;
00074             if (fTarget != null)
00075                 fTarget.connectorVisibility(true);
00076         }
00077 
00078         Connector target = findConnectionTarget(p.x, p.y, view.drawing());
00079         if (target != null)
00080             p = Geom.center(target.displayBox());
00081         fConnection.endPoint(p.x, p.y);
00082     }
00083 
00088     public void invokeEnd(int x, int y, int anchorX, int anchorY, DrawingView view) {
00089         Connector target = findConnectionTarget(x, y, view.drawing());
00090         if (target != null) {
00091             fConnection.connectStart(startConnector());
00092             fConnection.connectEnd(target);
00093             fConnection.updateConnection();
00094         } else
00095             view.drawing().remove(fConnection);
00096         fConnection = null;
00097         if (fTarget != null) {
00098             fTarget.connectorVisibility(false);
00099             fTarget = null;
00100         }
00101     }
00102 
00103     private Connector startConnector() {
00104         Point p = locate();
00105         return owner().connectorAt(p.x, p.y);
00106     }
00107 
00112     protected ConnectionFigure createConnection() {
00113               try {
00114                      return (ConnectionFigure)fPrototype.clone();
00115               } catch (Exception e) {}
00116               return null;
00117     }
00118 
00122     protected Connector findConnectionTarget(int x, int y, Drawing drawing) {
00123         Figure target = findConnectableFigure(x, y, drawing);
00124         if ((target != null) && target.canConnect()
00125              && !target.includes(owner())
00126              && fConnection.canConnect(owner(), target)) {
00127                 return findConnector(x, y, target);
00128         }
00129         return null;
00130     }
00131 
00132     private Figure findConnectableFigure(int x, int y, Drawing drawing) {
00133         FigureEnumeration k = drawing.figuresReverse();
00134         while (k.hasMoreElements()) {
00135             Figure figure = k.nextFigure();
00136             if (!figure.includes(fConnection) && figure.canConnect()) {
00137                 if (figure.containsPoint(x, y))
00138                     return figure;
00139             }
00140         }
00141         return null;
00142     }
00143 
00144     protected Connector findConnector(int x, int y, Figure f) {
00145         return f.connectorAt(x, y);
00146     }
00147 
00148 
00153     public void draw(Graphics g) {
00154         Rectangle r = displayBox();
00155         g.setColor(Color.blue);
00156         g.drawOval(r.x, r.y, r.width, r.height);
00157     }
00158 
00159 }