Package research.connection

Source Code of research.connection.LineConnection

/*
* @(#)LineConnection.java
*
* Project:    JHotdraw - a GUI framework for technical drawings
*        http://www.jhotdraw.org
*        http://jhotdraw.sourceforge.net
* Copyright:  ?by the original author(s) and all contributors
* License:    Lesser GNU Public License (LGPL)
*        http://www.opensource.org/licenses/lgpl-license.html
*/
package research.connection;

import research.figure.PolyLineFigure;
import research.figure.ArrowTip;
import research.*;
import research.connector.Connector;
import research.store.StorableOutput;
import research.store.StorableInput;

import java.awt.*;
import java.util.*;
import java.io.*;

/**
* A LineConnection is a standard implementation of the
* ConnectionFigure interface. The interface is implemented with PolyLineFigure.
*
* @see research.ConnectionFigure
*
* @version <$CURRENT_VERSION$>
*/
public class LineConnection extends PolyLineFigure implements ConnectionFigure {

    protected Connector myStartConnector;
    protected Connector myEndConnector;

    /*
     * Serialization support.
     */
    private static final long serialVersionUID = 6883731614578414801L;
    private int lineConnectionSerializedDataVersion = 1;

    /**
     * Constructs a LineConnection. A connection figure has
     * an arrow decoration at the start and end.
     */
    public LineConnection() {
        super(4);

        this.setAttribute("connectability", Boolean.valueOf(false));
    }

    /**
     * Ensures that a connection is updated if the connection
     * was moved.
     */
    @Override
    protected void basicMoveBy(int dx, int dy) {
        // don't move the start and end point since they are connected
        for (int i = 1; i < fPoints.size() - 1; i++) {
            ((Point) fPoints.elementAt(i)).translate(dx, dy);
        }

        //updateConnection(); // make sure that we are still connected
        layoutConnection();
    }

    /**
     * Sets the start figure of the connection.
     */
    public void connectStart(Connector newStartConnector) {
        setStartConnector(newStartConnector);
        startFigure().addFigureChangeListener(this);
    }

    /**
     * Sets the end figure of the connection.
     */
    public void connectEnd(Connector newEndConnector) {
        setEndConnector(newEndConnector);
        endFigure().addFigureChangeListener(this);
        handleConnect(startFigure(), endFigure());
    }

    /**
     * Disconnects the start figure.
     */
    public void disconnectStart() {
        startFigure().removeFigureChangeListener(this);
        setStartConnector(null);
    }

    /**
     * Disconnects the end figure.
     */
    public void disconnectEnd() {
        handleDisconnect(startFigure(), endFigure());
        endFigure().removeFigureChangeListener(this);
        setEndConnector(null);
    }

    /**
     * Tests whether a connection connects the same figures
     * as another ConnectionFigure.
     */
    public boolean connectsSame(ConnectionFigure other) {
        return other.getStartConnector() == getStartConnector() && other.getEndConnector() == getEndConnector();
    }

    /**
     * Handles the disconnection of a connection.
     * Override this method to handle this event.
     */
    protected void handleDisconnect(Figure start, Figure end) {
    }

    /**
     * Handles the connection of a connection.
     * Override this method to handle this event.
     */
    protected void handleConnect(Figure start, Figure end) {
    }

    /**
     * Gets the start figure of the connection.
     */
    public Figure startFigure() {
        if (getStartConnector() != null) {
            return getStartConnector().owner();
        }
        return null;
    }

    /**
     * Gets the end figure of the connection.
     */
    public Figure endFigure() {
        if (getEndConnector() != null) {
            return getEndConnector().owner();
        }
        return null;
    }

    protected void setStartConnector(Connector newStartConnector) {
        myStartConnector = newStartConnector;
    }

    /**
     * Gets the start figure of the connection.
     */
    public Connector getStartConnector() {
        return myStartConnector;
    }

    protected void setEndConnector(Connector newEndConnector) {
        myEndConnector = newEndConnector;
    }

    /**
     * Gets the end figure of the connection.
     */
    public Connector getEndConnector() {
        return myEndConnector;
    }

    /**
     * Tests whether two figures can be connected.
     */
    public boolean canConnect(Figure start, Figure end) {
        return true;
    }

    /**
     * Sets the start point.
     */
    public void startPoint(int x, int y) {
        willChange();
        _startPoint(x, y);
        changed();
    }

