1 7 8 package javax.swing.plaf.basic; 9 10 import java.awt.*; 11 import java.awt.datatransfer.*; 12 import java.awt.dnd.*; 13 import java.awt.event.*; 14 import java.util.Enumeration ; 15 import java.util.EventObject ; 16 import java.util.Hashtable ; 17 import java.util.TooManyListenersException ; 18 import javax.swing.*; 19 import javax.swing.event.*; 20 import javax.swing.plaf.*; 21 import javax.swing.text.*; 22 import javax.swing.table.*; 23 import javax.swing.plaf.basic.DragRecognitionSupport.BeforeDrag ; 24 import com.sun.java.swing.SwingUtilities2; 25 import static com.sun.java.swing.SwingUtilities2.DRAG_FIX; 26 27 28 import java.beans.PropertyChangeEvent ; 29 import java.beans.PropertyChangeListener ; 30 31 import sun.swing.DefaultLookup; 32 import sun.swing.UIAction; 33 34 41 public class BasicTableUI extends TableUI 42 { 43 44 48 protected JTable table; 50 protected CellRendererPane rendererPane; 51 52 protected KeyListener keyListener; 54 protected FocusListener focusListener; 55 protected MouseInputListener mouseInputListener; 56 57 private Handler handler; 58 59 62 private boolean isFileList = false; 63 64 68 private static class Actions extends UIAction { 69 private static final String CANCEL_EDITING = "cancel"; 70 private static final String SELECT_ALL = "selectAll"; 71 private static final String CLEAR_SELECTION = "clearSelection"; 72 private static final String START_EDITING = "startEditing"; 73 74 private static final String NEXT_ROW = "selectNextRow"; 75 private static final String NEXT_ROW_CELL = "selectNextRowCell"; 76 private static final String NEXT_ROW_EXTEND_SELECTION = 77 "selectNextRowExtendSelection"; 78 private static final String NEXT_ROW_CHANGE_LEAD = 79 "selectNextRowChangeLead"; 80 private static final String PREVIOUS_ROW = "selectPreviousRow"; 81 private static final String PREVIOUS_ROW_CELL = "selectPreviousRowCell"; 82 private static final String PREVIOUS_ROW_EXTEND_SELECTION = 83 "selectPreviousRowExtendSelection"; 84 private static final String PREVIOUS_ROW_CHANGE_LEAD = 85 "selectPreviousRowChangeLead"; 86 87 private static final String NEXT_COLUMN = "selectNextColumn"; 88 private static final String NEXT_COLUMN_CELL = "selectNextColumnCell"; 89 private static final String NEXT_COLUMN_EXTEND_SELECTION = 90 "selectNextColumnExtendSelection"; 91 private static final String NEXT_COLUMN_CHANGE_LEAD = 92 "selectNextColumnChangeLead"; 93 private static final String PREVIOUS_COLUMN = "selectPreviousColumn"; 94 private static final String PREVIOUS_COLUMN_CELL = 95 "selectPreviousColumnCell"; 96 private static final String PREVIOUS_COLUMN_EXTEND_SELECTION = 97 "selectPreviousColumnExtendSelection"; 98 private static final String PREVIOUS_COLUMN_CHANGE_LEAD = 99 "selectPreviousColumnChangeLead"; 100 101 private static final String SCROLL_LEFT_CHANGE_SELECTION = 102 "scrollLeftChangeSelection"; 103 private static final String SCROLL_LEFT_EXTEND_SELECTION = 104 "scrollLeftExtendSelection"; 105 private static final String SCROLL_RIGHT_CHANGE_SELECTION = 106 "scrollRightChangeSelection"; 107 private static final String SCROLL_RIGHT_EXTEND_SELECTION = 108 "scrollRightExtendSelection"; 109 110 private static final String SCROLL_UP_CHANGE_SELECTION = 111 "scrollUpChangeSelection"; 112 private static final String SCROLL_UP_EXTEND_SELECTION = 113 "scrollUpExtendSelection"; 114 private static final String SCROLL_DOWN_CHANGE_SELECTION = 115 "scrollDownChangeSelection"; 116 private static final String SCROLL_DOWN_EXTEND_SELECTION = 117 "scrollDownExtendSelection"; 118 119 private static final String FIRST_COLUMN = 120 "selectFirstColumn"; 121 private static final String FIRST_COLUMN_EXTEND_SELECTION = 122 "selectFirstColumnExtendSelection"; 123 private static final String LAST_COLUMN = 124 "selectLastColumn"; 125 private static final String LAST_COLUMN_EXTEND_SELECTION = 126 "selectLastColumnExtendSelection"; 127 128 private static final String FIRST_ROW = 129 "selectFirstRow"; 130 private static final String FIRST_ROW_EXTEND_SELECTION = 131 "selectFirstRowExtendSelection"; 132 private static final String LAST_ROW = 133 "selectLastRow"; 134 private static final String LAST_ROW_EXTEND_SELECTION = 135 "selectLastRowExtendSelection"; 136 137 private static final String ADD_TO_SELECTION = "addToSelection"; 139 140 private static final String TOGGLE_AND_ANCHOR = "toggleAndAnchor"; 142 143 private static final String EXTEND_TO = "extendTo"; 145 146 private static final String MOVE_SELECTION_TO = "moveSelectionTo"; 148 149 protected int dx; 150 protected int dy; 151 protected boolean extend; 152 protected boolean inSelection; 153 protected boolean forwards; 154 protected boolean vertically; 155 protected boolean toLimit; 156 157 protected int leadRow; 158 protected int leadColumn; 159 160 Actions(String name) { 161 super(name); 162 } 163 164 Actions(String name, int dx, int dy, boolean extend, 165 boolean inSelection) { 166 super(name); 167 168 if (inSelection) { 175 this.inSelection = true; 176 177 dx = sign(dx); 179 dy = sign(dy); 180 181 assert (dx == 0 || dy == 0) && !(dx == 0 && dy == 0); 183 } 184 185 this.dx = dx; 186 this.dy = dy; 187 this.extend = extend; 188 } 189 190 Actions(String name, boolean extend, boolean forwards, 191 boolean vertically, boolean toLimit) { 192 this(name, 0, 0, extend, false); 193 this.forwards = forwards; 194 this.vertically = vertically; 195 this.toLimit = toLimit; 196 } 197 198 private static int clipToRange(int i, int a, int b) { 199 return Math.min(Math.max(i, a), b-1); 200 } 201 202 private void moveWithinTableRange(JTable table, int dx, int dy) { 203 leadRow = clipToRange(leadRow+dy, 0, table.getRowCount()); 204 leadColumn = clipToRange(leadColumn+dx, 0, table.getColumnCount()); 205 } 206 207 private static int sign(int num) { 208 return (num < 0) ? -1 : ((num == 0) ? 0 : 1); 209 } 210 211 223 private boolean moveWithinSelectedRange(JTable table, int dx, int dy, 224 ListSelectionModel rsm, ListSelectionModel csm) { 225 226 229 int totalCount; 232 int minX, maxX, minY, maxY; 233 234 boolean rs = table.getRowSelectionAllowed(); 235 boolean cs = table.getColumnSelectionAllowed(); 236 237 if (rs && cs) { 239 totalCount = table.getSelectedRowCount() * table.getSelectedColumnCount(); 240 minX = csm.getMinSelectionIndex(); 241 maxX = csm.getMaxSelectionIndex(); 242 minY = rsm.getMinSelectionIndex(); 243 maxY = rsm.getMaxSelectionIndex(); 244 } else if (rs) { 246 totalCount = table.getSelectedRowCount(); 247 minX = 0; 248 maxX = table.getColumnCount() - 1; 249 minY = rsm.getMinSelectionIndex(); 250 maxY = rsm.getMaxSelectionIndex(); 251 } else if (cs) { 253 totalCount = table.getSelectedColumnCount(); 254 minX = csm.getMinSelectionIndex(); 255 maxX = csm.getMaxSelectionIndex(); 256 minY = 0; 257 maxY = table.getRowCount() - 1; 258 } else { 260 totalCount = 0; 261 minX = maxX = minY = maxY = 0; 265 } 266 267 boolean stayInSelection; 271 272 if (totalCount == 0 || 274 (totalCount == 1 && table.isCellSelected(leadRow, leadColumn))) { 276 277 stayInSelection = false; 278 279 maxX = table.getColumnCount() - 1; 280 maxY = table.getRowCount() - 1; 281 282 minX = Math.min(0, maxX); 284 minY = Math.min(0, maxY); 285 } else { 286 stayInSelection = true; 287 } 288 289 if (dy == 1 && leadColumn == -1) { 292 leadColumn = minX; 293 leadRow = -1; 294 } else if (dx == 1 && leadRow == -1) { 295 leadRow = minY; 296 leadColumn = -1; 297 } else if (dy == -1 && leadColumn == -1) { 298 leadColumn = maxX; 299 leadRow = maxY + 1; 300 } else if (dx == -1 && leadRow == -1) { 301 leadRow = maxY; 302 leadColumn = maxX + 1; 303 } 304 305 leadRow = Math.min(Math.max(leadRow, minY - 1), maxY + 1); 309 leadColumn = Math.min(Math.max(leadColumn, minX - 1), maxX + 1); 310 311 do { 313 calcNextPos(dx, minX, maxX, dy, minY, maxY); 314 } while (stayInSelection && !table.isCellSelected(leadRow, leadColumn)); 315 316 return stayInSelection; 317 } 318 319 323 private void calcNextPos(int dx, int minX, int maxX, 324 int dy, int minY, int maxY) { 325 326 if (dx != 0) { 327 leadColumn += dx; 328 if (leadColumn > maxX) { 329 leadColumn = minX; 330 leadRow++; 331 if (leadRow > maxY) { 332 leadRow = minY; 333 } 334 } else if (leadColumn < minX) { 335 leadColumn = maxX; 336 leadRow--; 337 if (leadRow < minY) { 338 leadRow = maxY; 339 } 340 } 341 } else { 342 leadRow += dy; 343 if (leadRow > maxY) { 344 leadRow = minY; 345 leadColumn++; 346 if (leadColumn > maxX) { 347 leadColumn = minX; 348 } 349 } else if (leadRow < minY) { 350 leadRow = maxY; 351 leadColumn--; 352 if (leadColumn < minX) { 353 leadColumn = maxX; 354 } 355 } 356 } 357 } 358 359 public void actionPerformed(ActionEvent e) { 360 String key = getName(); 361 JTable table = (JTable)e.getSource(); 362 363 ListSelectionModel rsm = table.getSelectionModel(); 364 leadRow = getAdjustedLead(table, true, rsm); 365 366 ListSelectionModel csm = table.getColumnModel().getSelectionModel(); 367 leadColumn = getAdjustedLead(table, false, csm); 368 369 if (!table.getComponentOrientation().isLeftToRight()) { 370 if (key == SCROLL_LEFT_CHANGE_SELECTION || 371 key == SCROLL_LEFT_EXTEND_SELECTION) { 372 forwards = true; 373 } else if (key == SCROLL_RIGHT_CHANGE_SELECTION || 374 key == SCROLL_RIGHT_EXTEND_SELECTION) { 375 forwards = false; 376 } 377 } 378 379 if (key == SCROLL_LEFT_CHANGE_SELECTION || key == SCROLL_LEFT_EXTEND_SELECTION || 381 key == SCROLL_RIGHT_CHANGE_SELECTION || 382 key == SCROLL_RIGHT_EXTEND_SELECTION || 383 key == SCROLL_UP_CHANGE_SELECTION || 384 key == SCROLL_UP_EXTEND_SELECTION || 385 key == SCROLL_DOWN_CHANGE_SELECTION || 386 key == SCROLL_DOWN_EXTEND_SELECTION || 387 key == FIRST_COLUMN || 388 key == FIRST_COLUMN_EXTEND_SELECTION || 389 key == FIRST_ROW || 390 key == FIRST_ROW_EXTEND_SELECTION || 391 key == LAST_COLUMN || 392 key == LAST_COLUMN_EXTEND_SELECTION || 393 key == LAST_ROW || 394 key == LAST_ROW_EXTEND_SELECTION) { 395 if (toLimit) { 396 if (vertically) { 397 int rowCount = table.getRowCount(); 398 this.dx = 0; 399 this.dy = forwards ? rowCount : -rowCount; 400 } 401 else { 402 int colCount = table.getColumnCount(); 403 this.dx = forwards ? colCount : -colCount; 404 this.dy = 0; 405 } 406 } 407 else { 408 if (!(table.getParent().getParent() instanceof 409 JScrollPane)) { 410 return; 411 } 412 413 Dimension delta = table.getParent().getSize(); 414 415 if (vertically) { 416 Rectangle r = table.getCellRect(leadRow, 0, true); 417 r.y += forwards ? delta.height : -delta.height; 418 this.dx = 0; 419 int newRow = table.rowAtPoint(r.getLocation()); 420 if (newRow == -1 && forwards) { 421 newRow = table.getRowCount(); 422 } 423 this.dy = newRow - leadRow; 424 } 425 else { 426 Rectangle r = table.getCellRect(0, leadColumn, true); 427 r.x += forwards ? delta.width : -delta.width; 428 int newColumn = table.columnAtPoint(r.getLocation()); 429 if (newColumn == -1 && forwards) { 430 newColumn = table.getColumnCount(); 431 } 432 this.dx = newColumn - leadColumn; 433 this.dy = 0; 434 } 435 } 436 } 437 if (key == NEXT_ROW || key == NEXT_ROW_CELL || 439 key == NEXT_ROW_EXTEND_SELECTION || 440 key == NEXT_ROW_CHANGE_LEAD || 441 key == NEXT_COLUMN || 442 key == NEXT_COLUMN_CELL || 443 key == NEXT_COLUMN_EXTEND_SELECTION || 444 key == NEXT_COLUMN_CHANGE_LEAD || 445 key == PREVIOUS_ROW || 446 key == PREVIOUS_ROW_CELL || 447 key == PREVIOUS_ROW_EXTEND_SELECTION || 448 key == PREVIOUS_ROW_CHANGE_LEAD || 449 key == PREVIOUS_COLUMN || 450 key == PREVIOUS_COLUMN_CELL || 451 key == PREVIOUS_COLUMN_EXTEND_SELECTION || 452 key == PREVIOUS_COLUMN_CHANGE_LEAD || 453 key == SCROLL_LEFT_CHANGE_SELECTION || 455 key == SCROLL_LEFT_EXTEND_SELECTION || 456 key == SCROLL_RIGHT_CHANGE_SELECTION || 457 key == SCROLL_RIGHT_EXTEND_SELECTION || 458 key == SCROLL_UP_CHANGE_SELECTION || 459 key == SCROLL_UP_EXTEND_SELECTION || 460 key == SCROLL_DOWN_CHANGE_SELECTION || 461 key == SCROLL_DOWN_EXTEND_SELECTION || 462 key == FIRST_COLUMN || 463 key == FIRST_COLUMN_EXTEND_SELECTION || 464 key == FIRST_ROW || 465 key == FIRST_ROW_EXTEND_SELECTION || 466 key == LAST_COLUMN || 467 key == LAST_COLUMN_EXTEND_SELECTION || 468 key == LAST_ROW || 469 key == LAST_ROW_EXTEND_SELECTION) { 470 471 if (table.isEditing() && 472 !table.getCellEditor().stopCellEditing()) { 473 return; 474 } 475 476 488 491 boolean changeLead = false; 492 if (key == NEXT_ROW_CHANGE_LEAD || key == PREVIOUS_ROW_CHANGE_LEAD) { 493 changeLead = (rsm.getSelectionMode() 494 == ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 495 } else if (key == NEXT_COLUMN_CHANGE_LEAD || key == PREVIOUS_COLUMN_CHANGE_LEAD) { 496 changeLead = (csm.getSelectionMode() 497 == ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 498 } 499 500 if (changeLead) { 501 moveWithinTableRange(table, dx, dy); 502 if (dy != 0) { 503 ((DefaultListSelectionModel)rsm).moveLeadSelectionIndex(leadRow); 506 if (getAdjustedLead(table, false, csm) == -1 507 && table.getColumnCount() > 0) { 508 509 ((DefaultListSelectionModel)csm).moveLeadSelectionIndex(0); 510 } 511 } else { 512 ((DefaultListSelectionModel)csm).moveLeadSelectionIndex(leadColumn); 515 if (getAdjustedLead(table, true, rsm) == -1 516 && table.getRowCount() > 0) { 517 518 ((DefaultListSelectionModel)rsm).moveLeadSelectionIndex(0); 519 } 520 } 521 522 Rectangle cellRect = table.getCellRect(leadRow, leadColumn, false); 523 if (cellRect != null) { 524 table.scrollRectToVisible(cellRect); 525 } 526 } else if (!inSelection) { 527 moveWithinTableRange(table, dx, dy); 528 table.changeSelection(leadRow, leadColumn, false, extend); 529 } 530 else { 531 if (table.getRowCount() <= 0 || table.getColumnCount() <= 0) { 532 return; 534 } 535 536 if (moveWithinSelectedRange(table, dx, dy, rsm, csm)) { 537 if (rsm.isSelectedIndex(leadRow)) { 540 rsm.addSelectionInterval(leadRow, leadRow); 541 } else { 542 rsm.removeSelectionInterval(leadRow, leadRow); 543 } 544 545 if (csm.isSelectedIndex(leadColumn)) { 546 csm.addSelectionInterval(leadColumn, leadColumn); 547 } else { 548 csm.removeSelectionInterval(leadColumn, leadColumn); 549 } 550 551 Rectangle cellRect = table.getCellRect(leadRow, leadColumn, false); 552 if (cellRect != null) { 553 table.scrollRectToVisible(cellRect); 554 } 555 } 556 else { 557 table.changeSelection(leadRow, leadColumn, 558 false, false); 559 } 560 } 561 562 575 } else if (key == CANCEL_EDITING) { 576 table.removeEditor(); 577 } else if (key == SELECT_ALL) { 578 table.selectAll(); 579 } else if (key == CLEAR_SELECTION) { 580 table.clearSelection(); 581 } else if (key == START_EDITING) { 582 if (!table.hasFocus()) { 583 CellEditor cellEditor = table.getCellEditor(); 584 if (cellEditor != null && !cellEditor.stopCellEditing()) { 585 return; 586 } 587 table.requestFocus(); 588 return; 589 } 590 table.editCellAt(leadRow, leadColumn); 591 Component editorComp = table.getEditorComponent(); 592 if (editorComp != null) { 593 editorComp.requestFocus(); 594 } 595 } else if (key == ADD_TO_SELECTION) { 596 if (!table.isCellSelected(leadRow, leadColumn)) { 597 int oldAnchorRow = rsm.getAnchorSelectionIndex(); 598 int oldAnchorColumn = csm.getAnchorSelectionIndex(); 599 rsm.setValueIsAdjusting(true); 600 csm.setValueIsAdjusting(true); 601 table.changeSelection(leadRow, leadColumn, true, false); 602 rsm.setAnchorSelectionIndex(oldAnchorRow); 603 csm.setAnchorSelectionIndex(oldAnchorColumn); 604 rsm.setValueIsAdjusting(false); 605 csm.setValueIsAdjusting(false); 606 } 607 } else if (key == TOGGLE_AND_ANCHOR) { 608 table.changeSelection(leadRow, leadColumn, true, false); 609 } else if (key == EXTEND_TO) { 610 table.changeSelection(leadRow, leadColumn, false, true); 611 } else if (key == MOVE_SELECTION_TO) { 612 table.changeSelection(leadRow, leadColumn, false, false); 613 } 614 } 615 616 public boolean isEnabled(Object sender) { 617 String key = getName(); 618 619 if (sender instanceof JTable && 620 Boolean.TRUE.equals(((JTable)sender).getClientProperty("Table.isFileList"))) { 621 if (key == NEXT_COLUMN || 622 key == NEXT_COLUMN_CELL || 623 key == NEXT_COLUMN_EXTEND_SELECTION || 624 key == NEXT_COLUMN_CHANGE_LEAD || 625 key == PREVIOUS_COLUMN || 626 key == PREVIOUS_COLUMN_CELL || 627 key == PREVIOUS_COLUMN_EXTEND_SELECTION || 628 key == PREVIOUS_COLUMN_CHANGE_LEAD || 629 key == SCROLL_LEFT_CHANGE_SELECTION || 630 key == SCROLL_LEFT_EXTEND_SELECTION || 631 key == SCROLL_RIGHT_CHANGE_SELECTION || 632 key == SCROLL_RIGHT_EXTEND_SELECTION || 633 key == FIRST_COLUMN || 634 key == FIRST_COLUMN_EXTEND_SELECTION || 635 key == LAST_COLUMN || 636 key == LAST_COLUMN_EXTEND_SELECTION || 637 key == NEXT_ROW_CELL || 638 key == PREVIOUS_ROW_CELL) { 639 640 return false; 641 } 642 } 643 644 if (key == CANCEL_EDITING && sender instanceof JTable) { 645 return ((JTable)sender).isEditing(); 646 } else if (key == NEXT_ROW_CHANGE_LEAD || 647 key == PREVIOUS_ROW_CHANGE_LEAD) { 648 return sender != null && 651 ((JTable)sender).getSelectionModel() 652 instanceof DefaultListSelectionModel; 653 } else if (key == NEXT_COLUMN_CHANGE_LEAD || 654 key == PREVIOUS_COLUMN_CHANGE_LEAD) { 655 return sender != null && 658 ((JTable)sender).getColumnModel().getSelectionModel() 659 instanceof DefaultListSelectionModel; 660 } else if (key == ADD_TO_SELECTION && sender instanceof JTable) { 661 JTable table = (JTable)sender; 669 int leadRow = getAdjustedLead(table, true); 670 int leadCol = getAdjustedLead(table, false); 671 return !(table.isEditing() || table.isCellSelected(leadRow, leadCol)); 672 } 673 674 return true; 675 } 676 } 677 678 679 683 692 public class KeyHandler implements KeyListener { 693 public void keyPressed(KeyEvent e) { 698 getHandler().keyPressed(e); 699 } 700 701 public void keyReleased(KeyEvent e) { 702 getHandler().keyReleased(e); 703 } 704 705 public void keyTyped(KeyEvent e) { 706 getHandler().keyTyped(e); 707 } 708 } 709 710 714 719 public class FocusHandler implements FocusListener { 720 public void focusGained(FocusEvent e) { 725 getHandler().focusGained(e); 726 } 727 728 public void focusLost(FocusEvent e) { 729 getHandler().focusLost(e); 730 } 731 } 732 733 737 742 public class MouseInputHandler implements MouseInputListener { 743 public void mouseClicked(MouseEvent e) { 748 getHandler().mouseClicked(e); 749 } 750 751 public void mousePressed(MouseEvent e) { 752 getHandler().mousePressed(e); 753 } 754 755 public void mouseReleased(MouseEvent e) { 756 getHandler().mouseReleased(e); 757 } 758 759 public void mouseEntered(MouseEvent e) { 760 getHandler().mouseEntered(e); 761 } 762 763 public void mouseExited(MouseEvent e) { 764 getHandler().mouseExited(e); 765 } 766 767 public void mouseMoved(MouseEvent e) { 768 getHandler().mouseMoved(e); 769 } 770 771 public void mouseDragged(MouseEvent e) { 772 getHandler().mouseDragged(e); 773 } 774 } 775 776 private class Handler implements FocusListener, MouseInputListener, 777 PropertyChangeListener { 778 779 private void repaintLeadCell( ) { 781 int lr = getAdjustedLead(table, true); 782 int lc = getAdjustedLead(table, false); 783 784 if (lr < 0 || lc < 0) { 785 return; 786 } 787 788 Rectangle dirtyRect = table.getCellRect(lr, lc, false); 789 table.repaint(dirtyRect); 790 } 791 792 public void focusGained(FocusEvent e) { 793 repaintLeadCell(); 794 } 795 796 public void focusLost(FocusEvent e) { 797 repaintLeadCell(); 798 } 799 800 801 public void keyPressed(KeyEvent e) { } 803 804 public void keyReleased(KeyEvent e) { } 805 806 public void keyTyped(KeyEvent e) { 807 KeyStroke keyStroke = KeyStroke.getKeyStroke(e.getKeyChar(), 808 e.getModifiers()); 809 810 InputMap map = table.getInputMap(JComponent.WHEN_FOCUSED); 816 if (map != null && map.get(keyStroke) != null) { 817 return; 818 } 819 map = table.getInputMap(JComponent. 820 WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); 821 if (map != null && map.get(keyStroke) != null) { 822 return; 823 } 824 825 keyStroke = KeyStroke.getKeyStrokeForEvent(e); 826 827 if (e.getKeyChar() == '\r') { 830 return; 831 } 832 833 int leadRow = getAdjustedLead(table, true); 834 int leadColumn = getAdjustedLead(table, false); 835 if (leadRow != -1 && leadColumn != -1 && !table.isEditing()) { 836 if (!table.editCellAt(leadRow, leadColumn)) { 837 return; 838 } 839 } 840 841 846 849 Component editorComp = table.getEditorComponent(); 850 if (table.isEditing() && editorComp != null) { 851 if (editorComp instanceof JComponent) { 852 JComponent component = (JComponent)editorComp; 853 map = component.getInputMap(JComponent.WHEN_FOCUSED); 854 Object binding = (map != null) ? map.get(keyStroke) : null; 855 if (binding == null) { 856 map = component.getInputMap(JComponent. 857 WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); 858 binding = (map != null) ? map.get(keyStroke) : null; 859 } 860 if (binding != null) { 861 ActionMap am = component.getActionMap(); 862 Action action = (am != null) ? am.get(binding) : null; 863 if (action != null && SwingUtilities. 864 notifyAction(action, keyStroke, e, component, 865 e.getModifiers())) { 866 e.consume(); 867 } 868 } 869 } 870 } 871 } 872 873 874 876 protected Component dispatchComponent; 879 private boolean selectedOnPress; 880 881 public void mouseClicked(MouseEvent e) {} 882 883 protected void setDispatchComponent(MouseEvent e) { 884 Component editorComponent = table.getEditorComponent(); 885 Point p = e.getPoint(); 886 Point p2 = SwingUtilities.convertPoint(table, p, editorComponent); 887 dispatchComponent = 888 SwingUtilities.getDeepestComponentAt(editorComponent, 889 p2.x, p2.y); 890 SwingUtilities2.setSkipClickCount(dispatchComponent, 891 e.getClickCount() - 1); 892 } 893 894 protected boolean repostEvent(MouseEvent e) { 895 if (dispatchComponent == null || !table.isEditing()) { 898 return false; 899 } 900 MouseEvent e2 = SwingUtilities.convertMouseEvent(table, e, 901 dispatchComponent); 902 dispatchComponent.dispatchEvent(e2); 903 return true; 904 } 905 906 protected void setValueIsAdjusting(boolean flag) { 907 table.getSelectionModel().setValueIsAdjusting(flag); 908 table.getColumnModel().getSelectionModel(). 909 setValueIsAdjusting(flag); 910 } 911 912 private boolean shouldIgnore0(MouseEvent e) { 913 return e.isConsumed() || SwingUtilities2.shouldIgnore(e, table); 914 } 915 916 public void mousePressed(MouseEvent e) { 917 if (e.isConsumed()) { 918 selectedOnPress = false; 919 return; 920 } 921 selectedOnPress = true; 922 adjustFocusAndSelection(e); 923 } 924 925 void adjustFocusAndSelection(MouseEvent e) { 926 if (shouldIgnore0(e)) { 927 return; 928 } 929 930 Point p = e.getPoint(); 931 int row = table.rowAtPoint(p); 932 int column = table.columnAtPoint(p); 933 if (pointOutsidePrefSize(table, row, column, p)) { 935 if (e.getID() == MouseEvent.MOUSE_PRESSED && 938 (!e.isShiftDown() || 939 table.getSelectionModel().getSelectionMode() == 940 ListSelectionModel.SINGLE_SELECTION)) { 941 table.clearSelection(); 942 TableCellEditor tce = table.getCellEditor(); 943 if (tce != null) { 944 tce.stopCellEditing(); 945 } 946 } 947 return; 948 } 949 if ((column == -1) || (row == -1)) { 952 return; 953 } 954 955 if (table.editCellAt(row, column, e)) { 956 setDispatchComponent(e); 957 repostEvent(e); 958 } 959 else { 960 SwingUtilities2.adjustFocus(table); 961 } 962 963 CellEditor editor = table.getCellEditor(); 964 if (editor == null || editor.shouldSelectCell(e)) { 965 boolean adjusting = (e.getID() == MouseEvent.MOUSE_PRESSED) ? 966 true : false; 967 setValueIsAdjusting(adjusting); 968 makeSelectionChange(row, column, e); 969 } 970 } 971 972 protected void makeSelectionChange(int row, int column, MouseEvent e) { 973 boolean ctrl = e.isControlDown(); 974 975 if (ctrl && e.isShiftDown()) { 980 ListSelectionModel rm = table.getSelectionModel(); 981 ListSelectionModel cm = table.getColumnModel().getSelectionModel(); 982 int anchorRow = rm.getAnchorSelectionIndex(); 983 int anchorCol = cm.getAnchorSelectionIndex(); 984 985 boolean anchorSelected = true; 986 if (anchorRow == -1 || anchorRow >= table.getRowCount()) { 987 anchorRow = 0; 988 anchorSelected = false; 989 } 990 991 if (anchorCol == -1 || anchorCol >= table.getColumnCount()) { 992 anchorCol = 0; 993 anchorSelected = false; 994 } 995 996 if (anchorSelected && table.isCellSelected(anchorRow, anchorCol)) { 997 rm.addSelectionInterval(anchorRow, row); 998 cm.addSelectionInterval(anchorCol, column); 999 } else { 1000 rm.removeSelectionInterval(anchorRow, row); 1001 cm.removeSelectionInterval(anchorCol, column); 1002 1003 if (isFileList) { 1005 rm.addSelectionInterval(row, row); 1006 rm.setAnchorSelectionIndex(anchorRow); 1007 cm.addSelectionInterval(column, column); 1008 cm.setAnchorSelectionIndex(anchorCol); 1009 } 1010 } 1011 } else { 1012 table.changeSelection(row, column, ctrl, !ctrl && e.isShiftDown()); 1013 } 1014 } 1015 1016 public void mouseReleased(MouseEvent e) { 1017 if (selectedOnPress) { 1018 if (shouldIgnore0(e)) { 1019 return; 1020 } 1021 1022 repostEvent(e); 1023 dispatchComponent = null; 1024 setValueIsAdjusting(false); 1025 } else { 1026 adjustFocusAndSelection(e); 1027 } 1028 } 1029 1030 1031 public void mouseEntered(MouseEvent e) {} 1032 1033 public void mouseExited(MouseEvent e) {} 1034 1035 public void mouseMoved(MouseEvent e) {} 1036 1037 public void mouseDragged(MouseEvent e) { 1038 if (shouldIgnore0(e)) { 1039 return; 1040 } 1041 1042 mouseDraggedImpl(e); 1043 } 1044 1045 protected void mouseDraggedImpl(MouseEvent e) { 1046 repostEvent(e); 1047 1048 if (isFileList || table.isEditing()) { 1052 return; 1053 } 1054 1055 Point p = e.getPoint(); 1056 int row = table.rowAtPoint(p); 1057 int column = table.columnAtPoint(p); 1058 if ((column == -1) || (row == -1)) { 1061 return; 1062 } 1063 1064 if (e.isControlDown()) { 1065 ListSelectionModel cm = table.getColumnModel().getSelectionModel(); 1066 ListSelectionModel rm = table.getSelectionModel(); 1067 int colAnchor = cm.getAnchorSelectionIndex(); 1068 int rowAnchor = rm.getAnchorSelectionIndex(); 1069 1070 boolean selected = true; 1071 1072 if (rowAnchor == -1 || rowAnchor >= table.getRowCount()) { 1073 rowAnchor = 0; 1074 selected = false; 1075 } 1076 1077 if (colAnchor == -1 || colAnchor >= table.getColumnCount()) { 1078 colAnchor = 0; 1079 selected = false; 1080 } 1081 1082 selected = selected && table.isCellSelected(rowAnchor, colAnchor); 1083 1084 changeSelectionModel(cm, colAnchor, selected, column); 1085 changeSelectionModel(rm, rowAnchor, selected, row); 1086 1087 if (table.getAutoscrolls()) { 1092 Rectangle cellRect = table.getCellRect(row, column, false); 1093 if (cellRect != null) { 1094 table.scrollRectToVisible(cellRect); 1095 } 1096 } 1097 } else { 1098 table.changeSelection(row, column, false, true); 1099 } 1100 } 1101 1102 private void changeSelectionModel(ListSelectionModel sm, 1103 int anchorIndex, 1104 boolean anchorSelected, 1105 int index) { 1106 1107 if (anchorSelected) { 1108 sm.addSelectionInterval(anchorIndex, index); 1109 } else { 1110 sm.removeSelectionInterval(anchorIndex, index); 1111 } 1112 } 1113 1114 1115 public void propertyChange(PropertyChangeEvent event) { 1117 String changeName = event.getPropertyName(); 1118 1119 if ("componentOrientation" == changeName) { 1120 JTableHeader header = table.getTableHeader(); 1121 if (header != null) { 1122 header.setComponentOrientation( 1123 (ComponentOrientation)event.getNewValue()); 1124 } 1125 } else if ("transferHandler" == changeName) { 1126 DropTarget dropTarget = table.getDropTarget(); 1127 if (dropTarget instanceof UIResource) { 1128 if (defaultDropTargetListener == null) { 1129 defaultDropTargetListener = 1130 new TableDropTargetListener(); 1131 } 1132 try { 1133 dropTarget.addDropTargetListener( 1134 defaultDropTargetListener); 1135 } catch (TooManyListenersException tmle) { 1136 } 1138 } 1139 } else if ("Table.isFileList" == changeName) { 1140 isFileList = Boolean.TRUE.equals(table.getClientProperty("Table.isFileList")); 1141 table.revalidate(); 1142 table.repaint(); 1143 } 1144 } 1145 } 1146 1147 1148 private class DragFixHandler extends Handler implements ListSelectionListener, 1149 ActionListener, 1150 BeforeDrag { 1151 1152 private int pressedRow; 1155 private int pressedCol; 1156 private MouseEvent pressedEvent; 1157 1158 private boolean dragPressDidSelection; 1162 1163 private boolean dragStarted; 1167 1168 private boolean shouldStartTimer; 1170 1171 private boolean outsidePrefSize; 1174 1175 private Timer timer = null; 1177 1178 private boolean canStartDrag() { 1179 if (pressedRow == -1 || pressedCol == -1) { 1180 return false; 1181 } 1182 1183 if (isFileList) { 1184 return !outsidePrefSize; 1185 } 1186 1187 if ((table.getSelectionModel().getSelectionMode() == 1189 ListSelectionModel.SINGLE_SELECTION) && 1190 (table.getColumnModel().getSelectionModel().getSelectionMode() == 1191 ListSelectionModel.SINGLE_SELECTION)) { 1192 1193 return true; 1194 } 1195 1196 return table.isCellSelected(pressedRow, pressedCol); 1197 } 1198 1199 public void mousePressed(MouseEvent e) { 1200 if (SwingUtilities2.shouldIgnore(e, table)) { 1201 return; 1202 } 1203 1204 if (table.isEditing() && !table.getCellEditor().stopCellEditing()) { 1205 Component editorComponent = table.getEditorComponent(); 1206 if (editorComponent != null && !editorComponent.hasFocus()) { 1207 BasicLookAndFeel.compositeRequestFocus(editorComponent); 1208 } 1209 return; 1210 } 1211 1212 Point p = e.getPoint(); 1213 pressedRow = table.rowAtPoint(p); 1214 pressedCol = table.columnAtPoint(p); 1215 outsidePrefSize = pointOutsidePrefSize(table, pressedRow, pressedCol, p); 1216 1217 if (isFileList) { 1218 shouldStartTimer = 1219 table.isCellSelected(pressedRow, pressedCol) && 1220 !e.isShiftDown() && 1221 !e.isControlDown() && 1222 !outsidePrefSize; 1223 } 1224 1225 if (table.getDragEnabled()) { 1226 mousePressedDND(e); 1227 } else { 1228 SwingUtilities2.adjustFocus(table); 1229 if (!isFileList) { 1230 setValueIsAdjusting(true); 1231 } 1232 adjustSelection(e); 1233 } 1234 } 1235 1236 private void mousePressedDND(MouseEvent e) { 1237 pressedEvent = e; 1238 boolean grabFocus = true; 1239 dragStarted = false; 1240 1241 if (canStartDrag() && DragRecognitionSupport.mousePressed(e)) { 1242 1243 dragPressDidSelection = false; 1244 1245 if (e.isControlDown() && isFileList) { 1246 return; 1249 } else if (!e.isShiftDown() && table.isCellSelected(pressedRow, pressedCol)) { 1250 table.getSelectionModel().addSelectionInterval(pressedRow, 1253 pressedRow); 1254 table.getColumnModel().getSelectionModel(). 1255 addSelectionInterval(pressedCol, pressedCol); 1256 1257 return; 1258 } 1259 1260 dragPressDidSelection = true; 1261 1262 grabFocus = false; 1264 } else if (!isFileList) { 1265 setValueIsAdjusting(true); 1268 } 1269 1270 if (grabFocus) { 1271 SwingUtilities2.adjustFocus(table); 1272 } 1273 1274 adjustSelection(e); 1275 } 1276 1277 private void adjustSelection(MouseEvent e) { 1278 if (outsidePrefSize) { 1280 if (e.getID() == MouseEvent.MOUSE_PRESSED && 1283 (!e.isShiftDown() || 1284 table.getSelectionModel().getSelectionMode() == 1285 ListSelectionModel.SINGLE_SELECTION)) { 1286 table.clearSelection(); 1287 TableCellEditor tce = table.getCellEditor(); 1288 if (tce != null) { 1289 tce.stopCellEditing(); 1290 } 1291 } 1292 return; 1293 } 1294 if ((pressedCol == -1) || (pressedRow == -1)) { 1297 return; 1298 } 1299 1300 boolean dragEnabled = table.getDragEnabled(); 1301 1302 if (!dragEnabled && !isFileList && table.editCellAt(pressedRow, pressedCol, e)) { 1303 setDispatchComponent(e); 1304 repostEvent(e); 1305 } 1306 1307 CellEditor editor = table.getCellEditor(); 1308 if (dragEnabled || editor == null || editor.shouldSelectCell(e)) { 1309 makeSelectionChange(pressedRow, pressedCol, e); 1310 } 1311 } 1312 1313 public void valueChanged(ListSelectionEvent e) { 1314 if (timer != null) { 1315 timer.stop(); 1316 timer = null; 1317 } 1318 } 1319 1320 public void actionPerformed(ActionEvent ae) { 1321 table.editCellAt(pressedRow, pressedCol, null); 1322 Component editorComponent = table.getEditorComponent(); 1323 if (editorComponent != null && !editorComponent.hasFocus()) { 1324 BasicLookAndFeel.compositeRequestFocus(editorComponent); 1325 } 1326 return; 1327 } 1328 1329 private void maybeStartTimer() { 1330 if (!shouldStartTimer) { 1331 return; 1332 } 1333 1334 if (timer == null) { 1335 timer = new Timer(1200, this); 1336 timer.setRepeats(false); 1337 } 1338 1339 timer.start(); 1340 } 1341 1342 public void mouseReleased(MouseEvent e) { 1343 if (SwingUtilities2.shouldIgnore(e, table)) { 1344 return; 1345 } 1346 1347 if (table.getDragEnabled()) { 1348 mouseReleasedDND(e); 1349 } else { 1350 if (isFileList) { 1351 maybeStartTimer(); 1352 } 1353 } 1354 1355 pressedEvent = null; 1356 repostEvent(e); 1357 dispatchComponent = null; 1358 setValueIsAdjusting(false); 1359 } 1360 1361 private void mouseReleasedDND(MouseEvent e) { 1362 MouseEvent me = DragRecognitionSupport.mouseReleased(e); 1363 if (me != null) { 1364 SwingUtilities2.adjustFocus(table); 1365 if (!dragPressDidSelection) { 1366 adjustSelection(me); 1367 } 1368 } 1369 1370 if (!dragStarted) { 1371 if (isFileList) { 1372 maybeStartTimer(); 1373 return; 1374 } 1375 1376 Point p = e.getPoint(); 1377 1378 if (pressedEvent != null && 1379 table.rowAtPoint(p) == pressedRow && 1380 table.columnAtPoint(p) == pressedCol && 1381 table.editCellAt(pressedRow, pressedCol, pressedEvent)) { 1382 1383 setDispatchComponent(pressedEvent); 1384 repostEvent(pressedEvent); 1385 1386 CellEditor ce = table.getCellEditor(); 1390 if (ce != null) { 1391 ce.shouldSelectCell(pressedEvent); 1392 } 1393 } 1394 } 1395 } 1396 1397 public void dragStarting(MouseEvent me) { 1398 dragStarted = true; 1399 1400 if (me.isControlDown() && isFileList) { 1401 table.getSelectionModel().addSelectionInterval(pressedRow, 1402 pressedRow); 1403 table.getColumnModel().getSelectionModel(). 1404 addSelectionInterval(pressedCol, pressedCol); 1405 } 1406 1407 pressedEvent = null; 1408 } 1409 1410 public void mouseDragged(MouseEvent e) { 1411 if (SwingUtilities2.shouldIgnore(e, table)) { 1412 return; 1413 } 1414 1415 if (table.getDragEnabled() && 1416 (DragRecognitionSupport.mouseDragged(e, this) || dragStarted)) { 1417 1418 return; 1419 } 1420 1421 mouseDraggedImpl(e); 1422 } 1423 1424 public void propertyChange(PropertyChangeEvent event) { 1425 super.propertyChange(event); 1426 1427 String changeName = event.getPropertyName(); 1428 1429 if ("Table.isFileList" == changeName) { 1430 if (isFileList) { 1431 table.getSelectionModel().addListSelectionListener(getDragFixHandler()); 1432 } else { 1433 table.getSelectionModel().removeListSelectionListener(getDragFixHandler()); 1434 timer = null; 1435 } 1436 } else if ("selectionModel" == changeName) { 1437 if (isFileList) { 1438 ListSelectionModel old = (ListSelectionModel)event.getOldValue(); 1439 old.removeListSelectionListener(getDragFixHandler()); 1440 table.getSelectionModel().addListSelectionListener(getDragFixHandler()); 1441 } 1442 } 1443 } 1444 } 1445 1446 1447 1452 private static boolean pointOutsidePrefSize(JTable table, 1453 int row, int column, Point p) { 1454 if (!Boolean.TRUE.equals(table.getClientProperty("Table.isFileList"))) { 1455 return false; 1456 } 1457 1458 return SwingUtilities2.pointOutsidePrefSize(table, row, column, p); 1459 } 1460 1461 1465 private Handler getHandler() { 1466 if (handler == null) { 1467 handler = DRAG_FIX ? new DragFixHandler() : new Handler(); 1468 } 1469 return handler; 1470 } 1471 1472 private DragFixHandler getDragFixHandler() { 1473 assert DRAG_FIX; 1475 return (DragFixHandler)handler; 1476 } 1477 1478 1481 protected KeyListener createKeyListener() { 1482 return null; 1483 } 1484 1485 1488 protected FocusListener createFocusListener() { 1489 return getHandler(); 1490 } 1491 1492 1495 protected MouseInputListener createMouseInputListener() { 1496 return getHandler(); 1497 } 1498 1499 1503 public static ComponentUI createUI(JComponent c) { 1504 return new BasicTableUI (); 1505 } 1506 1507 1509 public void installUI(JComponent c) { 1510 table = (JTable)c; 1511 1512 rendererPane = new CellRendererPane(); 1513 table.add(rendererPane); 1514 installDefaults(); 1515 installDefaults2(); 1516 installListeners(); 1517 installKeyboardActions(); 1518 } 1519 1520 1528 protected void installDefaults() { 1529 LookAndFeel.installColorsAndFont(table, "Table.background", 1530 "Table.foreground", "Table.font"); 1531 1539 LookAndFeel.installProperty(table, "opaque", Boolean.TRUE); 1540 1541 Color sbg = table.getSelectionBackground(); 1542 if (sbg == null || sbg instanceof UIResource) { 1543 table.setSelectionBackground(UIManager.getColor("Table.selectionBackground")); 1544 } 1545 1546 Color sfg = table.getSelectionForeground(); 1547 if (sfg == null || sfg instanceof UIResource) { 1548 table.setSelectionForeground(UIManager.getColor("Table.selectionForeground")); 1549 } 1550 1551 Color gridColor = table.getGridColor(); 1552 if (gridColor == null || gridColor instanceof UIResource) { 1553 table.setGridColor(UIManager.getColor("Table.gridColor")); 1554 } 1555 1556 Container parent = table.getParent(); if (parent != null) { 1559 parent = parent.getParent(); if (parent != null && parent instanceof JScrollPane) { 1561 LookAndFeel.installBorder((JScrollPane)parent, "Table.scrollPaneBorder"); 1562 } 1563 } 1564 1565 isFileList = Boolean.TRUE.equals(table.getClientProperty("Table.isFileList")); 1566 } 1567 1568 private void installDefaults2() { 1569 TransferHandler th = table.getTransferHandler(); 1570 if (th == null || th instanceof UIResource) { 1571 table.setTransferHandler(defaultTransferHandler); 1572 } 1573 DropTarget dropTarget = table.getDropTarget(); 1574 if (dropTarget instanceof UIResource) { 1575 if (defaultDropTargetListener == null) { 1576 defaultDropTargetListener = 1577 new TableDropTargetListener(); 1578 } 1579 try { 1580 dropTarget.addDropTargetListener(defaultDropTargetListener); 1581 } catch (TooManyListenersException tmle) { 1582 } 1584 } 1585 } 1586 1587 1590 protected void installListeners() { 1591 focusListener = createFocusListener(); 1592 keyListener = createKeyListener(); 1593 mouseInputListener = createMouseInputListener(); 1594 1595 table.addFocusListener(focusListener); 1596 table.addKeyListener(keyListener); 1597 if (!DRAG_FIX) { 1598 table.addMouseListener(defaultDragRecognizer); 1599 table.addMouseMotionListener(defaultDragRecognizer); 1600 } 1601 table.addMouseListener(mouseInputListener); 1602 table.addMouseMotionListener(mouseInputListener); 1603 table.addPropertyChangeListener(getHandler()); 1604 if (DRAG_FIX && isFileList) { 1605 table.getSelectionModel().addListSelectionListener(getDragFixHandler()); 1606 } 1607 } 1608 1609 1612 protected void installKeyboardActions() { 1613 LazyActionMap.installLazyActionMap(table, BasicTableUI .class, 1614 "Table.actionMap"); 1615 1616 InputMap inputMap = getInputMap(JComponent. 1617 WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); 1618 SwingUtilities.replaceUIInputMap(table, 1619 JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, 1620 inputMap); 1621 } 1622 1623 InputMap getInputMap(int condition) { 1624 if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) { 1625 InputMap keyMap = 1626 (InputMap)DefaultLookup.get(table, this, 1627 "Table.ancestorInputMap"); 1628 return keyMap; 1629 } 1630 return null; 1631 } 1632 1633 static void loadActionMap(LazyActionMap map) { 1634 1646 map.put(new Actions(Actions.NEXT_COLUMN, 1, 0, 1647 false, false)); 1648 map.put(new Actions(Actions.NEXT_COLUMN_CHANGE_LEAD, 1, 0, 1649 false, false)); 1650 map.put(new Actions(Actions.PREVIOUS_COLUMN, -1, 0, 1651 false, false)); 1652 map.put(new Actions(Actions.PREVIOUS_COLUMN_CHANGE_LEAD, -1, 0, 1653 false, false)); 1654 map.put(new Actions(Actions.NEXT_ROW, 0, 1, 1655 false, false)); 1656 map.put(new Actions(Actions.NEXT_ROW_CHANGE_LEAD, 0, 1, 1657 false, false)); 1658 map.put(new Actions(Actions.PREVIOUS_ROW, 0, -1, 1659 false, false)); 1660 map.put(new Actions(Actions.PREVIOUS_ROW_CHANGE_LEAD, 0, -1, 1661 false, false)); 1662 map.put(new Actions(Actions.NEXT_COLUMN_EXTEND_SELECTION, 1663 1, 0, true, false)); 1664 map.put(new Actions(Actions.PREVIOUS_COLUMN_EXTEND_SELECTION, 1665 -1, 0, true, false)); 1666 map.put(new Actions(Actions.NEXT_ROW_EXTEND_SELECTION, 1667 0, 1, true, false)); 1668 map.put(new Actions(Actions.PREVIOUS_ROW_EXTEND_SELECTION, 1669 0, -1, true, false)); 1670 map.put(new Actions(Actions.SCROLL_UP_CHANGE_SELECTION, 1671 false, false, true, false)); 1672 map.put(new Actions(Actions.SCROLL_DOWN_CHANGE_SELECTION, 1673 false, true, true, false)); 1674 map.put(new Actions(Actions.FIRST_COLUMN, 1675 false, false, false, true)); 1676 map.put(new Actions(Actions.LAST_COLUMN, 1677 false, true, false, true)); 1678 1679 map.put(new Actions(Actions.SCROLL_UP_EXTEND_SELECTION, 1680 true, false, true, false)); 1681 map.put(new Actions(Actions.SCROLL_DOWN_EXTEND_SELECTION, 1682 true, true, true, false)); 1683 map.put(new Actions(Actions.FIRST_COLUMN_EXTEND_SELECTION, 1684 true, false, false, true)); 1685 map.put(new Actions(Actions.LAST_COLUMN_EXTEND_SELECTION, 1686 true, true, false, true)); 1687 1688 map.put(new Actions(Actions.FIRST_ROW, false, false, true, true)); 1689 map.put(new Actions(Actions.LAST_ROW, false, true, true, true)); 1690 1691 map.put(new Actions(Actions.FIRST_ROW_EXTEND_SELECTION, 1692 true, false, true, true)); 1693 map.put(new Actions(Actions.LAST_ROW_EXTEND_SELECTION, 1694 true, true, true, true)); 1695 1696 map.put(new Actions(Actions.NEXT_COLUMN_CELL, 1697 1, 0, false, true)); 1698 map.put(new Actions(Actions.PREVIOUS_COLUMN_CELL, 1699 -1, 0, false, true)); 1700 map.put(new Actions(Actions.NEXT_ROW_CELL, 0, 1, false, true)); 1701 map.put(new Actions(Actions.PREVIOUS_ROW_CELL, 1702 0, -1, false, true)); 1703 1704 map.put(new Actions(Actions.SELECT_ALL)); 1705 map.put(new Actions(Actions.CLEAR_SELECTION)); 1706 map.put(new Actions(Actions.CANCEL_EDITING)); 1707 map.put(new Actions(Actions.START_EDITING)); 1708 1709 map.put(TransferHandler.getCutAction().getValue(Action.NAME), 1710 TransferHandler.getCutAction()); 1711 map.put(TransferHandler.getCopyAction().getValue(Action.NAME), 1712 TransferHandler.getCopyAction()); 1713 map.put(TransferHandler.getPasteAction().getValue(Action.NAME), 1714 TransferHandler.getPasteAction()); 1715 1716 map.put(new Actions(Actions.SCROLL_LEFT_CHANGE_SELECTION, 1717 false, false, false, false)); 1718 map.put(new Actions(Actions.SCROLL_RIGHT_CHANGE_SELECTION, 1719 false, true, false, false)); 1720 map.put(new Actions(Actions.SCROLL_LEFT_EXTEND_SELECTION, 1721 true, false, false, false)); 1722 map.put(new Actions(Actions.SCROLL_RIGHT_EXTEND_SELECTION, 1723 true, true, false, false)); 1724 1725 map.put(new Actions(Actions.ADD_TO_SELECTION)); 1726 map.put(new Actions(Actions.TOGGLE_AND_ANCHOR)); 1727 map.put(new Actions(Actions.EXTEND_TO)); 1728 map.put(new Actions(Actions.MOVE_SELECTION_TO)); 1729 } 1730 1731 1733 public void uninstallUI(JComponent c) { 1734 uninstallDefaults(); 1735 uninstallListeners(); 1736 uninstallKeyboardActions(); 1737 1738 table.remove(rendererPane); 1739 rendererPane = null; 1740 table = null; 1741 } 1742 1743 protected void uninstallDefaults() { 1744 if (table.getTransferHandler() instanceof UIResource) { 1745 table.setTransferHandler(null); 1746 } 1747 } 1748 1749 protected void uninstallListeners() { 1750 table.removeFocusListener(focusListener); 1751 table.removeKeyListener(keyListener); 1752 if (!DRAG_FIX) { 1753 table.removeMouseListener(defaultDragRecognizer); 1754 table.removeMouseMotionListener(defaultDragRecognizer); 1755 } 1756 table.removeMouseListener(mouseInputListener); 1757 table.removeMouseMotionListener(mouseInputListener); 1758 table.removePropertyChangeListener(getHandler()); 1759 if (DRAG_FIX && isFileList) { 1760 table.getSelectionModel().removeListSelectionListener(getDragFixHandler()); 1761 } 1762 1763 focusListener = null; 1764 keyListener = null; 1765 mouseInputListener = null; 1766 handler = null; 1767 } 1768 1769 protected void uninstallKeyboardActions() { 1770 SwingUtilities.replaceUIInputMap(table, JComponent. 1771 WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null); 1772 SwingUtilities.replaceUIActionMap(table, null); 1773 } 1774 1775 1779 private Dimension createTableSize(long width) { 1780 int height = 0; 1781 int rowCount = table.getRowCount(); 1782 if (rowCount > 0 && table.getColumnCount() > 0) { 1783 Rectangle r = table.getCellRect(rowCount-1, 0, true); 1784 height = r.y + r.height; 1785 } 1786 long tmp = Math.abs(width); 1789 if (tmp > Integer.MAX_VALUE) { 1790 tmp = Integer.MAX_VALUE; 1791 } 1792 return new Dimension((int)tmp, height); 1793 } 1794 1795 1800 public Dimension getMinimumSize(JComponent c) { 1801 long width = 0; 1802 Enumeration enumeration = table.getColumnModel().getColumns(); 1803 while (enumeration.hasMoreElements()) { 1804 TableColumn aColumn = (TableColumn)enumeration.nextElement(); 1805 width = width + aColumn.getMinWidth(); 1806 } 1807 return createTableSize(width); 1808 } 1809 1810 1815 public Dimension getPreferredSize(JComponent c) { 1816 long width = 0; 1817 Enumeration enumeration = table.getColumnModel().getColumns(); 1818 while (enumeration.hasMoreElements()) { 1819 TableColumn aColumn = (TableColumn)enumeration.nextElement(); 1820 width = width + aColumn.getPreferredWidth(); 1821 } 1822 return createTableSize(width); 1823 } 1824 1825 1830 public Dimension getMaximumSize(JComponent c) { 1831 long width = 0; 1832 Enumeration enumeration = table.getColumnModel().getColumns(); 1833 while (enumeration.hasMoreElements()) { 1834 TableColumn aColumn = (TableColumn)enumeration.nextElement(); 1835 width = width + aColumn.getMaxWidth(); 1836 } 1837 return createTableSize(width); 1838 } 1839 1840 1844 1847 public void paint(Graphics g, JComponent c) { 1848 Rectangle clip = g.getClipBounds(); 1849 1850 Rectangle bounds = table.getBounds(); 1851 bounds.x = bounds.y = 0; 1854 1855 if (table.getRowCount() <= 0 || table.getColumnCount() <= 0 || 1856 !bounds.intersects(clip)) { 1859 1860 return; 1861 } 1862 1863 Point upperLeft = clip.getLocation(); 1864 Point lowerRight = new Point(clip.x + clip.width - 1, clip.y + clip.height - 1); 1865 int rMin = table.rowAtPoint(upperLeft); 1866 int rMax = table.rowAtPoint(lowerRight); 1867 if (rMin == -1) { 1870 rMin = 0; 1871 } 1872 if (rMax == -1) { 1877 rMax = table.getRowCount()-1; 1878 } 1879 1880 boolean ltr = table.getComponentOrientation().isLeftToRight(); 1881 int cMin = table.columnAtPoint(ltr ? upperLeft : lowerRight); 1882 int cMax = table.columnAtPoint(ltr ? lowerRight : upperLeft); 1883 if (cMin == -1) { 1885 cMin = 0; 1886 } 1887 if (cMax == -1) { 1890 cMax = table.getColumnCount()-1; 1891 } 1892 1893 paintGrid(g, rMin, rMax, cMin, cMax); 1895 1896 paintCells(g, rMin, rMax, cMin, cMax); 1898 } 1899 1900 1907 private void paintGrid(Graphics g, int rMin, int rMax, int cMin, int cMax) { 1908 g.setColor(table.getGridColor()); 1909 1910 Rectangle minCell = table.getCellRect(rMin, cMin, true); 1911 Rectangle maxCell = table.getCellRect(rMax, cMax, true); 1912 Rectangle damagedArea = minCell.union( maxCell ); 1913 1914 if (table.getShowHorizontalLines()) { 1915 int tableWidth = damagedArea.x + damagedArea.width; 1916 int y = damagedArea.y; 1917 for (int row = rMin; row <= rMax; row++) { 1918 y += table.getRowHeight(row); 1919 g.drawLine(damagedArea.x, y - 1, tableWidth - 1, y - 1); 1920 } 1921 } 1922 if (table.getShowVerticalLines()) { 1923 TableColumnModel cm = table.getColumnModel(); 1924 int tableHeight = damagedArea.y + damagedArea.height; 1925 int x; 1926 if (table.getComponentOrientation().isLeftToRight()) { 1927 x = damagedArea.x; 1928 for (int column = cMin; column <= cMax; column++) { 1929 int w = cm.getColumn(column).getWidth(); 1930 x += w; 1931 g.drawLine(x - 1, 0, x - 1, tableHeight - 1); 1932 } 1933 } else { 1934 x = damagedArea.x + damagedArea.width; 1935 for (int column = cMin; column < cMax; column++) { 1936 int w = cm.getColumn(column).getWidth(); 1937 x -= w; 1938 g.drawLine(x - 1, 0, x - 1, tableHeight - 1); 1939 } 1940 x -= cm.getColumn(cMax).getWidth(); 1941 g.drawLine(x, 0, x, tableHeight - 1); 1942 } 1943 } 1944 } 1945 1946 private int viewIndexForColumn(TableColumn aColumn) { 1947 TableColumnModel cm = table.getColumnModel(); 1948 for (int column = 0; column < cm.getColumnCount(); column++) { 1949 if (cm.getColumn(column) == aColumn) { 1950 return column; 1951 } 1952 } 1953 return -1; 1954 } 1955 1956 private void paintCells(Graphics g, int rMin, int rMax, int cMin, int cMax) { 1957 JTableHeader header = table.getTableHeader(); 1958 TableColumn draggedColumn = (header == null) ? null : header.getDraggedColumn(); 1959 1960 TableColumnModel cm = table.getColumnModel(); 1961 int columnMargin = cm.getColumnMargin(); 1962 1963 Rectangle cellRect; 1964 TableColumn aColumn; 1965 int columnWidth; 1966 if (table.getComponentOrientation().isLeftToRight()) { 1967 for(int row = rMin; row <= rMax; row++) { 1968 cellRect = table.getCellRect(row, cMin, false); 1969 for(int column = cMin; column <= cMax; column++) { 1970 aColumn = cm.getColumn(column); 1971 columnWidth = aColumn.getWidth(); 1972 cellRect.width = columnWidth - columnMargin; 1973 if (aColumn != draggedColumn) { 1974 paintCell(g, cellRect, row, column); 1975 } 1976 cellRect.x += columnWidth; 1977 } 1978 } 1979 } else { 1980 for(int row = rMin; row <= rMax; row++) { 1981 cellRect = table.getCellRect(row, cMin, false); 1982 aColumn = cm.getColumn(cMin); 1983 if (aColumn != draggedColumn) { 1984 columnWidth = aColumn.getWidth(); 1985 cellRect.width = columnWidth - columnMargin; 1986 paintCell(g, cellRect, row, cMin); 1987 } 1988 for(int column = cMin+1; column <= cMax; column++) { 1989 aColumn = cm.getColumn(column); 1990 columnWidth = aColumn.getWidth(); 1991 cellRect.width = columnWidth - columnMargin; 1992 cellRect.x -= columnWidth; 1993 if (aColumn != draggedColumn) { 1994 paintCell(g, cellRect, row, column); 1995 } 1996 } 1997 } 1998 } 1999 2000 if (draggedColumn != null) { 2002 paintDraggedArea(g, rMin, rMax, draggedColumn, header.getDraggedDistance()); 2003 } 2004 2005 rendererPane.removeAll(); 2007 } 2008 2009 private void paintDraggedArea(Graphics g, int rMin, int rMax, TableColumn draggedColumn, int distance) { 2010 int draggedColumnIndex = viewIndexForColumn(draggedColumn); 2011 2012 Rectangle minCell = table.getCellRect(rMin, draggedColumnIndex, true); 2013 Rectangle maxCell = table.getCellRect(rMax, draggedColumnIndex, true); 2014 2015 Rectangle vacatedColumnRect = minCell.union(maxCell); 2016 2017 g.setColor(table.getParent().getBackground()); 2019 g.fillRect(vacatedColumnRect.x, vacatedColumnRect.y, 2020 vacatedColumnRect.width, vacatedColumnRect.height); 2021 2022 vacatedColumnRect.x += distance; 2024 2025 g.setColor(table.getBackground()); 2027 g.fillRect(vacatedColumnRect.x, vacatedColumnRect.y, 2028 vacatedColumnRect.width, vacatedColumnRect.height); 2029 2030 if (table.getShowVerticalLines()) { 2032 g.setColor(table.getGridColor()); 2033 int x1 = vacatedColumnRect.x; 2034 int y1 = vacatedColumnRect.y; 2035 int x2 = x1 + vacatedColumnRect.width - 1; 2036 int y2 = y1 + vacatedColumnRect.height - 1; 2037 g.drawLine(x1-1, y1, x1-1, y2); 2039 g.drawLine(x2, y1, x2, y2); 2041 } 2042 2043 for(int row = rMin; row <= rMax; row++) { 2044 Rectangle r = table.getCellRect(row, draggedColumnIndex, false); 2046 r.x += distance; 2047 paintCell(g, r, row, draggedColumnIndex); 2048 2049 if (table.getShowHorizontalLines()) { 2051 g.setColor(table.getGridColor()); 2052 Rectangle rcr = table.getCellRect(row, draggedColumnIndex, true); 2053 rcr.x += distance; 2054 int x1 = rcr.x; 2055 int y1 = rcr.y; 2056 int x2 = x1 + rcr.width - 1; 2057 int y2 = y1 + rcr.height - 1; 2058 g.drawLine(x1, y2, x2, y2); 2059 } 2060 } 2061 } 2062 2063 private void paintCell(Graphics g, Rectangle cellRect, int row, int column) { 2064 if (table.isEditing() && table.getEditingRow()==row && 2065 table.getEditingColumn()==column) { 2066 Component component = table.getEditorComponent(); 2067 component.setBounds(cellRect); 2068 component.validate(); 2069 } 2070 else { 2071 TableCellRenderer renderer = table.getCellRenderer(row, column); 2072 Component component = table.prepareRenderer(renderer, row, column); 2073 rendererPane.paintComponent(g, component, table, cellRect.x, cellRect.y, 2074 cellRect.width, cellRect.height, true); 2075 } 2076 } 2077 2078 private static int getAdjustedLead(JTable table, 2079 boolean row, 2080 ListSelectionModel model) { 2081 2082 int index = model.getLeadSelectionIndex(); 2083 int compare = row ? table.getRowCount() : table.getColumnCount(); 2084 return index < compare ? index : -1; 2085 } 2086 2087 private static int getAdjustedLead(JTable table, boolean row) { 2088 return row ? getAdjustedLead(table, row, table.getSelectionModel()) 2089 : getAdjustedLead(table, row, table.getColumnModel().getSelectionModel()); 2090 } 2091 2092 2093 private static final TableDragGestureRecognizer defaultDragRecognizer = 2094 DRAG_FIX ? null : new TableDragGestureRecognizer(); 2095 2096 2099 static class TableDragGestureRecognizer extends BasicDragGestureRecognizer { 2100 2101 2113 protected boolean isDragPossible(MouseEvent e) { 2114 if (super.isDragPossible(e)) { 2115 JTable table = (JTable) this.getComponent(e); 2116 if (table.getDragEnabled()) { 2117 Point p = e.getPoint(); 2118 int row = table.rowAtPoint(p); 2119 int column = table.columnAtPoint(p); 2120 if (pointOutsidePrefSize(table, row, column, p)) { 2123 return false; 2124 } 2125 if ((column != -1) && (row != -1) && table.isCellSelected(row, column)) { 2126 return true; 2127 } 2128 } 2129 } 2130 return false; 2131 } 2132 } 2133 2134 private static DropTargetListener defaultDropTargetListener = null; 2135 2136 2141 static class TableDropTargetListener extends BasicDropTargetListener { 2142 2143 2147 protected void saveComponentState(JComponent comp) { 2148 JTable table = (JTable) comp; 2149 rows = table.getSelectedRows(); 2150 cols = table.getSelectedColumns(); 2151 } 2152 2153 2157 protected void restoreComponentState(JComponent comp) { 2158 JTable table = (JTable) comp; 2159 table.clearSelection(); 2160 for (int i = 0; i < rows.length; i++) { 2161 table.addRowSelectionInterval(rows[i], rows[i]); 2162 } 2163 for (int i = 0; i < cols.length; i++) { 2164 table.addColumnSelectionInterval(cols[i], cols[i]); 2165 } 2166 } 2167 2168 2172 protected void updateInsertionLocation(JComponent comp, Point p) { 2173 JTable table = (JTable) comp; 2174 int row = table.rowAtPoint(p); 2175 int col = table.columnAtPoint(p); 2176 if (row != -1) { 2177 table.setRowSelectionInterval(row, row); 2178 } 2179 if (col != -1) { 2180 table.setColumnSelectionInterval(col, col); 2181 } 2182 } 2183 2184 private int[] rows; 2185 private int[] cols; 2186 } 2187 2188 private static final TransferHandler defaultTransferHandler = new TableTransferHandler(); 2189 2190 static class TableTransferHandler extends TransferHandler implements UIResource { 2191 2192 2201 protected Transferable createTransferable(JComponent c) { 2202 if (c instanceof JTable) { 2203 JTable table = (JTable) c; 2204 int[] rows; 2205 int[] cols; 2206 2207 if (!table.getRowSelectionAllowed() && !table.getColumnSelectionAllowed()) { 2208 return null; 2209 } 2210 2211 if (!table.getRowSelectionAllowed()) { 2212 int rowCount = table.getRowCount(); 2213 2214 rows = new int[rowCount]; 2215 for (int counter = 0; counter < rowCount; counter++) { 2216 rows[counter] = counter; 2217 } 2218 } else { 2219 rows = table.getSelectedRows(); 2220 } 2221 2222 if (!table.getColumnSelectionAllowed()) { 2223 int colCount = table.getColumnCount(); 2224 2225 cols = new int[colCount]; 2226 for (int counter = 0; counter < colCount; counter++) { 2227 cols[counter] = counter; 2228 } 2229 } else { 2230 cols = table.getSelectedColumns(); 2231 } 2232 2233 if (rows == null || cols == null || rows.length == 0 || cols.length == 0) { 2234 return null; 2235 } 2236 2237 StringBuffer plainBuf = new StringBuffer (); 2238 StringBuffer htmlBuf = new StringBuffer (); 2239 2240 htmlBuf.append("<html>\n<body>\n<table>\n"); 2241 2242 for (int row = 0; row < rows.length; row++) { 2243 htmlBuf.append("<tr>\n"); 2244 for (int col = 0; col < cols.length; col++) { 2245 Object obj = table.getValueAt(rows[row], cols[col]); 2246 String val = ((obj == null) ? "" : obj.toString()); 2247 plainBuf.append(val + "\t"); 2248 htmlBuf.append(" <td>" + val + "</td>\n"); 2249 } 2250 plainBuf.deleteCharAt(plainBuf.length() - 1).append("\n"); 2252 htmlBuf.append("</tr>\n"); 2253 } 2254 2255 plainBuf.deleteCharAt(plainBuf.length() - 1); 2257 htmlBuf.append("</table>\n</body>\n</html>"); 2258 2259 return new BasicTransferable (plainBuf.toString(), htmlBuf.toString()); 2260 } 2261 2262 return null; 2263 } 2264 2265 public int getSourceActions(JComponent c) { 2266 return COPY; 2267 } 2268 2269 } 2270} | Popular Tags |