Source for javax.swing.table.JTableHeader

   1: /* JTableHeader.java --
   2:    Copyright (C) 2003, 2004, 2005  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.table;
  40: 
  41: import java.awt.Color;
  42: import java.awt.Cursor;
  43: import java.awt.Dimension;
  44: import java.awt.Font;
  45: import java.awt.FontMetrics;
  46: import java.awt.Point;
  47: import java.awt.Rectangle;
  48: import java.awt.event.FocusListener;
  49: import java.beans.PropertyChangeListener;
  50: import java.util.Locale;
  51: 
  52: import javax.accessibility.Accessible;
  53: import javax.accessibility.AccessibleAction;
  54: import javax.accessibility.AccessibleComponent;
  55: import javax.accessibility.AccessibleContext;
  56: import javax.accessibility.AccessibleRole;
  57: import javax.accessibility.AccessibleSelection;
  58: import javax.accessibility.AccessibleStateSet;
  59: import javax.accessibility.AccessibleText;
  60: import javax.accessibility.AccessibleValue;
  61: import javax.swing.JComponent;
  62: import javax.swing.JTable;
  63: import javax.swing.UIManager;
  64: import javax.swing.event.ChangeEvent;
  65: import javax.swing.event.ListSelectionEvent;
  66: import javax.swing.event.TableColumnModelEvent;
  67: import javax.swing.event.TableColumnModelListener;
  68: import javax.swing.plaf.TableHeaderUI;
  69: 
  70: public class JTableHeader extends JComponent
  71:   implements TableColumnModelListener, Accessible
  72: {
  73:   protected class AccessibleJTableHeader extends AccessibleJComponent
  74:   {
  75:     protected class AccessibleJTableHeaderEntry extends AccessibleContext
  76:       implements Accessible, AccessibleComponent
  77:     {
  78:       public AccessibleJTableHeaderEntry(int c, JTableHeader p, JTable t) 
  79:       {
  80:         throw new Error("not implemented");
  81:       }
  82:       
  83:       public void addFocusListener(FocusListener l)
  84:       {
  85:         throw new Error("not implemented");
  86:       }
  87:       
  88:       public void addPropertyChangeListener(PropertyChangeListener l)
  89:       {
  90:         throw new Error("not implemented");
  91:       }
  92:       
  93:       public boolean contains(Point p)
  94:       {
  95:         throw new Error("not implemented");
  96:       }
  97:       
  98:       public AccessibleAction getAccessibleAction()
  99:       {
 100:         throw new Error("not implemented");
 101:       }
 102:       
 103:       public Accessible getAccessibleAt(Point p)
 104:       {
 105:         throw new Error("not implemented");
 106:       }
 107:       
 108:       public Accessible getAccessibleChild(int i)
 109:       {
 110:         throw new Error("not implemented");
 111:       }
 112:       
 113:       public int getAccessibleChildrenCount()
 114:       {
 115:         throw new Error("not implemented");
 116:       }
 117:       
 118:       public AccessibleComponent getAccessibleComponent()
 119:       {
 120:         throw new Error("not implemented");
 121:       }
 122:       
 123:       public AccessibleContext getAccessibleContext()
 124:       {
 125:         throw new Error("not implemented");
 126:       }
 127:       
 128:       public String getAccessibleDescription()
 129:       {
 130:         throw new Error("not implemented");
 131:       }
 132:       
 133:       public int getAccessibleIndexInParent()
 134:       {
 135:         throw new Error("not implemented");
 136:       }
 137:       
 138:       public String getAccessibleName()
 139:       {
 140:         throw new Error("not implemented");
 141:       }
 142:       
 143:       public AccessibleRole getAccessibleRole()
 144:       {
 145:         throw new Error("not implemented");
 146:       }
 147:       
 148:       public AccessibleSelection getAccessibleSelection()
 149:       {
 150:         throw new Error("not implemented");
 151:       }
 152:       
 153:       public AccessibleStateSet getAccessibleStateSet()
 154:       {
 155:         throw new Error("not implemented");
 156:       }
 157:       
 158:       public AccessibleText getAccessibleText()
 159:       {
 160:         throw new Error("not implemented");
 161:       }
 162:       
 163:       public AccessibleValue getAccessibleValue()
 164:       {
 165:         throw new Error("not implemented");
 166:       }
 167:       
 168:       public Color getBackground()
 169:       {
 170:         throw new Error("not implemented");
 171:       }
 172:       
 173:       public Rectangle getBounds()
 174:       {
 175:         throw new Error("not implemented");
 176:       }
 177:       
 178:       public Cursor getCursor()
 179:       {
 180:         throw new Error("not implemented");
 181:       }
 182:       
 183:       public Font getFont()
 184:       {
 185:         throw new Error("not implemented");
 186:       }
 187:       
 188:       public FontMetrics getFontMetrics(Font f)
 189:       {
 190:         throw new Error("not implemented");
 191:       }
 192:       
 193:       public Color getForeground()
 194:       {
 195:         throw new Error("not implemented");
 196:       }
 197:       
 198:       public Locale getLocale()
 199:       {
 200:         throw new Error("not implemented");
 201:       }
 202:       
 203:       public Point getLocation()
 204:       {
 205:         throw new Error("not implemented");
 206:       }
 207:       
 208:       public Point getLocationOnScreen()
 209:       {
 210:         throw new Error("not implemented");
 211:       }
 212:       
 213:       public Dimension getSize()
 214:       {
 215:         throw new Error("not implemented");
 216:       }
 217:       
 218:       public boolean isEnabled()
 219:       {
 220:         throw new Error("not implemented");
 221:       }
 222:       
 223:       public boolean isFocusTraversable()
 224:       {
 225:         throw new Error("not implemented");
 226:       }
 227:       
 228:       public boolean isShowing()
 229:       {
 230:         throw new Error("not implemented");
 231:       }
 232:       
 233:       public boolean isVisible()
 234:       {
 235:         throw new Error("not implemented");
 236:       }
 237:       
 238:       public void removeFocusListener(FocusListener l)
 239:       {
 240:         throw new Error("not implemented");
 241:       }
 242:       
 243:       public void removePropertyChangeListener(PropertyChangeListener l)
 244:       {
 245:         throw new Error("not implemented");
 246:       }
 247:       
 248:       public void requestFocus()
 249:       {
 250:         throw new Error("not implemented");
 251:       }
 252:       
 253:       public void setAccessibleDescription(String s)
 254:       {
 255:         throw new Error("not implemented");
 256:       }
 257:       
 258:       public void setAccessibleName(String s)
 259:       {
 260:         throw new Error("not implemented");
 261:       }
 262:       
 263:       public void setBackground(Color c)
 264:       {
 265:         throw new Error("not implemented");
 266:       }
 267:       
 268:       public void setBounds(Rectangle r)
 269:       {
 270:         throw new Error("not implemented");
 271:       }
 272:       
 273:       public void setCursor(Cursor c)
 274:       {
 275:         throw new Error("not implemented");
 276:       }
 277:       
 278:       public void setEnabled(boolean b)
 279:       {
 280:         throw new Error("not implemented");
 281:       }
 282:       
 283:       public void setFont(Font f)
 284:       {
 285:         throw new Error("not implemented");
 286:       }
 287:       
 288:       public void setForeground(Color c)
 289:       {
 290:         throw new Error("not implemented");
 291:       }
 292:       
 293:       public void setLocation(Point p)
 294:       {
 295:         throw new Error("not implemented");
 296:       }
 297:       
 298:       public void setSize(Dimension d)
 299:       {
 300:         throw new Error("not implemented");
 301:       }
 302:       
 303:       public void setVisible(boolean b)
 304:       {
 305:         throw new Error("not implemented");
 306:       }
 307:     };
 308:   }
 309: 
 310:   private static final long serialVersionUID = 5144633983372967710L;
 311: 
 312:   /**
 313:    * The columnModel property.
 314:    */
 315:   protected TableColumnModel columnModel;
 316: 
 317:   /**
 318:    * The draggedColumn property.
 319:    */
 320:   protected TableColumn draggedColumn;
 321: 
 322:   /**
 323:    * The draggedDistance property.
 324:    */
 325:   protected int draggedDistance;
 326: 
 327:   /**
 328:    * The opaque property.
 329:    */
 330:   boolean opaque;
 331: 
 332:   /**
 333:    * The reorderingAllowed property.
 334:    */
 335:   protected boolean reorderingAllowed;
 336: 
 337:   /**
 338:    * The resizingAllowed property.
 339:    */
 340:   protected boolean resizingAllowed = true;
 341: 
 342:   /**
 343:    * The resizingColumn property.
 344:    */
 345:   protected TableColumn resizingColumn;
 346: 
 347:   /**
 348:    * The table property.
 349:    */
 350:   protected JTable table;
 351: 
 352:   /**
 353:    * The updateTableInRealTime property.
 354:    */
 355:   protected boolean updateTableInRealTime;
 356: 
 357:   TableCellRenderer cellRenderer; 
 358: 
 359:   /**
 360:    * Creates a new default instance.
 361:    */
 362:   public JTableHeader()
 363:   {
 364:     this(null);
 365:   }
 366: 
 367:   /**
 368:    * Creates a new header.  If <code>cm</code> is <code>null</code>, a new
 369:    * table column model is created by calling 
 370:    * {@link #createDefaultColumnModel()}.
 371:    * 
 372:    * @param cm  the table column model (<code>null</code> permitted).
 373:    */
 374:   public JTableHeader(TableColumnModel cm)
 375:   {
 376:     columnModel = cm == null ? createDefaultColumnModel() : cm; 
 377:     initializeLocalVars();
 378:     updateUI();
 379:   }
 380: 
 381:   /**
 382:    * Creates a default table column model.
 383:    * 
 384:    * @return A default table column model.
 385:    */
 386:   protected TableColumnModel createDefaultColumnModel()
 387:   {
 388:     return new DefaultTableColumnModel();
 389:   }
 390: 
 391:   /**
 392:    * Get the value of the {@link #accessibleContext} property.
 393:    *
 394:    * @return The current value of the property
 395:    */
 396:   public AccessibleContext getAccessibleContext()
 397:   {
 398:     return accessibleContext;
 399:   }
 400: 
 401:   /**
 402:    * Get the value of the {@link #columnModel} property.
 403:    *
 404:    * @return The current value of the property
 405:    */
 406:   public TableColumnModel getColumnModel()
 407:   {
 408:     return columnModel;
 409:   }
 410: 
 411:   /**
 412:    * Get the value of the {@link #draggedColumn} property.
 413:    *
 414:    * @return The current value of the property
 415:    */
 416:   public TableColumn getDraggedColumn()
 417:   {
 418:     return draggedColumn;
 419:   }
 420: 
 421:   /**
 422:    * Get the value of the {@link #draggedDistance} property.
 423:    *
 424:    * @return The current value of the property
 425:    */
 426:   public int getDraggedDistance()
 427:   {
 428:     return draggedDistance;
 429:   }
 430: 
 431:   /**
 432:    * Get the value of the {@link #reorderingAllowed} property.
 433:    *
 434:    * @return The current value of the property
 435:    */
 436:   public boolean getReorderingAllowed()
 437:   {
 438:     return reorderingAllowed;
 439:   }
 440: 
 441:   /**
 442:    * Get the value of the {@link #resizingAllowed} property.
 443:    *
 444:    * @return The current value of the property
 445:    */
 446:   public boolean getResizingAllowed()
 447:   {
 448:     return resizingAllowed;
 449:   }
 450: 
 451:   /**
 452:    * Get the value of the {@link #resizingColumn} property.
 453:    *
 454:    * @return The current value of the property
 455:    */
 456:   public TableColumn getResizingColumn()
 457:   {
 458:     return resizingColumn;
 459:   }
 460: 
 461:   /**
 462:    * Get the value of the {@link #table} property.
 463:    *
 464:    * @return The current value of the property
 465:    */
 466:   public JTable getTable()
 467:   {
 468:     return table;
 469:   }
 470: 
 471:   /**
 472:    * Get the value of the {@link #updateTableInRealTime} property.
 473:    *
 474:    * @return The current value of the property
 475:    */
 476:   public boolean getUpdateTableInRealTime()
 477:   {
 478:     return updateTableInRealTime;
 479:   }
 480: 
 481:   /**
 482:    * Get the value of the {@link #opaque} property.
 483:    *
 484:    * @return The current value of the property
 485:    */
 486:   public boolean isOpaque()
 487:   {
 488:     return opaque;
 489:   }
 490: 
 491:   /**
 492:    * Set the value of the {@link #columnModel} property.
 493:    *
 494:    * @param c The new value of the property
 495:    */ 
 496:   public void setColumnModel(TableColumnModel c)
 497:   {
 498:     columnModel.removeColumnModelListener(this);
 499:     columnModel = c;
 500:     columnModel.addColumnModelListener(this);
 501:   }
 502: 
 503:   /**
 504:    * Set the value of the {@link #draggedColumn} property.
 505:    *
 506:    * @param d The new value of the property
 507:    */ 
 508:   public void setDraggedColumn(TableColumn d)
 509:   {
 510:     draggedColumn = d;
 511:   }
 512: 
 513:   /**
 514:    * Set the value of the {@link #draggedDistance} property.
 515:    *
 516:    * @param d The new value of the property
 517:    */ 
 518:   public void setDraggedDistance(int d)
 519:   {
 520:     draggedDistance = d;
 521:   }
 522: 
 523:   /**
 524:    * Set the value of the {@link #opaque} property.
 525:    *
 526:    * @param o The new value of the property
 527:    */ 
 528:   public void setOpaque(boolean o)
 529:   {
 530:     opaque = o;
 531:   }
 532: 
 533:   /**
 534:    * Set the value of the {@link #reorderingAllowed} property.
 535:    *
 536:    * @param r The new value of the property
 537:    */ 
 538:   public void setReorderingAllowed(boolean r)
 539:   {
 540:     reorderingAllowed = r;
 541:   }
 542: 
 543:   /**
 544:    * Set the value of the {@link #resizingAllowed} property.
 545:    *
 546:    * @param r The new value of the property
 547:    */ 
 548:   public void setResizingAllowed(boolean r)
 549:   {
 550:     resizingAllowed = r;
 551:   }
 552: 
 553:   /**
 554:    * Set the value of the {@link #resizingColumn} property.
 555:    *
 556:    * @param r The new value of the property
 557:    */ 
 558:   public void setResizingColumn(TableColumn r)
 559:   {
 560:     resizingColumn = r;
 561:   }
 562: 
 563:   /**
 564:    * Set the value of the {@link #table} property.
 565:    *
 566:    * @param t The new value of the property
 567:    */ 
 568:   public void setTable(JTable t)
 569:   {
 570:     table = t;
 571:   }
 572: 
 573:   /**
 574:    * Set the value of the {@link #updateTableInRealTime} property.
 575:    *
 576:    * @param u The new value of the property
 577:    */ 
 578:   public void setUpdateTableInRealTime(boolean u)
 579:   {
 580:     updateTableInRealTime = u;
 581:   }
 582: 
 583:   /**
 584:    * Creates a default renderer.
 585:    * 
 586:    * @return A default renderer.
 587:    */
 588:   protected TableCellRenderer createDefaultRenderer()
 589:   {
 590:     return new DefaultTableCellRenderer();
 591:   }
 592:   
 593:   /**
 594:    * Returns the default table cell renderer.
 595:    * 
 596:    * @return The default table cell renderer.
 597:    */
 598:   public TableCellRenderer getDefaultRenderer()
 599:   {
 600:     return cellRenderer;
 601:   }
 602: 
 603:   /**
 604:    * Sets the default table cell renderer.
 605:    * 
 606:    * @param cellRenderer  the renderer.
 607:    */
 608:   public void setDefaultRenderer(TableCellRenderer cellRenderer)
 609:   {
 610:     this.cellRenderer = cellRenderer;
 611:   }
 612: 
 613:   public Rectangle getHeaderRect(int column)
 614:   {
 615:     Rectangle r = getTable().getCellRect(-1, column, false);
 616:     r.height = getHeight();
 617:     return r;
 618:   }
 619: 
 620:   protected String paramString()
 621:   {
 622:     return "JTableHeader";
 623:   }
 624: 
 625:   // UI support
 626: 
 627:   public String getUIClassID()
 628:   {
 629:     return "TableHeaderUI";
 630:   }
 631: 
 632:   public TableHeaderUI getUI()
 633:   {
 634:     return (TableHeaderUI) ui;
 635:   }
 636: 
 637:   public void setUI(TableHeaderUI u)
 638:   {
 639:     super.setUI(u);
 640:   }
 641: 
 642:   public void updateUI()
 643:   {
 644:     setUI((TableHeaderUI) UIManager.getUI(this));
 645:   }
 646: 
 647:   /**
 648:    * Returns the index of the column at the specified point.
 649:    * 
 650:    * @param point  the point.
 651:    * 
 652:    * @return The column index, or -1.
 653:    */
 654:   public int columnAtPoint(Point point)
 655:   {
 656:     if (getBounds().contains(point))
 657:       return columnModel.getColumnIndexAtX(point.x);
 658:     
 659:     return -1;
 660:   }
 661: 
 662:   /**
 663:    * Receives notification when a column is added to the column model.
 664:    *
 665:    * @param event the table column model event
 666:    */
 667:   public void columnAdded(TableColumnModelEvent event)
 668:   {
 669:     // TODO: What else to do here (if anything)?
 670:     resizeAndRepaint();
 671:   }
 672: 
 673:   /**
 674:    * Receives notification when a column margin changes in the column model.
 675:    *
 676:    * @param event the table column model event
 677:    */
 678:   public void columnMarginChanged(ChangeEvent event)
 679:   {
 680:     // TODO: What else to do here (if anything)?
 681:     resizeAndRepaint();
 682:   }
 683: 
 684:   /**
 685:    * Receives notification when a column is moved within the column model.
 686:    *
 687:    * @param event the table column model event
 688:    */
 689:   public void columnMoved(TableColumnModelEvent event)
 690:   {
 691:     // TODO: What else to do here (if anything)?
 692:     resizeAndRepaint();
 693:   }
 694: 
 695:   /**
 696:    * Receives notification when a column is removed from the column model.
 697:    *
 698:    * @param event the table column model event
 699:    */
 700:   public void columnRemoved(TableColumnModelEvent event)
 701:   {
 702:     // TODO: What else to do here (if anything)?
 703:     resizeAndRepaint();
 704:   }
 705: 
 706:   /**
 707:    * Receives notification when the column selection has changed.
 708:    *
 709:    * @param event the table column model event
 710:    */
 711:   public void columnSelectionChanged(ListSelectionEvent event)
 712:   {
 713:     // TODO: What else to do here (if anything)?
 714:     resizeAndRepaint();
 715:   }
 716: 
 717:   /**
 718:    * Validates the layout of this table header and repaints it. This is
 719:    * equivalent to <code>revalidate()</code> followed by
 720:    * <code>repaint()</code>.
 721:    */
 722:   public void resizeAndRepaint()
 723:   {
 724:     revalidate();
 725:     repaint();
 726:   }
 727: 
 728:   /**
 729:    * Initializes the fields and properties of this class with default values.
 730:    * This is called by the constructors.
 731:    */
 732:   protected void initializeLocalVars()
 733:   {
 734:     accessibleContext = new AccessibleJTableHeader();
 735:     draggedColumn = null;
 736:     draggedDistance = 0;
 737:     opaque = true;
 738:     reorderingAllowed = true;
 739:     resizingAllowed = true;
 740:     resizingColumn = null;
 741:     table = null;
 742:     updateTableInRealTime = true;
 743:     cellRenderer = createDefaultRenderer();
 744:   }
 745: }