/*
* @(#)SelectAreaTracker.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.FigureEnumeration;
import research.Figure;
import research.TextHolder;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.Vector;
import java.util.Iterator;
import research.figure.RectangleFigure;
/**
* SelectAreaTracker implements a rubberband selection of an area.
*
* @version <$CURRENT_VERSION$>
*/
public class SelectAreaTracker extends Tool {
private Rectangle selectRect = null;
private Point anchor = null;
private RectangleFigure area = null;
@Override
public void mouseDown(MouseEvent e, int x, int y) {
super.mouseDown(e, x, y);
anchor = e.getPoint();
if(area == null){
area = new RectangleFigure();
}
area.setDisplayBox(anchor, anchor);
drawingView.add(area);
drawingView.repairDamage();
if(selectRect == null){
selectRect = new Rectangle();
}
selectRect.setLocation(anchor);
}
@Override
public void mouseDrag(MouseEvent e, int x, int y) {
super.mouseDrag(e, x, y);
area.setDisplayBox(anchor, new Point(x,y));
drawingView.repairDamage();
selectRect.setSize(x - anchor.x, y - anchor.y);
}
@Override
public void mouseUp(MouseEvent e, int x, int y) {
super.mouseUp(e, x, y);
drawingView.remove(area);
if(selectRect.width < 0){
selectRect.x += selectRect.width;
selectRect.width = -selectRect.width;
}
if(selectRect.height < 0){
selectRect.y += selectRect.height;
selectRect.height = -selectRect.height;
}
selectGroup(e.isShiftDown());
selectRect.width = 0;
selectRect.height = 0;
}
private void selectGroup(boolean toggle) {
FigureEnumeration k = drawingView.getDrawing().getFiguresReverse();
while (k.hasMoreElements()) {
Figure figure = k.nextFigure();
Boolean selectivity = (Boolean) figure.getAttribute("selectivity");
if (selectivity.booleanValue() == false)
continue;
Rectangle r2 = figure.getDisplayBox();
double scale = drawingView.getScale();
Rectangle realR2 = new Rectangle((int) (r2.x * scale), (int) (r2.y * scale), (int) (r2.width * scale) + 2, (int) (r2.height * scale) + 2);
if (selectRect.contains(realR2.x, realR2.y) && selectRect.contains(realR2.x + realR2.width, realR2.y + realR2.height)) {
if (toggle) {
drawingView.toggleSelection(figure);
} else {
drawingView.addToSelection(figure);
}
}
}
Vector fs = drawingView.getSelection();
Iterator iterator = fs.iterator();
while (iterator.hasNext()) {
Figure f = (Figure) iterator.next();
if (f instanceof TextHolder) {
TextHolder th = (TextHolder) f;
if(th.isConnected()){
Figure cf = th.getConnectedFigure();
if(fs.contains(cf)){
drawingView.toggleSelection(f);
}
}
}
}
}
}