Package research

Source Code of research.SaturnDrawingView

package research;

import research.util.Geom;
import research.util.AutoscrollHelper;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.dnd.DropTargetListener;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.util.Vector;
import java.util.Enumeration;
import java.io.ObjectInputStream;
import java.io.IOException;

import research.tool.Tool;
import research.connector.Connector;

/**
* author: zhangwei
* Date: 2003-5-11
* Time: 21:44:53
*/
public class SaturnDrawingView extends JPanel implements DrawingView, KeyListener, java.awt.dnd.Autoscroll, DropTargetListener {
   
    private static final String OS_NAME = System.getProperty("os.name");
   
    /**
     * the registered listeners for selection changes
     */
    private transient Vector selectionListeners;
   
    /**
     * The shown drawing.
     */
    private Drawing drawing;
   
    /**
     * the accumulated damaged area
     */
    private transient Rectangle damage = null;
   
    /**
     * The list of currently selected figures.
     */
    transient private Vector selection;
   
    /**
     * The shown selection handles.
     */
    transient private Vector selectionHandles;
   
    /**
     * The preferred size of the view
     */
    private Dimension viewSize;
   
    /**
     * The position of the last mouse click
     * inside the view.
     */
    private Point lastClick;
   
    private Tool tool = null;
   
    /**
     * The grid used to constrain points for snap to
     * grid functionality.
     */
    //private PointConstrainer fConstrainer;
   
    //added by zhangwei
    protected static final int DOING = 0;
    protected static final int SLEEP = 1;
   
    private int keyState = SLEEP;
    private int keyCode = -1;
   
    protected double scale = 1.0f;
   
    //protected JPopupMenu popupMenu = null;
   
   
    /**
     * Scrolling increment
     */
    public static final int MINIMUM_WIDTH = 400;
    public static final int MINIMUM_HEIGHT = 300;
    public static final int SCROLL_INCR = 100;
    public static final int SCROLL_OFFSET = 10;
   
   
    protected transient RenderingHints newHints = new RenderingHints(null);
    protected transient RenderingHints oldHints = null;
   
    protected int pageWidth = MINIMUM_WIDTH;
    protected int pageHeight = MINIMUM_HEIGHT;
   
    /*
     * Serialization support. In JavaDraw only the Drawing is serialized.
     * However, for beans support StandardDrawingView supports
     * serialization
     */
    private static final long serialVersionUID = -3878153366174603336L;
    private int drawingViewSerializedDataVersion = 1;
   
    protected final FigureChangeListener figureChangeListener = new InerFigureChangeListener();
   
    protected class InerFigureChangeListener implements FigureChangeListener {
       
        public void figureInvalidated(FigureChangeEvent e){}
        public void figureChanged(FigureChangeEvent e){}
        public void figureRequestRemove(FigureChangeEvent e){}
        public void figureRequestUpdate(FigureChangeEvent e){}
        /**
         * Sent when a figure was removed
         */
        public void figureRemoved(FigureChangeEvent e){
            removeFromSelection(e.getFigure());
        }
       
       
    }
   
    /**
     * Constructs the view.
     */
    public SaturnDrawingView() {
        this(MINIMUM_WIDTH, MINIMUM_HEIGHT);
       
    }
   
    public SaturnDrawingView(int width, int height) {
       
        pageWidth = width;
        pageHeight = height;
       
        viewSize = new Dimension(0,0);
       
        setLayout(null);
        setAutoscrolls(true);
        setSize(new Dimension(pageWidth, pageHeight));
        selectionListeners = new Vector();
        lastClick = new Point(0, 0);
        selection = new Vector();
        this.setOpaque(false);
        addMouseListener(ml);
        addMouseMotionListener(mml);
        addKeyListener(this);
       
        keyState = SLEEP;
        keyCode = -1;
       
        scale = 1f;
       
        /**
         * popupMenu = new JPopupMenu();
         * popupMenu.add("Copy***");
         * popupMenu.add("Cut***");
         * popupMenu.add("Paste***");
         * popupMenu.addSeparator();
         *
         * JMenu inerMenu = new JMenu("hello");
         * inerMenu.add("item1");
         * inerMenu.add("item2");
         * popupMenu.add(inerMenu);
         **/
    }
   