    protected void _startPoint(int x, int y) {
        Point start = new Point(x, y);

        if (fPoints.size() == 0) {
            fPoints.addElement(start);
        } else {
            fPoints.setElementAt(start, 0);
        }
    }

    /**
     * Sets the end point.
     */
    public void endPoint(int x, int y) {
        willChange();
        _endPoint(x, y);
        changed();
    }

    protected void _endPoint(int x, int y) {
        if (fPoints.size() < 2) {
            fPoints.addElement(new Point(x, y));
        } else {
            fPoints.setElementAt(new Point(x, y), fPoints.size() - 1);
        }
    }

    /**
     * Gets the start point.
     */
    //@Override
    public Point startPoint() {
        Point p = (Point) fPoints.firstElement();
        return new Point(p.x, p.y);
    }

    /**
     * Gets the end point.
     */
    public Point endPoint() {
        Point p = (Point) fPoints.lastElement();
        return new Point(p.x, p.y);
    }

    /**
     * Gets the handles of the figure. It returns the normal
     * PolyLineHandles but adds ChangeConnectionHandles at the
     * start and end.
     */
    @Override
    public Vector handles() {
        Vector handles = new Vector(fPoints.size());
        handles.addElement(new ChangeConnectionStartHandle(this));
        for (int i = 1; i < fPoints.size() - 1; i++) {
            handles.addElement(new PolyLineHandle(this, locator(i), i));
        }
        handles.addElement(new ChangeConnectionEndHandle(this));
        return handles;
    }

    /**
     * Sets the point and updates the connection.
     */
    @Override
    public void setPointAt(Point p, int i) {
        super.setPointAt(p, i);
    //layoutConnection();
    }

    /**
     * Inserts the point and updates the connection.
     */
    @Override
    public void insertPointAt(Point p, int i) {
        super.insertPointAt(p, i);
    //layoutConnection();
    }

    /**
     * Removes the point and updates the connection.
     */
    @Override
    public void removePointAt(int i) {
        super.removePointAt(i);
    //layoutConnection();
    }

    /**
     * Updates the connection.
     */
    public void updateConnection() {
        willChange();
        _updateConnection();
        changed();
    }

    protected void _updateConnection() {
        if (getStartConnector() != null) {
            Point start = getStartConnector().findStart(this);

            if (start != null) {
                _startPoint(start.x, start.y);
            }
        }
        if (getEndConnector() != null) {
            Point end = getEndConnector().findEnd(this);

            if (end != null) {
                _endPoint(end.x, end.y);
            }
        }
    }

    /**
     * Lays out the connection. This is called when the connection
     * itself changes. By default the connection is recalculated
     */
    public void layoutConnection() {
        willChange();
        _layoutConnection();
        changed();
    }

    protected void _layoutConnection() {
        _updateConnection();
    }

    public void figureChanged(FigureChangeEvent e) {
        //updateConnection();
        layoutConnection();
    }

    public void figureRemoved(FigureChangeEvent e) {
        if (listener() != null) {
            listener().figureRequestRemove(new FigureChangeEvent(this));
        }
    }

    public void figureRequestRemove(FigureChangeEvent e) {
    }

    public void figureInvalidated(FigureChangeEvent e) {
    }

    public void figureRequestUpdate(FigureChangeEvent e) {
    }

    @Override
    public void release() {
        super.release();
        handleDisconnect(startFigure(), endFigure());
        if (getStartConnector() != null) {
            startFigure().removeFigureChangeListener(this);
        }
        if (getEndConnector() != null) {
            endFigure().removeFigureChangeListener(this);
        }
    }

    public void write(StorableOutput dw) {
        super.write(dw);
        dw.writeStorable(getStartConnector());
        dw.writeStorable(getEndConnector());
    }

    public void read(StorableInput dr) throws IOException {
        super.read(dr);
        Connector start = (Connector) dr.readStorable();
        if (start != null) {
            connectStart(start);
        }
        Connector end = (Connector) dr.readStorable();
        if (end != null) {
            connectEnd(end);
        }
        if (start != null && end != null) {
            //updateConnection();
            layoutConnection();
        }
    }

    private void readObject(ObjectInputStream s)
            throws ClassNotFoundException, IOException {

        s.defaultReadObject();

        if (getStartConnector() != null) {
            connectStart(getStartConnector());
        }
        if (getEndConnector() != null) {
            connectEnd(getEndConnector());
        }
    }
}
TOP

Related Classes of research.connection.LineConnection

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.