Back to index

moin  1.9.0~rc2
LineConnection.java
Go to the documentation of this file.
00001 /*
00002  * @(#)LineConnection.java 5.1
00003  *
00004  */
00005 
00006 package CH.ifa.draw.figures;
00007 
00008 import java.awt.*;
00009 import java.util.*;
00010 import java.io.*;
00011 import CH.ifa.draw.framework.*;
00012 import CH.ifa.draw.standard.*;
00013 import CH.ifa.draw.util.*;
00014 
00020 public  class LineConnection extends PolyLineFigure implements ConnectionFigure {
00021 
00022     protected Connector    fStart = null;
00023     protected Connector    fEnd = null;
00024 
00025     /*
00026      * Serialization support.
00027      */
00028     private static final long serialVersionUID = 6883731614578414801L;
00029     private int lineConnectionSerializedDataVersion = 1;
00030 
00035     public LineConnection() {
00036         super(4);
00037         setStartDecoration(new ArrowTip());
00038         setEndDecoration(new ArrowTip());
00039     }
00040 
00045     public boolean canConnect() {
00046         return false;
00047     }
00048 
00053     protected void basicMoveBy(int dx, int dy) {
00054         // don't move the start and end point since they are connected
00055         for (int i = 1; i < fPoints.size()-1; i++)
00056             ((Point) fPoints.elementAt(i)).translate(dx, dy);
00057 
00058         updateConnection(); // make sure that we are still connected
00059     }
00060 
00064     public void connectStart(Connector start) {
00065         fStart = start;
00066         startFigure().addFigureChangeListener(this);
00067     }
00068 
00072     public void connectEnd(Connector end) {
00073         fEnd = end;
00074         endFigure().addFigureChangeListener(this);
00075         handleConnect(startFigure(), endFigure());
00076     }
00077 
00081     public void disconnectStart() {
00082         startFigure().removeFigureChangeListener(this);
00083         fStart = null;
00084     }
00085 
00089     public void disconnectEnd() {
00090         handleDisconnect(startFigure(), endFigure());
00091         endFigure().removeFigureChangeListener(this);
00092         fEnd = null;
00093     }
00094 
00099     public boolean connectsSame(ConnectionFigure other) {
00100         return other.start() == start() && other.end() == end();
00101     }
00102 
00107     protected void handleDisconnect(Figure start, Figure end) {}
00108 
00113     protected void handleConnect(Figure start, Figure end) {}
00114 
00118     public Figure startFigure() {
00119         if (start() != null)
00120             return start().owner();
00121         return null;
00122     }
00123 
00127     public Figure endFigure() {
00128         if (end() != null)
00129             return end().owner();
00130         return null;
00131     }
00132 
00136     public Connector start() {
00137         return fStart;
00138     }
00139 
00143     public Connector end() {
00144         return fEnd;
00145     }
00146 
00150     public boolean canConnect(Figure start, Figure end) {
00151         return true;
00152     }
00153 
00157     public void startPoint(int x, int y) {
00158         willChange();
00159         if (fPoints.size() == 0)
00160             fPoints.addElement(new Point(x, y));
00161         else
00162             fPoints.setElementAt(new Point(x, y), 0);
00163         changed();
00164     }
00165 
00169     public void endPoint(int x, int y) {
00170         willChange();
00171         if (fPoints.size() < 2)
00172             fPoints.addElement(new Point(x, y));
00173         else
00174             fPoints.setElementAt(new Point(x, y), fPoints.size()-1);
00175         changed();
00176     }
00177 
00181     public Point startPoint(){
00182         Point p = (Point)fPoints.firstElement();
00183         return new Point(p.x, p.y);
00184     }
00185 
00189     public Point endPoint() {
00190         Point p = (Point)fPoints.lastElement();
00191         return new Point(p.x, p.y);
00192     }
00193 
00199     public Vector handles() {
00200         Vector handles = new Vector(fPoints.size());
00201         handles.addElement(new ChangeConnectionStartHandle(this));
00202         for (int i = 1; i < fPoints.size()-1; i++)
00203             handles.addElement(new PolyLineHandle(this, locator(i), i));
00204         handles.addElement(new ChangeConnectionEndHandle(this));
00205         return handles;
00206     }
00207 
00211     public void setPointAt(Point p, int i) {
00212         super.setPointAt(p, i);
00213         layoutConnection();
00214     }
00215 
00219     public void insertPointAt(Point p, int i) {
00220         super.insertPointAt(p, i);
00221         layoutConnection();
00222     }
00223 
00227     public void removePointAt(int i) {
00228         super.removePointAt(i);
00229         layoutConnection();
00230     }
00231 
00235     public void updateConnection() {
00236         if (fStart != null) {
00237             Point start = fStart.findStart(this);
00238             startPoint(start.x, start.y);
00239         }
00240         if (fEnd != null) {
00241             Point end = fEnd.findEnd(this);
00242             endPoint(end.x, end.y);
00243         }
00244     }
00245 
00250     public void layoutConnection() {
00251         updateConnection();
00252     }
00253 
00254     public void figureChanged(FigureChangeEvent e) {
00255         updateConnection();
00256     }
00257 
00258     public void figureRemoved(FigureChangeEvent e) {
00259         if (listener() != null)
00260             listener().figureRequestRemove(new FigureChangeEvent(this));
00261     }
00262 
00263     public void figureRequestRemove(FigureChangeEvent e) {}
00264     public void figureInvalidated(FigureChangeEvent e) {}
00265     public void figureRequestUpdate(FigureChangeEvent e) {}
00266 
00267     public void release() {
00268         super.release();
00269         handleDisconnect(startFigure(), endFigure());
00270         if (fStart != null) startFigure().removeFigureChangeListener(this);
00271         if (fEnd   != null) endFigure().removeFigureChangeListener(this);
00272     }
00273 
00274     public void write(StorableOutput dw) {
00275         super.write(dw);
00276         dw.writeStorable(fStart);
00277         dw.writeStorable(fEnd);
00278     }
00279 
00280     public void read(StorableInput dr) throws IOException {
00281         super.read(dr);
00282         Connector start = (Connector)dr.readStorable();
00283         if (start != null)
00284             connectStart(start);
00285         Connector end = (Connector)dr.readStorable();
00286         if (end != null)
00287             connectEnd(end);
00288         if (start != null && end != null)
00289             updateConnection();
00290     }
00291 
00292     private void readObject(ObjectInputStream s)
00293         throws ClassNotFoundException, IOException {
00294 
00295         s.defaultReadObject();
00296 
00297         if (fStart != null)
00298             connectStart(fStart);
00299         if (fEnd != null)
00300             connectEnd(fEnd);
00301     }
00302 }