    public double getScale() {
        return scale;
    }
   
    public void setScale(double scale) {
        if (scale <= 0) return;
        if (this.scale == scale) return;
       
        this.scale = scale;
    }
   
    MouseListener ml = new MouseListener() {
        // listener methods we are not interested in
        public void mouseClicked(MouseEvent e) {
            if (getTool() == null) return;
            Point p = constrainPoint(e.getPoint());
            getTool().mouseClicked(e, p.x, p.y);
        }
       
        public void mouseExited(MouseEvent e) {
            if (getTool() == null) return;
            Point p = constrainPoint(e.getPoint());
            getTool().mouseExited(e, p.x, p.y);
        }
       
        public void mouseEntered(MouseEvent e) {
            if (getTool() == null) return;
            Point p = constrainPoint(e.getPoint());
            getTool().mouseEntered(e, p.x, p.y);
        }
       
        /**
         * Handles mouse down events. The event is delegated to the
         * currently active tool.
         * return whether the event was handled.
         */
        public void mousePressed(MouseEvent e) {
            requestFocus();
           
            if (getTool() == null) return;
           
            Point p = constrainPoint(e.getPoint());
            //fLastClick = new Point(e.getX(), e.getY());
            lastClick.setLocation(e.getX(), e.getY());
           
            getTool().mouseDown(e, p.x, p.y);
            checkDamage();
           
        }
       
        /**
         * Handles mouse up events. The event is delegated to the
         * currently active tool.
         * return whether the event was handled.
         */
        public void mouseReleased(MouseEvent e) {
            if (getTool() == null) return;
           
            Point p = constrainPoint(new Point(e.getPoint()));
            getTool().mouseUp(e, p.x, p.y);
            checkDamage();
           
        }
    };
   
    MouseMotionListener mml = new MouseMotionListener() {
        /**
         * Handles mouse drag events. The event is delegated to the
         * currently active tool.
         * return whether the event was handled.
         */
        public void mouseDragged(MouseEvent e) {
            if (getTool() == null) return;
            Point p = constrainPoint(e.getPoint());
            getTool().mouseDrag(e, p.x, p.y);
            checkDamage();
        }
       
        /**
         * Handles mouse move events. The event is delegated to the
         * currently active tool.
         * return whether the event was handled.
         */
        public void mouseMoved(MouseEvent e) {
            if (getTool() == null) return;
            getTool().mouseMove(e, e.getX(), e.getY());
        }
    };
   
    /**
     * Gets the current tool.
     */
    public Tool getTool() {
        return tool;
    }
   
    /**
     * Sets the current tool
     */
    public void setTool(Tool tool) {
       
        if (this.tool != null) {
            this.tool.end();
            this.tool.getContext().putValue(Tool.DRAWING_VIEW, null);
        }
        this.tool = tool;
        if (this.tool != null) {
            this.tool.getContext().putValue(Tool.DRAWING_VIEW, this);
        }
       
    }
   
    /**
     * Gets the drawing.
     */
    public Drawing getDrawing() {
        return drawing;
    }
   
    /**
     * Sets and installs another drawing in the view.
     */
    public void setDrawing(Drawing d) {
        if (drawing != null) {
            clearSelection();
            drawing.removeDrawingChangeListener(this);
            if(drawing instanceof AbstractFigure){
                ((AbstractFigure)drawing).removeFigureChangeListener(figureChangeListener);
            }
        }
       
        drawing = d;
        if (drawing != null) {
            drawing.addDrawingChangeListener(this);
            if(drawing instanceof AbstractFigure){
                ((AbstractFigure)drawing).addFigureChangeListener(figureChangeListener);
            }
        }
       
        checkMinimumSize();
        repaint();
    }
   
