Back to index

moin  1.9.0~rc2
ConnectionTool.java
Go to the documentation of this file.
00001 /*
00002  * @(#)ConnectionTool.java 5.1
00003  *
00004  */
00005 
00006 package CH.ifa.draw.standard;
00007 
00008 import java.awt.*;
00009 import java.awt.event.MouseEvent;
00010 import java.util.*;
00011 
00012 import CH.ifa.draw.framework.*;
00013 import CH.ifa.draw.util.Geom;
00014 
00032 public  class ConnectionTool extends AbstractTool {
00033 
00037     private Connector   fStartConnector;
00038     private Connector   fEndConnector;
00039     private Connector   fConnectorTarget = null;
00040 
00041     private Figure fTarget = null;
00042 
00046     private ConnectionFigure  fConnection;
00047 
00051     private int  fSplitPoint;
00052 
00056     private ConnectionFigure  fEditedConnection = null;
00057 
00062     private ConnectionFigure  fPrototype;
00063 
00064 
00065     public ConnectionTool(DrawingView view, ConnectionFigure prototype) {
00066         super(view);
00067         fPrototype = prototype;
00068     }
00069 
00073     public void mouseMove(MouseEvent e, int x, int y) {
00074         trackConnectors(e, x, y);
00075     }
00076 
00082     public void mouseDown(MouseEvent e, int x, int y)
00083     {
00084         int ex = e.getX();
00085         int ey = e.getY();
00086         fTarget = findConnectionStart(ex, ey, drawing());
00087         if (fTarget != null) {
00088             fStartConnector = findConnector(ex, ey, fTarget);
00089             if (fStartConnector != null) {
00090                 Point p = new Point(ex, ey);
00091                 fConnection = createConnection();
00092                 fConnection.startPoint(p.x, p.y);
00093                 fConnection.endPoint(p.x, p.y);
00094                 view().add(fConnection);
00095             }
00096         }
00097         else {
00098             ConnectionFigure connection = findConnection(ex, ey, drawing());
00099             if (connection != null) {
00100                 if (!connection.joinSegments(ex, ey)) {
00101                     fSplitPoint = connection.splitSegment(ex, ey);
00102                     fEditedConnection = connection;
00103                 } else {
00104                     fEditedConnection = null;
00105                 }
00106             }
00107         }
00108     }
00109 
00113     public void mouseDrag(MouseEvent e, int x, int y) {
00114         Point p = new Point(e.getX(), e.getY());
00115         if (fConnection != null) {
00116             trackConnectors(e, x, y);
00117             if (fConnectorTarget != null)
00118                 p = Geom.center(fConnectorTarget.displayBox());
00119             fConnection.endPoint(p.x, p.y);
00120         }
00121         else if (fEditedConnection != null) {
00122             Point pp = new Point(x, y);
00123             fEditedConnection.setPointAt(pp, fSplitPoint);
00124         }
00125     }
00126 
00131     public void mouseUp(MouseEvent e, int x, int y) {
00132         Figure c = null;
00133         if (fStartConnector != null)
00134             c = findTarget(e.getX(), e.getY(), drawing());
00135 
00136         if (c != null) {
00137             fEndConnector = findConnector(e.getX(), e.getY(), c);
00138             if (fEndConnector != null) {
00139                 fConnection.connectStart(fStartConnector);
00140                 fConnection.connectEnd(fEndConnector);
00141                 fConnection.updateConnection();
00142             }
00143         } else if (fConnection != null)
00144             view().remove(fConnection);
00145 
00146         fConnection = null;
00147         fStartConnector = fEndConnector = null;
00148         editor().toolDone();
00149     }
00150 
00151     public void deactivate() {
00152         super.deactivate();
00153         if (fTarget != null)
00154             fTarget.connectorVisibility(false);
00155     }
00156 
00161     protected ConnectionFigure createConnection() {
00162               try {
00163             return (ConnectionFigure)fPrototype.clone();
00164               } catch (Exception e) {}
00165               return null;
00166     }
00167 
00171     protected Figure findSource(int x, int y, Drawing drawing) {
00172         return findConnectableFigure(x, y, drawing);
00173     }
00174 
00178     protected Figure findTarget(int x, int y, Drawing drawing) {
00179         Figure target = findConnectableFigure(x, y, drawing);
00180         Figure start = fStartConnector.owner();
00181 
00182         if (target != null
00183              && fConnection != null
00184              && target.canConnect()
00185              && !target.includes(start)
00186              && fConnection.canConnect(start, target))
00187             return target;
00188         return null;
00189     }
00190 
00194     protected ConnectionFigure findConnection(int x, int y, Drawing drawing) {
00195         Enumeration k = drawing.figuresReverse();
00196         while (k.hasMoreElements()) {
00197             Figure figure = (Figure) k.nextElement();
00198             figure = figure.findFigureInside(x, y);
00199             if (figure != null && (figure instanceof ConnectionFigure))
00200                 return (ConnectionFigure)figure;
00201         }
00202         return null;
00203     }
00204 
00208     protected ConnectionFigure createdFigure() {
00209         return fConnection;
00210     }
00211 
00212     protected void trackConnectors(MouseEvent e, int x, int y) {
00213         Figure c = null;
00214 
00215         if (fStartConnector == null)
00216             c = findSource(x, y, drawing());
00217         else
00218             c = findTarget(x, y, drawing());
00219 
00220         // track the figure containing the mouse
00221         if (c != fTarget) {
00222             if (fTarget != null)
00223                 fTarget.connectorVisibility(false);
00224             fTarget = c;
00225             if (fTarget != null)
00226                 fTarget.connectorVisibility(true);
00227         }
00228 
00229         Connector cc = null;
00230         if (c != null)
00231             cc = findConnector(e.getX(), e.getY(), c);
00232         if (cc != fConnectorTarget)
00233             fConnectorTarget = cc;
00234 
00235         view().checkDamage();
00236     }
00237 
00238     private Connector findConnector(int x, int y, Figure f) {
00239         return f.connectorAt(x, y);
00240     }
00241 
00245     protected Figure findConnectionStart(int x, int y, Drawing drawing) {
00246         Figure target = findConnectableFigure(x, y, drawing);
00247         if ((target != null) && target.canConnect())
00248             return target;
00249         return null;
00250     }
00251 
00252     private Figure findConnectableFigure(int x, int y, Drawing drawing) {
00253         FigureEnumeration k = drawing.figuresReverse();
00254         while (k.hasMoreElements()) {
00255             Figure figure = k.nextFigure();
00256             if (!figure.includes(fConnection) && figure.canConnect()) {
00257                 if (figure.containsPoint(x, y))
00258                     return figure;
00259             }
00260         }
00261         return null;
00262     }
00263 
00264     protected Connector getStartConnector() {
00265         return fStartConnector;
00266     }
00267 
00268     protected Connector getEndConnector() {
00269         return fEndConnector;
00270     }
00271 
00272     protected Connector getTarget() {
00273         return fConnectorTarget;
00274     }
00275 
00276 }