Back to index

moin  1.9.0~rc2
ChangeConnectionHandle.java
Go to the documentation of this file.
00001 /*
00002  * @(#)ChangeConnectionHandle.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 
00020 public abstract class ChangeConnectionHandle extends AbstractHandle {
00021 
00022     protected Connector         fOriginalTarget;
00023     protected Figure            fTarget;
00024     protected ConnectionFigure  fConnection;
00025     protected Point             fStart;
00026 
00030     protected ChangeConnectionHandle(Figure owner) {
00031         super(owner);
00032         fConnection = (ConnectionFigure) owner();
00033         fTarget = null;
00034     }
00035 
00039     protected abstract Connector target();
00040 
00044     protected abstract void disconnect();
00045 
00049     protected abstract void connect(Connector c);
00050 
00054     protected abstract void setPoint(int x, int y);
00055 
00060     protected Connector source() {
00061         if (target() == fConnection.start())
00062             return fConnection.end();
00063         return fConnection.start();
00064     }
00065 
00069     public void invokeStart(int  x, int  y, DrawingView view) {
00070         fOriginalTarget = target();
00071         fStart = new Point(x, y);
00072         disconnect();
00073     }
00074 
00078     public void invokeStep (int x, int y, int anchorX, int anchorY, DrawingView view) {
00079         Point p = new Point(x, y);
00080         Figure f = findConnectableFigure(x, y, view.drawing());
00081         // track the figure containing the mouse
00082         if (f != fTarget) {
00083             if (fTarget != null)
00084                 fTarget.connectorVisibility(false);
00085             fTarget = f;
00086             if (fTarget != null)
00087                 fTarget.connectorVisibility(true);
00088         }
00089 
00090         Connector target = findConnectionTarget(p.x, p.y, view.drawing());
00091         if (target != null)
00092             p = Geom.center(target.displayBox());
00093         setPoint(p.x, p.y);
00094     }
00095 
00100     public void invokeEnd(int x, int y, int anchorX, int anchorY, DrawingView view) {
00101         Connector target = findConnectionTarget(x, y, view.drawing());
00102         if (target == null)
00103             target = fOriginalTarget;
00104 
00105         setPoint(x, y);
00106         connect(target);
00107         fConnection.updateConnection();
00108         if (fTarget != null) {
00109             fTarget.connectorVisibility(false);
00110             fTarget = null;
00111         }
00112     }
00113 
00114     private Connector findConnectionTarget(int x, int y, Drawing drawing) {
00115         Figure target = findConnectableFigure(x, y, drawing);
00116 
00117         if ((target != null) && target.canConnect()
00118              && target != fOriginalTarget
00119              && !target.includes(owner())
00120              && fConnection.canConnect(source().owner(), target)) {
00121                 return findConnector(x, y, target);
00122         }
00123         return null;
00124     }
00125 
00126     protected Connector findConnector(int x, int y, Figure f) {
00127         return f.connectorAt(x, y);
00128     }
00129 
00133     public void draw(Graphics g) {
00134         Rectangle r = displayBox();
00135 
00136         g.setColor(Color.green);
00137         g.fillRect(r.x, r.y, r.width, r.height);
00138 
00139         g.setColor(Color.black);
00140         g.drawRect(r.x, r.y, r.width, r.height);
00141     }
00142 
00143     private Figure findConnectableFigure(int x, int y, Drawing drawing) {
00144         FigureEnumeration k = drawing.figuresReverse();
00145         while (k.hasMoreElements()) {
00146             Figure figure = k.nextFigure();
00147             if (!figure.includes(fConnection) && figure.canConnect()) {
00148                 if (figure.containsPoint(x, y))
00149                     return figure;
00150             }
00151         }
00152         return null;
00153     }
00154 }