    /**
     * Adds a figure to the drawing.
     * @return the added figure.
     */
    public Figure add(Figure figure) {
        return getDrawing().add(figure);
    }
   
    /**
     * Removes a figure from the drawing.
     * @return the removed figure
     */
    public Figure remove(Figure figure) {
        return getDrawing().remove(figure);
    }
   
    /**
     * Adds a vector of figures to the drawing.
     */
    public void addAll(Vector figures) {
        FigureEnumeration k = new FigureEnumerator(figures);
        while (k.hasMoreElements()) {
            add(k.nextFigure());
        }
    }
   
    /**
     * Check existance of figure in the drawing
     */
    public boolean figureExists(Figure inf, FigureEnumeration e) {
        while (e.hasMoreElements()) {
            Figure figure = e.nextFigure();
           
            if (figure.includes(inf)) {
                return true;
            }
        }
       
        return false;
    }
   
    /**
     * Inserts a vector of figures and translates them by the
     * given offset. This function is used to insert figures from clipboards (cut/copy)
     *
     * @return enumeration which has been added to the drawing. The figures in the enumeration
     *         can have changed during adding them (e.g. they could have been decorated).
     */
    public FigureEnumeration insertFigures(FigureEnumeration fe, int dx, int dy, boolean bCheck) {
        if (fe == null) {
            return FigureEnumerator.getEmptyEnumeration();
        }
       
        Vector addedFigures = new Vector();
        Vector vCF = new Vector(10);
       
        while (fe.hasMoreElements()) {
            Figure figure = fe.nextFigure();
            if (figure instanceof ConnectionFigure) {
                vCF.addElement(figure);
            } else if (figure != null) {
                figure.moveBy(dx, dy);
                figure = add(figure);
                addToSelection(figure);
                // figure might has changed during adding so add it afterwards
                addedFigures.addElement(figure);
            }
        }
       
        FigureEnumeration ecf = new FigureEnumerator(vCF);
       
        while (ecf.hasMoreElements()) {
            ConnectionFigure cf = (ConnectionFigure) ecf.nextFigure();
            Figure sf = cf.startFigure();
            Figure ef = cf.endFigure();
           
            if (figureExists(sf, getDrawing().getFigures()) &&
                    figureExists(ef, getDrawing().getFigures()) &&
                    (!bCheck || cf.canConnect(sf, ef))) {
               
                if (bCheck) {
                    Point sp = sf.center();
                    Point ep = ef.center();
                    Connector fStartConnector = cf.startFigure().connectorAt(ep.x, ep.y);
                    Connector fEndConnector = cf.endFigure().connectorAt(sp.x, sp.y);
                   
                    if (fEndConnector != null && fStartConnector != null) {
                        cf.connectStart(fStartConnector);
                        cf.connectEnd(fEndConnector);
                        cf.updateConnection();
                    }
                }
               
                Figure nf = add(cf);
                addToSelection(nf);
                // figure might has changed during adding so add it afterwards
                addedFigures.addElement(nf);
            }
        }
       
        return new FigureEnumerator(addedFigures);
    }
   
    /**
     * Returns a vector of connectionfigures attached to this figure
     */
    public Vector getConnectionFigures(Figure inFigure) {
        // If no figure or figure is non connectable, just return null
        if (inFigure == null || !FigureHelper.isConnectable(inFigure)) {
            return null;
        }
       
        // if (inFigure instanceof ConnectionFigure)
        //  return null;
       
        Vector result = new Vector(5);
        FigureEnumeration figures = getDrawing().getFigures();
       
        // Find all connection figures
        while (figures.hasMoreElements()) {
            Figure f = figures.nextFigure();
           
            if ((f instanceof ConnectionFigure) && !(isFigureSelected(f))) {
                ConnectionFigure cf = (ConnectionFigure) f;
               
                if (cf.startFigure().includes(inFigure) ||
                        cf.endFigure().includes(inFigure)) {
                    result.addElement(f);
                }
            }
        }
       
        return result;
    }
   
