Back to index

moin  1.9.0~rc2
StandardDrawingView.java
Go to the documentation of this file.
00001 /*
00002  * @(#)StandardDrawingView.java 5.1
00003  *
00004  */
00005 
00006 package CH.ifa.draw.standard;
00007 
00008 import java.awt.*;
00009 import java.awt.event.*;
00010 import java.util.*;
00011 import java.io.*;
00012 import CH.ifa.draw.util.*;
00013 import CH.ifa.draw.framework.*;
00014 
00022 public  class StandardDrawingView
00023     extends Panel
00024         implements DrawingView,
00025                    MouseListener,
00026                    MouseMotionListener,
00027                    KeyListener {
00028 
00034     transient private DrawingEditor   fEditor;
00035 
00039     private Drawing         fDrawing;
00040 
00044     private transient Rectangle fDamage = null;
00045 
00049     transient private Vector fSelection;
00050 
00054     transient private Vector fSelectionHandles;
00055 
00059     private Dimension fViewSize;
00060 
00065     private Point fLastClick;
00066 
00072     private Vector fBackgrounds = null;
00073 
00079     private Vector fForegrounds = null;
00080 
00084     private Painter fUpdateStrategy;
00085 
00090     private PointConstrainer fConstrainer;
00091 
00092     private boolean showGuides;
00093 
00094     /*
00095      * Serialization support. In JavaDraw only the Drawing is serialized.
00096      * However, for beans support StandardDrawingView supports
00097      * serialization
00098      */
00099     private static final long serialVersionUID = -3878153366174603336L;
00100     private int drawingViewSerializedDataVersion = 1;
00101 
00105     public StandardDrawingView(DrawingEditor editor, int width, int height) {
00106         fEditor = editor;
00107         fViewSize = new Dimension(width,height);
00108         fLastClick = new Point(0, 0);
00109         fConstrainer = null;
00110         fSelection = new Vector();
00111        showGuides = true;
00112         setDisplayUpdate(new BufferedUpdateStrategy());
00113         setBackground(Color.lightGray);
00114 
00115         addMouseListener(this);
00116         addMouseMotionListener(this);
00117         addKeyListener(this);
00118     }
00119 
00123     public void setEditor(DrawingEditor editor) {
00124         fEditor = editor;
00125     }
00126 
00130     public Tool tool() {
00131         return fEditor.tool();
00132     }
00133 
00137     public Drawing drawing() {
00138         return fDrawing;
00139     }
00140 
00144     public void setDrawing(Drawing d) {
00145         clearSelection();
00146 
00147         if (fDrawing != null)
00148             fDrawing.removeDrawingChangeListener(this);
00149 
00150         fDrawing = d;
00151         if (fDrawing != null)
00152             fDrawing.addDrawingChangeListener(this);
00153         checkMinimumSize();
00154         repaint();
00155     }
00156 
00160     public DrawingEditor editor() {
00161         return fEditor;
00162     }
00163 
00168     public Figure add(Figure figure) {
00169         return drawing().add(figure);
00170     }
00171 
00176     public Figure remove(Figure figure) {
00177         return drawing().remove(figure);
00178     }
00179 
00183     public void addAll(Vector figures) {
00184         FigureEnumeration k = new FigureEnumerator(figures);
00185         while (k.hasMoreElements())
00186             add(k.nextFigure());
00187     }
00188 
00192     public Dimension getMinimumSize() {
00193         return fViewSize;
00194     }
00195 
00199     public Dimension getPreferredSize() {
00200         return getMinimumSize();
00201     }
00202 
00207     public void setDisplayUpdate(Painter updateStrategy) {
00208         fUpdateStrategy = updateStrategy;
00209     }
00210 
00216     public Vector selection() {
00217         // protect the vector with the current selection
00218         return (Vector)fSelection.clone();
00219     }
00220 
00224     public FigureEnumeration selectionElements() {
00225         return new FigureEnumerator(fSelection);
00226     }
00227 
00234     public Vector selectionZOrdered() {
00235         Vector result = new Vector(fSelection.size());
00236         FigureEnumeration figures = drawing().figures();
00237 
00238         while (figures.hasMoreElements()) {
00239             Figure f= figures.nextFigure();
00240             if (fSelection.contains(f)) {
00241                 result.addElement(f);
00242             }
00243         }
00244         return result;
00245     }
00246 
00250     public int selectionCount() {
00251         return fSelection.size();
00252     }
00253 
00257     public void addToSelection(Figure figure) {
00258         if (!fSelection.contains(figure)) {
00259             fSelection.addElement(figure);
00260             fSelectionHandles = null;
00261             figure.invalidate();
00262             selectionChanged();
00263         }
00264     }
00265 
00269     public void addToSelectionAll(Vector figures) {
00270         FigureEnumeration k = new FigureEnumerator(figures);
00271         while (k.hasMoreElements())
00272             addToSelection(k.nextFigure());
00273     }
00274 
00278     public void removeFromSelection(Figure figure) {
00279         if (fSelection.contains(figure)) {
00280             fSelection.removeElement(figure);
00281             fSelectionHandles = null;
00282             figure.invalidate();
00283             selectionChanged();
00284         }
00285     }
00286 
00291     public void toggleSelection(Figure figure) {
00292         if (fSelection.contains(figure))
00293             removeFromSelection(figure);
00294         else
00295             addToSelection(figure);
00296         selectionChanged();
00297     }
00298 
00302     public void clearSelection() {
00303         Figure figure;
00304 
00305         FigureEnumeration k = selectionElements();
00306 
00307         while (k.hasMoreElements())
00308             k.nextFigure().invalidate();
00309         fSelection = new Vector();
00310         fSelectionHandles = null;
00311         selectionChanged();
00312     }
00313 
00317     private Enumeration selectionHandles() {
00318         if (fSelectionHandles == null) {
00319             fSelectionHandles = new Vector();
00320             FigureEnumeration k = selectionElements();
00321             while (k.hasMoreElements()) {
00322                 Figure figure = k.nextFigure();
00323                 Enumeration kk = figure.handles().elements();
00324                 while (kk.hasMoreElements())
00325                     fSelectionHandles.addElement(kk.nextElement());
00326             }
00327         }
00328         return fSelectionHandles.elements();
00329     }
00330 
00335     public FigureSelection getFigureSelection() {
00336         return new FigureSelection(selectionZOrdered());
00337     }
00338 
00342     public void moveSelection(int dx, int dy) {
00343         FigureEnumeration figures = selectionElements();
00344         while (figures.hasMoreElements()) {
00345             Figure fig = figures.nextFigure();
00346             Figure obsrvd = (Figure) fig.getAttribute("observed.figure");
00347             if (obsrvd == null || !figures.contains(obsrvd)){
00348                 fig.moveBy(dx, dy);
00349             }
00350         }
00351         checkDamage();
00352     }
00353 
00358     public Handle findHandle(int x, int y) {
00359         Handle handle;
00360 
00361         Enumeration k = selectionHandles();
00362         while (k.hasMoreElements()) {
00363             handle = (Handle) k.nextElement();
00364             if (handle.containsPoint(x, y))
00365                 return handle;
00366         }
00367         return null;
00368     }
00369 
00375     protected void selectionChanged() {
00376         fEditor.selectionChanged(this);
00377     }
00378 
00382     public Point lastClick() {
00383         return fLastClick;
00384     }
00385 
00389     public void setConstrainer(PointConstrainer c) {
00390         fConstrainer = c;
00391     }
00392 
00396     public PointConstrainer getConstrainer() {
00397         return fConstrainer;
00398     }
00399 
00403     protected Point constrainPoint(Point p) {
00404         // constrin to view size
00405         Dimension size = getSize();
00406         //p.x = Math.min(size.width, Math.max(1, p.x));
00407         //p.y = Math.min(size.height, Math.max(1, p.y));
00408         p.x = Geom.range(1, size.width, p.x);
00409         p.y = Geom.range(1, size.height, p.y);
00410 
00411         if (fConstrainer != null )
00412             return fConstrainer.constrainPoint(p);
00413         return p;
00414        }
00415 
00421     public void mousePressed(MouseEvent e) {
00422         requestFocus(); // JDK1.1
00423         Point p = constrainPoint(new Point(e.getX(), e.getY()));
00424         fLastClick = new Point(e.getX(), e.getY());
00425         tool().mouseDown(e, p.x, p.y);
00426         checkDamage();
00427     }
00428 
00434     public void mouseDragged(MouseEvent e) {
00435         Point p = constrainPoint(new Point(e.getX(), e.getY()));
00436         tool().mouseDrag(e, p.x, p.y);
00437         checkDamage();
00438     }
00439 
00445     public void mouseMoved(MouseEvent e) {
00446         tool().mouseMove(e, e.getX(), e.getY());
00447     }
00448 
00454     public void mouseReleased(MouseEvent e) {
00455         Point p = constrainPoint(new Point(e.getX(), e.getY()));
00456         tool().mouseUp(e, p.x, p.y);
00457         checkDamage();
00458        checkMinimumSize();
00459     }
00460 
00467     public void keyPressed(KeyEvent e) {
00468         int code = e.getKeyCode();
00469         if ((code == KeyEvent.VK_BACK_SPACE) || (code == KeyEvent.VK_DELETE)) {
00470             Command cmd = new DeleteCommand("Delete", this);
00471             cmd.execute();
00472         } else if (code == KeyEvent.VK_DOWN || code == KeyEvent.VK_UP ||
00473             code == KeyEvent.VK_RIGHT || code == KeyEvent.VK_LEFT) {
00474             handleCursorKey(code);
00475         } else {
00476             tool().keyDown(e, code);
00477         }
00478         checkDamage();
00479     }
00480 
00485     protected void handleCursorKey(int key) {
00486         int dx = 0, dy = 0;
00487         int stepX = 1, stepY = 1;
00488         // should consider Null Object.
00489         if (fConstrainer != null) {
00490             stepX = fConstrainer.getStepX();
00491             stepY = fConstrainer.getStepY();
00492         }
00493 
00494         switch (key) {
00495         case KeyEvent.VK_DOWN:
00496             dy = stepY;
00497             break;
00498         case KeyEvent.VK_UP:
00499             dy = -stepY;
00500             break;
00501         case KeyEvent.VK_RIGHT:
00502             dx = stepX;
00503             break;
00504         case KeyEvent.VK_LEFT:
00505             dx = -stepX;
00506             break;
00507         }
00508         moveSelection(dx, dy);
00509     }
00510 
00514     public synchronized void checkDamage() {
00515         Enumeration each = drawing().drawingChangeListeners();
00516         while (each.hasMoreElements()) {
00517             Object l = each.nextElement();
00518             if (l instanceof DrawingView) {
00519                 ((DrawingView)l).repairDamage();
00520             }
00521         }
00522     }
00523 
00524     public void repairDamage() {
00525         if (fDamage != null) {
00526             repaint(fDamage.x, fDamage.y, fDamage.width, fDamage.height);
00527             fDamage = null;
00528         }
00529     }
00530 
00531     public void drawingInvalidated(DrawingChangeEvent e) {
00532         Rectangle r = e.getInvalidatedRectangle();
00533         if (fDamage == null)
00534             fDamage = r;
00535         else
00536             fDamage.add(r);
00537     }
00538 
00539     public void drawingRequestUpdate(DrawingChangeEvent e) {
00540         repairDamage();
00541     }
00542 
00546     public void update(Graphics g) {
00547         paint(g);
00548     }
00549 
00555     public void paint(Graphics g) {
00556         fUpdateStrategy.draw(g, this);
00557     }
00558 
00564     public void drawAll(Graphics g) {
00565        boolean isPrinting = (g instanceof PrintGraphics);
00566         drawBackground(g);
00567         if (fBackgrounds != null && !isPrinting)
00568             drawPainters(g, fBackgrounds);
00569         drawDrawing(g, showGuides);
00570         if (fForegrounds != null && !isPrinting)
00571             drawPainters(g, fForegrounds);
00572         if (!isPrinting)
00573             drawHandles(g);
00574     }
00575 
00579     public void drawHandles(Graphics g) {
00580         Enumeration k = selectionHandles();
00581         while (k.hasMoreElements())
00582             ((Handle) k.nextElement()).draw(g);
00583     }
00584 
00588     public void drawDrawing(Graphics g, boolean showGuides) {
00589         fDrawing.draw(g, showGuides);
00590     }
00591 
00597     public void drawBackground(Graphics g) {
00598         g.setColor(getBackground());
00599         g.fillRect(0, 0, getBounds().width, getBounds().height);
00600     }
00601 
00602     private void drawPainters(Graphics g, Vector v) {
00603         for (int i = 0; i < v.size(); i++)
00604             ((Painter)v.elementAt(i)).draw(g, this);
00605     }
00606 
00610     public void addBackground(Painter painter)  {
00611         if (fBackgrounds == null)
00612             fBackgrounds = new Vector(3);
00613         fBackgrounds.addElement(painter);
00614         repaint();
00615     }
00616 
00620     public void removeBackground(Painter painter)  {
00621         if (fBackgrounds != null)
00622             fBackgrounds.removeElement(painter);
00623         repaint();
00624     }
00625 
00629     public void removeForeground(Painter painter)  {
00630         if (fForegrounds != null)
00631             fForegrounds.removeElement(painter);
00632         repaint();
00633     }
00634 
00638     public void addForeground(Painter painter)  {
00639         if (fForegrounds == null)
00640             fForegrounds = new Vector(3);
00641         fForegrounds.addElement(painter);
00642         repaint();
00643     }
00644 
00649     public void freezeView() {
00650         drawing().lock();
00651     }
00652 
00657     public void unfreezeView() {
00658         drawing().unlock();
00659     }
00660 
00661     private void readObject(ObjectInputStream s)
00662         throws ClassNotFoundException, IOException {
00663 
00664         s.defaultReadObject();
00665 
00666         fSelection = new Vector(); // could use lazy initialization instead
00667         if (fDrawing != null)
00668             fDrawing.addDrawingChangeListener(this);
00669     }
00670 
00671     private Dimension getDrawingSize() {
00672         FigureEnumeration k = drawing().figures();
00673         Dimension d = new Dimension(0, 0);
00674         while (k.hasMoreElements()) {
00675             Rectangle r = k.nextFigure().displayBox();
00676             d.width = Math.max(d.width, r.x+r.width);
00677             d.height = Math.max(d.height, r.y+r.height);
00678         }
00679        return d;
00680     }
00681 
00686     private void checkMinimumSize() {
00687         Dimension d = getDrawingSize();
00688        boolean bloat = false;
00689         if (fViewSize.height < d.height) {
00690             fViewSize.height = d.height+10;
00691            bloat = true;
00692        }
00693        if (fViewSize.width < d.width) {
00694             fViewSize.width = d.width+10;
00695            bloat = true;
00696        }
00697        if (bloat)
00698             setSize(fViewSize);
00699     }
00700 
00701     public boolean isFocusTraversable() {
00702         return true;
00703     }
00704 
00705     public void enableGuides(boolean enable) {
00706        showGuides = enable;
00707     }
00708 
00709     public boolean guidesEnabled() {
00710        return showGuides;
00711     }
00712 
00713     // listener methods we are not interested in
00714     public void mouseEntered(MouseEvent e) {}
00715     public void mouseExited(MouseEvent e) {}
00716     public void mouseClicked(MouseEvent e) {}
00717     public void keyTyped(KeyEvent e) {}
00718     public void keyReleased(KeyEvent e) {}
00719 }