2c2 < * @(#)ConnectionTool.java 5.2 --- > * @(#)ConnectionTool.java 3a4,9 > * 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 7a14,17 > import CH.ifa.draw.framework.*; > import CH.ifa.draw.util.Geom; > import CH.ifa.draw.util.UndoableAdapter; > import CH.ifa.draw.util.Undoable; 12,14d21 < import CH.ifa.draw.framework.*; < import CH.ifa.draw.util.Geom; < 29a37,38 > * > * @version <$CURRENT_VERSION$> 31d39 < 37,39c45,47 < private Connector fStartConnector; < private Connector fEndConnector; < private Connector fConnectorTarget = null; --- > private Connector myStartConnector; > private Connector myEndConnector; > private Connector myTargetConnector; 41c49 < private Figure fTarget = null; --- > private Figure myTarget; 46c54 < private ConnectionFigure fConnection; --- > private ConnectionFigure myConnection; 56c64,70 < private ConnectionFigure fEditedConnection = null; --- > private ConnectionFigure fEditedConnection; > > /** > * the figure that was actually added > * Note, this can be a different figure from the one which has been created. > */ > private Figure myAddedFigure; 86,89c100,103 < fTarget = findConnectionStart(ex, ey, drawing()); < if (fTarget != null) { < fStartConnector = findConnector(ex, ey, fTarget); < if (fStartConnector != null) { --- > setTarget(findConnectionStart(ex, ey, drawing())); > if (getTarget() != null) { > setStartConnector(findConnector(ex, ey, getTarget())); > if (getStartConnector() != null) { 91,94c105,108 < fConnection = createConnection(); < fConnection.startPoint(p.x, p.y); < fConnection.endPoint(p.x, p.y); < view().add(fConnection); --- > setConnection(createConnection()); > getConnection().startPoint(p.x, p.y); > getConnection().endPoint(p.x, p.y); > setAddedFigure(view().add(getConnection())); 103c117,118 < } else { --- > } > else { 115c130 < if (fConnection != null) { --- > if (getConnection() != null) { 117,119c132,135 < if (fConnectorTarget != null) < p = Geom.center(fConnectorTarget.displayBox()); < fConnection.endPoint(p.x, p.y); --- > if (getTargetConnector() != null) { > p = Geom.center(getTargetConnector().displayBox()); > } > getConnection().endPoint(p.x, p.y); 133c149 < if (fStartConnector != null) --- > if (getStartConnector() != null) { 134a151 > } 137,141c154,166 < fEndConnector = findConnector(e.getX(), e.getY(), c); < if (fEndConnector != null) { < fConnection.connectStart(fStartConnector); < fConnection.connectEnd(fEndConnector); < fConnection.updateConnection(); --- > setEndConnector(findConnector(e.getX(), e.getY(), c)); > if (getEndConnector() != null) { > getConnection().connectStart(getStartConnector()); > getConnection().connectEnd(getEndConnector()); > getConnection().updateConnection(); > > setUndoActivity(createUndoActivity()); > getUndoActivity().setAffectedFigures( > new SingleFigureEnumerator(getAddedFigure())); > } > } > else if (getConnection() != null) { > view().remove(getConnection()); 143,144d167 < } else if (fConnection != null) < view().remove(fConnection); 146,147c169,171 < fConnection = null; < fStartConnector = fEndConnector = null; --- > setConnection(null); > setStartConnector(null); > setEndConnector(null); 153,154c177,179 < if (fTarget != null) < fTarget.connectorVisibility(false); --- > if (getTarget() != null) { > getTarget().connectorVisibility(false); > } 177c202 < Figure start = fStartConnector.owner(); --- > Figure start = getStartConnector().owner(); 180c205 < && fConnection != null --- > && getConnection() != null 183c208 < && fConnection.canConnect(start, target)) --- > && getConnection().canConnect(start, target)) { 184a210 > } 196c222 < if (figure != null && (figure instanceof ConnectionFigure)) --- > if (figure != null && (figure instanceof ConnectionFigure)) { 198a225 > } 201a229,232 > private void setConnection(ConnectionFigure newConnection) { > myConnection = newConnection; > } > 205,206c236,237 < protected ConnectionFigure createdFigure() { < return fConnection; --- > protected ConnectionFigure getConnection() { > return myConnection; 212c243 < if (fStartConnector == null) --- > if (getStartConnector() == null) { 214c245,246 < else --- > } > else { 215a248 > } 218,223c251,258 < if (c != fTarget) { < if (fTarget != null) < fTarget.connectorVisibility(false); < fTarget = c; < if (fTarget != null) < fTarget.connectorVisibility(true); --- > if (c != getTarget()) { > if (getTarget() != null) { > getTarget().connectorVisibility(false); > } > setTarget(c); > if (getTarget() != null) { > getTarget().connectorVisibility(true); > } 227c262 < if (c != null) --- > if (c != null) { 229,230c264,267 < if (cc != fConnectorTarget) < fConnectorTarget = cc; --- > } > if (cc != getTargetConnector()) { > setTargetConnector(cc); > } 244c281 < if ((target != null) && target.canConnect()) --- > if ((target != null) && target.canConnect()) { 245a283 > } 253,254c291,292 < if (!figure.includes(fConnection) && figure.canConnect()) { < if (figure.containsPoint(x, y)) --- > if (!figure.includes(getConnection()) && figure.canConnect() > && figure.containsPoint(x, y)) { 260a299,302 > private void setStartConnector(Connector newStartConnector) { > myStartConnector = newStartConnector; > } > 262c304,308 < return fStartConnector; --- > return myStartConnector; > } > > private void setEndConnector(Connector newEndConnector) { > myEndConnector = newEndConnector; 266c312 < return fEndConnector; --- > return myEndConnector; 269,270c315,316 < protected Connector getTarget() { < return fConnectorTarget; --- > private void setTargetConnector(Connector newTargetConnector) { > myTargetConnector = newTargetConnector; 272a319,415 > protected Connector getTargetConnector() { > return myTargetConnector; > } > > private void setTarget(Figure newTarget) { > myTarget = newTarget; > } > > protected Figure getTarget() { > return myTarget; > } > > /** > * Gets the figure that was actually added > * Note, this can be a different figure from the one which has been created. > */ > protected Figure getAddedFigure() { > return myAddedFigure; > } > > private void setAddedFigure(Figure newAddedFigure) { > myAddedFigure = newAddedFigure; > } > > /** > * Factory method for undo activity > */ > protected Undoable createUndoActivity() { > return new ConnectionTool.UndoActivity(view(), getConnection()); > } > > public static class UndoActivity extends UndoableAdapter { > > private ConnectionFigure myConnection; > private Connector myStartConnector; > private Connector myEndConnector; > > public UndoActivity(DrawingView newDrawingView, ConnectionFigure newConnection) { > super(newDrawingView); > setConnection(newConnection); > myStartConnector = getConnection().getStartConnector(); > myEndConnector = getConnection().getEndConnector(); > setUndoable(true); > setRedoable(true); > } > > /* > * Undo the activity > * @return true if the activity could be undone, false otherwise > */ > public boolean undo() { > if (!super.undo()) { > return false; > } > > getConnection().disconnectStart(); > getConnection().disconnectEnd(); > > FigureEnumeration fe = getAffectedFigures(); > while (fe.hasMoreElements()) { > getDrawingView().drawing().orphan(fe.nextFigure()); > } > > getDrawingView().clearSelection(); > > return true; > } > > /* > * Redo the activity > * @return true if the activity could be redone, false otherwise > */ > public boolean redo() { > if (!super.redo()) { > return false; > } > > getConnection().connectStart(myStartConnector); > getConnection().connectEnd(myEndConnector); > getConnection().updateConnection(); > > getDrawingView().insertFigures(getAffectedFigures(), 0, 0, false); > > return true; > } > > private void setConnection(ConnectionFigure newConnection) { > myConnection = newConnection; > } > > /** > * Gets the currently created figure > */ > protected ConnectionFigure getConnection() { > return myConnection; > } > }