    /**
     * Gets the minimum dimension of the drawing.
     */
    public Dimension getMinimumSize() {
        return getSize();
    }
   
    public Dimension getPageSize() {
        return new Dimension(pageWidth, pageHeight);
    }
   
    public void setSize(Dimension size){
        super.setSize(size);
        viewSize.width = size.width;
        viewSize.height = size.height;
        invalidate();
        repaint();
    }
   
    public void setSizeTemp(Dimension size){
        viewSize.width = size.width;
        viewSize.height = size.height;
    }
   
    public Dimension getSize(){
        return (Dimension)viewSize.clone();
    }
   
   
    /**
     * Gets the preferred dimension of the drawing..
     */
    public Dimension getPreferredSize() {
        return getSize();
    }
   
    /**
     * Gets the currently selected figures.
     * @return a vector with the selected figures. The vector
     * is a copy of the current selection.
     */
    public Vector getSelection() {
        // protect the vector with the current selection
        return (Vector) selection.clone();
    }
   
    /**
     * Gets an enumeration over the currently selected figures.
     */
    public FigureEnumeration selectionElements() {
        return new FigureEnumerator(getSelectionZOrdered());
    }
   
    /**
     * Gets the currently selected figures in Z order.
     * @see #selection
     * @return a vector with the selected figures. The vector
     * is a copy of the current selection.
     */
    public Vector getSelectionZOrdered() {
        Vector result = new Vector(selectionCount());
        FigureEnumeration figures = getDrawing().getFigures();
       
        while (figures.hasMoreElements()) {
            Figure f = figures.nextFigure();
            if (isFigureSelected(f)) {
                result.addElement(f);
            }
        }
        return result;
    }
   
    /**
     * Gets the number of selected figures.
     */
    public int selectionCount() {
        return selection.size();
    }
   
    /**
     * VTextIconTest whether a given figure is selected.
     */
    public boolean isFigureSelected(Figure checkFigure) {
        return selection.contains(checkFigure);
    }
   
    /**
     * Adds a figure to the current selection. The figure is only selected if
     * it is also contained in the Drawing associated with this DrawingView.
     */
    public void addToSelection(Figure figure) {
        addToSelection(figure, true);
    }
   
    protected void addToSelection(Figure figure, boolean shouldTriggerEvent) {
        if (!isFigureSelected(figure) && getDrawing().includes(figure)) {
            selection.addElement(figure);
            selectionHandles = null;
            figure.invalidate();
            if (shouldTriggerEvent)
                fireSelectionChanged();
        }
    }
   
    public void setSelection(Vector figures) {
        clearSelection(false);
        addToSelectionAll(figures);
    }
   
    public void setSelection(Figure figure) {
        clearSelection(false);
        addToSelection(figure);
    }
   
    /**
     * Adds a vector of figures to the current selection.
     */
    public void addToSelectionAll(Vector figures) {
        addToSelectionAll(new FigureEnumerator(figures));
    }
   
    /**
     * Adds a FigureEnumeration to the current selection.
     */
    public void addToSelectionAll(FigureEnumeration fe) {
        boolean flag = false;
        while (fe.hasMoreElements()) {
            Figure figure = fe.nextFigure();
            if (!isFigureSelected(figure) && getDrawing().includes(figure)) {
                flag = true;
                addToSelection(figure, false);
            }
        }
       
        if (flag) {
            fireSelectionChanged();
        }
    }
   
    /**
     * Removes a figure from the selection.
     */
    public void removeFromSelection(Figure figure) {
        if (isFigureSelected(figure)) {
            selection.removeElement(figure);
            selectionHandles = null;
            figure.invalidate();
            fireSelectionChanged();
        }
    }
   
