Back to index

moin  1.9.0~rc2
StandardDrawing.java
Go to the documentation of this file.
00001 /*
00002  * @(#)StandardDrawing.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 
00020 public class StandardDrawing extends CompositeFigure implements Drawing {
00021 
00022 
00026     private transient Vector              fListeners;
00027 
00034     private transient Thread    fDrawingLockHolder = null;
00035 
00036     /*
00037      * Serialization support
00038      */
00039     private static final long serialVersionUID = -2602151437447962046L;
00040     private int drawingSerializedDataVersion = 1;
00041 
00045     public StandardDrawing() {
00046         super();
00047         fListeners = new Vector(2);
00048     }
00049 
00050 
00054     public void addDrawingChangeListener(DrawingChangeListener listener) {
00055         fListeners.addElement(listener);
00056     }
00057 
00061     public void removeDrawingChangeListener(DrawingChangeListener listener) {
00062         fListeners.removeElement(listener);
00063     }
00064 
00068     public Enumeration drawingChangeListeners() {
00069         return fListeners.elements();
00070     }
00071 
00076     public synchronized Figure remove(Figure figure) {
00077         // ensure that we remove the top level figure in a drawing
00078         if (figure.listener() != null) {
00079             figure.listener().figureRequestRemove(new FigureChangeEvent(figure, null));
00080             return figure;
00081         }
00082         return null;
00083     }
00084 
00085 
00091     public void figureRequestRemove(FigureChangeEvent e) {
00092         Figure figure = e.getFigure();
00093         if (fFigures.contains(figure)) {
00094             fFigures.removeElement(figure);
00095             figure.removeFromContainer(this);   // will invalidate figure
00096             figure.release();
00097         } else
00098             System.out.println("Attempt to remove non-existing figure");
00099     }
00100 
00106     public void figureInvalidated(FigureChangeEvent e) {
00107         if (fListeners != null) {
00108             for (int i = 0; i < fListeners.size(); i++) {
00109                 DrawingChangeListener l = (DrawingChangeListener)fListeners.elementAt(i);
00110                 l.drawingInvalidated(new DrawingChangeEvent(this, e.getInvalidatedRectangle()));
00111             }
00112         }
00113     }
00114 
00118     public void figureRequestUpdate(FigureChangeEvent e) {
00119         if (fListeners != null) {
00120             for (int i = 0; i < fListeners.size(); i++) {
00121                 DrawingChangeListener l = (DrawingChangeListener)fListeners.elementAt(i);
00122                 l.drawingRequestUpdate(new DrawingChangeEvent(this, null));
00123             }
00124         }
00125     }
00126 
00131     public Vector handles() {
00132         Vector handles = new Vector();
00133         handles.addElement(new NullHandle(this, RelativeLocator.northWest()));
00134         handles.addElement(new NullHandle(this, RelativeLocator.northEast()));
00135         handles.addElement(new NullHandle(this, RelativeLocator.southWest()));
00136         handles.addElement(new NullHandle(this, RelativeLocator.southEast()));
00137         return handles;
00138     }
00139 
00143     public Rectangle displayBox() {
00144         if (fFigures.size() > 0) {
00145             FigureEnumeration k = figures();
00146 
00147             Rectangle r = k.nextFigure().displayBox();
00148 
00149             while (k.hasMoreElements())
00150                 r.add(k.nextFigure().displayBox());
00151             return r;
00152         }
00153         return new Rectangle(0, 0, 0, 0);
00154     }
00155 
00156     public void basicDisplayBox(Point p1, Point p2) {
00157     }
00158 
00162     public synchronized void lock() {
00163         // recursive lock
00164         Thread current = Thread.currentThread();
00165         if (fDrawingLockHolder == current)
00166             return;
00167         while (fDrawingLockHolder != null) {
00168             try { wait(); } catch (InterruptedException ex) { }
00169         }
00170         fDrawingLockHolder = current;
00171     }
00172 
00176     public synchronized void unlock() {
00177         if (fDrawingLockHolder != null) {
00178             fDrawingLockHolder = null;
00179             notifyAll();
00180         }
00181     }
00182 
00183     private void readObject(ObjectInputStream s)
00184         throws ClassNotFoundException, IOException {
00185 
00186         s.defaultReadObject();
00187 
00188         fListeners = new Vector(2);
00189     }
00190 }