Back to index

moin  1.9.0~rc2
CompositeFigure.java
Go to the documentation of this file.
00001 /*
00002  * @(#)CompositeFigure.java 5.1
00003  *
00004  */
00005 
00006 package CH.ifa.draw.standard;
00007 
00008 import CH.ifa.draw.util.*;
00009 import CH.ifa.draw.framework.*;
00010 import java.awt.*;
00011 import java.util.*;
00012 import java.io.*;
00013 
00027 public abstract class CompositeFigure
00028                 extends AbstractFigure
00029                 implements FigureChangeListener {
00030 
00036     protected Vector fFigures;
00037 
00038     /*
00039      * Serialization support.
00040      */
00041     private static final long serialVersionUID = 7408153435700021866L;
00042     private int compositeFigureSerializedDataVersion = 1;
00043 
00044     protected CompositeFigure() {
00045         fFigures = new Vector();
00046     }
00047 
00052     public Figure add(Figure figure) {
00053         if (!fFigures.contains(figure)) {
00054             fFigures.addElement(figure);
00055             figure.addToContainer(this);
00056         }
00057         return figure;
00058     }
00059 
00064     public void addAll(Vector newFigures) {
00065         Enumeration k = newFigures.elements();
00066         while (k.hasMoreElements())
00067             add((Figure) k.nextElement());
00068     }
00069 
00074     public Figure remove(Figure figure) {
00075         if (fFigures.contains(figure)) {
00076             figure.removeFromContainer(this);
00077             fFigures.removeElement(figure);
00078         }
00079         return figure;
00080     }
00081 
00086     public void removeAll(Vector figures) {
00087         Enumeration k = figures.elements();
00088         while (k.hasMoreElements())
00089             remove((Figure) k.nextElement());
00090     }
00091 
00096     public void removeAll() {
00097         FigureEnumeration k = figures();
00098         while (k.hasMoreElements()) {
00099             Figure figure = k.nextFigure();
00100             figure.removeFromContainer(this);
00101         }
00102         fFigures.removeAllElements();
00103     }
00104 
00110     public synchronized Figure orphan(Figure figure) {
00111         fFigures.removeElement(figure);
00112         return figure;
00113     }
00114 
00120     public void orphanAll(Vector newFigures) {
00121         Enumeration k = newFigures.elements();
00122         while (k.hasMoreElements())
00123             orphan((Figure) k.nextElement());
00124     }
00125 
00130     public synchronized void replace(Figure figure, Figure replacement) {
00131         int index = fFigures.indexOf(figure);
00132         if (index != -1) {
00133             replacement.addToContainer(this);   // will invalidate figure
00134             figure.changed();
00135             fFigures.setElementAt(replacement, index);
00136         }
00137     }
00138 
00142     public synchronized void sendToBack(Figure figure) {
00143         if (fFigures.contains(figure)) {
00144             fFigures.removeElement(figure);
00145             fFigures.insertElementAt(figure,0);
00146             figure.changed();
00147         }
00148     }
00149 
00153     public synchronized void bringToFront(Figure figure) {
00154         if (fFigures.contains(figure)) {
00155             fFigures.removeElement(figure);
00156             fFigures.addElement(figure);
00157             figure.changed();
00158         }
00159     }
00160 
00165     public void draw(Graphics g, boolean showGuides) {
00166         FigureEnumeration k = figures();
00167         while (k.hasMoreElements())
00168             k.nextFigure().draw(g, showGuides);
00169     }
00170 
00174     public Figure figureAt(int i) {
00175         return (Figure)fFigures.elementAt(i);
00176     }
00177 
00182     public final FigureEnumeration figures() {
00183         return new FigureEnumerator(fFigures);
00184     }
00185 
00189     public int figureCount() {
00190         return fFigures.size();
00191     }
00192 
00197     public final FigureEnumeration figuresReverse() {
00198         return new ReverseFigureEnumerator(fFigures);
00199     }
00200 
00205     public Figure findFigure(int x, int y) {
00206         FigureEnumeration k = figuresReverse();
00207         while (k.hasMoreElements()) {
00208             Figure figure = k.nextFigure();
00209             if (figure.containsPoint(x, y))
00210                 return figure;
00211         }
00212         return null;
00213     }
00214 
00218     public Figure findFigure(Rectangle r) {
00219         FigureEnumeration k = figuresReverse();
00220         while (k.hasMoreElements()) {
00221             Figure figure = k.nextFigure();
00222             Rectangle fr = figure.displayBox();
00223             if (r.intersects(fr))
00224                 return figure;
00225         }
00226         return null;
00227     }
00228 
00238     public Figure findFigureWithout(int x, int y, Figure without) {
00239         if (without == null)
00240             return findFigure(x, y);
00241         FigureEnumeration k = figuresReverse();
00242         while (k.hasMoreElements()) {
00243             Figure figure = k.nextFigure();
00244             if (figure.containsPoint(x, y) && !figure.includes(without))
00245                 return figure;
00246         }
00247         return null;
00248     }
00249 
00256     public Figure findFigure(Rectangle r, Figure without) {
00257         if (without == null)
00258             return findFigure(r);
00259         FigureEnumeration k = figuresReverse();
00260         while (k.hasMoreElements()) {
00261             Figure figure = k.nextFigure();
00262             Rectangle fr = figure.displayBox();
00263             if (r.intersects(fr) && !figure.includes(without))
00264                 return figure;
00265         }
00266         return null;
00267     }
00268 
00275     public Figure findFigureInside(int x, int y) {
00276         FigureEnumeration k = figuresReverse();
00277         while (k.hasMoreElements()) {
00278             Figure figure = k.nextFigure().findFigureInside(x, y);
00279             if (figure != null)
00280                 return figure;
00281         }
00282         return null;
00283     }
00284 
00291     public Figure findFigureInsideWithout(int x, int y, Figure without) {
00292         FigureEnumeration k = figuresReverse();
00293         while (k.hasMoreElements()) {
00294             Figure figure = k.nextFigure();
00295             if (figure != without) {
00296                 Figure found = figure.findFigureInside(x, y);
00297                 if (found != null)
00298                     return found;
00299             }
00300         }
00301         return null;
00302     }
00303 
00308     public boolean includes(Figure figure) {
00309         if (super.includes(figure))
00310             return true;
00311 
00312         FigureEnumeration k = figures();
00313         while (k.hasMoreElements()) {
00314             Figure f = k.nextFigure();
00315             if (f.includes(figure))
00316                 return true;
00317         }
00318         return false;
00319     }
00320 
00327     protected void basicMoveBy(int x, int y) {
00328         FigureEnumeration k = figures();
00329         while (k.hasMoreElements()) {
00330             Figure fig = k.nextFigure();
00331             Figure obsrvd = (Figure) fig.getAttribute("observed.figure");
00332             if (obsrvd == null || !includes(obsrvd)) {
00333                 fig.moveBy(x, y);
00334             }
00335         }
00336     }
00337 
00341     public void release() {
00342         super.release();
00343         FigureEnumeration k = figures();
00344         while (k.hasMoreElements()) {
00345             Figure figure = k.nextFigure();
00346             figure.release();
00347         }
00348     }
00349 
00354     public void figureInvalidated(FigureChangeEvent e) {
00355         if (listener() != null)
00356             listener().figureInvalidated(e);
00357     }
00358 
00363     public void figureRequestRemove(FigureChangeEvent e) {
00364         if (listener() != null)
00365             listener().figureRequestRemove(new FigureChangeEvent(this));
00366     }
00367 
00372     public void figureRequestUpdate(FigureChangeEvent e) {
00373         if (listener() != null)
00374             listener().figureRequestUpdate(e);
00375     }
00376 
00377     public void figureChanged(FigureChangeEvent e) {
00378     }
00379 
00380     public void figureRemoved(FigureChangeEvent e) {
00381     }
00382 
00386     public void write(StorableOutput dw) {
00387         super.write(dw);
00388         dw.writeInt(fFigures.size());
00389         Enumeration k = fFigures.elements();
00390         while (k.hasMoreElements())
00391             dw.writeStorable((Storable) k.nextElement());
00392     }
00393 
00394     public String getMap() {
00395        String areas = "";
00396         Enumeration k = fFigures.elements();
00397         while (k.hasMoreElements())
00398             areas = ((Storable) k.nextElement()).getMap() + areas;
00399        return areas;
00400     }
00401 
00405     public void read(StorableInput dr) throws IOException {
00406         super.read(dr);
00407         int size = dr.readInt();
00408         fFigures = new Vector(size);
00409         for (int i=0; i<size; i++)
00410             add((Figure)dr.readStorable());
00411     }
00412 
00413     private void readObject(ObjectInputStream s)
00414         throws ClassNotFoundException, IOException {
00415 
00416         s.defaultReadObject();
00417 
00418         FigureEnumeration k = figures();
00419         while (k.hasMoreElements()) {
00420             Figure figure = k.nextFigure();
00421             figure.addToContainer(this);
00422         }
00423     }
00424 }