    /**
     * If a figure isn't selected it is added to the selection.
     * Otherwise it is removed from the selection.
     */
    public void toggleSelection(Figure figure) {
        if (isFigureSelected(figure)) {
            removeFromSelection(figure);
        } else {
            addToSelection(figure);
        }
       
    }
   
    /**
     * Clears the current selection.
     */
    public void clearSelection() {
        // there is nothing selected
        //it seems wrong because this variable are not updated properly.(zhangwei)
        /**
         * if (selectionHandles == null) {
         * // avoid unnecessary selection changed event when nothing has to be cleared
         * return;
         * }
         **/
       
        clearSelection(true);
    }
   
    protected void clearSelection(boolean shouldTriggerEvent) {
        FigureEnumeration fe = selectionElements();
       
        if (!fe.hasMoreElements())
            return;
       
        while (fe.hasMoreElements()) {
            fe.nextFigure().invalidate();
        }
       
        if (selection != null) {
            selection.clear();
        } else {
            selection = new Vector();
        }
       
        selectionHandles = null;
       
        if (shouldTriggerEvent)
            fireSelectionChanged();
    }
   
    /**
     * Gets an enumeration of the currently active handles.
     */
    private Enumeration selectionHandles() {
        if (selectionHandles == null) {
            selectionHandles = new Vector();
            FigureEnumeration k = selectionElements();
            while (k.hasMoreElements()) {
                Figure figure = k.nextFigure();
                Enumeration kk = figure.handles().elements();
                while (kk.hasMoreElements()) {
                    selectionHandles.addElement(kk.nextElement());
                }
            }
        }
        return selectionHandles.elements();
    }
   
    /**
     * Gets the current selection as a FigureSelection. A FigureSelection
     * can be cut, copied, pasted.
     */
    public FigureSelection getFigureSelection() {
        return new StandardFigureSelection(new FigureEnumerator(getSelectionZOrdered()), selectionCount());
    }
   
    /**
     * Finds a handle at the given coordinates.
     * @return the hit handle, null if no handle is found.
     */
    public Handle findHandle(int x, int y) {
        Handle handle;
       
        int realX = (int) (x / scale + 0.5);
        int realY = (int) (y / scale + 0.5);
       
        Enumeration k = selectionHandles();
        while (k.hasMoreElements()) {
            handle = (Handle) k.nextElement();
            if (handle.containsPoint(realX, realY)) {
                return handle;
            }
        }
        return null;
    }
   
    /**
     * Informs that the current selection changed.
     * By default this event is forwarded to the
     * drawing editor.
     */
    protected void fireSelectionChanged() {
        if (selectionListeners != null) {
            for (int i = 0; i < selectionListeners.size(); i++) {
                FigureSelectionListener l = (FigureSelectionListener) selectionListeners.elementAt(i);
                l.figureSelectionChanged(this);
            }
        }
    }
   
    /**
     * Gets the position of the last click inside the view.
     */
    public Point getLastClick() {
        return new Point(lastClick);
    }
   
    /**
     * Constrains a point to the current grid.
     */
    protected Point constrainPoint(Point p) {
        // constrin to view size
        Dimension size = getSize();
        //p.x = Math.min(size.width, Math.max(1, p.x));
        //p.y = Math.min(size.height, Math.max(1, p.y));
        p.x = Geom.range(1, size.width, p.x);
        p.y = Geom.range(1, size.height, p.y);
       
        return p;
    }
   
    /**
     * Handles key down events. Cursor keys are handled
     * by the view the other key events are delegated to the
     * currently active tool.
     * return whether the event was handled.
     */
    public void keyPressed(KeyEvent e) {
       
        int code = e.getKeyCode();
       
        if (code == KeyEvent.VK_CONTROL) {
            getTool().keyDown(e, 0);
        }
       
        if (code != KeyEvent.VK_DOWN && code != KeyEvent.VK_UP &&
                code != KeyEvent.VK_RIGHT && code != KeyEvent.VK_LEFT)
            return;
       
        if ((keyState == DOING) && (keyCode != code))
            return;
       
        getTool().keyDown(e, keyState);
       
        keyState = DOING;
        keyCode = e.getKeyCode();
       
        checkDamage();
    }
   
