package jsetool.gui; import java.awt.Cursor; import java.awt.event.MouseEvent; /** * * @author Mike Lawson * * @since Nov 2, 2004 * * This is the state that the drawing pane will be in when the user is * connecting two states together with a directed edge. * */ class DrawingAnEdgeState extends FSMDrawingState { /** This is a temporary state that allows us to draw a temporary edge while dragging. */ protected LabeledState tempStateForDragging = new LabeledState(""); /** This is a temporary edge that allows us to draw a line while the user is draggin. */ protected ConnectivityEdge tempEdgeForDragging; protected Cursor drawingEdgeCursor = new Cursor(Cursor.CROSSHAIR_CURSOR); protected LabeledState sourceState; /** * Constructs a new state for drawing edges for the given pane. * @param pane */ public DrawingAnEdgeState(FSMDrawingPane pane) { super(pane); } /** * Entering this state results in the cursor being set * to the one associated with adding an edge, and dragging * is disabled. */ public void enter() { getPane().setCursor(drawingEdgeCursor); getPane().setIsDragging(false); } /** * Does nothing for this state. * @param e * @throws Exception */ public void mouseClicked(MouseEvent e) throws Exception { // nada for this state } /** * Draws a temporary line while the edge is being drawn * on the display. * @param e * @throws Exception */ public void mouseDragged(MouseEvent e) throws Exception { // Move the temporary state around to follow the mouse. // The temporary edge will follow magically. tempStateForDragging.setFrame(e.getX(), e.getY(), 1, 1); } /** * Starts the drawing process. If this object is not * on top of a state, it will blow up. The source object will * be selected if there is an object under the mouse. * @param e * @throws Exception * */ public void mousePressed(MouseEvent e) throws Exception { Object sourceObject = getPane().getObjectMouseIsOn(e); if (sourceObject == null) { throw new Exception("You must start an edge from a state"); } sourceState = (LabeledState)sourceObject; // Set the temporary state (which is invisible to the user) // to the X, Y coordinates of the source state. tempStateForDragging.setFrame(sourceState.getX(), sourceState.getY(), 1, 1); // Create a temporary edge that follow the temporary state around while // the user is dragging. tempEdgeForDragging = new ConnectivityEdge(sourceState, tempStateForDragging); getPane().addDrawingObject(tempEdgeForDragging); } /** * Stops the drawing process. If this object is not * on top of a state, it will blow up. The target object will * be selected, the new edge will be added to the pane. * @param e * @throws Exception */ public void mouseReleased(MouseEvent e) throws Exception { tempEdgeForDragging.objectIsBeingDeleted(); getPane().removeDrawingObject(tempEdgeForDragging); tempEdgeForDragging = null; Object targetObject = getPane().getObjectMouseIsOn(e); if (targetObject == null) { throw new Exception("You must end an edge on a state"); } if (targetObject instanceof LabeledState) { String edgeLabel = ""; ConnectivityEdge edge = new ConnectivityEdge(sourceState, (LabeledState)targetObject); edge.setEdgeLabel(edgeLabel); getPane().addDrawingObject(edge); getPane().drawingNormalOpsSelected(); } else { throw new Exception("You must end an edge on a state"); } } }