/*
* @(#)SelectionTool.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.tool;
import research.Handle;
import research.Figure;
import research.FigureEnumeration;
import research.util.GlobalFunction;
import java.awt.event.MouseEvent;
import java.awt.*;
import java.beans.PropertyChangeEvent;
/**
* Tool to select and manipulate figures.
* A selection tool is in one of three states, e.g., background
* selection, figure selection, handle manipulation. The different
* states are handled by different child tools.
* <hr>
* <b>Design Patterns</b><P>
* <img src="images/red-ball-small.gif" width=6 height=6 alt=" o ">
* <b><a href=../pattlets/sld032.htm>State</a></b><br>
* SelectionTool is the StateContext and child is the State.
* The SelectionTool delegates state specific
* behavior to its current child tool.
* <hr>
*
* @version <$CURRENT_VERSION$>
*/
public class SelectionTool extends Tool {
protected SelectAreaTracker selectAreaTracker = new SelectAreaTracker();
protected DragTracker dragTracker = new DragTracker();
protected HandleTracker handleTracker = new HandleTracker();
protected Tool currentTool = null;
public SelectionTool() {
initSubTools();
}
protected void initSubTools() {
selectAreaTracker = new SelectAreaTracker();
dragTracker = new DragTracker();
handleTracker = new HandleTracker();
}
protected void contextChange(PropertyChangeEvent e) {
super.contextChange(e);
String propertyName = e.getPropertyName();
if (Tool.DRAWING_VIEW.equals(propertyName)) {
selectAreaTracker.getContext().putValue(Tool.DRAWING_VIEW, drawingView);
dragTracker.getContext().putValue(Tool.DRAWING_VIEW, drawingView);
handleTracker.getContext().putValue(Tool.DRAWING_VIEW, drawingView);
}
}
public void mouseEntered(MouseEvent e, int x, int y) {
drawingView.setCursor(Cursor.getDefaultCursor());
}
public void mouseExited(MouseEvent e, int x, int y) {
drawingView.setCursor(Cursor.getDefaultCursor());
}
/**
* Handles mouse down events and starts the corresponding tracker.
*/
public void mouseDown(MouseEvent e, int x, int y) {
// on MS-Windows NT: AWT generates additional mouse down events
// when the left button is down && right button is clicked.
// To avoid dead locks we ignore such events
if (currentTool != null) {
return;
}
//drawingView.freezeView();
Handle handle = drawingView.findHandle(x, y);
if (handle != null) {
handleTracker.setHandle(handle);
currentTool = handleTracker;
} else {
double scale = drawingView.getScale();
int realX = (int) (e.getX() / scale + 0.5);
int realY = (int) (e.getY() / scale + 0.5);
FigureEnumeration fe = drawingView.getDrawing().getFiguresReverse();
Figure figure = null;
while (fe.hasMoreElements()) {
figure = fe.nextFigure();
if (!figure.containsPoint(realX, realY)) {
figure = null;
continue;
}
Boolean selectivity = (Boolean) figure.getAttribute("selectivity");
if (selectivity.booleanValue() == false) {
figure = null;
continue;
}
break;
}
if (figure != null) {
dragTracker.setFigure(figure);
currentTool = dragTracker;
} else {
if (!e.isShiftDown()) {
drawingView.clearSelection();
}
currentTool = selectAreaTracker;
}
}
currentTool.mouseDown(e, x, y);
}
/**
* Handles mouse moves (if the mouse button is up).
* Switches the cursors depending on whats under them.
*/
public void mouseMove(MouseEvent evt, int x, int y) {
GlobalFunction.setCursor(evt.getX(), evt.getY(), drawingView);
}
/**
* Handles mouse drag events. The events are forwarded to the
* current tracker.
*/
public void mouseDrag(MouseEvent e, int x, int y) {
if (currentTool != null) { // JDK1.1 doesn't guarantee mouseDown, mouseDrag, mouseUp
currentTool.mouseDrag(e, x, y);
}
}
/**
* Handles mouse up events. The events are forwarded to the
* current tracker.
*/
public void mouseUp(MouseEvent e, int x, int y) {
//drawingView.unfreezeView();
if (currentTool != null) { // JDK1.1 doesn't guarantee mouseDown, mouseDrag, mouseUp
currentTool.mouseUp(e, x, y);
currentTool = null;
}
}
}