16 package net.squiz.matrix.ui;
20 import java.awt.event.*;
21 import javax.swing.tree.*;
22 import javax.swing.table.*;
24 import javax.swing.plaf.*;
25 import net.squiz.matrix.inspector.*;
30 public class SelectionTool extends MouseAdapter implements MouseMotionListener {
32 private Component comp;
36 private Point initDragPoint = null;
42 private Rectangle selectionBounds =
new Rectangle(0, 0, 0, 0);
44 private java.util.List currSelection =
new ArrayList();
47 private boolean dragging =
false;
49 private java.util.List removeSelections =
new ArrayList();
51 private java.util.List addSelections =
new ArrayList();
55 selHandler =
new TreeSelection(comp);
60 selHandler =
new TableSelection(comp);
68 public Point getInitialDragPoint() {
93 currSelection.clear();
94 removeSelections.clear();
95 addSelections.clear();
107 if (initDragPoint == null) {
108 if (!selHandler.canSelect(evt.getPoint()))
111 initDragPoint = evt.getPoint();
114 selHandler.clearSelection();
116 selHandler.updateSelection(evt.getPoint());
128 if (initDragPoint == null)
131 int x = 0, y = 0, width = 0, height = 0;
151 if (initDragPoint.x < mouseX) {
153 width = mouseX - initDragPoint.x;
156 width = initDragPoint.x -mouseX;
159 if (initDragPoint.y > mouseY) {
161 height = initDragPoint.y - mouseY;
164 height = mouseY - initDragPoint.y;
167 selectionBounds.setBounds(x, y, width, height);
184 g2d.setColor(UIManager.getColor(
"SelectionTool.background"));
185 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
186 g2d.fillRect(x, y, width, height);
187 g2d.setColor(UIManager.getColor(
"SelectionTool.bordercolor"));
188 g2d.drawRect(x, y, width - 1, height - 1);
195 public TreeSelection(JTree tree) {
204 private TreePath[] pathsToArray(java.util.List paths) {
205 return (TreePath[]) paths.toArray(
new TreePath[paths.size()]);
208 public boolean canSelect(Point point) {
213 if (tree.getPathForLocation(point.x, point.y) != null)
218 public void clearSelection() {
219 tree.clearSelection();
222 public void updateSelection(Point point) {
223 TreePath mousePath = tree.getClosestPathForLocation(
224 initDragPoint.x, initDragPoint.y);
225 TreePath initPath = tree.getClosestPathForLocation(point.x, point.y);
231 TreePath[] rows = getPathBetweenRows(
232 tree.getRowForPath(mousePath), tree.getRowForPath(initPath));
234 currSelection.clear();
236 for (
int i = 0; i < rows.length; i++) {
240 if (selectionBounds.intersects(tree.getPathBounds(rows[i]))) {
241 currSelection.add(rows[i]);
244 if (currSelection != null && !currSelection.isEmpty())
245 intersectSelection(currSelection);
250 private TreePath[] getPathBetweenRows(
int index0,
int index1) {
251 int newMinIndex, newMaxIndex;
252 TreeUI ui = tree.getUI();
254 newMinIndex = Math.min(index0, index1);
255 newMaxIndex = Math.max(index0, index1);
258 TreePath[] selection =
new TreePath[newMaxIndex - newMinIndex + 1];
260 for (
int counter = newMinIndex; counter <= newMaxIndex; counter++) {
261 selection[counter - newMinIndex] = ui.getPathForRow(tree, counter);
278 protected void intersectSelection(java.util.List selPaths) {
279 TreePath[] currSelPaths = tree.getSelectionPaths();
283 if (currSelPaths == null) {
284 tree.addSelectionPaths(pathsToArray(selPaths));
288 addSelections.clear();
289 removeSelections.clear();
290 boolean found =
false;
291 TreePath[] selPathsArr = pathsToArray(selPaths);
293 for (
int i = 0; i < selPathsArr.length; i++) {
295 for (
int j = 0; j < currSelPaths.length; j++) {
297 if (selPathsArr[i] == currSelPaths[j]) {
303 addSelections.add(selPathsArr[i]);
305 for (
int i = 0; i < currSelPaths.length; i++) {
307 for (
int j = 0; j < selPathsArr.length; j++) {
310 if (currSelPaths[i] == selPathsArr[j]) {
316 removeSelections.add(currSelPaths[i]);
319 if (!addSelections.isEmpty())
320 tree.addSelectionPaths(pathsToArray(addSelections));
322 if (!removeSelections.isEmpty())
323 tree.removeSelectionPaths(pathsToArray(removeSelections));
327 class TableSelection
implements SelectionHandler {
329 private JTable table;
331 public TableSelection(JTable table) {
335 public boolean canSelect(Point point) {
336 if ( (table.rowAtPoint(point) == -1 ) || (table.columnAtPoint(point) == -1) )
338 if (table.getValueAt( table.rowAtPoint(point), table.columnAtPoint(point) ) == null)
343 InspectorGadget tempTable = (InspectorGadget) table;
344 if (!tempTable.mouseInsideCellComponent(point))
349 public void updateSelection(Point point) {
350 int currentRow = table.rowAtPoint(point);
351 int currentColumn = table.columnAtPoint(point);
352 int startRow = table.rowAtPoint(initDragPoint);
353 int startColumn = table.columnAtPoint(initDragPoint);
356 startRow = table.getRowCount() - 1;
358 table.changeSelection(currentRow,currentColumn,
false,
false);
359 table.changeSelection(startRow,startColumn,
false,
true);
362 public void clearSelection() {
363 table.clearSelection();