Back to index

moin  1.9.0~rc2
SelectAreaTracker.java
Go to the documentation of this file.
00001 /*
00002  * @(#)SelectAreaTracker.java 5.1
00003  *
00004  */
00005 
00006 package CH.ifa.draw.standard;
00007 
00008 import java.awt.*;
00009 import java.awt.event.MouseEvent;
00010 import java.util.*;
00011 import CH.ifa.draw.framework.*;
00012 
00016 public class SelectAreaTracker extends AbstractTool {
00017 
00018     private Rectangle fSelectGroup;
00019 
00020     public SelectAreaTracker(DrawingView view) {
00021         super(view);
00022     }
00023 
00024     public void mouseDown(MouseEvent e, int x, int y) {
00025         // use event coordinates to supress any kind of
00026         // transformations like constraining points to a grid
00027         super.mouseDown(e, e.getX(), e.getY());
00028         rubberBand(fAnchorX, fAnchorY, fAnchorX, fAnchorY);
00029     }
00030 
00031     public void mouseDrag(MouseEvent e, int x, int y) {
00032         super.mouseDrag(e, x, y);
00033         eraseRubberBand();
00034         rubberBand(fAnchorX, fAnchorY, x, y);
00035     }
00036 
00037     public void mouseUp(MouseEvent e, int x, int y) {
00038         super.mouseUp(e, x, y);
00039         eraseRubberBand();
00040         selectGroup(e.isShiftDown());
00041     }
00042 
00043     private void rubberBand(int x1, int y1, int x2, int y2)
00044     {
00045         fSelectGroup = new Rectangle(new Point(x1, y1));
00046         fSelectGroup.add(new Point(x2, y2));
00047         drawXORRect(fSelectGroup);
00048     }
00049 
00050     private void eraseRubberBand()
00051     {
00052         drawXORRect(fSelectGroup);
00053     }
00054 
00055     private void drawXORRect(Rectangle r)
00056     {
00057         Graphics g = view().getGraphics();
00058         g.setXORMode(view().getBackground());
00059         g.setColor(Color.black);
00060         g.drawRect(r.x, r.y, r.width, r.height);
00061     }
00062 
00063     private void selectGroup(boolean toggle)
00064     {
00065         FigureEnumeration k = drawing().figuresReverse();
00066         while (k.hasMoreElements()) {
00067             Figure figure = k.nextFigure();
00068             Rectangle r2 = figure.displayBox();
00069             if (fSelectGroup.contains(r2.x, r2.y) && fSelectGroup.contains(r2.x+r2.width, r2.y+r2.height)) {
00070                 if (toggle)
00071                     view().toggleSelection(figure);
00072                 else
00073                     view().addToSelection(figure);
00074             }
00075         }
00076     }
00077 }