    public void keyTyped(KeyEvent e) {
       
    }
   
    public void keyReleased(KeyEvent e) {
        if ((keyCode != e.getKeyCode()))
            return;
       
        keyState = SLEEP;
        keyCode = -1;
    }
   
    /**
     * Refreshes the drawing if there is some accumulated damage
     */
    public synchronized void checkDamage() {
        Enumeration each = getDrawing().drawingChangeListeners();
        while (each.hasMoreElements()) {
            Object l = each.nextElement();
            if (l instanceof DrawingView) {
                ((DrawingView) l).repairDamage();
            }
        }
    }
   
    public void repairDamage() {
        if (damage != null) {
           
            int realX = (int) (damage.x * scale);
            int realY = (int) (damage.y * scale);
            int realW = (int) (damage.width * scale) + 2;
            int realH = (int) (damage.height * scale) + 2;
           
            repaint(realX, realY, realW, realH);
           
            damage = null;
        }
    }
   
    public void drawingInvalidated(DrawingChangeEvent e) {
        Rectangle r = e.getInvalidatedRectangle();
       
        if (damage == null) {
            damage = r;
        } else {
            damage.add(r);
        }
    }
   
    public void drawingRequestUpdate(DrawingChangeEvent e) {
        repairDamage();
    }
   
    /**
     * Paints the drawing view. The actual drawing is delegated to
     * the current update strategy.
     */
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
       
