Back to index

moin  1.9.0~rc2
AbstractFigure.java
Go to the documentation of this file.
00001 /*
00002  * @(#)AbstractFigure.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 
00011 import java.awt.*;
00012 import java.util.*;
00013 import java.io.*;
00014 
00031 public abstract class AbstractFigure implements Figure {
00032 
00040     private FigureAttributes        fAttributes;
00041 
00048     private transient FigureChangeListener fListener;
00049 
00050     /*
00051      * Serialization support.
00052      */
00053     private static final long serialVersionUID = -10857585979273442L;
00054     private int abstractFigureSerializedDataVersion = 1;
00055 
00056     protected static Font dialogFont = Font.decode("dialog-PLAIN-12");
00057 
00058     protected AbstractFigure() { }
00059 
00063     public void moveBy(int dx, int dy) {
00064         willChange();
00065         basicMoveBy(dx, dy);
00066         changed();
00067     }
00068 
00075     protected abstract void basicMoveBy(int dx, int dy);
00076 
00085     public void displayBox(Point origin, Point corner) {
00086         willChange();
00087         basicDisplayBox(origin, corner);
00088         changed();
00089     }
00090 
00097     public abstract void basicDisplayBox(Point origin, Point corner);
00098 
00102     public abstract Rectangle displayBox();
00103 
00110     public abstract Vector handles();
00111 
00116     public FigureEnumeration figures() {
00117         Vector figures = new Vector(1);
00118         figures.addElement(this);
00119         return new FigureEnumerator(figures);
00120     }
00121 
00125     public Dimension size() {
00126         return new Dimension(displayBox().width, displayBox().height);
00127     }
00128 
00134     public boolean isEmpty() {
00135         return (size().width < 3) || (size().height < 3);
00136     }
00137 
00145     public Figure findFigureInside(int x, int y) {
00146         if (containsPoint(x, y))
00147             return this;
00148         return null;
00149     }
00150 
00154     public boolean containsPoint(int x, int y) {
00155         return displayBox().contains(x, y);
00156     }
00157 
00164     public void displayBox(Rectangle r) {
00165         displayBox(new Point(r.x, r.y), new Point(r.x+r.width, r.y+r.height));
00166     }
00167 
00171     public boolean includes(Figure figure) {
00172         return figure == this;
00173     }
00174 
00181     public FigureEnumeration decompose() {
00182         Vector figures = new Vector(1);
00183         figures.addElement(this);
00184         return new FigureEnumerator(figures);
00185     }
00186 
00193     public void addToContainer(FigureChangeListener c) {
00194         addFigureChangeListener(c);
00195         invalidate();
00196     }
00197 
00202     public void removeFromContainer(FigureChangeListener c) {
00203         invalidate();
00204         removeFigureChangeListener(c);
00205         changed();
00206     }
00207 
00211     public void addFigureChangeListener(FigureChangeListener l) {
00212         fListener = FigureChangeEventMulticaster.add(fListener, l);
00213     }
00214 
00218     public void removeFigureChangeListener(FigureChangeListener l) {
00219         fListener = FigureChangeEventMulticaster.remove(fListener, l);
00220     }
00221 
00225     public FigureChangeListener listener() {
00226         return fListener;
00227     }
00228 
00234     public void release() {
00235         if (fListener != null)
00236             fListener.figureRemoved(new FigureChangeEvent(this));
00237     }
00238 
00244     public void invalidate() {
00245         if (fListener != null) {
00246             Rectangle r = displayBox();
00247             r.grow(Handle.HANDLESIZE, Handle.HANDLESIZE);
00248             fListener.figureInvalidated(new FigureChangeEvent(this, r));
00249         }
00250     }
00251 
00258     public void willChange() {
00259         invalidate();
00260     }
00261 
00268     public void changed() {
00269         invalidate();
00270         if (fListener != null)
00271             fListener.figureChanged(new FigureChangeEvent(this));
00272 
00273     }
00274 
00279     public Point center() {
00280         return Geom.center(displayBox());
00281     }
00282 
00287     public boolean canConnect() {
00288         return true;
00289     }
00290 
00298     public Insets connectionInsets() {
00299         return new Insets(0, 0, 0, 0);
00300     }
00301 
00307     public Connector connectorAt(int x, int y) {
00308         return new ChopBoxConnector(this);
00309     }
00310 
00315     public void connectorVisibility(boolean isVisible) {
00316     }
00317 
00321     public Locator connectedTextLocator(Figure text) {
00322         return RelativeLocator.center();
00323     }
00324 
00329     public Object defaultAttribute(String name) {
00330        return null;
00331     }
00332 
00339     public Object getAttribute(String name) {
00340         if (fAttributes != null) {
00341             if (fAttributes.hasDefined(name))
00342                 return fAttributes.get(name);
00343         }
00344         return defaultAttribute(name);
00345     }
00346 
00350     public void setAttribute(String name, Object value) {
00351         if (fAttributes == null)
00352             fAttributes = new FigureAttributes();
00353         fAttributes.set(name, value);
00354         changed();
00355     }
00356 
00364     public Object clone() {
00365         Object clone = null;
00366         ByteArrayOutputStream output = new ByteArrayOutputStream(200);
00367         try {
00368             ObjectOutput writer = new ObjectOutputStream(output);
00369             writer.writeObject(this);
00370             writer.close();
00371         } catch (IOException e) {
00372             System.out.println("Class not found: " + e);
00373         }
00374 
00375         InputStream input = new ByteArrayInputStream(output.toByteArray());
00376         try {
00377             ObjectInput reader = new ObjectInputStream(input);
00378             clone = (Object) reader.readObject();
00379         } catch (IOException e) {
00380             System.out.println(e.toString());
00381         }
00382         catch (ClassNotFoundException e) {
00383             System.out.println("Class not found: " + e);
00384         }
00385         return clone;
00386     }
00387 
00391     public void write(StorableOutput dw) {
00392         if (fAttributes == null)
00393             dw.writeString("no_attributes");
00394         else {
00395             dw.writeString("attributes");
00396             fAttributes.write(dw);
00397         }
00398     }
00399 
00403     public void read(StorableInput dr) throws IOException {
00404        // test for number being next; if it's a number this is
00405        // an old-format file with no attributes on this figure type
00406        if (dr.testForNumber())
00407            return;
00408        String s = dr.readString();
00409        if (s.toLowerCase().equals("attributes")) {
00410            fAttributes = new FigureAttributes();
00411            fAttributes.read(dr);
00412        }
00413     }
00414 }