Source for javax.swing.JTable

   1: /* JTable.java -- 
   2:    Copyright (C) 2002, 2004, 2005, 2006  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.swing;
  40: 
  41: import java.awt.Color;
  42: import java.awt.Component;
  43: import java.awt.Cursor;
  44: import java.awt.Dimension;
  45: import java.awt.Font;
  46: import java.awt.FontMetrics;
  47: import java.awt.Point;
  48: import java.awt.Rectangle;
  49: import java.awt.event.FocusListener;
  50: import java.beans.PropertyChangeEvent;
  51: import java.beans.PropertyChangeListener;
  52: import java.text.DateFormat;
  53: import java.text.NumberFormat;
  54: import java.util.Date;
  55: import java.util.EventObject;
  56: import java.util.Hashtable;
  57: import java.util.Locale;
  58: import java.util.Vector;
  59: 
  60: import javax.accessibility.Accessible;
  61: import javax.accessibility.AccessibleComponent;
  62: import javax.accessibility.AccessibleContext;
  63: import javax.accessibility.AccessibleExtendedTable;
  64: import javax.accessibility.AccessibleRole;
  65: import javax.accessibility.AccessibleSelection;
  66: import javax.accessibility.AccessibleState;
  67: import javax.accessibility.AccessibleStateSet;
  68: import javax.accessibility.AccessibleTable;
  69: import javax.accessibility.AccessibleTableModelChange;
  70: import javax.swing.event.CellEditorListener;
  71: import javax.swing.event.ChangeEvent;
  72: import javax.swing.event.ListSelectionEvent;
  73: import javax.swing.event.ListSelectionListener;
  74: import javax.swing.event.TableColumnModelEvent;
  75: import javax.swing.event.TableColumnModelListener;
  76: import javax.swing.event.TableModelEvent;
  77: import javax.swing.event.TableModelListener;
  78: import javax.swing.plaf.TableUI;
  79: import javax.swing.table.DefaultTableCellRenderer;
  80: import javax.swing.table.DefaultTableColumnModel;
  81: import javax.swing.table.DefaultTableModel;
  82: import javax.swing.table.JTableHeader;
  83: import javax.swing.table.TableCellEditor;
  84: import javax.swing.table.TableCellRenderer;
  85: import javax.swing.table.TableColumn;
  86: import javax.swing.table.TableColumnModel;
  87: import javax.swing.table.TableModel;
  88: 
  89: /**
  90:  * The table component, displaying information, organized in rows and columns.
  91:  * The table can be placed in the scroll bar and have the optional header
  92:  * that is always visible. Cell values may be editable after double clicking
  93:  * on the cell. Cell columns may have various data types, that are 
  94:  * displayed and edited by the different renderers and editors. It is possible
  95:  * to set different column width. The columns are also resizeable by 
  96:  * dragging the column boundary in the header.
  97:  */
  98: public class JTable
  99:   extends JComponent
 100:   implements TableModelListener, Scrollable, TableColumnModelListener,
 101:              ListSelectionListener, CellEditorListener, Accessible
 102: {
 103:   /**
 104:    * Provides accessibility support for <code>JTable</code>.
 105:    *
 106:    * @author Roman Kennke (kennke@aicas.com)
 107:    */
 108:   protected class AccessibleJTable
 109:     extends AccessibleJComponent
 110:     implements AccessibleSelection, ListSelectionListener, TableModelListener,
 111:     TableColumnModelListener, CellEditorListener, PropertyChangeListener,
 112:     AccessibleExtendedTable
 113:   {
 114: 
 115:     /**
 116:      * Provides accessibility support for table cells.
 117:      *
 118:      * @author Roman Kennke (kennke@aicas.com)
 119:      */
 120:     protected class AccessibleJTableCell
 121:       extends AccessibleContext
 122:       implements Accessible, AccessibleComponent
 123:     {
 124: 
 125:       /**
 126:        * The table of this cell.
 127:        */
 128:       private JTable table;
 129: 
 130:       /**
 131:        * The row index of this cell.
 132:        */
 133:       private int row;
 134: 
 135:       /**
 136:        * The column index of this cell.
 137:        */
 138:       private int column;
 139: 
 140:       /**
 141:        * The index of this cell inside the AccessibleJTable parent.
 142:        */
 143:       private int index;
 144: 
 145:       /**
 146:        * Creates a new <code>AccessibleJTableCell</code>.
 147:        *
 148:        * @param t the table
 149:        * @param r the row
 150:        * @param c the column
 151:        * @param i the index of this cell inside the accessible table parent
 152:        */
 153:       public AccessibleJTableCell(JTable t, int r, int c, int i)
 154:       {
 155:         table = t;
 156:         row = r;
 157:         column = c;
 158:         index = i;
 159:       }
 160: 
 161:       /**
 162:        * Returns the accessible row for the table cell.
 163:        *
 164:        * @return the accessible row for the table cell
 165:        */
 166:       public AccessibleRole getAccessibleRole()
 167:       {
 168:         // TODO: What is the role of the table cell?
 169:         // Seems like the RI returns UNKNOWN here for 'normal' cells, might
 170:         // be different for special renderers though (not tested yet).
 171:         return AccessibleRole.UNKNOWN;
 172:       }
 173: 
 174:       /**
 175:        * Returns the accessible state set of this accessible table cell.
 176:        *
 177:        * @return the accessible state set of this accessible table cell
 178:        */
 179:       public AccessibleStateSet getAccessibleStateSet()
 180:       {
 181:         AccessibleStateSet state = new AccessibleStateSet();
 182: 
 183:         // Figure out the SHOWING state.
 184:         Rectangle visibleRect = getVisibleRect();
 185:         Rectangle cellRect = getCellRect(row, column, false);
 186:         if (visibleRect.intersects(cellRect))
 187:           state.add(AccessibleState.SHOWING);
 188: 
 189:         // Figure out SELECTED state.
 190:         if (isCellSelected(row, column))
 191:           state.add(AccessibleState.SELECTED);
 192: 
 193:         // Figure out ACTIVE state.
 194:         if (row == getSelectedRow() && column == getSelectedColumn())
 195:           state.add(AccessibleState.ACTIVE);
 196: 
 197:         // TRANSIENT seems to be always set in the RI.
 198:         state.add(AccessibleState.TRANSIENT);
 199: 
 200:         // TODO: Any other state to handle here?
 201:         return state;
 202:       }
 203: 
 204:       /**
 205:        * Returns the index of this cell in the parent object.
 206:        *
 207:        * @return the index of this cell in the parent object
 208:        */
 209:       public int getAccessibleIndexInParent()
 210:       {
 211:         return index;
 212:       }
 213: 
 214:       /**
 215:        * Returns the number of children of this object. Table cells cannot have
 216:        * children, so we return <code>0</code> here.
 217:        *
 218:        * @return <code>0</code>
 219:        */
 220:       public int getAccessibleChildrenCount()
 221:       {
 222:         return 0;
 223:       }
 224: 
 225:       /**
 226:        * Returns the accessible child at index <code>i</code>. Table cells
 227:        * don't have children, so we return <code>null</code> here.
 228:        *
 229:        * @return <code>null</code>
 230:        */
 231:       public Accessible getAccessibleChild(int i)
 232:       {
 233:         return null;
 234:       }
 235: 
 236:       /**
 237:        * Returns the locale setting for this accessible table cell.
 238:        *
 239:        * @return the locale setting for this accessible table cell
 240:        */
 241:       public Locale getLocale()
 242:       {
 243:         // TODO: For now, we return english here. This must be fixed as soon
 244:         // as we have a localized Swing.
 245:         return Locale.ENGLISH;
 246:       }
 247: 
 248:       /**
 249:        * Returns the accessible context of this table cell. Since accessible
 250:        * table cells are their own accessible context, we return
 251:        * <code>this</code>.
 252:        *
 253:        * @return the accessible context of this table cell
 254:        */
 255:       public AccessibleContext getAccessibleContext()
 256:       {
 257:         return this;
 258:       }
 259: 
 260:       /**
 261:        * Returns the background color of this cell.
 262:        *
 263:        * @return the background color of this cell
 264:        */
 265:       public Color getBackground()
 266:       {
 267:         return table.getBackground();
 268:       }
 269: 
 270:       /**
 271:        * Sets the background of the cell. Since table cells cannot have
 272:        * individual background colors, this method does nothing. Set the
 273:        * background directly on the table instead.
 274:        * 
 275:        * @param color not used
 276:        */
 277:       public void setBackground(Color color)
 278:       {
 279:         // This method does nothing. See API comments.
 280:       }
 281: 
 282:       /**
 283:        * Returns the foreground color of the table cell.
 284:        *
 285:        * @return the foreground color of the table cell
 286:        */
 287:       public Color getForeground()
 288:       {
 289:         return table.getForeground();
 290:       }
 291: 
 292:       /**
 293:        * Sets the foreground of the cell. Since table cells cannot have
 294:        * individual foreground colors, this method does nothing. Set the
 295:        * foreground directly on the table instead.
 296:        * 
 297:        * @param color not used
 298:        */
 299:       public void setForeground(Color color)
 300:       {
 301:         // This method does nothing. See API comments.
 302:       }
 303: 
 304:       /**
 305:        * Returns the cursor for this table cell.
 306:        *
 307:        * @return the cursor for this table cell
 308:        */
 309:       public Cursor getCursor()
 310:       {
 311:         return table.getCursor();
 312:       }
 313: 
 314:       /**
 315:        * Sets the cursor of the cell. Since table cells cannot have
 316:        * individual cursors, this method does nothing. Set the
 317:        * cursor directly on the table instead.
 318:        * 
 319:        * @param cursor not used
 320:        */
 321:       public void setCursor(Cursor cursor)
 322:       {
 323:         // This method does nothing. See API comments.
 324:       }
 325: 
 326:       /**
 327:        * Returns the font of the table cell.
 328:        *
 329:        * @return the font of the table cell
 330:        */
 331:       public Font getFont()
 332:       {
 333:         return table.getFont();
 334:       }
 335: 
 336:       /**
 337:        * Sets the font of the cell. Since table cells cannot have
 338:        * individual fonts, this method does nothing. Set the
 339:        * font directly on the table instead.
 340:        * 
 341:        * @param font not used
 342:        */
 343:       public void setFont(Font font)
 344:       {
 345:         // This method does nothing. See API comments.
 346:       }
 347: 
 348:       /**
 349:        * Returns the font metrics for a specified font.
 350:        *
 351:        * @param font the font for which we return the metrics
 352:        *
 353:        * @return the font metrics for a specified font
 354:        */
 355:       public FontMetrics getFontMetrics(Font font)
 356:       {
 357:         return table.getFontMetrics(font);
 358:       }
 359: 
 360:       /**
 361:        * Returns <code>true</code> if this table cell is enabled,
 362:        * <code>false</code> otherwise.
 363:        *
 364:        * @return <code>true</code> if this table cell is enabled,
 365:        *         <code>false</code> otherwise
 366:        */
 367:       public boolean isEnabled()
 368:       {
 369:         return table.isEnabled();
 370:       }
 371: 
 372:       /**
 373:        * Table cells cannot be disabled or enabled individually, so this method
 374:        * does nothing. Set the enabled flag on the table itself.
 375:        *
 376:        * @param b not used here
 377:        */
 378:       public void setEnabled(boolean b)
 379:       {
 380:         // This method does nothing. See API comments.
 381:       }
 382: 
 383:       /**
 384:        * Returns <code>true</code> if this cell is visible, <code>false</code>
 385:        * otherwise.
 386:        *
 387:        * @return <code>true</code> if this cell is visible, <code>false</code>
 388:        *         otherwise
 389:        */
 390:       public boolean isVisible()
 391:       {
 392:         return table.isVisible();
 393:       }
 394: 
 395:       /**
 396:        * The visibility cannot be set on individual table cells, so this method
 397:        * does nothing. Set the visibility on the table itself.
 398:        *
 399:        * @param b not used
 400:        */
 401:       public void setVisible(boolean b)
 402:       {
 403:         // This method does nothing. See API comments.
 404:       }
 405: 
 406:       /**
 407:        * Returns <code>true</code> if this table cell is currently showing on
 408:        * screen.
 409:        *
 410:        * @return <code>true</code> if this table cell is currently showing on
 411:        *         screen
 412:        */
 413:       public boolean isShowing()
 414:       {
 415:         return table.isShowing();
 416:       }
 417: 
 418:       /**
 419:        * Returns <code>true</code> if this table cell contains the location
 420:        * at <code>point</code>, <code>false</code> otherwise.
 421:        * <code>point</code> is interpreted as relative to the coordinate system
 422:        * of the table cell.
 423:        *
 424:        * @return <code>true</code> if this table cell contains the location
 425:        *         at <code>point</code>, <code>false</code> otherwise
 426:        */
 427:       public boolean contains(Point point)
 428:       {
 429:         Rectangle cellRect = table.getCellRect(row, column, true);
 430:         cellRect.x = 0;
 431:         cellRect.y = 0;
 432:         return cellRect.contains(point);
 433:       }
 434: 
 435:       /**
 436:        * Returns the screen location of the table cell.
 437:        *
 438:        * @return the screen location of the table cell
 439:        */
 440:       public Point getLocationOnScreen()
 441:       {
 442:         Point tableLoc = table.getLocationOnScreen();
 443:         Rectangle cellRect = table.getCellRect(row, column, true);
 444:         tableLoc.x += cellRect.x;
 445:         tableLoc.y += cellRect.y;
 446:         return tableLoc;
 447:       }
 448: 
 449:       /**
 450:        * Returns the location of this cell relative to the table's bounds.
 451:        *
 452:        * @return the location of this cell relative to the table's bounds
 453:        */
 454:       public Point getLocation()
 455:       {
 456:         Rectangle cellRect = table.getCellRect(row, column, true);
 457:         return new Point(cellRect.x, cellRect.y);
 458:       }
 459: 
 460:       /**
 461:        * The location of the table cells cannot be manipulated directly, so
 462:        * this method does nothing.
 463:        *
 464:        * @param point not used
 465:        */
 466:       public void setLocation(Point point)
 467:       {
 468:         // This method does nothing. See API comments.
 469:       }
 470: 
 471:       /**
 472:        * Returns the bounds of the cell relative to its table.
 473:        *
 474:        * @return the bounds of the cell relative to its table
 475:        */
 476:       public Rectangle getBounds()
 477:       {
 478:         return table.getCellRect(row, column, true);
 479:       }
 480: 
 481:       /**
 482:        * The bounds of the table cells cannot be manipulated directly, so
 483:        * this method does nothing.
 484:        *
 485:        * @param rectangle not used
 486:        */
 487:       public void setBounds(Rectangle rectangle)
 488:       {
 489:         // This method does nothing. See API comments.
 490:       }
 491: 
 492:       /**
 493:        * Returns the size of the table cell.
 494:        *
 495:        * @return the size of the table cell
 496:        */
 497:       public Dimension getSize()
 498:       {
 499:         Rectangle cellRect = table.getCellRect(row, column, true);
 500:         return new Dimension(cellRect.width, cellRect.height);
 501:       }
 502: 
 503:       /**
 504:        * The size cannot be set on table cells directly, so this method does
 505:        * nothing.
 506:        *
 507:        * @param dimension not used
 508:        */
 509:       public void setSize(Dimension dimension)
 510:       {
 511:         // This method does nothing. See API comments.
 512:       }
 513: 
 514:       /**
 515:        * Table cells have no children, so we return <code>null</code> here.
 516:        *
 517:        * @return <code>null</code>
 518:        */
 519:       public Accessible getAccessibleAt(Point point)
 520:       {
 521:         return null;
 522:       }
 523: 
 524:       /**
 525:        * Returns <code>true</code> if this table cell is focus traversable,
 526:        * <code>false</code> otherwise.
 527:        *
 528:        * @return <code>true</code> if this table cell is focus traversable,
 529:        *         <code>false</code> otherwise
 530:        */
 531:       public boolean isFocusTraversable()
 532:       {
 533:         return table.isFocusable();
 534:       }
 535: 
 536:       /**
 537:        * Requests that this table cell gets the keyboard focus.
 538:        */
 539:       public void requestFocus()
 540:       {
 541:         // We first set the selection models' lead selection to this cell.
 542:         table.getColumnModel().getSelectionModel()
 543:         .setLeadSelectionIndex(column);
 544:         table.getSelectionModel().setLeadSelectionIndex(row);
 545:         // Now we request that the table receives focus.
 546:         table.requestFocus();
 547:       }
 548: 
 549:       /**
 550:        * Adds a focus listener to this cell. The focus listener is really
 551:        * added to the table, so there is no way to find out when an individual
 552:        * cell changes the focus.
 553:        *
 554:        * @param listener the focus listener to add
 555:        */
 556:       public void addFocusListener(FocusListener listener)
 557:       {
 558:         table.addFocusListener(listener);
 559:       }
 560: 
 561:       /**
 562:        * Removes a focus listener from the cell. The focus listener is really
 563:        * removed from the table.
 564:        *
 565:        * @param listener the listener to remove
 566:        */
 567:       public void removeFocusListener(FocusListener listener)
 568:       {
 569:         table.removeFocusListener(listener);
 570:       }
 571:         
 572:     }
 573: 
 574:     protected class AccessibleJTableModelChange
 575:       implements AccessibleTableModelChange
 576:     {
 577:       protected int type;
 578:       protected int firstRow;
 579:       protected int lastRow;
 580:       protected int firstColumn;
 581:       protected int lastColumn;
 582: 
 583:       protected AccessibleJTableModelChange(int type, int firstRow,
 584:                                             int lastRow, int firstColumn,
 585:                                             int lastColumn)
 586:       {
 587:         this.type = type;
 588:         this.firstRow = firstRow;
 589:         this.lastRow = lastRow;
 590:         this.firstColumn = firstColumn;
 591:         this.lastColumn = lastColumn;
 592:       }
 593: 
 594:       public int getType()
 595:       {
 596:         return type;
 597:       }
 598: 
 599:       public int getFirstRow()
 600:       {
 601:         return firstRow;
 602:       }
 603: 
 604:       public int getLastRow()
 605:       {
 606:         return lastRow;
 607:       }
 608: 
 609:       public int getFirstColumn()
 610:       {
 611:         return firstColumn;
 612:       }
 613: 
 614:       public int getLastColumn()
 615:       {
 616:         return lastColumn;
 617:       }
 618:     }
 619: 
 620:     /**
 621:      * The RI returns an instance with this name in
 622:      * {@link #getAccessibleColumnHeader()}, this makes sense, so we do the
 623:      * same.
 624:      */
 625:     private class AccessibleTableHeader
 626:       implements AccessibleTable
 627:     {
 628: 
 629:       /**
 630:        * The JTableHeader wrapped by this class.
 631:        */
 632:       private JTableHeader header;
 633: 
 634:       /**
 635:        * Creates a new instance.
 636:        *
 637:        * @param h the JTableHeader to wrap
 638:        */
 639:       private AccessibleTableHeader(JTableHeader h)
 640:       {
 641:         header = h;
 642:       }
 643: 
 644:       /**
 645:        * Returns the caption for the table header.
 646:        *
 647:        * @return the caption for the table header
 648:        */
 649:       public Accessible getAccessibleCaption()
 650:       {
 651:         // The RI seems to always return null here, so do we.
 652:         return null;
 653:       }
 654: 
 655:       /**
 656:        * Sets the caption for the table header.
 657:        *
 658:        * @param caption the caption to set
 659:        */
 660:       public void setAccessibleCaption(Accessible caption)
 661:       {
 662:         // This seems to be a no-op in the RI, so we do the same.
 663:       }
 664: 
 665:       /**
 666:        * Returns the caption for the table header.
 667:        *
 668:        * @return the caption for the table header
 669:        */
 670:       public Accessible getAccessibleSummary()
 671:       {
 672:         // The RI seems to always return null here, so do we.
 673:         return null;
 674:       }
 675: 
 676:       /**
 677:        * Sets the summary for the table header.
 678:        *
 679:        * @param summary the caption to set
 680:        */
 681:       public void setAccessibleSummary(Accessible summary)
 682:       {
 683:         // This seems to be a no-op in the RI, so we do the same.
 684:       }
 685: 
 686:       /**
 687:        * Returns the number of rows, which is always 1 for the table header.
 688:        *
 689:        * @return the number of rows
 690:        */
 691:       public int getAccessibleRowCount()
 692:       {
 693:         return 1;
 694:       }
 695: 
 696:       /**
 697:        * Returns the number of columns in the table header.
 698:        *
 699:        * @return the number of columns in the table header
 700:        */
 701:       public int getAccessibleColumnCount()
 702:       {
 703:         return header.getColumnModel().getColumnCount();
 704:       }
 705: 
 706:       /**
 707:        * Returns the accessible child at the specified row and column.
 708:        * The row number is ignored here, and we return an
 709:        * AccessibleJTableHeaderCell here with the renderer component as
 710:        * component.
 711:        *
 712:        * @param r the row number
 713:        * @param c the column number
 714:        *
 715:        * @return the accessible child at the specified row and column
 716:        */
 717:       public Accessible getAccessibleAt(int r, int c)
 718:       {
 719:         TableColumn column = header.getColumnModel().getColumn(c);
 720:         TableCellRenderer rend = column.getHeaderRenderer();
 721:         if (rend == null)
 722:           rend = header.getDefaultRenderer();
 723:         Component comp =
 724:           rend.getTableCellRendererComponent(header.getTable(),
 725:                                              column.getHeaderValue(), false,
 726:                                              false, -1, c);
 727:         return new AccessibleJTableHeaderCell(header, comp, r, c);
 728:       }
 729: 
 730:       public int getAccessibleRowExtentAt(int r, int c)
 731:       {
 732:         // TODO Auto-generated method stub
 733:         return 0;
 734:       }
 735: 
 736:       public int getAccessibleColumnExtentAt(int r, int c)
 737:       {
 738:         // TODO Auto-generated method stub
 739:         return 0;
 740:       }
 741: 
 742:       public AccessibleTable getAccessibleRowHeader()
 743:       {
 744:         // TODO Auto-generated method stub
 745:         return null;
 746:       }
 747: 
 748:       public void setAccessibleRowHeader(AccessibleTable header)
 749:       {
 750:         // TODO Auto-generated method stub
 751:         
 752:       }
 753: 
 754:       public AccessibleTable getAccessibleColumnHeader()
 755:       {
 756:         // TODO Auto-generated method stub
 757:         return null;
 758:       }
 759: 
 760:       public void setAccessibleColumnHeader(AccessibleTable header)
 761:       {
 762:         // TODO Auto-generated method stub
 763:         
 764:       }
 765: 
 766:       public Accessible getAccessibleRowDescription(int r)
 767:       {
 768:         // TODO Auto-generated method stub
 769:         return null;
 770:       }
 771: 
 772:       public void setAccessibleRowDescription(int r, Accessible description)
 773:       {
 774:         // TODO Auto-generated method stub
 775:         
 776:       }
 777: 
 778:       public Accessible getAccessibleColumnDescription(int c)
 779:       {
 780:         // TODO Auto-generated method stub
 781:         return null;
 782:       }
 783: 
 784:       public void setAccessibleColumnDescription(int c, Accessible description)
 785:       {
 786:         // TODO Auto-generated method stub
 787:         
 788:       }
 789: 
 790:       public boolean isAccessibleSelected(int r, int c)
 791:       {
 792:         // TODO Auto-generated method stub
 793:         return false;
 794:       }
 795: 
 796:       public boolean isAccessibleRowSelected(int r)
 797:       {
 798:         // TODO Auto-generated method stub
 799:         return false;
 800:       }
 801: 
 802:       public boolean isAccessibleColumnSelected(int c)
 803:       {
 804:         // TODO Auto-generated method stub
 805:         return false;
 806:       }
 807: 
 808:       public int[] getSelectedAccessibleRows()
 809:       {
 810:         // TODO Auto-generated method stub
 811:         return null;
 812:       }
 813: 
 814:       public int[] getSelectedAccessibleColumns()
 815:       {
 816:         // TODO Auto-generated method stub
 817:         return null;
 818:       }
 819:         
 820:     }
 821: 
 822:     /**
 823:      * The RI returns an instance of such class for table header cells. This
 824:      * makes sense so I added this class. This still needs to be fully
 825:      * implemented, I just don't feel motivated enough to do so just now.
 826:      */
 827:     private class AccessibleJTableHeaderCell
 828:       extends AccessibleContext
 829:       implements Accessible, AccessibleComponent
 830:     {
 831: 
 832:       JTableHeader header;
 833:       
 834:       int columnIndex;
 835:       
 836:       /**
 837:        * 
 838:        * @param h  the table header.
 839:        * @param comp
 840:        * @param r
 841:        * @param c  the column index.
 842:        */
 843:       private AccessibleJTableHeaderCell(JTableHeader h, Component comp, int r,
 844:                                          int c)
 845:       {
 846:         header = h;
 847:         columnIndex = c;
 848:       }
 849: 
 850:       /**
 851:        * Returns the header renderer.
 852:        * 
 853:        * @return The header renderer.
 854:        */
 855:       Component getColumnHeaderRenderer()
 856:       {
 857:         TableColumn tc = header.getColumnModel().getColumn(columnIndex);
 858:         TableCellRenderer r = tc.getHeaderRenderer();
 859:         if (r == null)
 860:           r = header.getDefaultRenderer();
 861:         return r.getTableCellRendererComponent(header.getTable(), 
 862:             tc.getHeaderValue(), false, false, -1, columnIndex);
 863:       }
 864:       
 865:       /**
 866:        * Returns the accessible role for the table header cell.
 867:        * 
 868:        * @return The accessible role.
 869:        */
 870:       public AccessibleRole getAccessibleRole()
 871:       {
 872:         Component renderer = getColumnHeaderRenderer();
 873:         if (renderer instanceof Accessible)
 874:           {
 875:             Accessible ac = (Accessible) renderer;
 876:             return ac.getAccessibleContext().getAccessibleRole();
 877:           }
 878:         return null;
 879:       }
 880: 
 881:       public AccessibleStateSet getAccessibleStateSet()
 882:       {
 883:         // TODO Auto-generated method stub
 884:         return null;
 885:       }
 886: 
 887:       public int getAccessibleIndexInParent()
 888:       {
 889:         // TODO Auto-generated method stub
 890:         return 0;
 891:       }
 892: 
 893:       public int getAccessibleChildrenCount()
 894:       {
 895:         // TODO Auto-generated method stub
 896:         return 0;
 897:       }
 898: 
 899:       public Accessible getAccessibleChild(int i)
 900:       {
 901:         // TODO Auto-generated method stub
 902:         return null;
 903:       }
 904: 
 905:       public Locale getLocale()
 906:       {
 907:         // TODO Auto-generated method stub
 908:         return null;
 909:       }
 910: 
 911:       /**
 912:        * Returns the accessible context.
 913:        * 
 914:        * @return <code>this</code>.
 915:        */
 916:       public AccessibleContext getAccessibleContext()
 917:       {
 918:         return this;
 919:       }
 920: 
 921:       public Color getBackground()
 922:       {
 923:         // TODO Auto-generated method stub
 924:         return null;
 925:       }
 926: 
 927:       public void setBackground(Color color)
 928:       {
 929:         // TODO Auto-generated method stub
 930:         
 931:       }
 932: 
 933:       public Color getForeground()
 934:       {
 935:         // TODO Auto-generated method stub
 936:         return null;
 937:       }
 938: 
 939:       public void setForeground(Color color)
 940:       {
 941:         // TODO Auto-generated method stub
 942:         
 943:       }
 944: 
 945:       public Cursor getCursor()
 946:       {
 947:         // TODO Auto-generated method stub
 948:         return null;
 949:       }
 950: 
 951:       public void setCursor(Cursor cursor)
 952:       {
 953:         // TODO Auto-generated method stub
 954:         
 955:       }
 956: 
 957:       public Font getFont()
 958:       {
 959:         // TODO Auto-generated method stub
 960:         return null;
 961:       }
 962: 
 963:       public void setFont(Font font)
 964:       {
 965:         // TODO Auto-generated method stub
 966:         
 967:       }
 968: 
 969:       public FontMetrics getFontMetrics(Font font)
 970:       {
 971:         // TODO Auto-generated method stub
 972:         return null;
 973:       }
 974: 
 975:       public boolean isEnabled()
 976:       {
 977:         // TODO Auto-generated method stub
 978:         return false;
 979:       }
 980: 
 981:       public void setEnabled(boolean b)
 982:       {
 983:         // TODO Auto-generated method stub
 984:         
 985:       }
 986: 
 987:       public boolean isVisible()
 988:       {
 989:         // TODO Auto-generated method stub
 990:         return false;
 991:       }
 992: 
 993:       public void setVisible(boolean b)
 994:       {
 995:         // TODO Auto-generated method stub
 996:         
 997:       }
 998: 
 999:       public boolean isShowing()
1000:       {
1001:         // TODO Auto-generated method stub
1002:         return false;
1003:       }
1004: 
1005:       public boolean contains(Point point)
1006:       {
1007:         // TODO Auto-generated method stub
1008:         return false;
1009:       }
1010: 
1011:       public Point getLocationOnScreen()
1012:       {
1013:         // TODO Auto-generated method stub
1014:         return null;
1015:       }
1016: 
1017:       public Point getLocation()
1018:       {
1019:         // TODO Auto-generated method stub
1020:         return null;
1021:       }
1022: 
1023:       public void setLocation(Point point)
1024:       {
1025:         // TODO Auto-generated method stub
1026:         
1027:       }
1028: 
1029:       public Rectangle getBounds()
1030:       {
1031:         // TODO Auto-generated method stub
1032:         return null;
1033:       }
1034: 
1035:       public void setBounds(Rectangle rectangle)
1036:       {
1037:         // TODO Auto-generated method stub
1038:         
1039:       }
1040: 
1041:       public Dimension getSize()
1042:       {
1043:         // TODO Auto-generated method stub
1044:         return null;
1045:       }
1046: 
1047:       public void setSize(Dimension dimension)
1048:       {
1049:         // TODO Auto-generated method stub
1050:         
1051:       }
1052: 
1053:       public Accessible getAccessibleAt(Point point)
1054:       {
1055:         // TODO Auto-generated method stub
1056:         return null;
1057:       }
1058: 
1059:       public boolean isFocusTraversable()
1060:       {
1061:         // TODO Auto-generated method stub
1062:         return false;
1063:       }
1064: 
1065:       public void requestFocus()
1066:       {
1067:         // TODO Auto-generated method stub
1068:         
1069:       }
1070: 
1071:       public void addFocusListener(FocusListener listener)
1072:       {
1073:         // TODO Auto-generated method stub
1074:         
1075:       }
1076: 
1077:       public void removeFocusListener(FocusListener listener)
1078:       {
1079:         // TODO Auto-generated method stub
1080:         
1081:       }
1082:       
1083:     }
1084: 
1085:     /**
1086:      * The last selected row. This is needed to track the selection in
1087:      * {@link #valueChanged(ListSelectionEvent)}.
1088:      */
1089:     private int lastSelectedRow;
1090: 
1091:     /**
1092:      * The last selected column. This is needed to track the selection in
1093:      * {@link #valueChanged(ListSelectionEvent)}.
1094:      */
1095:     private int lastSelectedColumn;
1096: 
1097:     /**
1098:      * The caption of the table.
1099:      */
1100:     private Accessible caption;
1101: 
1102:     /**
1103:      * The summary of the table.
1104:      */
1105:     private Accessible summary;
1106: 
1107:     /**
1108:      * Accessible descriptions for rows.
1109:      */
1110:     private Accessible[] rowDescriptions;
1111: 
1112:     /**
1113:      * Accessible descriptions for columns.
1114:      */
1115:     private Accessible[] columnDescriptions;
1116: 
1117:     /**
1118:      * Creates a new <code>AccessibleJTable</code>.
1119:      *
1120:      * @since JDK1.5
1121:      */
1122:     protected AccessibleJTable()
1123:     {
1124:       getModel().addTableModelListener(this);
1125:       getSelectionModel().addListSelectionListener(this);
1126:       getColumnModel().addColumnModelListener(this);
1127:       lastSelectedRow = getSelectedRow();
1128:       lastSelectedColumn = getSelectedColumn();
1129:       TableCellEditor editor = getCellEditor();
1130:       if (editor != null)
1131:         editor.addCellEditorListener(this);
1132:     }
1133: 
1134:     /**
1135:      * Returns the accessible role for the <code>JTable</code> component.
1136:      *
1137:      * @return {@link AccessibleRole#TABLE}.
1138:      */
1139:     public AccessibleRole getAccessibleRole()
1140:     {
1141:       return AccessibleRole.TABLE;
1142:     }
1143:     
1144:     /**
1145:      * Returns the accessible table.
1146:      * 
1147:      * @return <code>this</code>.
1148:      */
1149:     public AccessibleTable getAccessibleTable()
1150:     {
1151:       return this;
1152:     }
1153:     
1154:     /**
1155:      * Returns the number of selected items in this table.
1156:      */
1157:     public int getAccessibleSelectionCount()
1158:     {
1159:       return getSelectedColumnCount();
1160:     }
1161: 
1162:     /**
1163:      * Returns the selected accessible object with the specified index
1164:      * <code>i</code>. This basically returns the i-th selected cell in the
1165:      * table when going though it row-wise, and inside the rows, column-wise.
1166:      *
1167:      * @param i the index of the selected object to find
1168:      *
1169:      * @return the selected accessible object with the specified index
1170:      *         <code>i</code>
1171:      */
1172:     public Accessible getAccessibleSelection(int i)
1173:     {
1174:       Accessible found = null;
1175: 
1176:       int[] selectedRows = getSelectedRows();
1177:       int[] selectedColumns = getSelectedColumns();
1178:       int numCols = getColumnCount();
1179:       int numRows = getRowCount();
1180: 
1181:       // We have to go through every selected row and column and count until we
1182:       // find the specified index. This is potentially inefficient, but I can't
1183:       // think of anything better atm.
1184:       if (getRowSelectionAllowed() && getColumnSelectionAllowed())
1185:         {
1186:           int current = -1;
1187:           int newIndex = current;
1188:           int lastSelectedRow = -1;
1189:           // Go through the selected rows array, don't forget the selected
1190:           // cells inside the not-selected rows' columns.
1191:           for (int j = 0; i < selectedRows.length; i++)
1192:             {
1193:               // Handle unselected rows between this selected and the last
1194:               // selected row, if any.
1195:               int selectedRow = selectedRows[j];
1196:               int r = -1;
1197:               int ci = -1;
1198:               for (r = lastSelectedRow + 1;
1199:                    r < selectedRow && current < i; r++)
1200:                 {
1201:                   for (ci = 0; ci < selectedColumns.length && current < i;
1202:                        ci++)
1203:                     {
1204:                       current++;
1205:                     }
1206:                 }
1207:               if (current == i)
1208:                 {
1209:                   // We found the cell in the above loops, now get out of here.
1210:                   found = getAccessibleChild(r * numCols
1211:                                              + selectedColumns[ci]);
1212:                   break;
1213:                 }
1214: 
1215:               // If we're still here, handle the current selected row.
1216:               if (current < i && current + numCols >= i)
1217:                 {
1218:                   // The cell must be in that row, which one is it?
1219:                   found = getAccessibleChild(r * numCols + (i - current));
1220:                   break;
1221:                 }
1222:               current += numCols;
1223:             }
1224:           if (found == null)
1225:             {
1226:               // The cell can still be in the last couple of unselected rows.
1227:               int r = 0;
1228:               int ci = 0;
1229:               for (r = lastSelectedRow + 1;
1230:                    r < numRows && current < i; r++)
1231:                 {
1232:                   for (ci = 0; ci < selectedColumns.length && current < i;
1233:                        ci++)
1234:                     {
1235:                       current++;
1236:                     }
1237:                 }
1238:               if (current == i)
1239:                 {
1240:                   // We found the cell in the above loops, now get out of here.
1241:                   found = getAccessibleChild(r * numCols
1242:                                              + selectedColumns[ci]);
1243:                 }
1244:             }
1245:         }
1246:       // One or more rows can be completely selected.
1247:       else if (getRowSelectionAllowed())
1248:         {
1249:           int c = i % numCols;
1250:           int r = selectedRows[i / numCols];
1251:           found = getAccessibleChild(r * numCols + c);
1252:         }
1253:       // One or more columns can be completely selected.
1254:       else if (getRowSelectionAllowed())
1255:         {
1256:           int numSelectedColumns = selectedColumns.length;
1257:           int c = selectedColumns[i % numSelectedColumns];
1258:           int r = i / numSelectedColumns;
1259:           found = getAccessibleChild(r * numCols + c);
1260:         }
1261: 
1262:       return found;
1263:     }
1264: 
1265:     /**
1266:      * Returns <code>true</code> if the accessible child with the index
1267:      * <code>i</code> is selected, <code>false</code> otherwise.
1268:      *
1269:      * @param i the index of the accessible to check
1270:      *
1271:      * @return <code>true</code> if the accessible child with the index
1272:      *         <code>i</code> is selected, <code>false</code> otherwise
1273:      */
1274:     public boolean isAccessibleChildSelected(int i)
1275:     {
1276:       int r = getAccessibleRowAtIndex(i);
1277:       int c = getAccessibleColumnAtIndex(i);
1278:       return isCellSelected(r, c);
1279:     }
1280: 
1281:     /**
1282:      * Adds the accessible child with the specified index <code>i</code> to the
1283:      * selection.
1284:      *
1285:      * @param i the index of the accessible child to add to the selection
1286:      */
1287:     public void addAccessibleSelection(int i)
1288:     {
1289:       int r = getAccessibleRowAtIndex(i);
1290:       int c = getAccessibleColumnAtIndex(i);
1291:       changeSelection(r, c, true, false);
1292:     }
1293: 
1294:     /**
1295:      * Removes the accessible child with the specified index <code>i</code>
1296:      * from the current selection. This will only work on tables that have
1297:      * cell selection enabled (<code>rowSelectionAllowed == false &&
1298:      * columnSelectionAllowed == false</code>).
1299:      *
1300:      * @param i the index of the accessible to be removed from the selection
1301:      */
1302:     public void removeAccessibleSelection(int i)
1303:     {
1304:       if (! getRowSelectionAllowed() && ! getColumnSelectionAllowed())
1305:         {
1306:           int r = getAccessibleRowAtIndex(i);
1307:           int c = getAccessibleColumnAtIndex(i);
1308:           removeRowSelectionInterval(r, r);
1309:           removeColumnSelectionInterval(c, c);
1310:         }
1311:     }
1312: 
1313:     /**
1314:      * Deselects all selected accessible children.
1315:      */
1316:     public void clearAccessibleSelection()
1317:     {
1318:       clearSelection();
1319:     }
1320: 
1321:     /**
1322:      * Selects all accessible children that can be selected. This will only
1323:      * work on tables that support multiple selections and that have individual
1324:      * cell selection enabled.
1325:      */
1326:     public void selectAllAccessibleSelection()
1327:     {
1328:       selectAll();
1329:     }
1330: 
1331:     /**
1332:      * Receives notification when the row selection changes and fires
1333:      * appropriate property change events.
1334:      *
1335:      * @param event the list selection event
1336:      */
1337:     public void valueChanged(ListSelectionEvent event)
1338:     {
1339:       firePropertyChange(AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY,
1340:                          Boolean.FALSE, Boolean.TRUE);
1341:       int r = getSelectedRow();
1342:       int c = getSelectedColumn();
1343:       if (r != lastSelectedRow || c != lastSelectedColumn)
1344:         {
1345:           Accessible o = getAccessibleAt(lastSelectedRow,
1346:                                          lastSelectedColumn);
1347:           Accessible n = getAccessibleAt(r, c);
1348:           firePropertyChange(AccessibleContext
1349:                              .ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY, o, n);
1350:           lastSelectedRow = r;
1351:           lastSelectedColumn = c;
1352:         }
1353:     }
1354: 
1355:     /**
1356:      * Receives notification when the table model changes. Depending on the
1357:      * type of change, this method calls {@link #tableRowsInserted} or
1358:      * {@link #tableRowsDeleted}.
1359:      *
1360:      * @param event the table model event
1361:      */
1362:     public void tableChanged(TableModelEvent event)
1363:     {
1364:       switch (event.getType())
1365:         {
1366:         case TableModelEvent.INSERT:
1367:           tableRowsInserted(event);
1368:           break;
1369:         case TableModelEvent.DELETE:
1370:           tableRowsDeleted(event);
1371:           break;
1372:         }
1373:     }
1374: 
1375:     /**
1376:      * Receives notification when one or more rows have been inserted into the
1377:      * table and fires appropriate property change events.
1378:      *
1379:      * @param event the table model event
1380:      */
1381:     public void tableRowsInserted(TableModelEvent event)
1382:     {
1383:       handleRowChange(event);
1384:     }
1385: 
1386:     /**
1387:      * Receives notification when one or more rows have been deleted from the
1388:      * table.
1389:      *
1390:      * @param event the table model event
1391:      */
1392:     public void tableRowsDeleted(TableModelEvent event)
1393:     {
1394:       handleRowChange(event);
1395:     }
1396: 
1397:     /**
1398:      * Fires a PropertyChangeEvent for inserted or deleted rows.
1399:      *
1400:      * @param event the table model event
1401:      */
1402:     private void handleRowChange(TableModelEvent event)
1403:     {
1404:       firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
1405:                          null, null);
1406:       int firstColumn = event.getColumn();
1407:       int lastColumn = event.getColumn();
1408:       if (firstColumn == TableModelEvent.ALL_COLUMNS)
1409:         {
1410:           firstColumn = 0;
1411:           lastColumn = getColumnCount() - 1;
1412:         }
1413:       AccessibleJTableModelChange change = new AccessibleJTableModelChange
1414:          (event.getType(), event.getFirstRow(), event.getLastRow(),
1415:           firstColumn, lastColumn);
1416:       firePropertyChange(AccessibleContext.ACCESSIBLE_TABLE_MODEL_CHANGED,
1417:                          null, change);
1418:     }
1419: 
1420:     public void columnAdded(TableColumnModelEvent event)
1421:     {
1422:       firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
1423:                          null, null);
1424:       handleColumnChange(AccessibleTableModelChange.INSERT,
1425:                          event.getFromIndex(), event.getToIndex());
1426:     }
1427: 
1428:     public void columnRemoved(TableColumnModelEvent event)
1429:     {
1430:       firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
1431:                          null, null);
1432:       handleColumnChange(AccessibleTableModelChange.DELETE,
1433:                          event.getFromIndex(), event.getToIndex());
1434:     }
1435: 
1436:     public void columnMoved(TableColumnModelEvent event)
1437:     {
1438:       firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
1439:                          null, null);
1440:       handleColumnChange(AccessibleTableModelChange.DELETE,
1441:                          event.getFromIndex(), event.getFromIndex());
1442:       handleColumnChange(AccessibleTableModelChange.INSERT,
1443:                          event.getFromIndex(), event.getToIndex());
1444:     }
1445: 
1446:     /**
1447:      * Fires a PropertyChangeEvent for inserted or deleted columns.
1448:      *
1449:      * @param type the type of change
1450:      * @param from the start of the change
1451:      * @param to the target of the change
1452:      */
1453:     private void handleColumnChange(int type, int from, int to)
1454:     {
1455:       AccessibleJTableModelChange change =
1456:         new AccessibleJTableModelChange(type, 0, 0, from, to);
1457:       firePropertyChange(AccessibleContext.ACCESSIBLE_TABLE_MODEL_CHANGED,
1458:                          null, change);
1459:     }
1460: 
1461:     public void columnMarginChanged(ChangeEvent event)
1462:     {
1463:       firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
1464:                          null, null);
1465:     }
1466: 
1467:     public void columnSelectionChanged(ListSelectionEvent event)
1468:     {
1469:       // AFAICS, nothing is done here.
1470:     }
1471: 
1472:     public void editingCanceled(ChangeEvent event)
1473:     {
1474:       // AFAICS, nothing is done here.
1475:     }
1476: 
1477:     public void editingStopped(ChangeEvent event)
1478:     {
1479:       firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
1480:                          null, null);
1481:     }
1482: 
1483:     /**
1484:      * Receives notification when any of the JTable's properties changes. This
1485:      * is used to replace the listeners on the table's model, selection model,
1486:      * column model and cell editor.
1487:      *
1488:      * @param e the property change event
1489:      */
1490:     public void propertyChange(PropertyChangeEvent e)
1491:     {
1492:       String propName = e.getPropertyName(); 
1493:       if (propName.equals("tableModel"))
1494:         {
1495:           TableModel oldModel = (TableModel) e.getOldValue();
1496:           oldModel.removeTableModelListener(this);
1497:           TableModel newModel = (TableModel) e.getNewValue();
1498:           newModel.addTableModelListener(this);
1499:         }
1500:       else if (propName.equals("columnModel"))
1501:         {
1502:           TableColumnModel oldModel = (TableColumnModel) e.getOldValue();
1503:           oldModel.removeColumnModelListener(this);
1504:           TableColumnModel newModel = (TableColumnModel) e.getNewValue();
1505:           newModel.addColumnModelListener(this);
1506:         }
1507:       else if (propName.equals("selectionModel"))
1508:         {
1509:           ListSelectionModel oldModel = (ListSelectionModel) e.getOldValue();
1510:           oldModel.removeListSelectionListener(this);
1511:           ListSelectionModel newModel = (ListSelectionModel) e.getNewValue();
1512:           newModel.addListSelectionListener(this);
1513:         }
1514:       else if (propName.equals("cellEditor"))
1515:         {
1516:           CellEditor oldEd = (CellEditor) e.getOldValue();
1517:           oldEd.removeCellEditorListener(this);
1518:           CellEditor newEd = (CellEditor) e.getNewValue();
1519:           newEd.addCellEditorListener(this);
1520:         }
1521:     }
1522: 
1523:     /**
1524:      * Returns the row number of an accessible child (cell) with the specified
1525:      * index.
1526:      *
1527:      * @param index the index of the cell of which the row number is queried
1528:      * 
1529:      * @return the row number of an accessible child (cell) with the specified
1530:      *         index 
1531:      */
1532:     public int getAccessibleRow(int index)
1533:     {
1534:       return getAccessibleRowAtIndex(index);
1535:     }
1536: 
1537:     /**
1538:      * Returns the column number of an accessible child (cell) with the
1539:      * specified index.
1540:      *
1541:      * @param index the index of the cell of which the column number is queried
1542:      * 
1543:      * @return the column number of an accessible child (cell) with the
1544:      *         specified index 
1545:      */
1546:     public int getAccessibleColumn(int index)
1547:     {
1548:       return getAccessibleColumnAtIndex(index);
1549:     }
1550: 
1551:     /**
1552:      * Returns the index of the accessible child at the specified row and
1553:      * column.
1554:      *
1555:      * @param r the row number
1556:      * @param c the column number
1557:      *
1558:      * @return the index of the accessible child at the specified row and
1559:      *         column
1560:      */
1561:     public int getAccessibleIndex(int r, int c)
1562:     {
1563:       return getAccessibleIndexAt(r, c);
1564:     }
1565: 
1566:     /**
1567:      * Returns the caption of the table.
1568:      *
1569:      * @return the caption of the table
1570:      *
1571:      * @see #setAccessibleCaption(Accessible)
1572:      */
1573:     public Accessible getAccessibleCaption()
1574:     {
1575:       return caption;
1576:     }
1577: 
1578:     /**
1579:      * Sets the caption for the table.
1580:      *
1581:      * @param c the caption to set
1582:      */
1583:     public void setAccessibleCaption(Accessible c)
1584:     {
1585:       caption = c;
1586:     }
1587: 
1588:     /**
1589:      * Returns the summary for the table.
1590:      *
1591:      * @return the summary for the table
1592:      */
1593:     public Accessible getAccessibleSummary()
1594:     {
1595:       return summary;
1596:     }
1597: 
1598:     /**
1599:      * Sets the summary for the table.
1600:      *
1601:      * @param s the summary to set
1602:      */
1603:     public void setAccessibleSummary(Accessible s)
1604:     {
1605:       summary = s;
1606:     }
1607: 
1608:     /**
1609:      * Returns the number of rows in the table.
1610:      *
1611:      * @return the number of rows in the table
1612:      */
1613:     public int getAccessibleRowCount()
1614:     {
1615:       return getRowCount();
1616:     }
1617: 
1618:     /**
1619:      * Returns the number of columns in the table.
1620:      *
1621:      * @return the number of columns in the table
1622:      */
1623:     public int getAccessibleColumnCount()
1624:     {
1625:       return getColumnCount();
1626:     }
1627: 
1628:     /**
1629:      * Returns the accessible child at the given index.
1630:      *
1631:      * @param index  the child index.
1632:      * 
1633:      * @return The accessible child.
1634:      */
1635:     public Accessible getAccessibleChild(int index)
1636:     {
1637:       int r = getAccessibleRow(index);
1638:       int c = getAccessibleColumn(index);
1639:       return getAccessibleAt(r, c);  
1640:     }
1641:     
1642:     /**
1643:      * Returns the accessible child (table cell) at the specified row and
1644:      * column.
1645:      *
1646:      * @param r the row number
1647:      * @param c the column number
1648:      *
1649:      * @return the accessible child (table cell) at the specified row and
1650:      *         column
1651:      */
1652:     public Accessible getAccessibleAt(int r, int c)
1653:     {
1654:       TableCellRenderer cellRenderer = getCellRenderer(r, c);
1655:       Component renderer = cellRenderer.getTableCellRendererComponent(
1656:           JTable.this, getValueAt(r, c), isCellSelected(r, c), false, r, c);
1657:       if (renderer instanceof Accessible)
1658:         return (Accessible) renderer;
1659:       return null;
1660:     }
1661: 
1662:     /**
1663:      * Returns the number of rows that the specified cell occupies. The
1664:      * standard table cells only occupy one row, so we return <code>1</code>
1665:      * here.
1666:      *
1667:      * @param r the row number
1668:      * @param c the column number
1669:      *
1670:      * @return the number of rows that the specified cell occupies
1671:      */
1672:     public int getAccessibleRowExtentAt(int r, int c)
1673:     {
1674:       return 1;
1675:     }
1676: 
1677:     /**
1678:      * Returns the number of columns that the specified cell occupies. The
1679:      * standard table cells only occupy one column, so we return <code>1</code>
1680:      * here.
1681:      *
1682:      * @param r the row number
1683:      * @param c the column number
1684:      *
1685:      * @return the number of rows that the specified cell occupies
1686:      */
1687:     public int getAccessibleColumnExtentAt(int r, int c)
1688:     {
1689:       return 1;
1690:     }
1691: 
1692:     /**
1693:      * Returns the accessible row header.
1694:      *
1695:      * @return the accessible row header
1696:      */
1697:     public AccessibleTable getAccessibleRowHeader()
1698:     {
1699:       // The RI seems to always return null here, so do we.
1700:       return null;
1701:     }
1702: 
1703:     /**
1704:      * Sets the accessible row header.
1705:      *
1706:      * @param header the header to set
1707:      */
1708:     public void setAccessibleRowHeader(AccessibleTable header)
1709:     {
1710:       // In the RI this seems to be a no-op.    
1711:     }
1712: 
1713:     /**
1714:      * Returns the column header.
1715:      *
1716:      * @return the column header, or <code>null</code> if there is no column
1717:      *         header
1718:      */
1719:     public AccessibleTable getAccessibleColumnHeader()
1720:     {
1721:       JTableHeader h = getTableHeader();
1722:       AccessibleTable header = null;
1723:       if (h != null)
1724:         header = new AccessibleTableHeader(h);
1725:       return header;
1726:     }
1727: 
1728:     /**
1729:      * Sets the accessible column header. The default implementation doesn't
1730:      * allow changing the header this way, so this is a no-op.
1731:      *
1732:      * @param header the accessible column header to set
1733:      */
1734:     public void setAccessibleColumnHeader(AccessibleTable header)
1735:     {
1736:       // The RI doesn't seem to do anything, so we also do nothing.
1737:     }
1738: 
1739:     /**
1740:      * Returns the accessible description for the row with the specified index,
1741:      * or <code>null</code> if no description has been set.
1742:      *
1743:      * @param r the row for which the description is queried
1744:      *
1745:      * @return the accessible description for the row with the specified index,
1746:      *         or <code>null</code> if no description has been set
1747:      */
1748:     public Accessible getAccessibleRowDescription(int r)
1749:     {
1750:       Accessible descr = null;
1751:       if (rowDescriptions != null)
1752:         descr = rowDescriptions[r];
1753:       return descr;
1754:     }
1755: 
1756:     /**
1757:      * Sets the accessible description for the row with the specified index.
1758:      *
1759:      * @param r the row number for which to set the description
1760:      * @param description the description to set
1761:      */
1762:     public void setAccessibleRowDescription(int r, Accessible description)
1763:     {
1764:       if (rowDescriptions == null)
1765:         rowDescriptions = new Accessible[getAccessibleRowCount()];
1766:       rowDescriptions[r] = description;
1767:     }
1768: 
1769:     /**
1770:      * Returns the accessible description for the column with the specified
1771:      * index, or <code>null</code> if no description has been set.
1772:      *
1773:      * @param c the column for which the description is queried
1774:      *
1775:      * @return the accessible description for the column with the specified
1776:      *         index, or <code>null</code> if no description has been set
1777:      */
1778:     public Accessible getAccessibleColumnDescription(int c)
1779:     {
1780:       Accessible descr = null;
1781:       if (columnDescriptions != null)
1782:         descr = columnDescriptions[c];
1783:       return descr;
1784:     }
1785: 
1786:     /**
1787:      * Sets the accessible description for the column with the specified index.
1788:      *
1789:      * @param c the column number for which to set the description
1790:      * @param description the description to set
1791:      */
1792:     public void setAccessibleColumnDescription(int c, Accessible description)
1793:     {
1794:       if (columnDescriptions == null)
1795:         columnDescriptions = new Accessible[getAccessibleRowCount()];
1796:       columnDescriptions[c] = description;
1797:     }
1798: 
1799:     /**
1800:      * Returns <code>true</code> if the accessible child at the specified
1801:      * row and column is selected, <code>false</code> otherwise.
1802:      *
1803:      * @param r the row number of the child
1804:      * @param c the column number of the child
1805:      *
1806:      * @return <code>true</code> if the accessible child at the specified
1807:      *         row and column is selected, <code>false</code> otherwise
1808:      */
1809:     public boolean isAccessibleSelected(int r, int c)
1810:     {
1811:       return isCellSelected(r, c);
1812:     }
1813: 
1814:     /**
1815:      * Returns <code>true</code> if the row with the specified index is
1816:      * selected, <code>false</code> otherwise.
1817:      *
1818:      * @param r the row number
1819:      *
1820:      * @return <code>true</code> if the row with the specified index is
1821:      *        selected, <code>false</code> otherwise
1822:      */
1823:     public boolean isAccessibleRowSelected(int r)
1824:     {
1825:       return isRowSelected(r);
1826:     }
1827: 
1828:     /**
1829:      * Returns <code>true</code> if the column with the specified index is
1830:      * selected, <code>false</code> otherwise.
1831:      *
1832:      * @param c the column number
1833:      *
1834:      * @return <code>true</code> if the column with the specified index is
1835:      *        selected, <code>false</code> otherwise
1836:      */
1837:     public boolean isAccessibleColumnSelected(int c)
1838:     {
1839:       return isColumnSelected(c);
1840:     }
1841: 
1842:     /**
1843:      * Returns the indices of all selected rows.
1844:      *
1845:      * @return the indices of all selected rows
1846:      */
1847:     public int[] getSelectedAccessibleRows()
1848:     {
1849:       return getSelectedRows();
1850:     }
1851: 
1852:     /**
1853:      * Returns the indices of all selected columns.
1854:      *
1855:      * @return the indices of all selected columns
1856:      */
1857:     public int[] getSelectedAccessibleColumns()
1858:     {
1859:       return getSelectedColumns();
1860:     }
1861: 
1862:     /**
1863:      * Returns the accessible row at the specified index.
1864:      *
1865:      * @param index the index for which to query the row
1866:      *
1867:      * @return the row number at the specified table index
1868:      */
1869:     public int getAccessibleRowAtIndex(int index)
1870:     {
1871:       // TODO: Back this up by a Mauve test and update API docs accordingly.
1872:       return index / getColumnCount();
1873:     }
1874: 
1875:     /**
1876:      * Returns the accessible column at the specified index.
1877:      *
1878:      * @param index the index for which to query the column
1879:      *
1880:      * @return the column number at the specified table index
1881:      */
1882:     public int getAccessibleColumnAtIndex(int index)
1883:     {
1884:       // TODO: Back this up by a Mauve test and update API docs accordingly.
1885:       return index % getColumnCount();
1886:     }
1887: 
1888:     /**
1889:      * Returns the accessible child index at the specified column and row.
1890:      *
1891:      * @param row the row
1892:      * @param column the column
1893:      *
1894:      * @return the index of the accessible child at the specified row and
1895:      *         column
1896:      */
1897:     public int getAccessibleIndexAt(int row, int column)
1898:     {
1899:       // TODO: Back this up by a Mauve test and update API docs accordingly.
1900:       return row * getColumnCount() + column;
1901:     }
1902:   }
1903:   /**
1904:    * Handles property changes from the <code>TableColumn</code>s of this
1905:    * <code>JTable</code>.
1906:    *
1907:    * More specifically, this triggers a {@link #revalidate()} call if the
1908:    * preferredWidth of one of the observed columns changes.
1909:    */
1910:   class TableColumnPropertyChangeHandler implements PropertyChangeListener
1911:   {
1912:     /**
1913:      * Receives notification that a property of the observed TableColumns has
1914:      * changed.
1915:      * 
1916:      * @param ev the property change event
1917:      */
1918:     public void propertyChange(PropertyChangeEvent ev)
1919:     {
1920:       if (ev.getPropertyName().equals("preferredWidth"))
1921:         {
1922:           JTableHeader header = getTableHeader();
1923:           if (header != null)
1924:             // Do nothing if the table is in the resizing mode.
1925:             if (header.getResizingColumn() == null)
1926:               {
1927:                 TableColumn col = (TableColumn) ev.getSource();
1928:                 header.setResizingColumn(col);
1929:                 doLayout();
1930:                 header.setResizingColumn(null);
1931:               }
1932:         }
1933:     }
1934:   }
1935: 
1936:   /**
1937:    * A cell renderer for boolean values.
1938:    */
1939:   private class BooleanCellRenderer
1940:     extends DefaultTableCellRenderer
1941:   {
1942:     /**
1943:      * The CheckBox that is used for rendering.
1944:      */
1945:     private final JCheckBox checkBox;
1946:     
1947:     /**
1948:      * Creates a new checkbox based boolean cell renderer. The checkbox is
1949:      * centered by default.
1950:      */
1951:     BooleanCellRenderer()
1952:     {
1953:        checkBox = new JCheckBox();
1954:        checkBox.setHorizontalAlignment(SwingConstants.CENTER);
1955:     }
1956:    
1957:     /**
1958:      * Get the check box.
1959:      */
1960:     JCheckBox getCheckBox()
1961:     {
1962:       return checkBox;
1963:     }
1964: 
1965:     /**
1966:      * Returns the component that is used for rendering the value.
1967:      * 
1968:      * @param table the JTable
1969:      * @param value the value of the object
1970:      * @param isSelected is the cell selected?
1971:      * @param hasFocus has the cell the focus?
1972:      * @param row the row to render
1973:      * @param column the cell to render
1974:      * @return this component (the default table cell renderer)
1975:      */
1976:     public Component getTableCellRendererComponent(JTable table, Object value,
1977:                                                    boolean isSelected,
1978:                                                    boolean hasFocus, int row,
1979:                                                    int column)
1980:     {
1981:       if (isSelected)
1982:         {
1983:           checkBox.setBackground(table.getSelectionBackground());
1984:           checkBox.setForeground(table.getSelectionForeground());
1985:         }
1986:       else
1987:         {
1988:           checkBox.setBackground(table.getBackground());
1989:           checkBox.setForeground(table.getForeground());
1990:         }
1991: 
1992:       if (hasFocus)
1993:         {
1994:           checkBox.setBorder(
1995:             UIManager.getBorder("Table.focusCellHighlightBorder"));
1996:           if (table.isCellEditable(row, column))
1997:             {
1998:               checkBox.setBackground(
1999:                 UIManager.getColor("Table.focusCellBackground"));
2000:               checkBox.setForeground(
2001:                 UIManager.getColor("Table.focusCellForeground"));
2002:             }
2003:         }
2004:       else
2005:         checkBox.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
2006: 
2007:       // Null is rendered as false.
2008:       if (value == null)
2009:         checkBox.setSelected(false);
2010:       else
2011:         {
2012:           Boolean boolValue = (Boolean) value;
2013:           checkBox.setSelected(boolValue.booleanValue());
2014:         }
2015:       return checkBox;
2016:     }
2017:   }
2018: 
2019:   /**
2020:    * A cell renderer for Date values.
2021:    */
2022:   private class DateCellRenderer
2023:     extends DefaultTableCellRenderer
2024:   {
2025:     /**
2026:      * Returns the component that is used for rendering the value.
2027:      *
2028:      * @param table the JTable
2029:      * @param value the value of the object
2030:      * @param isSelected is the cell selected?
2031:      * @param hasFocus has the cell the focus?
2032:      * @param row the row to render
2033:      * @param column the cell to render
2034:      * 
2035:      * @return this component (the default table cell renderer)
2036:      */
2037:     public Component getTableCellRendererComponent(JTable table, Object value,
2038:                                                    boolean isSelected,
2039:                                                    boolean hasFocus, int row,
2040:                                                    int column)
2041:     {
2042:       super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
2043:                                           row, column);
2044:       if (value instanceof Date)
2045:         {
2046:           Date dateValue = (Date) value;
2047:           DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
2048:           setText(df.format(dateValue));
2049:         }
2050:       return this;
2051:     }
2052:   }
2053: 
2054:   /**
2055:    * A cell renderer for Double values.
2056:    */
2057:   private class DoubleCellRenderer
2058:     extends DefaultTableCellRenderer
2059:   {
2060:     /**
2061:      * Creates a new instance of NumberCellRenderer.
2062:      */
2063:     public DoubleCellRenderer()
2064:     {
2065:       setHorizontalAlignment(JLabel.RIGHT);
2066:     }
2067: 
2068:     /**
2069:      * Returns the component that is used for rendering the value.
2070:      *
2071:      * @param table the JTable
2072:      * @param value the value of the object
2073:      * @param isSelected is the cell selected?
2074:      * @param hasFocus has the cell the focus?
2075:      * @param row the row to render
2076:      * @param column the cell to render
2077:      * 
2078:      * @return this component (the default table cell renderer)
2079:      */
2080:     public Component getTableCellRendererComponent(JTable table, Object value,
2081:                                                    boolean isSelected,
2082:                                                    boolean hasFocus, int row,
2083:                                                    int column)
2084:     {
2085:       super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
2086:                                           row, column);
2087:       if (value instanceof Double)
2088:         {
2089:           Double doubleValue = (Double) value;
2090:           NumberFormat nf = NumberFormat.getInstance();
2091:           setText(nf.format(doubleValue.doubleValue()));
2092:         }
2093:       return this;
2094:     }
2095:   }
2096: 
2097:   /**
2098:    * A cell renderer for Float values.
2099:    */
2100:   private class FloatCellRenderer
2101:     extends DefaultTableCellRenderer
2102:   {
2103:     /**
2104:      * Creates a new instance of NumberCellRenderer.
2105:      */
2106:     public FloatCellRenderer()
2107:     {
2108:       setHorizontalAlignment(JLabel.RIGHT);
2109:     }
2110: 
2111:     /**
2112:      * Returns the component that is used for rendering the value.
2113:      *
2114:      * @param table the JTable
2115:      * @param value the value of the object
2116:      * @param isSelected is the cell selected?
2117:      * @param hasFocus has the cell the focus?
2118:      * @param row the row to render
2119:      * @param column the cell to render
2120:      * 
2121:      * @return this component (the default table cell renderer)
2122:      */
2123:     public Component getTableCellRendererComponent(JTable table, Object value,
2124:                                                    boolean isSelected,
2125:                                                    boolean hasFocus, int row,
2126:                                                    int column)
2127:     {
2128:       super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
2129:                                           row, column);
2130:       if (value instanceof Float)
2131:         {
2132:           Float floatValue = (Float) value;
2133:           NumberFormat nf = NumberFormat.getInstance();
2134:           setText(nf.format(floatValue.floatValue()));
2135:         }
2136:       return this;
2137:     }
2138:   }
2139: 
2140:   /**
2141:    * A cell renderer for Number values.
2142:    */
2143:   private class NumberCellRenderer
2144:     extends DefaultTableCellRenderer
2145:   {
2146:     /**
2147:      * Creates a new instance of NumberCellRenderer.
2148:      */
2149:     public NumberCellRenderer()
2150:     {
2151:       setHorizontalAlignment(JLabel.RIGHT);
2152:     }
2153:   }
2154: 
2155:   /**
2156:    * A cell renderer for Icon values.
2157:    */
2158:   private class IconCellRenderer
2159:     extends DefaultTableCellRenderer
2160:   {
2161:     IconCellRenderer()
2162:     {
2163:       setHorizontalAlignment(SwingConstants.CENTER);
2164:     }
2165:     
2166:     
2167:     /**
2168:      * Returns the component that is used for rendering the value.
2169:      *
2170:      * @param table the JTable
2171:      * @param value the value of the object
2172:      * @param isSelected is the cell selected?
2173:      * @param hasFocus has the cell the focus?
2174:      * @param row the row to render
2175:      * @param column the cell to render
2176:      * 
2177:      * @return this component (the default table cell renderer)
2178:      */
2179:     public Component getTableCellRendererComponent(JTable table, Object value,
2180:                                                    boolean isSelected,
2181:                                                    boolean hasFocus, int row,
2182:                                                    int column)
2183:     {
2184:       super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
2185:                                           row, column);
2186:       if (value instanceof Icon)
2187:         {
2188:           Icon iconValue = (Icon) value;
2189:           setIcon(iconValue);
2190:         }
2191:       else
2192:         {
2193:           setIcon(null);
2194:         }
2195:       setText("");
2196:       return this;
2197:     }
2198:   }
2199:   
2200:     /**
2201:      * The JTable text component (used in editing) always has the table
2202:      * as its parent. The scrollRectToVisible must be adjusted taking the
2203:      * relative component position.
2204:      *
2205:      * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
2206:      */
2207:     private class TableTextField extends JTextField
2208:     {
2209:       /**
2210:        * Create the text field without the border.
2211:        */
2212:       TableTextField()
2213:       {
2214:         setBorder(BorderFactory.createLineBorder(getGridColor(), 2));
2215:       }
2216:     }    
2217:   
2218: 
2219:   private static final long serialVersionUID = 3876025080382781659L;
2220:   
2221:   /**
2222:    * This table, for referring identically name methods from inner classes.
2223:    */
2224:   final JTable this_table = this;
2225: 
2226: 
2227:   /**
2228:    * When resizing columns, do not automatically change any columns. In this
2229:    * case the table should be enclosed in a {@link JScrollPane} in order to
2230:    * accomodate cases in which the table size exceeds its visible area.
2231:    */
2232:   public static final int AUTO_RESIZE_OFF = 0;
2233: 
2234:   /**
2235:    * When resizing column <code>i</code>, automatically change only the
2236:    * single column <code>i+1</code> to provide or absorb excess space
2237:    * requirements.
2238:    */
2239:   public static final int AUTO_RESIZE_NEXT_COLUMN = 1;
2240: 
2241:   /**
2242:    * When resizing column <code>i</code> in a table of <code>n</code>
2243:    * columns, automatically change all columns in the range <code>[i+1,
2244:    * n)</code>, uniformly, to provide or absorb excess space requirements.
2245:    */
2246:   public static final int AUTO_RESIZE_SUBSEQUENT_COLUMNS = 2;
2247:   
2248:   /**
2249:    * When resizing column <code>i</code> in a table of <code>n</code>
2250:    * columns, automatically change all columns in the range <code>[0,
2251:    * n)</code> (with the exception of column i) uniformly, to provide or
2252:    * absorb excess space requirements.
2253:    */
2254:   public static final int AUTO_RESIZE_ALL_COLUMNS = 4;
2255: 
2256:   /**
2257:    * When resizing column <code>i</code> in a table of <code>n</code>
2258:    * columns, automatically change column <code>n-1</code> (the last column
2259:    * in the table) to provide or absorb excess space requirements.
2260:    */
2261:   public static final int AUTO_RESIZE_LAST_COLUMN = 3;
2262: 
2263:   /**
2264:    * A table mapping {@link java.lang.Class} objects to 
2265:    * {@link TableCellEditor} objects. This table is consulted by the 
2266:    * FIXME
2267:    */
2268:   protected Hashtable defaultEditorsByColumnClass = new Hashtable();
2269: 
2270:   /**
2271:    * A table mapping {@link java.lang.Class} objects to 
2272:    * {@link TableCellEditor} objects. This table is consulted by the 
2273:    * FIXME
2274:    */
2275:   protected Hashtable defaultRenderersByColumnClass = new Hashtable();
2276: 
2277:   /**
2278:    * The column that is edited, -1 if the table is not edited currently.
2279:    */
2280:   protected int editingColumn;
2281: 
2282:   /**
2283:    * The row that is edited, -1 if the table is not edited currently.
2284:    */
2285:   protected int editingRow;
2286: 
2287:   /**
2288:    * The component that is used for editing.
2289:    * <code>null</code> if the table is not editing currently.
2290:    *
2291:    */
2292:   protected transient Component editorComp;
2293: 
2294: 
2295:   /**
2296:    * Whether or not the table should automatically compute a matching
2297:    * {@link TableColumnModel} and assign it to the {@link #columnModel}
2298:    * property when the {@link #dataModel} property is changed. 
2299:    *
2300:    * @see #setModel(TableModel)
2301:    * @see #createDefaultColumnsFromModel()
2302:    * @see #setColumnModel(TableColumnModel)
2303:    * @see #setAutoCreateColumnsFromModel(boolean)
2304:    * @see #getAutoCreateColumnsFromModel()
2305:    */
2306:   protected boolean autoCreateColumnsFromModel;
2307: 
2308:   /**
2309:    * A numeric code specifying the resizing behavior of the table. Must be
2310:    * one of {@link #AUTO_RESIZE_ALL_COLUMNS} (the default), {@link
2311:    * #AUTO_RESIZE_LAST_COLUMN}, {@link #AUTO_RESIZE_NEXT_COLUMN}, {@link
2312:    * #AUTO_RESIZE_SUBSEQUENT_COLUMNS}, or {@link #AUTO_RESIZE_OFF}.
2313:    * 
2314:    * @see #doLayout()
2315:    * @see #setAutoResizeMode(int)
2316:    * @see #getAutoResizeMode()
2317:    */
2318:   protected int autoResizeMode;
2319: 
2320:   /**
2321:    * The height in pixels of any row of the table. All rows in a table are
2322:    * of uniform height. This differs from column width, which varies on a
2323:    * per-column basis, and is stored in the individual columns of the
2324:    * {@link #columnModel}.
2325:    * 
2326:    * @see #getRowHeight()
2327:    * @see #setRowHeight(int)
2328:    * @see TableColumn#getWidth()
2329:    * @see TableColumn#setWidth(int)
2330:    */
2331:   protected int rowHeight;
2332: 
2333:   /**
2334:    * The height in pixels of the gap left between any two rows of the table. 
2335:    * 
2336:    * @see #setRowMargin(int)
2337:    * @see #getRowHeight()
2338:    * @see #getIntercellSpacing()
2339:    * @see #setIntercellSpacing(Dimension)
2340:    * @see TableColumnModel#getColumnMargin()
2341:    * @see TableColumnModel#setColumnMargin(int)
2342:    */
2343:   protected int rowMargin;
2344: 
2345:   /**
2346:    * Whether or not the table should allow row selection. If the table
2347:    * allows both row <em>and</em> column selection, it is said to allow
2348:    * "cell selection". Previous versions of the JDK supported cell
2349:    * selection as an independent concept, but it is now represented solely
2350:    * in terms of simultaneous row and column selection.
2351:    *
2352:    * @see TableColumnModel#getColumnSelectionAllowed()
2353:    * @see #setRowSelectionAllowed(boolean)
2354:    * @see #getRowSelectionAllowed()
2355:    * @see #getCellSelectionEnabled()
2356:    * @see #setCellSelectionEnabled(boolean)
2357:    */
2358:   protected boolean rowSelectionAllowed;
2359: 
2360:   /**
2361:    * Obsolete. Use {@link #rowSelectionAllowed}, {@link 
2362:    * #getColumnSelectionAllowed}, or the combined methods {@link
2363:    * #getCellSelectionEnabled} and {@link #setCellSelectionEnabled(boolean)}.
2364:    */
2365:   protected boolean cellSelectionEnabled;
2366:   
2367:   /**
2368:    * The model for data stored in the table. Confusingly, the published API
2369:    * requires that this field be called <code>dataModel</code>, despite its
2370:    * property name. The table listens to its model as a {@link
2371:    * TableModelListener}.
2372:    *
2373:    * @see #tableChanged(TableModelEvent)
2374:    * @see TableModel#addTableModelListener(TableModelListener)
2375:    */
2376:   protected TableModel dataModel;
2377: 
2378:   /**
2379:    * <p>A model of various aspects of the columns of the table, <em>not
2380:    * including</em> the data stored in them. The {@link TableColumnModel}
2381:    * is principally concerned with holding a set of {@link TableColumn}
2382:    * objects, each of which describes the display parameters of a column
2383:    * and the numeric index of the column from the data model which the
2384:    * column is presenting.</p>
2385:    *
2386:    * <p>The TableColumnModel also contains a {@link ListSelectionModel} which
2387:    * indicates which columns are currently selected. This selection model
2388:    * works in combination with the {@link #selectionModel} of the table
2389:    * itself to specify a <em>table selection</em>: a combination of row and
2390:    * column selections.</p>
2391:    *
2392:    * <p>Most application programmers do not need to work with this property
2393:    * at all: setting {@link #autoCreateColumnsFromModel} will construct the
2394:    * columnModel automatically, and the table acts as a facade for most of
2395:    * the interesting properties of the columnModel anyways.</p>
2396:    * 
2397:    * @see #setColumnModel(TableColumnModel)
2398:    * @see #getColumnModel()
2399:    */
2400:   protected TableColumnModel columnModel;
2401: 
2402:   /**
2403:    * A model of the rows of this table which are currently selected. This
2404:    * model is used in combination with the column selection model held as a
2405:    * member of the {@link #columnModel} property, to represent the rows and
2406:    * columns (or both: cells) of the table which are currently selected.
2407:    *
2408:    * @see #rowSelectionAllowed
2409:    * @see #setSelectionModel(ListSelectionModel)
2410:    * @see #getSelectionModel()
2411:    * @see TableColumnModel#getSelectionModel()
2412:    * @see ListSelectionModel#addListSelectionListener(ListSelectionListener)   
2413:    */
2414:   protected ListSelectionModel selectionModel;
2415: 
2416:   /**
2417:    * The current cell editor. 
2418:    */
2419:   protected TableCellEditor cellEditor;
2420: 
2421:   /**
2422:    * Whether or not drag-and-drop is enabled on this table.
2423:    *
2424:    * @see #setDragEnabled(boolean)
2425:    * @see #getDragEnabled()
2426:    */
2427:   private boolean dragEnabled;
2428: 
2429:   /**
2430:    * The color to paint the grid lines of the table, when either {@link
2431:    * #showHorizontalLines} or {@link #showVerticalLines} is set.
2432:    *
2433:    * @see #setGridColor(Color)
2434:    * @see #getGridColor()
2435:    */
2436:   protected Color gridColor;
2437: 
2438:   /**
2439:    * The size this table would prefer its viewport assume, if it is
2440:    * contained in a {@link JScrollPane}.
2441:    *
2442:    * @see #setPreferredScrollableViewportSize(Dimension)
2443:    * @see #getPreferredScrollableViewportSize()
2444:    */
2445:   protected Dimension preferredViewportSize;
2446: 
2447:   /**
2448:    * The color to paint the background of selected cells. Fires a property
2449:    * change event with name {@link #SELECTION_BACKGROUND_CHANGED_PROPERTY}
2450:    * when its value changes.
2451:    *
2452:    * @see #setSelectionBackground(Color)
2453:    * @see #getSelectionBackground()
2454:    */
2455:   protected Color selectionBackground;
2456: 
2457:   /**
2458:    * The name carried in property change events when the {@link
2459:    * #selectionBackground} property changes.
2460:    */
2461:   private static final String SELECTION_BACKGROUND_CHANGED_PROPERTY = "selectionBackground";
2462: 
2463:   /**
2464:    * The color to paint the foreground of selected cells. Fires a property
2465:    * change event with name {@link #SELECTION_FOREGROUND_CHANGED_PROPERTY}
2466:    * when its value changes.
2467:    *
2468:    * @see #setSelectionForeground(Color)
2469:    * @see #getSelectionForeground()
2470:    */
2471:   protected Color selectionForeground;
2472: 
2473:   /**
2474:    * The name carried in property change events when the
2475:    * {@link #selectionForeground} property changes.
2476:    */
2477:   private static final String SELECTION_FOREGROUND_CHANGED_PROPERTY = "selectionForeground";
2478: 
2479:   /**
2480:    * The showHorizontalLines property.
2481:    */
2482:   protected boolean showHorizontalLines;
2483: 
2484:   /**
2485:    * The showVerticalLines property.
2486:    */
2487:   protected boolean showVerticalLines;
2488: 
2489:   /**
2490:    * The tableHeader property.
2491:    */
2492:   protected JTableHeader tableHeader;
2493: 
2494:   /**
2495:    * The property handler for this table's columns.
2496:    */
2497:   TableColumnPropertyChangeHandler tableColumnPropertyChangeHandler =
2498:     new TableColumnPropertyChangeHandler();
2499: 
2500:   /**
2501:    * Whether cell editors should receive keyboard focus when the table is
2502:    * activated.
2503:    */
2504:   private boolean surrendersFocusOnKeystroke = false;
2505: 
2506:   /**
2507:    * A Rectangle object to be reused in {@link #getCellRect}. 
2508:    */
2509:   private Rectangle rectCache = new Rectangle();
2510: 
2511:   /**
2512:    * Indicates if the rowHeight property has been set by a client program or by
2513:    * the UI.
2514:    *
2515:    * @see #setUIProperty(String, Object)
2516:    * @see LookAndFeel#installProperty(JComponent, String, Object)
2517:    */
2518:   private boolean clientRowHeightSet = false;
2519: 
2520:   /**
2521:    * Stores the sizes and positions of each row, when using non-uniform row
2522:    * heights. Initially the height of all rows is equal and stored in
2523:    * {link #rowHeight}. However, when an application calls
2524:    * {@link #setRowHeight(int,int)}, the table switches to non-uniform
2525:    * row height mode which stores the row heights in the SizeSequence
2526:    * object instead.
2527:    *
2528:    * @see #setRowHeight(int)
2529:    * @see #getRowHeight()
2530:    * @see #getRowHeight(int)
2531:    * @see #setRowHeight(int, int)
2532:    */
2533:   private SizeSequence rowHeights;
2534:   
2535:   /**
2536:    * This editor serves just a marker that the value must be simply changed to
2537:    * the opposite one instead of starting the editing session.
2538:    */
2539:   private transient TableCellEditor booleanInvertingEditor; 
2540:   
2541:   /**
2542:    * Creates a new <code>JTable</code> instance.
2543:    */
2544:   public JTable ()
2545:   {
2546:     this(null, null, null);
2547:   }
2548: 
2549:   /**
2550:    * Creates a new <code>JTable</code> instance with the given number
2551:    * of rows and columns.
2552:    *
2553:    * @param numRows an <code>int</code> value
2554:    * @param numColumns an <code>int</code> value
2555:    */
2556:   public JTable (int numRows, int numColumns)
2557:   {
2558:     this(new DefaultTableModel(numRows, numColumns));
2559:   }
2560: 
2561:   /**
2562:    * Creates a new <code>JTable</code> instance, storing the given data 
2563:    * array and heaving the given column names. To see the column names,
2564:    * you must place the JTable into the {@link JScrollPane}.
2565:    *
2566:    * @param data an <code>Object[][]</code> the table data
2567:    * @param columnNames an <code>Object[]</code> the column headers
2568:    */
2569:   public JTable(Object[][] data, Object[] columnNames)
2570:   {
2571:     this(new DefaultTableModel(data, columnNames));
2572:   }
2573: 
2574:   /**
2575:    * Creates a new <code>JTable</code> instance, using the given data model
2576:    * object that provides information about the table content. The table model
2577:    * object is asked for the table size, other features and also receives
2578:    * notifications in the case when the table has been edited by the user.
2579:    * 
2580:    * @param model
2581:    *          the table model.
2582:    */
2583:   public JTable (TableModel model)
2584:   {
2585:     this(model, null, null);
2586:   }
2587: 
2588:   /**
2589:    * Creates a new <code>JTable</code> instance, using the given model object
2590:    * that provides information about the table content. The table data model
2591:    * object is asked for the table size, other features and also receives
2592:    * notifications in the case when the table has been edited by the user. The
2593:    * table column model provides more detailed control on the table column
2594:    * related features.
2595:    * 
2596:    * @param dm
2597:    *          the table data mode
2598:    * @param cm
2599:    *          the table column model
2600:    */
2601:   public JTable (TableModel dm, TableColumnModel cm)
2602:   {
2603:     this(dm, cm, null);
2604:   }
2605: 
2606:   /**
2607:    * Creates a new <code>JTable</code> instance, providing data model,
2608:    * column model and list selection model. The list selection model
2609:    * manages the selections.
2610:    *
2611:    * @param dm data model (manages table data)
2612:    * @param cm column model (manages table columns)
2613:    * @param sm list selection model (manages table selections)
2614:    */
2615:   public JTable (TableModel dm, TableColumnModel cm, ListSelectionModel sm)
2616:   {
2617:     boolean autoCreate = false;
2618:     TableColumnModel columnModel;
2619:     if (cm != null)
2620:         columnModel = cm;
2621:     else 
2622:       {
2623:         columnModel = createDefaultColumnModel();
2624:         autoCreate = true;
2625:       }
2626:     
2627:     // Initialise the intercelar spacing before setting the column model to
2628:     // avoid firing unnecessary events.
2629:     // The initial incellar spacing is new Dimenstion(1,1). 
2630:     rowMargin = 1;
2631:     columnModel.setColumnMargin(1);
2632:     setColumnModel(columnModel);
2633:     
2634:     setSelectionModel(sm == null ? createDefaultSelectionModel() : sm);
2635:     setModel(dm == null ? createDefaultDataModel() : dm);
2636:     setAutoCreateColumnsFromModel(autoCreate);
2637:     initializeLocalVars();
2638:     // The following four lines properly set the lead selection indices.
2639:     // After this, the UI will handle the lead selection indices.
2640:     // FIXME: this should probably not be necessary, if the UI is installed
2641:     // before the TableModel is set then the UI will handle things on its
2642:     // own, but certain variables need to be set before the UI can be installed
2643:     // so we must get the correct order for all the method calls in this
2644:     // constructor.
2645:     selectionModel.setAnchorSelectionIndex(0);    
2646:     selectionModel.setLeadSelectionIndex(0);
2647:     columnModel.getSelectionModel().setAnchorSelectionIndex(0);
2648:     columnModel.getSelectionModel().setLeadSelectionIndex(0);
2649:     updateUI();
2650:   }
2651:   
2652:   /**
2653:    * Creates a new <code>JTable</code> instance that uses data and column
2654:    * names, stored in {@link Vector}s.
2655:    *
2656:    * @param data the table data
2657:    * @param columnNames the table column names.
2658:    */
2659:   public JTable(Vector data, Vector columnNames)
2660:   {
2661:     this(new DefaultTableModel(data, columnNames));
2662:   }  
2663:   
2664:   /**
2665:    * Initialize local variables to default values.
2666:    */
2667:   protected void initializeLocalVars()
2668:   {
2669:     setTableHeader(createDefaultTableHeader());
2670:     if (autoCreateColumnsFromModel)
2671:       createDefaultColumnsFromModel();
2672:     this.columnModel.addColumnModelListener(this);
2673: 
2674:     this.autoResizeMode = AUTO_RESIZE_SUBSEQUENT_COLUMNS;
2675:     setRowHeight(16);
2676:     this.rowMargin = 1;
2677:     this.rowSelectionAllowed = true;
2678:     // this.accessibleContext = new AccessibleJTable();
2679:     this.cellEditor = null;
2680:     // COMPAT: Both Sun and IBM have drag enabled
2681:     this.dragEnabled = true;
2682:     this.preferredViewportSize = new Dimension(450,400);
2683:     this.showHorizontalLines = true;
2684:     this.showVerticalLines = true;
2685:     this.editingColumn = -1;
2686:     this.editingRow = -1;
2687:   }
2688:   
2689:   /**
2690:    * Add the new table column. The table column class allows to specify column
2691:    * features more precisely, setting the preferred width, column data type
2692:    * (column class) and table headers.
2693:    * 
2694:    * There is no need the add columns to the table if the default column 
2695:    * handling is sufficient.
2696:    * 
2697:    * @param column
2698:    *          the new column to add.
2699:    */
2700:   public void addColumn(TableColumn column)
2701:   {
2702:     if (column.getHeaderValue() == null)
2703:       {
2704:         String name = dataModel.getColumnName(column.getModelIndex());
2705:         column.setHeaderValue(name);
2706:       }
2707:     
2708:     columnModel.addColumn(column);
2709:     column.addPropertyChangeListener(tableColumnPropertyChangeHandler);
2710:   }
2711:   
2712:   /**
2713:    * Create the default editors for this table. The default method creates
2714:    * the editor for Booleans.
2715:    * 
2716:    * Other fields are edited as strings at the moment.
2717:    */
2718:   protected void createDefaultEditors()
2719:   {
2720:     JCheckBox box = new BooleanCellRenderer().getCheckBox();
2721:     box.setBorder(BorderFactory.createLineBorder(getGridColor(), 2));
2722:     box.setBorderPainted(true);
2723:     booleanInvertingEditor = new DefaultCellEditor(box);    
2724:     setDefaultEditor(Boolean.class, booleanInvertingEditor);
2725:   }
2726:   
2727:   /**
2728:    * Create the default renderers for this table. The default method creates
2729:    * renderers for Boolean, Number, Double, Date, Icon and ImageIcon.
2730:    *
2731:    */
2732:   protected void createDefaultRenderers()
2733:   {
2734:     setDefaultRenderer(Boolean.class, new BooleanCellRenderer());
2735:     setDefaultRenderer(Number.class, new NumberCellRenderer());
2736:     setDefaultRenderer(Double.class, new DoubleCellRenderer());
2737:     setDefaultRenderer(Double.class, new FloatCellRenderer());
2738:     setDefaultRenderer(Date.class, new DateCellRenderer());
2739:     setDefaultRenderer(Icon.class, new IconCellRenderer());
2740:     setDefaultRenderer(ImageIcon.class, new IconCellRenderer());    
2741:   }
2742:   
2743:   /**
2744:    * @deprecated 1.0.2, replaced by <code>new JScrollPane(JTable)</code>
2745:    */
2746:   public static JScrollPane createScrollPaneForTable(JTable table)
2747:   {
2748:     return new JScrollPane(table);
2749:   }
2750:   
2751:   /**
2752:    * Create the default table column model that is used if the user-defined
2753:    * column model is not provided. The default method creates
2754:    * {@link DefaultTableColumnModel}.
2755:    * 
2756:    * @return the created table column model.
2757:    */
2758:   protected TableColumnModel createDefaultColumnModel()
2759:   {
2760:     return new DefaultTableColumnModel();
2761:   }
2762: 
2763:   /**
2764:    * Create the default table data model that is used if the user-defined
2765:    * data model is not provided. The default method creates
2766:    * {@link DefaultTableModel}.
2767:    * 
2768:    * @return the created table data model.
2769:    */
2770:   protected TableModel createDefaultDataModel()
2771:   {
2772:     return new DefaultTableModel();
2773:   }
2774: 
2775:   /**
2776:    * Create the default table selection model that is used if the user-defined
2777:    * selection model is not provided. The default method creates
2778:    * {@link DefaultListSelectionModel}.
2779:    * 
2780:    * @return the created table data model.
2781:    */
2782:   protected ListSelectionModel createDefaultSelectionModel()
2783:   {
2784:     return new DefaultListSelectionModel();
2785:   }
2786:   
2787:   /**
2788:    * Create the default table header, if the user - defined table header is not
2789:    * provided.
2790:    * 
2791:    * @return the default table header.
2792:    */
2793:   protected JTableHeader createDefaultTableHeader()
2794:   {
2795:     return new JTableHeader(columnModel);
2796:   }
2797:   
2798:   /**
2799:    * Invoked when the column is added. Revalidates and repains the table.
2800:    */
2801:   public void columnAdded (TableColumnModelEvent event)
2802:   {
2803:     revalidate();
2804:     repaint();
2805:   }
2806: 
2807:   /**
2808:    * Invoked when the column margin is changed. 
2809:    * Revalidates and repains the table.
2810:    */
2811:   public void columnMarginChanged (ChangeEvent event)
2812:   {
2813:     revalidate();
2814:     repaint();
2815:   }
2816: 
2817:   /**
2818:    * Invoked when the column is moved. Revalidates and repains the table.
2819:    */
2820:   public void columnMoved (TableColumnModelEvent event)
2821:   {
2822:     if (isEditing())
2823:       editingCanceled(null);
2824:     revalidate();
2825:     repaint();
2826:   }
2827: 
2828:   /**
2829:    * Invoked when the column is removed. Revalidates and repains the table.
2830:    */
2831:   public void columnRemoved (TableColumnModelEvent event)
2832:   {
2833:     revalidate();
2834:     repaint();
2835:   }
2836:   
2837:   /**
2838:    * Invoked when the the column selection changes, repaints the changed
2839:    * columns. It is not recommended to override this method, register the
2840:    * listener instead.
2841:    */
2842:   public void columnSelectionChanged (ListSelectionEvent event)
2843:   {
2844:     // We must limit the indices to the bounds of the JTable's model, because
2845:     // we might get values of -1 or greater then columnCount in the case
2846:     // when columns get removed.
2847:     int idx0 = Math.max(0, Math.min(getColumnCount() - 1,
2848:                                     event.getFirstIndex()));
2849:     int idxn = Math.max(0, Math.min(getColumnCount() - 1,
2850:                                     event.getLastIndex()));
2851: 
2852:     int minRow = 0;
2853:     int maxRow = getRowCount() - 1;
2854:     if (getRowSelectionAllowed())
2855:       {
2856:         minRow = selectionModel.getMinSelectionIndex();
2857:         maxRow = selectionModel.getMaxSelectionIndex();
2858:         int leadRow = selectionModel.getLeadSelectionIndex();
2859:         if (minRow == -1 && maxRow == -1)
2860:           {
2861:             minRow = leadRow;
2862:             maxRow = leadRow;
2863:           }
2864:         else
2865:           {
2866:             // In this case we need to repaint also the range to leadRow, not
2867:             // only between min and max.
2868:             if (leadRow != -1)
2869:               {
2870:                 minRow = Math.min(minRow, leadRow);
2871:                 maxRow = Math.max(maxRow, leadRow);
2872:               }
2873:           }
2874:       }
2875:     if (minRow != -1 && maxRow != -1)
2876:       {
2877:         Rectangle first = getCellRect(minRow, idx0, false);
2878:         Rectangle last = getCellRect(maxRow, idxn, false);
2879:         Rectangle dirty = SwingUtilities.computeUnion(first.x, first.y,
2880:                                                       first.width,
2881:                                                       first.height, last);
2882:         repaint(dirty);
2883:       }
2884:   }
2885:  
2886:   /**
2887:    * Invoked when the editing is cancelled.
2888:    */
2889:   public void editingCanceled (ChangeEvent event)
2890:   {
2891:     if (editorComp!=null)
2892:       {
2893:         remove(editorComp);
2894:         repaint(editorComp.getBounds());        
2895:         editorComp = null;
2896:       }
2897:   }
2898:   
2899:   /**
2900:    * Finish the current editing session and update the table with the
2901:    * new value by calling {@link #setValueAt}.
2902:    * 
2903:    * @param event the change event
2904:    */
2905:   public void editingStopped (ChangeEvent event)
2906:   {
2907:     if (editorComp!=null)
2908:       {
2909:         remove(editorComp);        
2910:         setValueAt(cellEditor.getCellEditorValue(), editingRow, editingColumn);            
2911:         repaint(editorComp.getBounds());
2912:         editorComp = null;
2913:       }
2914:     requestFocusInWindow();
2915:   }
2916: 
2917:   /**
2918:    * Invoked when the table changes.
2919:    * <code>null</code> means everything changed.
2920:    */
2921:   public void tableChanged (TableModelEvent event)
2922:   {
2923:     // update the column model from the table model if the structure has
2924:     // changed and the flag autoCreateColumnsFromModel is set
2925:     if (event == null || (event.getFirstRow() == TableModelEvent.HEADER_ROW))
2926:       handleCompleteChange(event);
2927:     else if (event.getType() == TableModelEvent.INSERT)
2928:       handleInsert(event);
2929:     else if (event.getType() == TableModelEvent.DELETE)
2930:       handleDelete(event);
2931:     else
2932:       handleUpdate(event);
2933:   }
2934: 
2935:   /**
2936:    * Handles a request for complete relayout. This is the case when
2937:    * event.getFirstRow() == TableModelEvent.HEADER_ROW.
2938:    *
2939:    * @param ev the table model event
2940:    */
2941:   private void handleCompleteChange(TableModelEvent ev)
2942:   {
2943:     clearSelection();
2944:     checkSelection();
2945:     rowHeights = null;
2946:     if (getAutoCreateColumnsFromModel())
2947:       createDefaultColumnsFromModel();
2948:     else
2949:       resizeAndRepaint();
2950:   }
2951: 
2952:   /**
2953:    * Handles table model insertions.
2954:    *
2955:    * @param ev the table model event
2956:    */
2957:   private void handleInsert(TableModelEvent ev)
2958:   {
2959:     // Sync selection model with data model.
2960:     int first = ev.getFirstRow();
2961:     if (first < 0)
2962:       first = 0;
2963:     int last = ev.getLastRow();
2964:     if (last < 0)
2965:       last = getRowCount() - 1;
2966:     selectionModel.insertIndexInterval(first, last - first + 1, true);
2967:     checkSelection();
2968: 
2969:     // For variable height rows we must update the SizeSequence thing.
2970:     if (rowHeights != null)
2971:       {
2972:         rowHeights.insertEntries(first, last - first + 1, rowHeight);
2973:         // TODO: We repaint the whole thing when the rows have variable
2974:         // heights. We might want to handle this better though.
2975:         repaint();
2976:       }
2977:     else
2978:       {
2979:         // Repaint the dirty region and revalidate.
2980:         int rowHeight = getRowHeight();
2981:         Rectangle dirty = new Rectangle(0, first * rowHeight,
2982:                                         getColumnModel().getTotalColumnWidth(),
2983:                                         (getRowCount() - first) * rowHeight);
2984:         repaint(dirty);
2985:       }
2986:     revalidate();
2987:   }
2988: 
2989:   /**
2990:    * Handles table model deletions.
2991:    *
2992:    * @param ev the table model event
2993:    */
2994:   private void handleDelete(TableModelEvent ev)
2995:   {
2996:     // Sync selection model with data model.
2997:     int first = ev.getFirstRow();
2998:     if (first < 0)
2999:       first = 0;
3000:     int last = ev.getLastRow();
3001:     if (last < 0)
3002:       last = getRowCount() - 1;
3003: 
3004:     selectionModel.removeIndexInterval(first, last);
3005: 
3006:     checkSelection();
3007: 
3008:     if (dataModel.getRowCount() == 0)
3009:       clearSelection();
3010: 
3011:     // For variable height rows we must update the SizeSequence thing.
3012:     if (rowHeights != null)
3013:       {
3014:         rowHeights.removeEntries(first, last - first + 1);
3015:         // TODO: We repaint the whole thing when the rows have variable
3016:         // heights. We might want to handle this better though.
3017:         repaint();
3018:       }
3019:     else
3020:       {
3021:         // Repaint the dirty region and revalidate.
3022:         int rowHeight = getRowHeight();
3023:         int oldRowCount = getRowCount() + last - first + 1;
3024:         Rectangle dirty = new Rectangle(0, first * rowHeight,
3025:                                         getColumnModel().getTotalColumnWidth(),
3026:                                         (oldRowCount - first) * rowHeight);
3027:         repaint(dirty);
3028:       }
3029:     revalidate();
3030:   }
3031: 
3032:   /**
3033:    * Handles table model updates without structural changes.
3034:    *
3035:    * @param ev the table model event
3036:    */
3037:   private void handleUpdate(TableModelEvent ev)
3038:   {
3039:     if (rowHeights == null)
3040:       {
3041:         // Some cells have been changed without changing the structure.
3042:         // Figure out the dirty rectangle and repaint.
3043:         int firstRow = ev.getFirstRow();
3044:         int lastRow = ev.getLastRow();
3045:         int col = ev.getColumn();
3046:         Rectangle dirty;
3047:         if (col == TableModelEvent.ALL_COLUMNS)
3048:           {
3049:             // All columns changed. 
3050:             dirty = new Rectangle(0, firstRow * getRowHeight(),
3051:                                   getColumnModel().getTotalColumnWidth(), 0);
3052:           }
3053:         else
3054:           {
3055:             // Only one cell or column of cells changed.
3056:             // We need to convert to view column first.
3057:             int column = convertColumnIndexToModel(col);
3058:             dirty = getCellRect(firstRow, column, false);
3059:           }
3060: 
3061:         // Now adjust the height of the dirty region.
3062:         dirty.height = (lastRow + 1) * getRowHeight();
3063:         // .. and repaint.
3064:         repaint(dirty);
3065:       }
3066:     else
3067:       {
3068:         // TODO: We repaint the whole thing when the rows have variable
3069:         // heights. We might want to handle this better though.
3070:         repaint();
3071:       }
3072:   }
3073: 
3074:   /**
3075:    * Helper method for adjusting the lead and anchor indices when the
3076:    * table structure changed. This sets the lead and anchor to -1 if there's
3077:    * no more rows, or set them to 0 when they were at -1 and there are actually
3078:    * some rows now.
3079:    */
3080:   private void checkSelection()
3081:   {
3082:     TableModel m = getModel();
3083:     ListSelectionModel sm = selectionModel;
3084:     if (m != null)
3085:       {
3086:         int lead = sm.getLeadSelectionIndex();
3087:         int c = m.getRowCount();
3088:         if (c == 0 && lead != -1)
3089:           {
3090:             // No rows in the model, reset lead and anchor to -1.
3091:             sm.setValueIsAdjusting(true);
3092:             sm.setAnchorSelectionIndex(-1);
3093:             sm.setLeadSelectionIndex(-1);
3094:             sm.setValueIsAdjusting(false);
3095:           }
3096:         else if (c != 0 && lead == -1)
3097:           {
3098:             // We have rows, but no lead/anchor. Set them to 0. We
3099:             // do a little trick here so that the actual selection is not
3100:             // touched.
3101:             if (sm.isSelectedIndex(0))
3102:               sm.addSelectionInterval(0, 0);
3103:             else
3104:               sm.removeSelectionInterval(0, 0);
3105:           }
3106:         // Nothing to do in the other cases.
3107:       }
3108:   }
3109: 
3110:   /**
3111:    * Invoked when another table row is selected. It is not recommended
3112:    * to override thid method, register the listener instead.
3113:    */
3114:   public void valueChanged (ListSelectionEvent event)
3115:   {
3116:     // If we are in the editing process, end the editing session.
3117:     if (isEditing())
3118:       editingStopped(null);
3119:     
3120:     // Repaint the changed region.
3121:     int first = Math.max(0, Math.min(getRowCount() - 1, event.getFirstIndex()));
3122:     int last = Math.max(0, Math.min(getRowCount() - 1, event.getLastIndex()));
3123:     Rectangle rect1 = getCellRect(first, 0, false);
3124:     Rectangle rect2 = getCellRect(last, getColumnCount() - 1, false);
3125:     Rectangle dirty = SwingUtilities.computeUnion(rect2.x, rect2.y,
3126:                                                   rect2.width, rect2.height,
3127:                                                   rect1);
3128:     repaint(dirty);
3129:   }
3130: 
3131:  /**
3132:    * Returns index of the column that contains specified point 
3133:    * or -1 if this table doesn't contain this point.
3134:    *
3135:    * @param point point to identify the column
3136:    * @return index of the column that contains specified point or 
3137:    * -1 if this table doesn't contain this point.
3138:    */
3139:   public int columnAtPoint(Point point)
3140:   {
3141:     int ncols = getColumnCount();
3142:     Dimension gap = getIntercellSpacing();
3143:     TableColumnModel cols = getColumnModel();
3144:     int x = point.x;
3145: 
3146:     for (int i = 0; i < ncols; ++i)
3147:       {
3148:         int width = cols.getColumn(i).getWidth()
3149:                     + (gap == null ? 0 : gap.width);
3150:         if (0 <= x && x < width)
3151:           return i;
3152:         x -= width;
3153:       }
3154:     return -1;
3155:   }
3156: 
3157:   /**
3158:    * Returns index of the row that contains specified point or -1 if this table
3159:    * doesn't contain this point.
3160:    * 
3161:    * @param point point to identify the row
3162:    * @return index of the row that contains specified point or -1 if this table
3163:    *         doesn't contain this point.
3164:    */
3165:   public int rowAtPoint(Point point)
3166:   {
3167:     if (point != null)
3168:       {
3169:         int nrows = getRowCount();
3170:         int r;
3171:         int y = point.y;
3172:         if (rowHeights == null)
3173:           {
3174:             int height = getRowHeight();
3175:             r = y / height;
3176:           }
3177:         else
3178:           r = rowHeights.getIndex(y);
3179: 
3180:         if (r < 0 || r >= nrows)
3181:           return -1;
3182:         else
3183:           return r;
3184:       }
3185:     else
3186:       return -1;
3187:   }
3188: 
3189:   /** 
3190:    * Calculate the visible rectangle for a particular row and column. The
3191:    * row and column are specified in visual terms; the column may not match
3192:    * the {@link #dataModel} column.
3193:    *
3194:    * @param row the visible row to get the cell rectangle of
3195:    *
3196:    * @param column the visible column to get the cell rectangle of, which may
3197:    * differ from the {@link #dataModel} column
3198:    *
3199:    * @param includeSpacing whether or not to include the cell margins in the
3200:    * resulting cell. If <code>false</code>, the result will only contain the
3201:    * inner area of the target cell, not including its margins.
3202:    *
3203:    * @return a rectangle enclosing the specified cell
3204:    */
3205:   public Rectangle getCellRect(int row,
3206:                                int column,
3207:                                boolean includeSpacing)
3208:   {
3209:     Rectangle cellRect = new Rectangle(0, 0, 0, 0);
3210: 
3211:     // Check for valid range vertically.
3212:     if (row >= getRowCount())
3213:       {
3214:         cellRect.height = getHeight();
3215:       }
3216:     else if (row >= 0)
3217:       {
3218:         cellRect.height = getRowHeight(row);
3219:         if (rowHeights == null)
3220:           cellRect.y = row * cellRect.height;
3221:         else
3222:           cellRect.y = rowHeights.getPosition(row);
3223: 
3224:         if (! includeSpacing)
3225:           {
3226:             // The rounding here is important.
3227:             int rMargin = getRowMargin();
3228:             cellRect.y += rMargin / 2;
3229:             cellRect.height -= rMargin;
3230:           }
3231:       }
3232:     // else row < 0, y = height = 0
3233: 
3234:     // Check for valid range horizontally.
3235:     if (column < 0)
3236:       {
3237:         if (! getComponentOrientation().isLeftToRight())
3238:           {
3239:             cellRect.x = getWidth();
3240:           }
3241:       }
3242:     else if (column >= getColumnCount())
3243:       {
3244:         if (getComponentOrientation().isLeftToRight())
3245:           {
3246:             cellRect.x = getWidth();
3247:           }
3248:       }
3249:     else
3250:       {
3251:         TableColumnModel tcm = getColumnModel();
3252:         if (getComponentOrientation().isLeftToRight())
3253:           {
3254:             for (int i = 0; i < column; i++)
3255:               cellRect.x += tcm.getColumn(i).getWidth();
3256:           }
3257:         else
3258:           {
3259:             for (int i = tcm.getColumnCount() - 1; i > column; i--)
3260:               cellRect.x += tcm.getColumn(i).getWidth();
3261:           }
3262:         cellRect.width = tcm.getColumn(column).getWidth();
3263:         if (! includeSpacing)
3264:           {
3265:             // The rounding here is important.
3266:             int cMargin = tcm.getColumnMargin();
3267:             cellRect.x += cMargin / 2;
3268:             cellRect.width -= cMargin;
3269:           }
3270:       }
3271: 
3272:     return cellRect;
3273:   }
3274: 
3275:   public void clearSelection()
3276:   {
3277:     selectionModel.clearSelection();
3278:     getColumnModel().getSelectionModel().clearSelection();
3279:   }
3280: 
3281:   /**
3282:    * Get the value of the selectedRow property by delegation to
3283:    * the {@link ListSelectionModel#getMinSelectionIndex} method of the
3284:    * {@link #selectionModel} field.
3285:    *
3286:    * @return The current value of the selectedRow property
3287:    */
3288:   public int getSelectedRow ()
3289:   {    
3290:     return selectionModel.getMinSelectionIndex();
3291:   }
3292:   
3293:   /**
3294:    * Get the value of the {@link #selectionModel} property.
3295:    *
3296:    * @return The current value of the property
3297:    */
3298:   public ListSelectionModel getSelectionModel()
3299:   {
3300:     //Neither Sun nor IBM returns null if rowSelection not allowed
3301:     return selectionModel;
3302:   }
3303:   
3304:   public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
3305:   {
3306:     if (orientation == SwingConstants.VERTICAL)
3307:       return visibleRect.height * direction;
3308:     else
3309:       return visibleRect.width * direction;
3310:   }
3311: 
3312:   /**
3313:    * Get the value of the <code>scrollableTracksViewportHeight</code> property.
3314:    *
3315:    * @return The constant value <code>false</code>
3316:    */
3317:   public boolean getScrollableTracksViewportHeight()
3318:   {
3319:     return false;
3320:   }
3321:   
3322:   /**
3323:    * Get the value of the <code>scrollableTracksViewportWidth</code> property.
3324:    *
3325:    * @return <code>true</code> unless the {@link #autoResizeMode} property is
3326:    * <code>AUTO_RESIZE_OFF</code>
3327:    */
3328:   public boolean getScrollableTracksViewportWidth()
3329:   {
3330:     if (autoResizeMode == AUTO_RESIZE_OFF)
3331:       return false;
3332:     else
3333:       return true;
3334:   }
3335:   
3336:   /**
3337:    * Return the preferred scrolling amount (in pixels) for the given scrolling
3338:    * direction and orientation. This method handles a partially exposed row by
3339:    * returning the distance required to completely expose the item. When
3340:    * scrolling the top item is completely exposed.
3341:    * 
3342:    * @param visibleRect the currently visible part of the component.
3343:    * @param orientation the scrolling orientation
3344:    * @param direction the scrolling direction (negative - up, positive -down).
3345:    *          The values greater than one means that more mouse wheel or similar
3346:    *          events were generated, and hence it is better to scroll the longer
3347:    *          distance.
3348:    * @author Audrius Meskauskas (audriusa@bioinformatics.org)
3349:    */
3350:   public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation,
3351:                                         int direction)
3352:   {
3353:     int h = (rowHeight + rowMargin);
3354:     int delta = h * direction;
3355: 
3356:     // Round so that the top would start from the row boundary
3357:     if (orientation == SwingConstants.VERTICAL)
3358:       {
3359:         // Completely expose the top row
3360:         int near = ((visibleRect.y + delta + h / 2) / h) * h;
3361:         int diff = visibleRect.y + delta - near;
3362:         delta -= diff;
3363:       }
3364:     return delta;
3365:     // TODO when scrollng horizontally, scroll into the column boundary.
3366:   }
3367: 
3368:   /**
3369:    * Get the cell editor, suitable for editing the given cell. The default
3370:    * method requests the editor from the column model. If the column model does
3371:    * not provide the editor, the call is forwarded to the
3372:    * {@link #getDefaultEditor(Class)} with the parameter, obtained from
3373:    * {@link TableModel#getColumnClass(int)}.
3374:    * 
3375:    * @param row the cell row
3376:    * @param column the cell column
3377:    * @return the editor to edit that cell
3378:    */
3379:   public TableCellEditor getCellEditor(int row, int column)
3380:   {
3381:     TableCellEditor editor = columnModel.getColumn(column).getCellEditor();
3382: 
3383:     if (editor == null)
3384:       {
3385:         int mcolumn = convertColumnIndexToModel(column);
3386:         editor = getDefaultEditor(dataModel.getColumnClass(mcolumn));
3387:       }
3388: 
3389:     return editor;
3390:   }
3391:   
3392:   /**
3393:    * Get the default editor for editing values of the given type
3394:    * (String, Boolean and so on).
3395:    * 
3396:    * @param columnClass the class of the value that will be edited.
3397:    * 
3398:    * @return the editor, suitable for editing this data type
3399:    */
3400:   public TableCellEditor getDefaultEditor(Class columnClass)
3401:   {
3402:     if (defaultEditorsByColumnClass.containsKey(columnClass))
3403:       return (TableCellEditor) defaultEditorsByColumnClass.get(columnClass);
3404:     else
3405:       {
3406:         JTextField t = new TableTextField();        
3407:         TableCellEditor r = new DefaultCellEditor(t);
3408:         defaultEditorsByColumnClass.put(columnClass, r);
3409:         return r;
3410:       }
3411:   }
3412:   
3413:   /**
3414:    * Get the cell renderer for rendering the given cell.
3415:    * 
3416:    * @param row the cell row
3417:    * @param column the cell column
3418:    * @return the cell renderer to render that cell.
3419:    */
3420:   public TableCellRenderer getCellRenderer(int row, int column)
3421:   {
3422:     TableCellRenderer renderer = columnModel.getColumn(column).getCellRenderer();
3423:     if (renderer == null)
3424:       {
3425:         int mcolumn = convertColumnIndexToModel(column);
3426:         renderer = getDefaultRenderer(dataModel.getColumnClass(mcolumn));
3427:       }
3428:     return renderer;
3429:   }
3430:   
3431:   /**
3432:    * Set default renderer for rendering the given data type.
3433:    * 
3434:    * @param columnClass the data type (String, Boolean and so on) that must be
3435:    *          rendered.
3436:    * @param rend the renderer that will rend this data type
3437:    */
3438:   public void setDefaultRenderer(Class columnClass, TableCellRenderer rend)
3439:   {
3440:     defaultRenderersByColumnClass.put(columnClass, rend);
3441:   }
3442:   
3443:   /**
3444:    * Get the default renderer for rendering the given data type.
3445:    * 
3446:    * @param columnClass the data that must be rendered
3447:    * 
3448:    * @return the appropriate defauld renderer for rendering that data type.
3449:    */
3450:   public TableCellRenderer getDefaultRenderer(Class columnClass)
3451:   {
3452:     if (defaultRenderersByColumnClass.containsKey(columnClass))
3453:       return (TableCellRenderer) defaultRenderersByColumnClass.get(columnClass);
3454:     else
3455:       {
3456:         TableCellRenderer r = new DefaultTableCellRenderer();
3457:         defaultRenderersByColumnClass.put(columnClass, r);
3458:         return r;
3459:       }
3460:   }
3461:   
3462:   /**
3463:    * Convert the table model index into the table column number.
3464:    * The model number need not match the real column position. The columns
3465:    * may be rearranged by the user with mouse at any time by dragging the
3466:    * column headers.
3467:    *
3468:    * @param vc the column number (0=first).
3469:    * 
3470:    * @return the table column model index of this column.
3471:    * 
3472:    * @see TableColumn#getModelIndex()
3473:    */
3474:   public int convertColumnIndexToModel(int vc)
3475:   {
3476:     if (vc < 0)
3477:       return vc;
3478:     else
3479:       return columnModel.getColumn(vc).getModelIndex();
3480:   }
3481:   
3482:   /**
3483:    * Convert the table column number to the table column model index.
3484:    * The model number need not match the real column position. The columns
3485:    * may be rearranged by the user with mouse at any time by dragging the
3486:    * column headers.
3487:    *  
3488:    * @param mc the table column index (0=first).
3489:    * 
3490:    * @return the table column number in the model
3491:    * 
3492:    * @see TableColumn#getModelIndex() 
3493:    */
3494:   public int convertColumnIndexToView(int mc)
3495:   {
3496:     if (mc < 0)
3497:       return mc;
3498:     int ncols = getColumnCount();
3499:     for (int vc = 0; vc < ncols; ++vc)
3500:       {
3501:         if (columnModel.getColumn(vc).getModelIndex() == mc)
3502:           return vc;
3503:       }
3504:     return -1;
3505:   }
3506:   
3507:   /**
3508:    * Prepare the renderer for rendering the given cell.
3509:    * 
3510:    * @param renderer the renderer being prepared
3511:    * @param row the row of the cell being rendered
3512:    * @param column the column of the cell being rendered
3513:    * 
3514:    * @return the component which .paint() method will paint the cell.
3515:    */
3516:   public Component prepareRenderer(TableCellRenderer renderer,
3517:                                    int row,
3518:                                    int column)
3519:   {
3520:     boolean rowSelAllowed = getRowSelectionAllowed();
3521:     boolean colSelAllowed = getColumnSelectionAllowed();
3522:     boolean isSel = false;
3523:     if (rowSelAllowed && colSelAllowed || !rowSelAllowed && !colSelAllowed)
3524:       isSel = isCellSelected(row, column);
3525:     else
3526:       isSel = isRowSelected(row) && getRowSelectionAllowed()
3527:            || isColumnSelected(column) && getColumnSelectionAllowed();
3528: 
3529:     // Determine the focused cell. The focused cell is the cell at the
3530:     // leadSelectionIndices of the row and column selection model.
3531:     ListSelectionModel rowSel = getSelectionModel();
3532:     ListSelectionModel colSel = getColumnModel().getSelectionModel();
3533:     boolean hasFocus = hasFocus() && isEnabled()
3534:                        && rowSel.getLeadSelectionIndex() == row
3535:                        && colSel.getLeadSelectionIndex() == column;
3536: 
3537:     return renderer.getTableCellRendererComponent(this,
3538:                                                   dataModel.getValueAt(row, 
3539:                                                convertColumnIndexToModel(column)),
3540:                                                   isSel,
3541:                                                   hasFocus,
3542:                                                   row, column);
3543:   }
3544: 
3545: 
3546:   /**
3547:    * Get the value of the {@link #autoCreateColumnsFromModel} property.
3548:    *
3549:    * @return The current value of the property
3550:    */
3551:   public boolean getAutoCreateColumnsFromModel()
3552:   {
3553:     return autoCreateColumnsFromModel;
3554:   }
3555: 
3556:   /**
3557:    * Get the value of the {@link #autoResizeMode} property.
3558:    *
3559:    * @return The current value of the property
3560:    */
3561:   public int getAutoResizeMode()
3562:   {
3563:     return autoResizeMode;
3564:   }
3565: 
3566:   /**
3567:    * Get the value of the {@link #rowHeight} property.
3568:    *
3569:    * @return The current value of the property
3570:    */
3571:   public int getRowHeight()
3572:   {
3573:     return rowHeight;
3574:   }
3575: 
3576:   /**
3577:    * Get the height of the specified row.
3578:    *
3579:    * @param row the row whose height to return
3580:    */
3581:   public int getRowHeight(int row)
3582:   {
3583:     int rh = rowHeight;
3584:     if (rowHeights != null)
3585:       rh = rowHeights.getSize(row);
3586:     return rh;
3587:   }
3588: 
3589: 
3590:   /**
3591:    * Get the value of the {@link #rowMargin} property.
3592:    *
3593:    * @return The current value of the property
3594:    */
3595:   public int getRowMargin()
3596:   {
3597:     return rowMargin;
3598:   }
3599: 
3600:   /**
3601:    * Get the value of the {@link #rowSelectionAllowed} property.
3602:    *
3603:    * @return The current value of the property
3604:    * 
3605:    * @see #setRowSelectionAllowed(boolean)
3606:    */
3607:   public boolean getRowSelectionAllowed()
3608:   {
3609:     return rowSelectionAllowed;
3610:   }
3611: 
3612:   /**
3613:    * Get the value of the {@link #cellSelectionEnabled} property.
3614:    *
3615:    * @return The current value of the property
3616:    */
3617:   public boolean getCellSelectionEnabled()
3618:   {
3619:     return getColumnSelectionAllowed() && getRowSelectionAllowed();
3620:   }
3621: 
3622:   /**
3623:    * Get the value of the {@link #dataModel} property.
3624:    *
3625:    * @return The current value of the property
3626:    */
3627:   public TableModel getModel()
3628:   {
3629:     return dataModel;
3630:   }
3631: 
3632:   /**
3633:    * Get the value of the <code>columnCount</code> property by
3634:    * delegation to the {@link #columnModel} field.
3635:    *
3636:    * @return The current value of the columnCount property
3637:    */
3638:   public int getColumnCount()
3639:   {
3640:     return columnModel.getColumnCount();    
3641:   }
3642: 
3643:   /**
3644:    * Get the value of the <code>rowCount</code> property by
3645:    * delegation to the {@link #dataModel} field.
3646:    *
3647:    * @return The current value of the rowCount property
3648:    */
3649:   public int getRowCount()
3650:   {
3651:     return dataModel.getRowCount();
3652:   }
3653: 
3654:   /**
3655:    * Get the value of the {@link #columnModel} property.
3656:    *
3657:    * @return The current value of the property
3658:    */
3659:   public TableColumnModel getColumnModel()
3660:   {
3661:     return columnModel;
3662:   }
3663: 
3664:   /**
3665:    * Get the value of the <code>selectedColumn</code> property by
3666:    * delegation to the {@link #columnModel} field.
3667:    *
3668:    * @return The current value of the selectedColumn property
3669:    */
3670:   public int getSelectedColumn()
3671:   {
3672:     return columnModel.getSelectionModel().getMinSelectionIndex();
3673:   }
3674: 
3675:   private static int countSelections(ListSelectionModel lsm)
3676:   {
3677:     int lo = lsm.getMinSelectionIndex();
3678:     int hi = lsm.getMaxSelectionIndex();
3679:     int sum = 0;
3680:     if (lo != -1 && hi != -1)
3681:       {
3682:         switch (lsm.getSelectionMode())
3683:           {
3684:           case ListSelectionModel.SINGLE_SELECTION:
3685:             sum = 1;
3686:             break;
3687:             
3688:           case ListSelectionModel.SINGLE_INTERVAL_SELECTION:
3689:             sum = hi - lo + 1;
3690:             break;
3691:             
3692:           case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION:        
3693:             for (int i = lo; i <= hi; ++i)
3694:               if (lsm.isSelectedIndex(i))        
3695:                 ++sum;
3696:             break;
3697:           }
3698:       }
3699:     return sum;
3700:   }
3701: 
3702:   private static int[] getSelections(ListSelectionModel lsm)
3703:   {
3704:     int sz = countSelections(lsm);
3705:     int [] ret = new int[sz];
3706: 
3707:     int lo = lsm.getMinSelectionIndex();
3708:     int hi = lsm.getMaxSelectionIndex();
3709:     int j = 0;
3710:     if (lo != -1 && hi != -1)
3711:       {
3712:         switch (lsm.getSelectionMode())
3713:           {
3714:           case ListSelectionModel.SINGLE_SELECTION:
3715:             ret[0] = lo;
3716:             break;      
3717:       
3718:           case ListSelectionModel.SINGLE_INTERVAL_SELECTION:            
3719:             for (int i = lo; i <= hi; ++i)
3720:               ret[j++] = i;
3721:             break;
3722:             
3723:           case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION:        
3724:             for (int i = lo; i <= hi; ++i)
3725:               if (lsm.isSelectedIndex(i))        
3726:                 ret[j++] = i;
3727:             break;
3728:           }
3729:       }
3730:     return ret;
3731:   }
3732: 
3733:   /**
3734:    * Get the value of the <code>selectedColumnCount</code> property by
3735:    * delegation to the {@link #columnModel} field.
3736:    *
3737:    * @return The current value of the selectedColumnCount property
3738:    */  
3739:   public int getSelectedColumnCount()
3740:   {
3741:     return countSelections(columnModel.getSelectionModel());
3742:   }
3743: 
3744:   /**
3745:    * Get the value of the <code>selectedColumns</code> property by
3746:    * delegation to the {@link #columnModel} field.
3747:    *
3748:    * @return The current value of the selectedColumns property
3749:    */
3750:   public int[] getSelectedColumns()
3751:   {
3752:     return getSelections(columnModel.getSelectionModel());
3753:   }
3754: 
3755:   /**
3756:    * Get the value of the <code>columnSelectionAllowed</code> property.
3757:    *
3758:    * @return The current value of the columnSelectionAllowed property
3759:    * 
3760:    * @see #setColumnSelectionAllowed(boolean)
3761:    */
3762:   public boolean getColumnSelectionAllowed()
3763:   {
3764:     return getColumnModel().getColumnSelectionAllowed();
3765:   }
3766: 
3767:   /**
3768:    * Get the value of the <code>selectedRowCount</code> property by
3769:    * delegation to the {@link #selectionModel} field.
3770:    *
3771:    * @return The current value of the selectedRowCount property
3772:    */
3773:   public int getSelectedRowCount()
3774:   {
3775:     return countSelections(selectionModel);
3776:   }
3777: 
3778:   /**
3779:    * Get the value of the <code>selectedRows</code> property by
3780:    * delegation to the {@link #selectionModel} field.
3781:    *
3782:    * @return The current value of the selectedRows property
3783:    */
3784:   public int[] getSelectedRows()
3785:   {
3786:     return getSelections(selectionModel);
3787:   }
3788: 
3789:   /**
3790:    * Get the value of the {@link #accessibleContext} property.
3791:    *
3792:    * @return The current value of the property
3793:    */
3794:   public AccessibleContext getAccessibleContext()
3795:   {
3796:     if (accessibleContext == null)
3797:       {
3798:         AccessibleJTable ctx = new AccessibleJTable();
3799:         addPropertyChangeListener(ctx);
3800:         TableColumnModel tcm = getColumnModel();
3801:         tcm.addColumnModelListener(ctx);
3802:         tcm.getSelectionModel().addListSelectionListener(ctx);
3803:         getSelectionModel().addListSelectionListener(ctx);
3804:         
3805:         accessibleContext = ctx;
3806:       }
3807:     return accessibleContext;
3808:   }
3809: 
3810:   /**
3811:    * Get the value of the {@link #cellEditor} property.
3812:    *
3813:    * @return The current value of the property
3814:    */
3815:   public TableCellEditor getCellEditor()
3816:   {
3817:     return cellEditor;
3818:   }
3819: 
3820:   /**
3821:    * Get the value of the {@link #dragEnabled} property.
3822:    *
3823:    * @return The current value of the property
3824:    */
3825:   public boolean getDragEnabled()
3826:   {
3827:     return dragEnabled;
3828:   }
3829: 
3830:   /**
3831:    * Get the value of the {@link #gridColor} property.
3832:    *
3833:    * @return The current value of the property
3834:    */
3835:   public Color getGridColor()
3836:   {
3837:     return gridColor;
3838:   }
3839: 
3840:   /**
3841:    * Get the value of the <code>intercellSpacing</code> property.
3842:    *
3843:    * @return The current value of the property
3844:    */
3845:   public Dimension getIntercellSpacing()
3846:   {
3847:     return new Dimension(columnModel.getColumnMargin(), rowMargin);
3848:   }
3849: 
3850:   /**
3851:    * Get the value of the {@link #preferredViewportSize} property.
3852:    *
3853:    * @return The current value of the property
3854:    */
3855:   public Dimension getPreferredScrollableViewportSize()
3856:   {
3857:     return preferredViewportSize;
3858:   }
3859: 
3860:   /**
3861:    * Get the value of the {@link #selectionBackground} property.
3862:    *
3863:    * @return The current value of the property
3864:    */
3865:   public Color getSelectionBackground()
3866:   {
3867:     return selectionBackground;
3868:   }
3869: 
3870:   /**
3871:    * Get the value of the {@link #selectionForeground} property.
3872:    *
3873:    * @return The current value of the property
3874:    */
3875:   public Color getSelectionForeground()
3876:   {
3877:     return selectionForeground;
3878:   }
3879: 
3880:   /**
3881:    * Get the value of the {@link #showHorizontalLines} property.
3882:    *
3883:    * @return The current value of the property
3884:    */
3885:   public boolean getShowHorizontalLines()
3886:   {
3887:     return showHorizontalLines;
3888:   }
3889: 
3890:   /**
3891:    * Get the value of the {@link #showVerticalLines} property.
3892:    *
3893:    * @return The current value of the property
3894:    */
3895:   public boolean getShowVerticalLines()
3896:   {
3897:     return showVerticalLines;
3898:   }
3899: 
3900:   /**
3901:    * Get the value of the {@link #tableHeader} property.
3902:    *
3903:    * @return The current value of the property
3904:    */
3905:   public JTableHeader getTableHeader()
3906:   {
3907:     return tableHeader;
3908:   }
3909: 
3910:   /**
3911:    * Removes specified column from displayable columns of this table.
3912:    *
3913:    * @param column column to removed
3914:    */
3915:   public void removeColumn(TableColumn column)
3916:   {    
3917:     columnModel.removeColumn(column);
3918:   }
3919: 
3920:   /**
3921:    * Moves column at the specified index to new given location.
3922:    *
3923:    * @param column index of the column to move
3924:    * @param targetColumn index specifying new location of the column
3925:    */ 
3926:   public void moveColumn(int column,int targetColumn) 
3927:   {
3928:     columnModel.moveColumn(column, targetColumn);
3929:   }
3930: 
3931:   /**
3932:    * Set the value of the {@link #autoCreateColumnsFromModel} flag.  If the
3933:    * flag changes from <code>false</code> to <code>true</code>, the
3934:    * {@link #createDefaultColumnsFromModel()} method is called.
3935:    *
3936:    * @param autoCreate  the new value of the flag.
3937:    */ 
3938:   public void setAutoCreateColumnsFromModel(boolean autoCreate)
3939:   {
3940:     if (autoCreateColumnsFromModel != autoCreate)
3941:     {
3942:       autoCreateColumnsFromModel = autoCreate;
3943:       if (autoCreate)
3944:         createDefaultColumnsFromModel();
3945:     }
3946:   }
3947: 
3948:   /**
3949:    * Set the value of the {@link #autoResizeMode} property.
3950:    *
3951:    * @param a The new value of the autoResizeMode property
3952:    */ 
3953:   public void setAutoResizeMode(int a)
3954:   {
3955:     autoResizeMode = a;
3956:     revalidate();
3957:     repaint();
3958:   }
3959: 
3960:   /**
3961:    * Sets the height for all rows in the table. If you want to change the
3962:    * height of a single row instead, use {@link #setRowHeight(int, int)}.
3963:    *
3964:    * @param r the height to set for all rows
3965:    *
3966:    * @see #getRowHeight()
3967:    * @see #setRowHeight(int, int)
3968:    * @see #getRowHeight(int)
3969:    */ 
3970:   public void setRowHeight(int r)
3971:   {
3972:     if (r < 1)
3973:       throw new IllegalArgumentException();
3974: 
3975:     clientRowHeightSet = true;
3976: 
3977:     rowHeight = r;
3978:     rowHeights = null;
3979:     revalidate();
3980:     repaint();
3981:   }
3982:   
3983:   /**
3984:    * Sets the height of a single row in the table.
3985:    * 
3986:    * @param rh the new row height
3987:    * @param row the row to change the height of
3988:    */
3989:   public void setRowHeight(int row, int rh)
3990:   {
3991:     if (rowHeights == null)
3992:       {
3993:         rowHeights = new SizeSequence(getRowCount(), rowHeight);
3994:       }
3995:     rowHeights.setSize(row, rh);
3996:   }
3997:   
3998:   /**
3999:    * Set the value of the {@link #rowMargin} property.
4000:    *
4001:    * @param r The new value of the rowMargin property
4002:    */ 
4003:   public void setRowMargin(int r)
4004:   {
4005:     rowMargin = r;
4006:     revalidate();
4007:     repaint();
4008:   }
4009: 
4010:   /**
4011:    * Set the value of the {@link #rowSelectionAllowed} property.
4012:    *
4013:    * @param r The new value of the rowSelectionAllowed property
4014:    * 
4015:    * @see #getRowSelectionAllowed()
4016:    */ 
4017:   public void setRowSelectionAllowed(boolean r)
4018:   {
4019:     if (rowSelectionAllowed != r) 
4020:       {
4021:         rowSelectionAllowed = r;
4022:         firePropertyChange("rowSelectionAllowed", !r, r);
4023:         repaint();
4024:       }
4025:   }
4026: 
4027:   /**
4028:    * Set the value of the {@link #cellSelectionEnabled} property.
4029:    *
4030:    * @param c The new value of the cellSelectionEnabled property
4031:    */ 
4032:   public void setCellSelectionEnabled(boolean c)
4033:   {
4034:     setColumnSelectionAllowed(c);
4035:     setRowSelectionAllowed(c);
4036:     // for backward-compatibility sake:
4037:     cellSelectionEnabled = true;
4038:   }
4039: 
4040:   /**
4041:    * <p>Set the value of the {@link #dataModel} property.</p>
4042:    *
4043:    * <p>Unregister <code>this</code> as a {@link TableModelListener} from
4044:    * previous {@link #dataModel} and register it with new parameter
4045:    * <code>m</code>.</p>
4046:    *
4047:    * @param m The new value of the model property
4048:    */ 
4049:   public void setModel(TableModel m)
4050:   {
4051:     // Throw exception is m is null.
4052:     if (m == null)
4053:       throw new IllegalArgumentException();
4054:    
4055:     // Don't do anything if setting the current model again.
4056:     if (dataModel == m)
4057:       return;
4058: 
4059:     TableModel oldModel = dataModel;
4060: 
4061:     // Remove table as TableModelListener from old model.
4062:     if (dataModel != null)
4063:       dataModel.removeTableModelListener(this);
4064:     
4065:     if (m != null)
4066:       {
4067:         // Set property.
4068:         dataModel = m;
4069: 
4070:         // Add table as TableModelListener to new model.
4071:         dataModel.addTableModelListener(this);
4072: 
4073:         // Notify the tableChanged method.
4074:         tableChanged(new TableModelEvent(dataModel,
4075:                                          TableModelEvent.HEADER_ROW));
4076: 
4077:         // Automatically create columns.
4078:         if (autoCreateColumnsFromModel)
4079:           createDefaultColumnsFromModel();
4080:       }
4081: 
4082:     // This property is bound, so we fire a property change event.
4083:     firePropertyChange("model", oldModel, dataModel);
4084: 
4085:     // Repaint table.
4086:     revalidate();
4087:     repaint();
4088:   }
4089: 
4090:   /**
4091:    * <p>Set the value of the {@link #columnModel} property.</p>
4092:    *
4093:    * <p>Unregister <code>this</code> as a {@link TableColumnModelListener}
4094:    * from previous {@link #columnModel} and register it with new parameter
4095:    * <code>c</code>.</p>
4096:    *
4097:    * @param c The new value of the columnModel property
4098:    */ 
4099:   public void setColumnModel(TableColumnModel c)
4100:   {
4101:     if (c == null)
4102:       throw new IllegalArgumentException();
4103:     TableColumnModel tmp = columnModel;
4104:     if (tmp != null)
4105:       tmp.removeColumnModelListener(this);
4106:     if (c != null)
4107:       c.addColumnModelListener(this);
4108:     columnModel = c;
4109:     if (dataModel != null && columnModel != null)
4110:       {
4111:         int ncols = getColumnCount();
4112:         TableColumn column;
4113:         for (int i = 0; i < ncols; ++i)
4114:           {
4115:             column = columnModel.getColumn(i); 
4116:             if (column.getHeaderValue()==null)
4117:               column.setHeaderValue(dataModel.getColumnName(i));
4118:           }
4119:       }
4120: 
4121:     // according to Sun's spec we also have to set the tableHeader's
4122:     // column model here
4123:     if (tableHeader != null)
4124:       tableHeader.setColumnModel(c);
4125: 
4126:     revalidate();
4127:     repaint();
4128:   }
4129: 
4130:   /**
4131:    * Set the value of the <code>columnSelectionAllowed</code> property.
4132:    *
4133:    * @param c The new value of the property
4134:    * 
4135:    * @see #getColumnSelectionAllowed()
4136:    */ 
4137:   public void setColumnSelectionAllowed(boolean c)
4138:   {
4139:     if (columnModel.getColumnSelectionAllowed() != c)
4140:       {
4141:         columnModel.setColumnSelectionAllowed(c);
4142:         firePropertyChange("columnSelectionAllowed", !c, c);
4143:         repaint();
4144:       }
4145:   }
4146: 
4147:   /**
4148:    * <p>Set the value of the {@link #selectionModel} property.</p>
4149:    *
4150:    * <p>Unregister <code>this</code> as a {@link ListSelectionListener}
4151:    * from previous {@link #selectionModel} and register it with new
4152:    * parameter <code>s</code>.</p>
4153:    *
4154:    * @param s The new value of the selectionModel property
4155:    */ 
4156:   public void setSelectionModel(ListSelectionModel s)
4157:   {
4158:     if (s == null)
4159:       throw new IllegalArgumentException();
4160:     ListSelectionModel tmp = selectionModel;
4161:     if (tmp != null)
4162:       tmp.removeListSelectionListener(this);
4163:     if (s != null)
4164:       s.addListSelectionListener(this);
4165:     selectionModel = s;
4166:     checkSelection();
4167:   }
4168: 
4169:   /**
4170:    * Set the value of the <code>selectionMode</code> property by
4171:    * delegation to the {@link #selectionModel} field. The same selection
4172:    * mode is set for row and column selection models.
4173:    *
4174:    * @param s The new value of the property
4175:    */ 
4176:   public void setSelectionMode(int s)
4177:   { 
4178:     selectionModel.setSelectionMode(s);    
4179:     columnModel.getSelectionModel().setSelectionMode(s);
4180:     
4181:     repaint();
4182:   }
4183: 
4184:   /**
4185:    * <p>Set the value of the {@link #cellEditor} property.</p>
4186:    *
4187:    * <p>Unregister <code>this</code> as a {@link CellEditorListener} from
4188:    * previous {@link #cellEditor} and register it with new parameter
4189:    * <code>c</code>.</p>
4190:    *
4191:    * @param c The new value of the cellEditor property
4192:    */ 
4193:   public void setCellEditor(TableCellEditor c)
4194:   {
4195:     TableCellEditor tmp = cellEditor;
4196:     if (tmp != null)
4197:       tmp.removeCellEditorListener(this);
4198:     if (c != null)
4199:       c.addCellEditorListener(this);
4200:     cellEditor = c;
4201:   }
4202: 
4203:   /**
4204:    * Set the value of the {@link #dragEnabled} property.
4205:    *
4206:    * @param d The new value of the dragEnabled property
4207:    */ 
4208:   public void setDragEnabled(boolean d)
4209:   {
4210:     dragEnabled = d;
4211:   }
4212: 
4213:   /**
4214:    * Set the value of the {@link #gridColor} property.
4215:    *
4216:    * @param g The new value of the gridColor property
4217:    */ 
4218:   public void setGridColor(Color g)
4219:   {
4220:     gridColor = g;
4221:     repaint();
4222:   }
4223: 
4224:   /**
4225:    * Set the value of the <code>intercellSpacing</code> property.
4226:    *
4227:    * @param i The new value of the intercellSpacing property
4228:    */ 
4229:   public void setIntercellSpacing(Dimension i)
4230:   {
4231:     rowMargin = i.height;
4232:     columnModel.setColumnMargin(i.width);
4233:     repaint();
4234:   }
4235: 
4236:   /**
4237:    * Set the value of the {@link #preferredViewportSize} property.
4238:    *
4239:    * @param p The new value of the preferredViewportSize property
4240:    */ 
4241:   public void setPreferredScrollableViewportSize(Dimension p)
4242:   {
4243:     preferredViewportSize = p;
4244:     revalidate();
4245:     repaint();
4246:   }
4247: 
4248:   /**
4249:    * <p>Set the value of the {@link #selectionBackground} property.</p>
4250:    *
4251:    * <p>Fire a PropertyChangeEvent with name {@link
4252:    * #SELECTION_BACKGROUND_CHANGED_PROPERTY} to registered listeners, if
4253:    * selectionBackground changed.</p>
4254:    *
4255:    * @param s The new value of the selectionBackground property
4256:    */ 
4257:   public void setSelectionBackground(Color s)
4258:   {
4259:     Color tmp = selectionBackground;
4260:     selectionBackground = s;
4261:     if (((tmp == null && s != null)
4262:          || (s == null && tmp != null)
4263:          || (tmp != null && s != null && !tmp.equals(s))))
4264:       firePropertyChange(SELECTION_BACKGROUND_CHANGED_PROPERTY, tmp, s);
4265:     repaint();
4266:   }
4267: 
4268:   /**
4269:    * <p>Set the value of the {@link #selectionForeground} property.</p>
4270:    *
4271:    * <p>Fire a PropertyChangeEvent with name {@link
4272:    * #SELECTION_FOREGROUND_CHANGED_PROPERTY} to registered listeners, if
4273:    * selectionForeground changed.</p>
4274:    *
4275:    * @param s The new value of the selectionForeground property
4276:    */ 
4277:   public void setSelectionForeground(Color s)
4278:   {
4279:     Color tmp = selectionForeground;
4280:     selectionForeground = s;
4281:     if (((tmp == null && s != null)
4282:          || (s == null && tmp != null)
4283:          || (tmp != null && s != null && !tmp.equals(s))))
4284:       firePropertyChange(SELECTION_FOREGROUND_CHANGED_PROPERTY, tmp, s);
4285:     repaint();
4286:   }
4287: 
4288:   /**
4289:    * Set the value of the <code>showGrid</code> property.
4290:    *
4291:    * @param s The new value of the showGrid property
4292:    */ 
4293:   public void setShowGrid(boolean s)
4294:   {
4295:     setShowVerticalLines(s);
4296:     setShowHorizontalLines(s);
4297:   }
4298: 
4299:   /**
4300:    * Set the value of the {@link #showHorizontalLines} property.
4301:    *
4302:    * @param s The new value of the showHorizontalLines property
4303:    */ 
4304:   public void setShowHorizontalLines(boolean s)
4305:   {
4306:     showHorizontalLines = s;
4307:     repaint();
4308:   }
4309: 
4310:   /**
4311:    * Set the value of the {@link #showVerticalLines} property.
4312:    *
4313:    * @param s The new value of the showVerticalLines property
4314:    */ 
4315:   public void setShowVerticalLines(boolean s)
4316:   {
4317:     showVerticalLines = s;
4318:     repaint();
4319:   }
4320: 
4321:   /**
4322:    * Set the value of the {@link #tableHeader} property.
4323:    *
4324:    * @param t The new value of the tableHeader property
4325:    */ 
4326:   public void setTableHeader(JTableHeader t)
4327:   {
4328:     if (tableHeader != null)
4329:       tableHeader.setTable(null);
4330:     tableHeader = t;
4331:     if (tableHeader != null)
4332:       tableHeader.setTable(this);
4333:     revalidate();
4334:     repaint();
4335:   }
4336: 
4337:   protected void configureEnclosingScrollPane()
4338:   {
4339:     JScrollPane jsp = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, this);
4340:     if (jsp != null && tableHeader != null)
4341:       {
4342:         jsp.setColumnHeaderView(tableHeader);
4343:       }
4344:   }
4345: 
4346:   protected void unconfigureEnclosingScrollPane()
4347:   {
4348:     JScrollPane jsp = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, this);
4349:     if (jsp != null)
4350:       {
4351:         jsp.setColumnHeaderView(null);
4352:       }    
4353:   }
4354: 
4355: 
4356:   public void addNotify()
4357:   {
4358:     super.addNotify();
4359:     configureEnclosingScrollPane();
4360:   }
4361: 
4362:   public void removeNotify()
4363:   {
4364:     super.addNotify();
4365:     unconfigureEnclosingScrollPane();
4366:   }
4367: 
4368: 
4369:   /**
4370:    * This distributes the superfluous width in a table evenly on its columns.
4371:    *
4372:    * The implementation used here is different to that one described in
4373:    * the JavaDocs. It is much simpler, and seems to work very well.
4374:    *
4375:    * TODO: correctly implement the algorithm described in the JavaDoc
4376:    */
4377:   private void distributeSpill(TableColumn[] cols, int spill)
4378:   {
4379:     int average = spill / cols.length;
4380:     for (int i = 0; i < cols.length; i++)
4381:       {
4382:         if (cols[i] != null)
4383:           cols[i].setWidth(cols[i].getPreferredWidth() + average);
4384:       }
4385:   }
4386:   
4387:   /**
4388:    * This distributes the superfluous width in a table, setting the width of the
4389:    * column being resized strictly to its preferred width.
4390:    */
4391:   private void distributeSpillResizing(TableColumn[] cols, int spill,
4392:                                        TableColumn resizeIt)
4393:   {
4394:     int average = 0;
4395:     if (cols.length != 1)
4396:       average = spill / (cols.length-1);
4397:     for (int i = 0; i < cols.length; i++)
4398:       {
4399:         if (cols[i] != null && !cols[i].equals(resizeIt))
4400:           cols[i].setWidth(cols[i].getPreferredWidth() + average);
4401:       }
4402:     resizeIt.setWidth(resizeIt.getPreferredWidth());
4403:   }  
4404:   
4405:   /**
4406:    * Set the widths of all columns, taking they preferred widths into
4407:    * consideration. The excess space, if any, will be distrubuted between
4408:    * all columns. This method also handles special cases when one of the
4409:    * collumns is currently being resized.
4410:    * 
4411:    * @see TableColumn#setPreferredWidth(int)
4412:    */
4413:   public void doLayout()
4414:   {
4415:     TableColumn resizingColumn = null;
4416:     
4417:     int ncols = getColumnCount();
4418:     if (ncols < 1)
4419:       return;
4420: 
4421:     int prefSum = 0;
4422:     int rCol = -1;
4423: 
4424:     if (tableHeader != null)
4425:       resizingColumn = tableHeader.getResizingColumn();
4426: 
4427:     for (int i = 0; i < ncols; ++i)
4428:       {
4429:         TableColumn col = columnModel.getColumn(i);
4430:         int p = col.getPreferredWidth();
4431:         prefSum += p;
4432:         if (resizingColumn == col)
4433:           rCol = i;
4434:       }
4435: 
4436:     int spill = getWidth() - prefSum;
4437: 
4438:     if (resizingColumn != null)
4439:       {
4440:         TableColumn col;
4441:         TableColumn [] cols;
4442:         
4443:         switch (getAutoResizeMode())
4444:           {
4445:           case AUTO_RESIZE_LAST_COLUMN:
4446:             col = columnModel.getColumn(ncols-1);
4447:             col.setWidth(col.getPreferredWidth() + spill);
4448:             break;
4449:             
4450:           case AUTO_RESIZE_NEXT_COLUMN:
4451:             col = columnModel.getColumn(ncols-1);
4452:             col.setWidth(col.getPreferredWidth() + spill);
4453:             break;
4454: 
4455:           case AUTO_RESIZE_ALL_COLUMNS:
4456:             cols = new TableColumn[ncols];
4457:             for (int i = 0; i < ncols; ++i)
4458:               cols[i] = columnModel.getColumn(i);
4459:             distributeSpillResizing(cols, spill, resizingColumn);
4460:             break;
4461: 
4462:           case AUTO_RESIZE_SUBSEQUENT_COLUMNS:
4463:             
4464:             // Subtract the width of the non-resized columns from the spill.
4465:             int w = 0;
4466:             int wp = 0;
4467:             TableColumn column;
4468:             for (int i = 0; i < rCol; i++)
4469:               {
4470:                 column = columnModel.getColumn(i);
4471:                 w += column.getWidth();
4472:                 wp+= column.getPreferredWidth();
4473:               }
4474: 
4475:             // The number of columns right from the column being resized.
4476:             int n = ncols-rCol-1;
4477:             if (n>0)
4478:               {
4479:                 // If there are any columns on the right sied to resize.
4480:                 spill = (getWidth()-w) - (prefSum-wp);
4481:                 int average = spill / n;
4482:             
4483:                  // For all columns right from the column being resized:
4484:                 for (int i = rCol+1; i < ncols; i++)
4485:                   {
4486:                     column = columnModel.getColumn(i);
4487:                     column.setWidth(column.getPreferredWidth() + average);
4488:                   }
4489:               }
4490:             resizingColumn.setWidth(resizingColumn.getPreferredWidth());
4491:             break;
4492: 
4493:           case AUTO_RESIZE_OFF:
4494:           default:
4495:             int prefWidth = resizingColumn.getPreferredWidth();
4496:             resizingColumn.setWidth(prefWidth);
4497:           }
4498:       }
4499:     else
4500:       {
4501:         TableColumn [] cols = new TableColumn[ncols];
4502:         for (int i = 0; i < ncols; ++i)
4503:           cols[i] = columnModel.getColumn(i);
4504:         distributeSpill(cols, spill);
4505:       }
4506:     
4507:     if (editorComp!=null)
4508:       moveToCellBeingEdited(editorComp);
4509:     
4510:     int leftBoundary = getLeftResizingBoundary();
4511:     int width = getWidth() - leftBoundary;
4512:     repaint(leftBoundary, 0, width, getHeight());
4513:     if (tableHeader != null)
4514:       tableHeader.repaint(leftBoundary, 0, width, tableHeader.getHeight());
4515:   }
4516:   
4517:   /**
4518:    * Get the left boundary of the rectangle which changes during the column
4519:    * resizing.
4520:    */
4521:   int getLeftResizingBoundary()
4522:   {
4523:     if (tableHeader == null || getAutoResizeMode() == AUTO_RESIZE_ALL_COLUMNS)
4524:       return 0;
4525:     else
4526:       {
4527:         TableColumn resizingColumn = tableHeader.getResizingColumn();
4528:         if (resizingColumn == null)
4529:           return 0;
4530: 
4531:         int rc = convertColumnIndexToView(resizingColumn.getModelIndex());
4532:         int p = 0;
4533: 
4534:         for (int i = 0; i < rc; i++)
4535:           p += columnModel.getColumn(i).getWidth();
4536:         
4537:         return p;
4538:       }
4539:   }
4540:   
4541:   
4542:   /**
4543:    * @deprecated Replaced by <code>doLayout()</code>
4544:    */
4545:   public void sizeColumnsToFit(boolean lastColumnOnly)
4546:   {
4547:     doLayout();
4548:   }
4549:   
4550:   /**
4551:    * Obsolete since JDK 1.4. Please use <code>doLayout()</code>.
4552:    */
4553:   public void sizeColumnsToFit(int resizingColumn)
4554:   {
4555:     doLayout();
4556:   }
4557: 
4558:   public String getUIClassID()
4559:   {
4560:     return "TableUI";
4561:   }
4562: 
4563:   /**
4564:    * This method returns the table's UI delegate.
4565:    *
4566:    * @return The table's UI delegate.
4567:    */
4568:   public TableUI getUI()
4569:   {
4570:     return (TableUI) ui;
4571:   }
4572: 
4573:   /**
4574:    * This method sets the table's UI delegate.
4575:    *
4576:    * @param ui The table's UI delegate.
4577:    */
4578:   public void setUI(TableUI ui)
4579:   {
4580:     super.setUI(ui);
4581:     // The editors and renderers must be recreated because they constructors
4582:     // may use the look and feel properties.
4583:     createDefaultEditors();
4584:     createDefaultRenderers();
4585:   }
4586: 
4587:   public void updateUI()
4588:   {
4589:     setUI((TableUI) UIManager.getUI(this));
4590:   }
4591:   
4592:   /**
4593:    * Get the class (datatype) of the column. The cells are rendered and edited
4594:    * differently, depending from they data type.
4595:    * 
4596:    * @param column the column (not the model index).
4597:    * 
4598:    * @return the class, defining data type of that column (String.class for
4599:    * String, Boolean.class for boolean and so on).
4600:    */
4601:   public Class getColumnClass(int column)
4602:   {
4603:     return getModel().getColumnClass(convertColumnIndexToModel(column));
4604:   }
4605:   
4606:   /**
4607:    * Get the name of the column. If the column has the column identifier set,
4608:    * the return value is the result of the .toString() method call on that
4609:    * identifier. If the identifier is not explicitly set, the returned value
4610:    * is calculated by 
4611:    * {@link javax.swing.table.AbstractTableModel#getColumnName(int)}.
4612:    * 
4613:    * @param column the column
4614:    * 
4615:    * @return the name of that column.
4616:    */
4617:   public String getColumnName(int column)
4618:   {
4619:     int modelColumn = columnModel.getColumn(column).getModelIndex();
4620:     return dataModel.getColumnName(modelColumn);
4621:   }
4622:   
4623:   /**
4624:    * Get the column, currently being edited
4625:    * 
4626:    * @return the column, currently being edited.
4627:    */
4628:   public int getEditingColumn()
4629:   {
4630:     return editingColumn;
4631:   }
4632:   
4633:   /**
4634:    * Set the column, currently being edited
4635:    * 
4636:    * @param column the column, currently being edited.
4637:    */
4638:   public void setEditingColumn(int column)
4639:   {
4640:     editingColumn = column;
4641:   }
4642:   
4643:   /**
4644:    * Get the row currently being edited.
4645:    * 
4646:    * @return the row, currently being edited.
4647:    */
4648:   public int getEditingRow()
4649:   {
4650:     return editingRow;
4651:   }
4652:   
4653:   /**
4654:    * Set the row currently being edited.
4655:    * 
4656:    * @param row the row, that will be edited
4657:    */
4658:   public void setEditingRow(int row)
4659:   {
4660:     editingRow = row;
4661:   }
4662:   
4663:   /**
4664:    * Get the editor component that is currently editing one of the cells
4665:    * 
4666:    * @return the editor component or null, if none of the cells is being
4667:    * edited.
4668:    */
4669:   public Component getEditorComponent()
4670:   {
4671:     return editorComp;
4672:   }
4673:   
4674:   /**
4675:    * Check if one of the table cells is currently being edited.
4676:    * 
4677:    * @return true if there is a cell being edited.
4678:    */
4679:   public boolean isEditing()
4680:   {
4681:     return editorComp != null;
4682:   }
4683:   
4684:   /**
4685:    * Set the default editor for the given column class (column data type).
4686:    * By default, String is handled by text field and Boolean is handled by
4687:    * the check box.
4688:    *  
4689:    * @param columnClass the column data type
4690:    * @param editor the editor that will edit this data type
4691:    * 
4692:    * @see TableModel#getColumnClass(int)
4693:    */
4694:   public void setDefaultEditor(Class columnClass, TableCellEditor editor)
4695:   {
4696:     if (editor != null)
4697:       defaultEditorsByColumnClass.put(columnClass, editor);
4698:     else
4699:       defaultEditorsByColumnClass.remove(columnClass);
4700:   }
4701:   
4702:   public void addColumnSelectionInterval(int index0, int index1)
4703:   {
4704:     if ((index0 < 0 || index0 > (getColumnCount()-1)
4705:          || index1 < 0 || index1 > (getColumnCount()-1)))
4706:       throw new IllegalArgumentException("Column index out of range.");
4707:     
4708:     getColumnModel().getSelectionModel().addSelectionInterval(index0, index1);
4709:   }
4710:   
4711:   public void addRowSelectionInterval(int index0, int index1)
4712:   {            
4713:     if ((index0 < 0 || index0 > (getRowCount()-1)
4714:          || index1 < 0 || index1 > (getRowCount()-1)))
4715:       throw new IllegalArgumentException("Row index out of range.");
4716:           
4717:     getSelectionModel().addSelectionInterval(index0, index1);
4718:   }
4719:   
4720:   public void setColumnSelectionInterval(int index0, int index1)
4721:   {
4722:     if ((index0 < 0 || index0 > (getColumnCount()-1)
4723:          || index1 < 0 || index1 > (getColumnCount()-1)))
4724:       throw new IllegalArgumentException("Column index out of range.");
4725: 
4726:     getColumnModel().getSelectionModel().setSelectionInterval(index0, index1);
4727:   }
4728:   
4729:   public void setRowSelectionInterval(int index0, int index1)
4730:   {    
4731:     if ((index0 < 0 || index0 > (getRowCount()-1)
4732:          || index1 < 0 || index1 > (getRowCount()-1)))
4733:       throw new IllegalArgumentException("Row index out of range.");
4734: 
4735:     getSelectionModel().setSelectionInterval(index0, index1);
4736:   }
4737:   
4738:   public void removeColumnSelectionInterval(int index0, int index1)  
4739:   {
4740:     if ((index0 < 0 || index0 > (getColumnCount()-1)
4741:          || index1 < 0 || index1 > (getColumnCount()-1)))
4742:       throw new IllegalArgumentException("Column index out of range.");
4743: 
4744:     getColumnModel().getSelectionModel().removeSelectionInterval(index0, index1);
4745:   }
4746:   
4747:   public void removeRowSelectionInterval(int index0, int index1)
4748:   {
4749:     if ((index0 < 0 || index0 > (getRowCount()-1)
4750:          || index1 < 0 || index1 > (getRowCount()-1)))
4751:       throw new IllegalArgumentException("Row index out of range.");
4752: 
4753:     getSelectionModel().removeSelectionInterval(index0, index1);
4754:   }
4755:   
4756:   /**
4757:    * Checks if the given column is selected.
4758:    * 
4759:    * @param column the column
4760:    * 
4761:    * @return true if the column is selected (as reported by the selection
4762:    * model, associated with the column model), false otherwise.
4763:    */
4764:   public boolean isColumnSelected(int column)
4765:   {
4766:     return getColumnModel().getSelectionModel().isSelectedIndex(column);
4767:   }
4768:   
4769:   /**
4770:    * Checks if the given row is selected.
4771:    * 
4772:    * @param row the row
4773:    * 
4774:    * @return true if the row is selected (as reported by the selection model),
4775:    * false otherwise.
4776:    */
4777:   public boolean isRowSelected(int row)
4778:   {
4779:     return getSelectionModel().isSelectedIndex(row);
4780:   }
4781:   
4782:   /**
4783:    * Checks if the given cell is selected. The cell is selected if both
4784:    * the cell row and the cell column are selected.
4785:    * 
4786:    * @param row the cell row
4787:    * @param column the cell column
4788:    * 
4789:    * @return true if the cell is selected, false otherwise
4790:    */
4791:   public boolean isCellSelected(int row, int column)
4792:   {
4793:     return isRowSelected(row) && isColumnSelected(column);
4794:   }
4795:   
4796:   /**
4797:    * Select all table.
4798:    */
4799:   public void selectAll()
4800:   {
4801:     // The table is empty - nothing to do!
4802:     if (getRowCount() == 0 || getColumnCount() == 0)
4803:       return;
4804:     
4805:     // rowLead and colLead store the current lead selection indices
4806:     int rowLead = selectionModel.getLeadSelectionIndex();
4807:     int colLead = getColumnModel().getSelectionModel().getLeadSelectionIndex();
4808:     // the following calls to setSelectionInterval change the lead selection
4809:     // indices
4810:     setColumnSelectionInterval(0, getColumnCount() - 1);
4811:     setRowSelectionInterval(0, getRowCount() - 1);
4812:     // the following addSelectionInterval calls restore the lead selection
4813:     // indices to their previous values
4814:     addColumnSelectionInterval(colLead,colLead);
4815:     addRowSelectionInterval(rowLead, rowLead);
4816:   }
4817:   
4818:   /**
4819:    * Get the cell value at the given position.
4820:    * 
4821:    * @param row the row to get the value
4822:    * @param column the actual column number (not the model index) 
4823:    * to get the value.
4824:    * 
4825:    * @return the cell value, as returned by model.
4826:    */
4827:   public Object getValueAt(int row, int column)
4828:   {
4829:     return dataModel.getValueAt(row, convertColumnIndexToModel(column));
4830:   }
4831:   
4832:   /**
4833:    * Set value for the cell at the given position. The modified cell is
4834:    * repainted.
4835:    * 
4836:    * @param value the value to set
4837:    * @param row the row of the cell being modified
4838:    * @param column the column of the cell being modified
4839:    */
4840:   public void setValueAt(Object value, int row, int column)
4841:   {
4842:     dataModel.setValueAt(value, row, convertColumnIndexToModel(column));
4843:     
4844:     repaint(getCellRect(row, column, true));
4845:   }
4846:   
4847:   /**
4848:    * Get table column with the given identified.
4849:    * 
4850:    * @param identifier the column identifier
4851:    * 
4852:    * @return the table column with this identifier
4853:    * 
4854:    * @throws IllegalArgumentException if <code>identifier</code> is 
4855:    *         <code>null</code> or there is no column with that identifier.
4856:    * 
4857:    * @see TableColumn#setIdentifier(Object)
4858:    */
4859:   public TableColumn getColumn(Object identifier)
4860:   {
4861:     return columnModel.getColumn(columnModel.getColumnIndex(identifier));
4862:   }
4863: 
4864:   /**
4865:    * Returns <code>true</code> if the specified cell is editable, and
4866:    * <code>false</code> otherwise.
4867:    *
4868:    * @param row  the row index.
4869:    * @param column  the column index.
4870:    *
4871:    * @return true if the cell is editable, false otherwise.
4872:    */
4873:   public boolean isCellEditable(int row, int column)
4874:   {
4875:     return dataModel.isCellEditable(row, convertColumnIndexToModel(column));
4876:   }
4877: 
4878:   /**
4879:    * Clears any existing columns from the <code>JTable</code>'s
4880:    * {@link TableColumnModel} and creates new columns to match the values in
4881:    * the data ({@link TableModel}) used by the table.
4882:    *
4883:    * @see #setAutoCreateColumnsFromModel(boolean)
4884:    */
4885:   public void createDefaultColumnsFromModel()
4886:   {
4887:     assert columnModel != null : "The columnModel must not be null.";
4888: 
4889:     // remove existing columns
4890:     int columnIndex = columnModel.getColumnCount() - 1;
4891:     while (columnIndex >= 0)
4892:     {
4893:       columnModel.removeColumn(columnModel.getColumn(columnIndex));
4894:       columnIndex--;
4895:     }
4896:   
4897:     // add new columns to match the TableModel
4898:     int columnCount = dataModel.getColumnCount();
4899:     for (int c = 0; c < columnCount; c++)
4900:     {
4901:       TableColumn column = new TableColumn(c);
4902:       column.setIdentifier(dataModel.getColumnName(c));
4903:       column.setHeaderValue(dataModel.getColumnName(c));
4904:       columnModel.addColumn(column);
4905:       column.addPropertyChangeListener(tableColumnPropertyChangeHandler);
4906:     }
4907:   }
4908: 
4909:   public void changeSelection (int rowIndex, int columnIndex, boolean toggle, boolean extend)
4910:   {
4911:     if (toggle && extend)
4912:       {
4913:         // Leave the selection state as is, but move the anchor
4914:         //   index to the specified location
4915:         selectionModel.setAnchorSelectionIndex(rowIndex);
4916:         getColumnModel().getSelectionModel().setAnchorSelectionIndex(columnIndex);
4917:       }
4918:     else if (toggle)
4919:       {
4920:         // Toggle the state of the specified cell
4921:         if (isCellSelected(rowIndex,columnIndex))
4922:           {
4923:             selectionModel.removeSelectionInterval(rowIndex,rowIndex);
4924:             getColumnModel().getSelectionModel().removeSelectionInterval(columnIndex,columnIndex);
4925:           }
4926:         else
4927:           {
4928:             selectionModel.addSelectionInterval(rowIndex,rowIndex);
4929:             getColumnModel().getSelectionModel().addSelectionInterval(columnIndex,columnIndex);
4930:           }
4931:       }
4932:     else if (extend)
4933:       {
4934:         // Extend the previous selection from the anchor to the 
4935:         // specified cell, clearing all other selections
4936:         selectionModel.setLeadSelectionIndex(rowIndex);
4937:         getColumnModel().getSelectionModel().setLeadSelectionIndex(columnIndex);
4938:       }
4939:     else
4940:       {
4941:         // Clear the previous selection and ensure the new cell
4942:         // is selected
4943:          selectionModel.clearSelection();
4944:         selectionModel.setSelectionInterval(rowIndex,rowIndex);
4945:         getColumnModel().getSelectionModel().clearSelection();
4946:         getColumnModel().getSelectionModel().setSelectionInterval(columnIndex, columnIndex);
4947:         
4948:         
4949:       }
4950:   }
4951: 
4952:   /**
4953:    * Programmatically starts editing the specified cell.
4954:    * 
4955:    * @param row the row of the cell to edit.
4956:    * @param column the column of the cell to edit.
4957:    */
4958:   public boolean editCellAt(int row, int column)
4959:   {
4960:     // Complete the previous editing session, if still active.
4961:     if (isEditing())
4962:       editingStopped(new ChangeEvent("editingStopped"));
4963: 
4964:     TableCellEditor editor = getCellEditor(row, column);
4965:     
4966:     // The boolean values are inverted by the single click without the
4967:     // real editing session.
4968:     if (editor == booleanInvertingEditor && isCellEditable(row, column))
4969:       {
4970:         if (Boolean.TRUE.equals(getValueAt(row, column)))
4971:           setValueAt(Boolean.FALSE, row, column);
4972:         else
4973:           setValueAt(Boolean.TRUE, row, column);
4974:         return false;
4975:       }
4976:     else
4977:       {
4978:         editingRow = row;
4979:         editingColumn = column;
4980: 
4981:         setCellEditor(editor);
4982:         editorComp = prepareEditor(cellEditor, row, column);
4983: 
4984:         // Remove the previous editor components, if present. Only one
4985:         // editor component at time is allowed in the table.
4986:         removeAll();
4987:         add(editorComp);
4988:         moveToCellBeingEdited(editorComp);
4989:         scrollRectToVisible(editorComp.getBounds());
4990:         editorComp.requestFocusInWindow();
4991:         
4992:         // Deliver the should select event.
4993:         return editor.shouldSelectCell(null);        
4994:       }
4995:   }
4996: 
4997:   /**
4998:    * Move the given component under the cell being edited. 
4999:    * The table must be in the editing mode.
5000:    * 
5001:    * @param component the component to move.
5002:    */
5003:   private void moveToCellBeingEdited(Component component)
5004:   {
5005:      Rectangle r = getCellRect(editingRow, editingColumn, true);
5006:      // Adjust bounding box of the editing component, so that it lies
5007:      // 'above' the grid on all edges, not only right and bottom.
5008:      // The table grid is painted only at the right and bottom edge of a cell.
5009:      r.x -= 1;
5010:      r.y -= 1;
5011:      r.width += 1;
5012:      r.height += 1;
5013:      component.setBounds(r);
5014:   }
5015: 
5016:   /**
5017:    * Programmatically starts editing the specified cell.
5018:    *
5019:    * @param row the row of the cell to edit.
5020:    * @param column the column of the cell to edit.
5021:    */
5022:   public boolean editCellAt (int row, int column, EventObject e)
5023:   {
5024:     return editCellAt(row, column);
5025:   }
5026: 
5027:   /**
5028:    * Discards the editor object.
5029:    */
5030:   public void removeEditor()
5031:   {
5032:     editingStopped(new ChangeEvent(this));
5033:   }
5034: 
5035:   /**
5036:    * Prepares the editor by querying for the value and selection state of the
5037:    * cell at (row, column).
5038:    *
5039:    * @param editor the TableCellEditor to set up
5040:    * @param row the row of the cell to edit
5041:    * @param column the column of the cell to edit
5042:    * @return the Component being edited
5043:    */
5044:   public Component prepareEditor (TableCellEditor editor, int row, int column)
5045:   {
5046:     return editor.getTableCellEditorComponent
5047:       (this, getValueAt(row, column), isCellSelected(row, column), row, column);
5048:   }
5049: 
5050:   /**
5051:    * This revalidates the <code>JTable</code> and queues a repaint.
5052:    */
5053:   protected void resizeAndRepaint()
5054:   {
5055:     revalidate();
5056:     repaint();
5057:   }
5058: 
5059:   /**
5060:    * Sets whether cell editors of this table should receive keyboard focus
5061:    * when the editor is activated by a keystroke. The default setting is
5062:    * <code>false</code> which means that the table should keep the keyboard
5063:    * focus until the cell is selected by a mouse click.
5064:    *
5065:    * @param value the value to set
5066:    *
5067:    * @since 1.4
5068:    */
5069:   public void setSurrendersFocusOnKeystroke(boolean value)
5070:   {
5071:     // TODO: Implement functionality of this property (in UI impl).
5072:     surrendersFocusOnKeystroke = value;
5073:   }
5074:   
5075:   /**
5076:    * Returns whether cell editors of this table should receive keyboard focus
5077:    * when the editor is activated by a keystroke. The default setting is
5078:    * <code>false</code> which means that the table should keep the keyboard
5079:    * focus until the cell is selected by a mouse click.
5080:    *
5081:    * @return whether cell editors of this table should receive keyboard focus
5082:    *         when the editor is activated by a keystroke
5083:    *
5084:    * @since 1.4
5085:    */
5086:   public boolean getSurrendersFocusOnKeystroke()
5087:   {
5088:     // TODO: Implement functionality of this property (in UI impl).
5089:     return surrendersFocusOnKeystroke;
5090:   }
5091: 
5092:   /**
5093:    * Helper method for
5094:    * {@link LookAndFeel#installProperty(JComponent, String, Object)}.
5095:    * 
5096:    * @param propertyName the name of the property
5097:    * @param value the value of the property
5098:    *
5099:    * @throws IllegalArgumentException if the specified property cannot be set
5100:    *         by this method
5101:    * @throws ClassCastException if the property value does not match the
5102:    *         property type
5103:    * @throws NullPointerException if <code>c</code> or
5104:    *         <code>propertyValue</code> is <code>null</code>
5105:    */
5106:   void setUIProperty(String propertyName, Object value)
5107:   {
5108:     if (propertyName.equals("rowHeight"))
5109:       {
5110:         if (! clientRowHeightSet)
5111:           {
5112:             setRowHeight(((Integer) value).intValue());
5113:             clientRowHeightSet = false;
5114:           }
5115:       }
5116:     else
5117:       {
5118:         super.setUIProperty(propertyName, value);
5119:       }
5120:   }
5121: }