        Graphics2D g2d = (Graphics2D) g;
        g2d.scale(scale, scale);
        drawAll(g);
        g2d.scale(1 / scale, 1 / (scale));
       
    }
   
   
    @Override
    public void print(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        //newHints.clear();
        //newHints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        //newHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
       
        //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        //g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
       
        //Color oldC = g.getColor();
        //g.setColor(new Color(255,255,255,0));
        //g.fillRect(0, 0, viewSize.width, viewSize.height);
        //g.setColor(oldC);
       
        //g2d.scale(2, 2);
        drawDrawing(g);
        //g2d.scale(0.5, 0.5);
    }
   
    /**
     * Draws the contents of the drawing view.
     * The view has three layers: background, drawing, handles.
     * The layers are drawn in back to front order.
     */
    public void drawAll(Graphics g) {
        openHints((Graphics2D) g);
       
        boolean isPrinting = g instanceof PrintGraphics;
        drawBackground(g);
        drawDrawing(g);
        if (!isPrinting) {
            drawHandles(g);
        }
       
        closeHints((Graphics2D) g);
    }
   
   
    //added by zhangwei
    protected void openHints(Graphics2D g2d) {
        oldHints = g2d.getRenderingHints();
       
        newHints.clear();
        newHints.putAll(oldHints);
       
        newHints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
       
        if(OS_NAME.indexOf("Mac") >= 0){
            newHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        } else{
            if (Math.abs(scale - 1) >= 0.2) {
                newHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            } else {
                newHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
            }
        }
       
        g2d.setRenderingHints(newHints);
    }
   
    protected void closeHints(Graphics2D g2d) {
       
        if(oldHints != null){
            g2d.setRenderingHints(oldHints);
        }
       
    }
   
    /**
     * Draws the given figures.
     * The view has three layers: background, drawing, handles.
     * The layers are drawn in back to front order.
     * No background is drawn.
     */
    public void draw(Graphics g, FigureEnumeration fe) {
        boolean isPrinting = g instanceof PrintGraphics;
       
        drawing.draw(g, fe);
       
        if (!isPrinting) {
            drawHandles(g);
        }
    }
   
    /**
     * Draws the currently active handles.
     */
    public void drawHandles(Graphics g) {
        Enumeration k = selectionHandles();
        while (k.hasMoreElements()) {
            ((Handle) k.nextElement()).draw(g);
        }
    }
   
    /**
     * Draws the drawing.
     */
    public void drawDrawing(Graphics g) {
        drawing.draw(g);
    }
   
    /**
     * Draws the background. If a background pattern is set it
     * is used to fill the background. Otherwise the background
     * is filled in the background color.
     */
    public void drawBackground(Graphics g) {
        g.setColor(getBackground());
        //g.fillRect(0, 0, getBounds().width, getBounds().height);
        Dimension size = getPageSize();
        g.fillRect(0, 0, size.width, size.height);
        //g.setColor(getForeground());
        //g.drawRect(0, 0, size.width, size.height);
    }
   
    /**
     * Freezes the view by acquiring the drawing lock.
     * @see research.Drawing#lock
     */
    public void freezeView() {
        getDrawing().lock();
    }
   
    /**
     * Unfreezes the view by releasing the drawing lock.
     * @see research.Drawing#unlock
     */
    public void unfreezeView() {
        getDrawing().unlock();
    }
   
    private void readObject(ObjectInputStream s)
    throws ClassNotFoundException, IOException {
       
        s.defaultReadObject();
       
        selection = new Vector(); // could use lazy initialization instead
        if (drawing != null) {
            drawing.addDrawingChangeListener(this);
        }
        selectionListeners = new Vector();
    }
   
    private void checkMinimumSize() {
        FigureEnumeration k = getDrawing().getFigures();
        Dimension d = new Dimension(0, 0);
        while (k.hasMoreElements()) {
            Rectangle r = k.nextFigure().getDisplayBox();
            d.width = Math.max(d.width, r.x + r.width);
            d.height = Math.max(d.height, r.y + r.height);
        }
       
        if (viewSize.height < d.height || viewSize.width < d.width) {
            viewSize.height = (int) (d.height * scale);
            viewSize.width = (int) (d.width * scale);
           
           
            setSize(viewSize);
        }
    }
   
    public boolean isFocusable() {
        return true;
    }
   
    public boolean isInteractive() {
        return true;
    }
   
    /**
     * Add a listener for selection changes.
     * @param fsl jhotdraw.framework.FigureSelectionListener
     */
    public void addFigureSelectionListener(FigureSelectionListener fsl) {
        selectionListeners.add(fsl);
    }
   
    /**
     * Remove a listener for selection changes.
     * @param fsl jhotdraw.framework.FigureSelectionListener
     */
    public void removeFigureSelectionListener(FigureSelectionListener fsl) {
        selectionListeners.remove(fsl);
    }
   
    public int getDefaultDNDActions() {
        return java.awt.dnd.DnDConstants.ACTION_COPY_OR_MOVE;
    }
   
   
    /***** Autoscroll support *****/
    private SaturnDrawingView.ASH ash = new SaturnDrawingView.ASH(10);
   
    public void autoscroll(java.awt.Point p) {
        ash.autoscroll(p);
    }
   
    public Insets getAutoscrollInsets() {
        return ash.getAutoscrollInsets();
    }
   
    class ASH extends AutoscrollHelper {
        public ASH(int margin) {
            super(margin);
        }
       
        public Dimension getSize() {
            return SaturnDrawingView.this.getSize();
        }
       
        public Rectangle getVisibleRect() {
            return SaturnDrawingView.this.getVisibleRect();
        }
       
        public void scrollRectToVisible(Rectangle aRect) {
            SaturnDrawingView.this.scrollRectToVisible(aRect);
        }
    }
   
    /***************************/
    public void dragEnter(DropTargetDragEvent dtde){
       
    }
   
    public void dragOver(DropTargetDragEvent dtde){
       
    }
   
    public void dropActionChanged(DropTargetDragEvent dtde){
       
    }
   
    public void dragExit(DropTargetEvent dte){
       
    }
   
    public void drop(DropTargetDropEvent dtde){
       
    }
}
TOP

Related Classes of research.SaturnDrawingView

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.