KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > table > JTableHeader


1 /*
2  * @(#)JTableHeader.java 1.66 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.table;
9
10 import java.util.*;
11 import java.awt.*;
12 import java.awt.event.*;
13
14 import javax.swing.*;
15 import javax.swing.event.*;
16 import javax.swing.plaf.*;
17 import javax.accessibility.*;
18
19 import java.beans.PropertyChangeListener JavaDoc;
20
21 import java.io.ObjectOutputStream JavaDoc;
22 import java.io.ObjectInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24
25
26 /**
27  * This is the object which manages the header of the <code>JTable</code>.
28  * <p>
29  * <strong>Warning:</strong>
30  * Serialized objects of this class will not be compatible with
31  * future Swing releases. The current serialization support is
32  * appropriate for short term storage or RMI between applications running
33  * the same version of Swing. As of 1.4, support for long term storage
34  * of all JavaBeans<sup><font size="-2">TM</font></sup>
35  * has been added to the <code>java.beans</code> package.
36  * Please see {@link java.beans.XMLEncoder}.
37  *
38  * @version 1.66 12/19/03
39  * @author Alan Chung
40  * @author Philip Milne
41  * @see javax.swing.JTable
42  */

43 public class JTableHeader extends JComponent implements TableColumnModelListener, Accessible
44 {
45     /**
46      * @see #getUIClassID
47      * @see #readObject
48      */

49     private static final String JavaDoc uiClassID = "TableHeaderUI";
50
51 //
52
// Instance Variables
53
//
54
/**
55      * The table for which this object is the header;
56      * the default is <code>null</code>.
57      */

58     protected JTable table;
59
60     /**
61      * The <code>TableColumnModel</code> of the table header.
62      */

63     protected TableColumnModel JavaDoc columnModel;
64
65     /**
66      * If true, reordering of columns are allowed by the user;
67      * the default is true.
68      */

69     protected boolean reorderingAllowed;
70
71     /**
72      * If true, resizing of columns are allowed by the user;
73      * the default is true.
74      */

75     protected boolean resizingAllowed;
76
77     /**
78      * Obsolete as of Java 2 platform v1.3. Real time repaints, in response
79      * to column dragging or resizing, are now unconditional.
80      */

81     /*
82      * If this flag is true, then the header will repaint the table as
83      * a column is dragged or resized; the default is true.
84      */

85     protected boolean updateTableInRealTime;
86
87     /** The index of the column being resized. <code>null</code> if not resizing. */
88     transient protected TableColumn JavaDoc resizingColumn;
89
90     /** The index of the column being dragged. <code>null</code> if not dragging. */
91     transient protected TableColumn JavaDoc draggedColumn;
92
93     /** The distance from its original position the column has been dragged. */
94     transient protected int draggedDistance;
95
96     /**
97       * The default renderer to be used when a <code>TableColumn</code>
98       * does not define a <code>headerRenderer</code>.
99       */

100     private TableCellRenderer JavaDoc defaultRenderer;
101
102 //
103
// Constructors
104
//
105

106     /**
107      * Constructs a <code>JTableHeader</code> with a default
108      * <code>TableColumnModel</code>.
109      *
110      * @see #createDefaultColumnModel
111      */

112     public JTableHeader() {
113     this(null);
114     }
115
116     /**
117      * Constructs a <code>JTableHeader</code> which is initialized with
118      * <code>cm</code> as the column model. If <code>cm</code> is
119      * <code>null</code> this method will initialize the table header
120      * with a default <code>TableColumnModel</code>.
121      *
122      * @param cm the column model for the table
123      * @see #createDefaultColumnModel
124      */

125     public JTableHeader(TableColumnModel JavaDoc cm) {
126     super();
127
128     setFocusable(false);
129
130     if (cm == null)
131         cm = createDefaultColumnModel();
132     setColumnModel(cm);
133
134     // Initialize local ivars
135
initializeLocalVars();
136
137     // Get UI going
138
updateUI();
139     }
140
141 //
142
// Local behavior attributes
143
//
144

145     /**
146      * Sets the table associated with this header.
147      * @param table the new table
148      * @beaninfo
149      * bound: true
150      * description: The table associated with this header.
151      */

152     public void setTable(JTable table) {
153     JTable old = this.table;
154     this.table = table;
155     firePropertyChange("table", old, table);
156     }
157
158     /**
159       * Returns the table associated with this header.
160       * @return the <code>table</code> property
161       */

162     public JTable getTable() {
163     return table;
164     }
165
166     /**
167      * Sets whether the user can drag column headers to reorder columns.
168      *
169      * @param reorderingAllowed true if the table view should allow
170      * reordering; otherwise false
171      * @see #getReorderingAllowed
172      * @beaninfo
173      * bound: true
174      * description: Whether the user can drag column headers to reorder columns.
175      */

176     public void setReorderingAllowed(boolean reorderingAllowed) {
177     boolean old = this.reorderingAllowed;
178     this.reorderingAllowed = reorderingAllowed;
179     firePropertyChange("reorderingAllowed", old, reorderingAllowed);
180     }
181
182     /**
183      * Returns true if the user is allowed to rearrange columns by
184      * dragging their headers, false otherwise. The default is true. You can
185      * rearrange columns programmatically regardless of this setting.
186      *
187      * @return the <code>reorderingAllowed</code> property
188      * @see #setReorderingAllowed
189      */

190     public boolean getReorderingAllowed() {
191     return reorderingAllowed;
192     }
193
194     /**
195      * Sets whether the user can resize columns by dragging between headers.
196      *
197      * @param resizingAllowed true if table view should allow
198      * resizing
199      * @see #getResizingAllowed
200      * @beaninfo
201      * bound: true
202      * description: Whether the user can resize columns by dragging between headers.
203      */

204     public void setResizingAllowed(boolean resizingAllowed) {
205     boolean old = this.resizingAllowed;
206     this.resizingAllowed = resizingAllowed;
207     firePropertyChange("resizingAllowed", old, resizingAllowed);
208     }
209
210     /**
211      * Returns true if the user is allowed to resize columns by dragging
212      * between their headers, false otherwise. The default is true. You can
213      * resize columns programmatically regardless of this setting.
214      *
215      * @return the <code>resizingAllowed</code> property
216      * @see #setResizingAllowed
217      */

218     public boolean getResizingAllowed() {
219     return resizingAllowed;
220     }
221
222     /**
223      * Returns the the dragged column, if and only if, a drag is in
224      * process, otherwise returns <code>null</code>.
225      *
226      * @return the dragged column, if a drag is in
227      * process, otherwise returns <code>null</code>
228      * @see #getDraggedDistance
229      */

230     public TableColumn JavaDoc getDraggedColumn() {
231     return draggedColumn;
232     }
233
234     /**
235      * Returns the column's horizontal distance from its original
236      * position, if and only if, a drag is in process. Otherwise, the
237      * the return value is meaningless.
238      *
239      * @return the column's horizontal distance from its original
240      * position, if a drag is in process, otherwise the return
241      * value is meaningless
242      * @see #getDraggedColumn
243      */

244     public int getDraggedDistance() {
245     return draggedDistance;
246     }
247
248     /**
249      * Returns the resizing column. If no column is being
250      * resized this method returns <code>null</code>.
251      *
252      * @return the resizing column, if a resize is in process, otherwise
253      * returns <code>null</code>
254      */

255     public TableColumn JavaDoc getResizingColumn() {
256     return resizingColumn;
257     }
258
259     /**
260      * Obsolete as of Java 2 platform v1.3. Real time repaints, in response to
261      * column dragging or resizing, are now unconditional.
262      */

263     /*
264      * Sets whether the body of the table updates in real time when
265      * a column is resized or dragged.
266      *
267      * @param flag true if tableView should update
268      * the body of the table in real time
269      * @see #getUpdateTableInRealTime
270      */

271     public void setUpdateTableInRealTime(boolean flag) {
272     updateTableInRealTime = flag;
273     }
274
275     /**
276      * Obsolete as of Java 2 platform v1.3. Real time repaints, in response to
277      * column dragging or resizing, are now unconditional.
278      */

279     /*
280      * Returns true if the body of the table view updates in real
281      * time when a column is resized or dragged. User can set this flag to
282      * false to speed up the table's response to user resize or drag actions.
283      * The default is true.
284      *
285      * @return true if the table updates in real time
286      * @see #setUpdateTableInRealTime
287      */

288     public boolean getUpdateTableInRealTime() {
289     return updateTableInRealTime;
290     }
291
292     /**
293      * Sets the default renderer to be used when no <code>headerRenderer</code>
294      * is defined by a <code>TableColumn</code>.
295      * @param defaultRenderer the default renderer
296      */

297     public void setDefaultRenderer(TableCellRenderer JavaDoc defaultRenderer) {
298     this.defaultRenderer = defaultRenderer;
299     }
300
301     /**
302      * Returns the default renderer used when no <code>headerRenderer</code>
303      * is defined by a <code>TableColumn</code>.
304      * @return the default renderer
305      */

306     public TableCellRenderer JavaDoc getDefaultRenderer() {
307     return defaultRenderer;
308     }
309
310     /**
311      * Returns the index of the column that <code>point</code> lies in, or -1 if it
312      * lies out of bounds.
313      *
314      * @return the index of the column that <code>point</code> lies in, or -1 if it
315      * lies out of bounds
316      */

317     public int columnAtPoint(Point point) {
318         int x = point.x;
319         if (!getComponentOrientation().isLeftToRight()) {
320             x = getWidthInRightToLeft() - x;
321         }
322         return getColumnModel().getColumnIndexAtX(x);
323     }
324
325     /**
326      * Returns the rectangle containing the header tile at <code>column</code>.
327      * When the <code>column</code> parameter is out of bounds this method uses the
328      * same conventions as the <code>JTable</code> method <code>getCellRect</code>.
329      *
330      * @return the rectangle containing the header tile at <code>column</code>
331      * @see JTable#getCellRect
332      */

333     public Rectangle getHeaderRect(int column) {
334         Rectangle r = new Rectangle();
335     TableColumnModel JavaDoc cm = getColumnModel();
336
337     r.height = getHeight();
338
339     if (column < 0) {
340         // x = width = 0;
341
if( !getComponentOrientation().isLeftToRight() ) {
342         r.x = getWidthInRightToLeft();
343             }
344     }
345     else if (column >= cm.getColumnCount()) {
346             if( getComponentOrientation().isLeftToRight() ) {
347         r.x = getWidth();
348             }
349     }
350     else {
351             for(int i = 0; i < column; i++) {
352                 r.x += cm.getColumn(i).getWidth();
353             }
354             if( !getComponentOrientation().isLeftToRight() ) {
355                 r.x = getWidthInRightToLeft() - r.x - cm.getColumn(column).getWidth();
356             }
357
358         r.width = cm.getColumn(column).getWidth();
359     }
360     return r;
361     }
362
363
364     /**
365      * Allows the renderer's tips to be used if there is text set.
366      * @param event the location of the event identifies the proper
367      * renderer and, therefore, the proper tip
368      * @return the tool tip for this component
369      */

370     public String JavaDoc getToolTipText(MouseEvent event) {
371     String JavaDoc tip = null;
372     Point p = event.getPoint();
373     int column;
374
375     // Locate the renderer under the event location
376
if ((column = columnAtPoint(p)) != -1) {
377         TableColumn JavaDoc aColumn = columnModel.getColumn(column);
378         TableCellRenderer JavaDoc renderer = aColumn.getHeaderRenderer();
379             if (renderer == null) {
380                 renderer = defaultRenderer;
381             }
382         Component component = renderer.getTableCellRendererComponent(
383                   getTable(), aColumn.getHeaderValue(), false, false,
384                   -1, column);
385
386         // Now have to see if the component is a JComponent before
387
// getting the tip
388
if (component instanceof JComponent) {
389         // Convert the event to the renderer's coordinate system
390
MouseEvent newEvent;
391         Rectangle cellRect = getHeaderRect(column);
392
393         p.translate(-cellRect.x, -cellRect.y);
394         newEvent = new MouseEvent(component, event.getID(),
395                       event.getWhen(), event.getModifiers(),
396                       p.x, p.y, event.getClickCount(),
397                       event.isPopupTrigger());
398
399         tip = ((JComponent)component).getToolTipText(newEvent);
400         }
401     }
402
403     // No tip from the renderer get our own tip
404
if (tip == null)
405         tip = getToolTipText();
406
407     return tip;
408     }
409
410 //
411
// Managing TableHeaderUI
412
//
413

414     /**
415      * Returns the look and feel (L&F) object that renders this component.
416      *
417      * @return the <code>TableHeaderUI</code> object that renders this component
418      */

419     public TableHeaderUI getUI() {
420     return (TableHeaderUI)ui;
421     }
422
423     /**
424      * Sets the look and feel (L&F) object that renders this component.
425      *
426      * @param ui the <code>TableHeaderUI</code> L&F object
427      * @see UIDefaults#getUI
428      */

429     public void setUI(TableHeaderUI ui){
430         if (this.ui != ui) {
431             super.setUI(ui);
432             repaint();
433         }
434     }
435
436     /**
437      * Notification from the <code>UIManager</code> that the look and feel
438      * (L&F) has changed.
439      * Replaces the current UI object with the latest version from the
440      * <code>UIManager</code>.
441      *
442      * @see JComponent#updateUI
443      */

444     public void updateUI(){
445     setUI((TableHeaderUI)UIManager.getUI(this));
446     resizeAndRepaint();
447     invalidate();//PENDING
448
}
449
450
451     /**
452      * Returns the suffix used to construct the name of the look and feel
453      * (L&F) class used to render this component.
454      * @return the string "TableHeaderUI"
455      *
456      * @return "TableHeaderUI"
457      * @see JComponent#getUIClassID
458      * @see UIDefaults#getUI
459      */

460     public String JavaDoc getUIClassID() {
461     return uiClassID;
462     }
463
464
465 //
466
// Managing models
467
//
468

469
470     /**
471      * Sets the column model for this table to <code>newModel</code> and registers
472      * for listener notifications from the new column model.
473      *
474      * @param columnModel the new data source for this table
475      * @exception IllegalArgumentException
476      * if <code>newModel</code> is <code>null</code>
477      * @see #getColumnModel
478      * @beaninfo
479      * bound: true
480      * description: The object governing the way columns appear in the view.
481      */

482     public void setColumnModel(TableColumnModel JavaDoc columnModel) {
483         if (columnModel == null) {
484             throw new IllegalArgumentException JavaDoc("Cannot set a null ColumnModel");
485         }
486         TableColumnModel JavaDoc old = this.columnModel;
487         if (columnModel != old) {
488             if (old != null) {
489                 old.removeColumnModelListener(this);
490         }
491             this.columnModel = columnModel;
492             columnModel.addColumnModelListener(this);
493
494         firePropertyChange("columnModel", old, columnModel);
495             resizeAndRepaint();
496         }
497     }
498
499     /**
500      * Returns the <code>TableColumnModel</code> that contains all column information
501      * of this table header.
502      *
503      * @return the <code>columnModel</code> property
504      * @see #setColumnModel
505      */

506     public TableColumnModel JavaDoc getColumnModel() {
507     return columnModel;
508     }
509
510 //
511
// Implementing TableColumnModelListener interface
512
//
513

514     /**
515      * Invoked when a column is added to the table column model.
516      * <p>
517      * Application code will not use these methods explicitly, they
518      * are used internally by <code>JTable</code>.
519      *
520      * @param e the event received
521      * @see TableColumnModelListener
522      */

523     public void columnAdded(TableColumnModelEvent e) { resizeAndRepaint(); }
524
525
526     /**
527      * Invoked when a column is removed from the table column model.
528      * <p>
529      * Application code will not use these methods explicitly, they
530      * are used internally by <code>JTable</code>.
531      *
532      * @param e the event received
533      * @see TableColumnModelListener
534      */

535     public void columnRemoved(TableColumnModelEvent e) { resizeAndRepaint(); }
536
537
538     /**
539      * Invoked when a column is repositioned.
540      * <p>
541      * Application code will not use these methods explicitly, they
542      * are used internally by <code>JTable</code>.
543      *
544      * @param e the event received
545      * @see TableColumnModelListener
546      */

547     public void columnMoved(TableColumnModelEvent e) { repaint(); }
548
549
550     /**
551      * Invoked when a column is moved due to a margin change.
552      * <p>
553      * Application code will not use these methods explicitly, they
554      * are used internally by <code>JTable</code>.
555      *
556      * @param e the event received
557      * @see TableColumnModelListener
558      */

559     public void columnMarginChanged(ChangeEvent e) { resizeAndRepaint(); }
560
561
562     // --Redrawing the header is slow in cell selection mode.
563
// --Since header selection is ugly and it is always clear from the
564
// --view which columns are selected, don't redraw the header.
565
/**
566      * Invoked when the selection model of the <code>TableColumnModel</code>
567      * is changed. This method currently has no effect (the header is not
568      * redrawn).
569      * <p>
570      * Application code will not use these methods explicitly, they
571      * are used internally by <code>JTable</code>.
572      *
573      * @param e the event received
574      * @see TableColumnModelListener
575      */

576     public void columnSelectionChanged(ListSelectionEvent e) { } // repaint(); }
577

578 //
579
// Package Methods
580
//
581

582     /**
583      * Returns the default column model object which is
584      * a <code>DefaultTableColumnModel</code>. A subclass can override this
585      * method to return a different column model object
586      *
587      * @return the default column model object
588      */

589     protected TableColumnModel JavaDoc createDefaultColumnModel() {
590     return new DefaultTableColumnModel JavaDoc();
591     }
592
593     /**
594      * Returns a default renderer to be used when no header renderer
595      * is defined by a <code>TableColumn</code>.
596      *
597      * @return the default table column renderer
598      */

599     protected TableCellRenderer JavaDoc createDefaultRenderer() {
600     DefaultTableCellRenderer JavaDoc label = new UIResourceTableCellRenderer();
601     label.setHorizontalAlignment(JLabel.CENTER);
602     return label;
603     }
604
605     private static class UIResourceTableCellRenderer extends DefaultTableCellRenderer JavaDoc implements UIResource {
606         public Component getTableCellRendererComponent(JTable table, Object JavaDoc value,
607                          boolean isSelected, boolean hasFocus, int row, int column) {
608             if (table != null) {
609                 JTableHeader JavaDoc header = table.getTableHeader();
610                 if (header != null) {
611                     setForeground(header.getForeground());
612                     setBackground(header.getBackground());
613                     setFont(header.getFont());
614                 }
615                 }
616
617                 setText((value == null) ? "" : value.toString());
618         setBorder(UIManager.getBorder("TableHeader.cellBorder"));
619             return this;
620             }
621     }
622
623     /**
624      * Initializes the local variables and properties with default values.
625      * Used by the constructor methods.
626      */

627     protected void initializeLocalVars() {
628         setOpaque(true);
629     table = null;
630     reorderingAllowed = true;
631     resizingAllowed = true;
632     draggedColumn = null;
633     draggedDistance = 0;
634     resizingColumn = null;
635     updateTableInRealTime = true;
636
637     // I'm registered to do tool tips so we can draw tips for the
638
// renderers
639
ToolTipManager toolTipManager = ToolTipManager.sharedInstance();
640     toolTipManager.registerComponent(this);
641     setDefaultRenderer(createDefaultRenderer());
642     }
643
644     /**
645      * Sizes the header and marks it as needing display. Equivalent
646      * to <code>revalidate</code> followed by <code>repaint</code>.
647      */

648     public void resizeAndRepaint() {
649         revalidate();
650     repaint();
651     }
652
653     /**
654       * Sets the header's <code>draggedColumn</code> to <code>aColumn</code>.
655       * <p>
656       * Application code will not use this method explicitly, it is used
657       * internally by the column dragging mechanism.
658       *
659       * @param aColumn the column being dragged, or <code>null</code> if
660       * no column is being dragged
661       */

662     public void setDraggedColumn(TableColumn JavaDoc aColumn) {
663     draggedColumn = aColumn;
664     }
665
666     /**
667       * Sets the header's <code>draggedDistance</code> to <code>distance</code>.
668       * @param distance the distance dragged
669       */

670     public void setDraggedDistance(int distance) {
671     draggedDistance = distance;
672     }
673
674     /**
675       * Sets the header's <code>resizingColumn</code> to <code>aColumn</code>.
676       * <p>
677       * Application code will not use this method explicitly, it
678       * is used internally by the column sizing mechanism.
679       *
680       * @param aColumn the column being resized, or <code>null</code> if
681       * no column is being resized
682       */

683     public void setResizingColumn(TableColumn JavaDoc aColumn) {
684     resizingColumn = aColumn;
685     }
686
687     /**
688      * See <code>readObject</code> and <code>writeObject</code> in
689      * <code>JComponent</code> for more
690      * information about serialization in Swing.
691      */

692     private void writeObject(ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
693         s.defaultWriteObject();
694     if ((ui != null) && (getUIClassID().equals(uiClassID))) {
695         ui.installUI(this);
696     }
697     }
698
699     private int getWidthInRightToLeft() {
700     if ((table != null) &&
701         (table.getAutoResizeMode() != JTable.AUTO_RESIZE_OFF)) {
702         return table.getWidth();
703     }
704     return super.getWidth();
705     }
706
707     /**
708      * Returns a string representation of this <code>JTableHeader</code>. This method
709      * is intended to be used only for debugging purposes, and the
710      * content and format of the returned string may vary between
711      * implementations. The returned string may be empty but may not
712      * be <code>null</code>.
713      * <P>
714      * Overriding <code>paramString</code> to provide information about the
715      * specific new aspects of the JFC components.
716      *
717      * @return a string representation of this <code>JTableHeader</code>
718      */

719     protected String JavaDoc paramString() {
720         String JavaDoc reorderingAllowedString = (reorderingAllowed ?
721                       "true" : "false");
722         String JavaDoc resizingAllowedString = (resizingAllowed ?
723                     "true" : "false");
724         String JavaDoc updateTableInRealTimeString = (updateTableInRealTime ?
725                           "true" : "false");
726
727         return super.paramString() +
728         ",draggedDistance=" + draggedDistance +
729         ",reorderingAllowed=" + reorderingAllowedString +
730         ",resizingAllowed=" + resizingAllowedString +
731         ",updateTableInRealTime=" + updateTableInRealTimeString;
732     }
733
734 /////////////////
735
// Accessibility support
736
////////////////
737

738     /**
739      * Gets the AccessibleContext associated with this JTableHeader.
740      * For JTableHeaders, the AccessibleContext takes the form of an
741      * AccessibleJTableHeader.
742      * A new AccessibleJTableHeader instance is created if necessary.
743      *
744      * @return an AccessibleJTableHeader that serves as the
745      * AccessibleContext of this JTableHeader
746      */

747     public AccessibleContext getAccessibleContext() {
748     if (accessibleContext == null) {
749         accessibleContext = new AccessibleJTableHeader();
750     }
751     return accessibleContext;
752     }
753
754     //
755
// *** should also implement AccessibleSelection?
756
// *** and what's up with keyboard navigation/manipulation?
757
//
758
/**
759      * This class implements accessibility support for the
760      * <code>JTableHeader</code> class. It provides an implementation of the
761      * Java Accessibility API appropriate to table header user-interface
762      * elements.
763      * <p>
764      * <strong>Warning:</strong>
765      * Serialized objects of this class will not be compatible with
766      * future Swing releases. The current serialization support is
767      * appropriate for short term storage or RMI between applications running
768      * the same version of Swing. As of 1.4, support for long term storage
769      * of all JavaBeans<sup><font size="-2">TM</font></sup>
770      * has been added to the <code>java.beans</code> package.
771      * Please see {@link java.beans.XMLEncoder}.
772      */

773     protected class AccessibleJTableHeader extends AccessibleJComponent {
774
775         /**
776          * Get the role of this object.
777          *
778          * @return an instance of AccessibleRole describing the role of the
779      * object
780          * @see AccessibleRole
781          */

782         public AccessibleRole getAccessibleRole() {
783             return AccessibleRole.PANEL;
784         }
785
786         /**
787          * Returns the Accessible child, if one exists, contained at the local
788          * coordinate Point.
789          *
790          * @param p The point defining the top-left corner of the Accessible,
791          * given in the coordinate space of the object's parent.
792          * @return the Accessible, if it exists, at the specified location;
793          * else null
794          */

795         public Accessible getAccessibleAt(Point p) {
796             int column;
797
798             // Locate the renderer under the Point
799
if ((column = JTableHeader.this.columnAtPoint(p)) != -1) {
800                 TableColumn JavaDoc aColumn = JTableHeader.this.columnModel.getColumn(column);
801                 TableCellRenderer JavaDoc renderer = aColumn.getHeaderRenderer();
802         if (renderer == null) {
803             if (defaultRenderer != null) {
804             renderer = defaultRenderer;
805             } else {
806             return null;
807             }
808         }
809                 Component component = renderer.getTableCellRendererComponent(
810                                   JTableHeader.this.getTable(),
811                                   aColumn.getHeaderValue(), false, false,
812                                   -1, column);
813
814                 return new AccessibleJTableHeaderEntry(column, JTableHeader.this, JTableHeader.this.table);
815             } else {
816         return null;
817         }
818         }
819
820         /**
821          * Returns the number of accessible children in the object. If all
822          * of the children of this object implement Accessible, than this
823          * method should return the number of children of this object.
824          *
825          * @return the number of accessible children in the object.
826          */

827         public int getAccessibleChildrenCount() {
828             return JTableHeader.this.columnModel.getColumnCount();
829         }
830
831         /**
832          * Return the nth Accessible child of the object.
833          *
834          * @param i zero-based index of child
835          * @return the nth Accessible child of the object
836          */

837         public Accessible getAccessibleChild(int i) {
838             if (i < 0 || i >= getAccessibleChildrenCount()) {
839                 return null;
840             } else {
841                 TableColumn JavaDoc aColumn = JTableHeader.this.columnModel.getColumn(i)
842 ;
843                 TableCellRenderer JavaDoc renderer = aColumn.getHeaderRenderer();
844         if (renderer == null) {
845             if (defaultRenderer != null) {
846             renderer = defaultRenderer;
847             } else {
848             return null;
849             }
850         }
851                 Component component = renderer.getTableCellRendererComponent(
852                                   JTableHeader.this.getTable(),
853                                   aColumn.getHeaderValue(), false, false,
854                                   -1, i);
855
856                 return new AccessibleJTableHeaderEntry(i, JTableHeader.this, JTableHeader.this.table);
857             }
858         }
859
860       /**
861        * This class provides an implementation of the Java Accessibility
862        * API appropropriate for JTableHeader entries.
863        */

864         protected class AccessibleJTableHeaderEntry extends AccessibleContext
865             implements Accessible, AccessibleComponent {
866
867             private JTableHeader JavaDoc parent;
868             private int column;
869         private JTable table;
870
871             /**
872              * Constructs an AccessiblJTableHeaaderEntry
873              */

874             public AccessibleJTableHeaderEntry(int c, JTableHeader JavaDoc p, JTable t) {
875                 parent = p;
876                 column = c;
877         table = t;
878         this.setAccessibleParent(parent);
879             }
880
881             /**
882              * Get the AccessibleContext associated with this object.
883          * In the implementation of the Java Accessibility API
884          * for this class, returns this object, which serves as
885          * its own AccessibleContext.
886              *
887              * @return this object
888              */

889             public AccessibleContext getAccessibleContext() {
890                 return this;
891             }
892
893         private AccessibleContext getCurrentAccessibleContext() {
894         TableColumnModel JavaDoc tcm = table.getColumnModel();
895                 if (tcm != null) {
896             // Fixes 4772355 - ArrayOutOfBoundsException in
897
// JTableHeader
898
if (column < 0 || column >= tcm.getColumnCount()) {
899             return null;
900             }
901             TableColumn JavaDoc aColumn = tcm.getColumn(column);
902             TableCellRenderer JavaDoc renderer = aColumn.getHeaderRenderer();
903             if (renderer == null) {
904             if (defaultRenderer != null) {
905                 renderer = defaultRenderer;
906             } else {
907                 return null;
908             }
909             }
910             Component c = renderer.getTableCellRendererComponent(
911                       JTableHeader.this.getTable(),
912                       aColumn.getHeaderValue(), false, false,
913                       -1, column);
914             if (c instanceof Accessible) {
915             return ((Accessible) c).getAccessibleContext();
916             }
917         }
918         return null;
919         }
920
921         private Component getCurrentComponent() {
922         TableColumnModel JavaDoc tcm = table.getColumnModel();
923                 if (tcm != null) {
924             // Fixes 4772355 - ArrayOutOfBoundsException in
925
// JTableHeader
926
if (column < 0 || column >= tcm.getColumnCount()) {
927             return null;
928             }
929             TableColumn JavaDoc aColumn = tcm.getColumn(column);
930             TableCellRenderer JavaDoc renderer = aColumn.getHeaderRenderer();
931             if (renderer == null) {
932             if (defaultRenderer != null) {
933                 renderer = defaultRenderer;
934             } else {
935                 return null;
936             }
937             }
938             return renderer.getTableCellRendererComponent(
939                       JTableHeader.this.getTable(),
940                       aColumn.getHeaderValue(), false, false,
941                       -1, column);
942         } else {
943             return null;
944         }
945             }
946
947         // AccessibleContext methods
948

949             public String JavaDoc getAccessibleName() {
950                 AccessibleContext ac = getCurrentAccessibleContext();
951                 if (ac != null) {
952             String JavaDoc name = ac.getAccessibleName();
953                     if ((name != null) && (name != "")) {
954             return ac.getAccessibleName();
955             }
956                 }
957         if ((accessibleName != null) && (accessibleName != "")) {
958             return accessibleName;
959         } else {
960                     return table.getColumnName(column);
961                 }
962             }
963
964             public void setAccessibleName(String JavaDoc s) {
965                 AccessibleContext ac = getCurrentAccessibleContext();
966                 if (ac != null) {
967                     ac.setAccessibleName(s);
968                 } else {
969             super.setAccessibleName(s);
970         }
971             }
972
973         //
974
// *** should check toolTip text for desc. (needs MouseEvent)
975
//
976
public String JavaDoc getAccessibleDescription() {
977                 AccessibleContext ac = getCurrentAccessibleContext();
978                 if (ac != null) {
979                     return ac.getAccessibleDescription();
980                 } else {
981                     return super.getAccessibleDescription();
982                 }
983             }
984
985             public void setAccessibleDescription(String JavaDoc s) {
986                 AccessibleContext ac = getCurrentAccessibleContext();
987                 if (ac != null) {
988                     ac.setAccessibleDescription(s);
989                 } else {
990             super.setAccessibleDescription(s);
991         }
992             }
993
994             public AccessibleRole getAccessibleRole() {
995                 AccessibleContext ac = getCurrentAccessibleContext();
996                 if (ac != null) {
997                     return ac.getAccessibleRole();
998                 } else {
999                     return AccessibleRole.COLUMN_HEADER;
1000                }
1001            }
1002
1003            public AccessibleStateSet getAccessibleStateSet() {
1004                AccessibleContext ac = getCurrentAccessibleContext();
1005                if (ac != null) {
1006                    AccessibleStateSet states = ac.getAccessibleStateSet();
1007                    if (isShowing()) {
1008                        states.add(AccessibleState.SHOWING);
1009                    }
1010                    return states;
1011                } else {
1012                    return new AccessibleStateSet(); // must be non null?
1013
}
1014            }
1015
1016            public int getAccessibleIndexInParent() {
1017                return column;
1018            }
1019
1020            public int getAccessibleChildrenCount() {
1021                AccessibleContext ac = getCurrentAccessibleContext();
1022                if (ac != null) {
1023                    return ac.getAccessibleChildrenCount();
1024                } else {
1025                    return 0;
1026                }
1027            }
1028
1029            public Accessible getAccessibleChild(int i) {
1030                AccessibleContext ac = getCurrentAccessibleContext();
1031                if (ac != null) {
1032                    Accessible accessibleChild = ac.getAccessibleChild(i);
1033            ac.setAccessibleParent(this);
1034            return accessibleChild;
1035                } else {
1036                    return null;
1037                }
1038            }
1039
1040            public Locale getLocale() {
1041                AccessibleContext ac = getCurrentAccessibleContext();
1042                if (ac != null) {
1043                    return ac.getLocale();
1044                } else {
1045                    return null;
1046                }
1047            }
1048
1049            public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
1050                AccessibleContext ac = getCurrentAccessibleContext();
1051                if (ac != null) {
1052                    ac.addPropertyChangeListener(l);
1053                } else {
1054            super.addPropertyChangeListener(l);
1055        }
1056            }
1057
1058            public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
1059                AccessibleContext ac = getCurrentAccessibleContext();
1060                if (ac != null) {
1061                    ac.removePropertyChangeListener(l);
1062                } else {
1063            super.removePropertyChangeListener(l);
1064        }
1065            }
1066
1067        public AccessibleAction getAccessibleAction() {
1068                return getCurrentAccessibleContext().getAccessibleAction();
1069        }
1070
1071       /**
1072            * Get the AccessibleComponent associated with this object. In the
1073            * implementation of the Java Accessibility API for this class,
1074            * return this object, which is responsible for implementing the
1075            * AccessibleComponent interface on behalf of itself.
1076            *
1077            * @return this object
1078        */

1079        public AccessibleComponent getAccessibleComponent() {
1080                return this; // to override getBounds()
1081
}
1082
1083        public AccessibleSelection getAccessibleSelection() {
1084                return getCurrentAccessibleContext().getAccessibleSelection();
1085        }
1086
1087        public AccessibleText getAccessibleText() {
1088                return getCurrentAccessibleContext().getAccessibleText();
1089        }
1090
1091        public AccessibleValue getAccessibleValue() {
1092                return getCurrentAccessibleContext().getAccessibleValue();
1093        }
1094
1095
1096        // AccessibleComponent methods
1097

1098            public Color getBackground() {
1099                AccessibleContext ac = getCurrentAccessibleContext();
1100                if (ac instanceof AccessibleComponent) {
1101                    return ((AccessibleComponent) ac).getBackground();
1102                } else {
1103            Component c = getCurrentComponent();
1104            if (c != null) {
1105                        return c.getBackground();
1106            } else {
1107            return null;
1108            }
1109                }
1110            }
1111
1112            public void setBackground(Color c) {
1113                AccessibleContext ac = getCurrentAccessibleContext();
1114                if (ac instanceof AccessibleComponent) {
1115                    ((AccessibleComponent) ac).setBackground(c);
1116                } else {
1117                    Component cp = getCurrentComponent();
1118                    if (cp != null) {
1119                        cp.setBackground(c);
1120                    }
1121                }
1122            }
1123
1124            public Color getForeground() {
1125                AccessibleContext ac = getCurrentAccessibleContext();
1126                if (ac instanceof AccessibleComponent) {
1127                    return ((AccessibleComponent) ac).getForeground();
1128                } else {
1129                    Component c = getCurrentComponent();
1130                    if (c != null) {
1131                        return c.getForeground();
1132                    } else {
1133            return null;
1134            }
1135                }
1136            }
1137
1138            public void setForeground(Color c) {
1139                AccessibleContext ac = getCurrentAccessibleContext();
1140                if (ac instanceof AccessibleComponent) {
1141                    ((AccessibleComponent) ac).setForeground(c);
1142                } else {
1143                    Component cp = getCurrentComponent();
1144                    if (cp != null) {
1145                        cp.setForeground(c);
1146                    }
1147                }
1148            }
1149
1150            public Cursor getCursor() {
1151                AccessibleContext ac = getCurrentAccessibleContext();
1152                if (ac instanceof AccessibleComponent) {
1153                    return ((AccessibleComponent) ac).getCursor();
1154                } else {
1155                    Component c = getCurrentComponent();
1156                    if (c != null) {
1157                        return c.getCursor();
1158                    } else {
1159            Accessible ap = getAccessibleParent();
1160            if (ap instanceof AccessibleComponent) {
1161                return ((AccessibleComponent) ap).getCursor();
1162            } else {
1163                return null;
1164            }
1165            }
1166                }
1167            }
1168
1169            public void setCursor(Cursor c) {
1170                AccessibleContext ac = getCurrentAccessibleContext();
1171                if (ac instanceof AccessibleComponent) {
1172                    ((AccessibleComponent) ac).setCursor(c);
1173                } else {
1174                    Component cp = getCurrentComponent();
1175                    if (cp != null) {
1176                        cp.setCursor(c);
1177            }
1178                }
1179            }
1180
1181            public Font getFont() {
1182                AccessibleContext ac = getCurrentAccessibleContext();
1183                if (ac instanceof AccessibleComponent) {
1184                    return ((AccessibleComponent) ac).getFont();
1185                } else {
1186                    Component c = getCurrentComponent();
1187                    if (c != null) {
1188                        return c.getFont();
1189            } else {
1190            return null;
1191            }
1192                }
1193            }
1194
1195            public void setFont(Font f) {
1196                AccessibleContext ac = getCurrentAccessibleContext();
1197                if (ac instanceof AccessibleComponent) {
1198                    ((AccessibleComponent) ac).setFont(f);
1199                } else {
1200                    Component c = getCurrentComponent();
1201                    if (c != null) {
1202                        c.setFont(f);
1203            }
1204        }
1205            }
1206
1207            public FontMetrics getFontMetrics(Font f) {
1208                AccessibleContext ac = getCurrentAccessibleContext();
1209                if (ac instanceof AccessibleComponent) {
1210                    return ((AccessibleComponent) ac).getFontMetrics(f);
1211                } else {
1212                    Component c = getCurrentComponent();
1213                    if (c != null) {
1214                        return c.getFontMetrics(f);
1215            } else {
1216            return null;
1217            }
1218                }
1219            }
1220
1221            public boolean isEnabled() {
1222                AccessibleContext ac = getCurrentAccessibleContext();
1223                if (ac instanceof AccessibleComponent) {
1224                    return ((AccessibleComponent) ac).isEnabled();
1225                } else {
1226                    Component c = getCurrentComponent();
1227                    if (c != null) {
1228                        return c.isEnabled();
1229                    } else {
1230            return false;
1231            }
1232                }
1233            }
1234
1235            public void setEnabled(boolean b) {
1236                AccessibleContext ac = getCurrentAccessibleContext();
1237                if (ac instanceof AccessibleComponent) {
1238                    ((AccessibleComponent) ac).setEnabled(b);
1239                } else {
1240                    Component c = getCurrentComponent();
1241                    if (c != null) {
1242                        c.setEnabled(b);
1243                    }
1244                }
1245            }
1246
1247            public boolean isVisible() {
1248                AccessibleContext ac = getCurrentAccessibleContext();
1249                if (ac instanceof AccessibleComponent) {
1250                    return ((AccessibleComponent) ac).isVisible();
1251                } else {
1252                    Component c = getCurrentComponent();
1253                    if (c != null) {
1254                        return c.isVisible();
1255                    } else {
1256                        return false;
1257                    }
1258                }
1259            }
1260
1261            public void setVisible(boolean b) {
1262                AccessibleContext ac = getCurrentAccessibleContext();
1263                if (ac instanceof AccessibleComponent) {
1264                    ((AccessibleComponent) ac).setVisible(b);
1265                } else {
1266                    Component c = getCurrentComponent();
1267                    if (c != null) {
1268                        c.setVisible(b);
1269                    }
1270                }
1271            }
1272
1273            public boolean isShowing() {
1274                if (isVisible() && JTableHeader.this.isShowing()) {
1275                    return true;
1276                } else {
1277                    return false;
1278                }
1279            }
1280
1281            public boolean contains(Point p) {
1282                AccessibleContext ac = getCurrentAccessibleContext();
1283                if (ac instanceof AccessibleComponent) {
1284                    Rectangle r = ((AccessibleComponent) ac).getBounds();
1285                    return r.contains(p);
1286                } else {
1287                    Component c = getCurrentComponent();
1288                    if (c != null) {
1289            Rectangle r = c.getBounds();
1290                        return r.contains(p);
1291                    } else {
1292                        return getBounds().contains(p);
1293                    }
1294        }
1295            }
1296
1297            public Point getLocationOnScreen() {
1298                if (parent != null) {
1299                    Point parentLocation = parent.getLocationOnScreen();
1300                    Point componentLocation = getLocation();
1301                    componentLocation.translate(parentLocation.x, parentLocation.y);
1302                    return componentLocation;
1303                } else {
1304                    return null;
1305                }
1306            }
1307
1308            public Point getLocation() {
1309                AccessibleContext ac = getCurrentAccessibleContext();
1310                if (ac instanceof AccessibleComponent) {
1311                    Rectangle r = ((AccessibleComponent) ac).getBounds();
1312                    return r.getLocation();
1313                } else {
1314                    Component c = getCurrentComponent();
1315                    if (c != null) {
1316            Rectangle r = c.getBounds();
1317                        return r.getLocation();
1318                    } else {
1319                        return getBounds().getLocation();
1320                    }
1321        }
1322            }
1323
1324            public void setLocation(Point p) {
1325// if ((parent != null) && (parent.contains(p))) {
1326
// ensureIndexIsVisible(indexInParent);
1327
// }
1328
}
1329
1330            public Rectangle getBounds() {
1331                  Rectangle r = table.getCellRect(-1, column, false);
1332                  r.y = 0;
1333                  return r;
1334
1335// AccessibleContext ac = getCurrentAccessibleContext();
1336
// if (ac instanceof AccessibleComponent) {
1337
// return ((AccessibleComponent) ac).getBounds();
1338
// } else {
1339
// Component c = getCurrentComponent();
1340
// if (c != null) {
1341
// return c.getBounds();
1342
// } else {
1343
// Rectangle r = table.getCellRect(-1, column, false);
1344
// r.y = 0;
1345
// return r;
1346
// }
1347
// }
1348
}
1349
1350            public void setBounds(Rectangle r) {
1351                AccessibleContext ac = getCurrentAccessibleContext();
1352                if (ac instanceof AccessibleComponent) {
1353                    ((AccessibleComponent) ac).setBounds(r);
1354                } else {
1355            Component c = getCurrentComponent();
1356            if (c != null) {
1357            c.setBounds(r);
1358            }
1359        }
1360            }
1361
1362            public Dimension getSize() {
1363        return getBounds().getSize();
1364// AccessibleContext ac = getCurrentAccessibleContext();
1365
// if (ac instanceof AccessibleComponent) {
1366
// Rectangle r = ((AccessibleComponent) ac).getBounds();
1367
// return r.getSize();
1368
// } else {
1369
// Component c = getCurrentComponent();
1370
// if (c != null) {
1371
// Rectangle r = c.getBounds();
1372
// return r.getSize();
1373
// } else {
1374
// return getBounds().getSize();
1375
// }
1376
// }
1377
}
1378
1379            public void setSize (Dimension d) {
1380                AccessibleContext ac = getCurrentAccessibleContext();
1381                if (ac instanceof AccessibleComponent) {
1382                    ((AccessibleComponent) ac).setSize(d);
1383                } else {
1384            Component c = getCurrentComponent();
1385            if (c != null) {
1386            c.setSize(d);
1387            }
1388        }
1389            }
1390
1391            public Accessible getAccessibleAt(Point p) {
1392                AccessibleContext ac = getCurrentAccessibleContext();
1393                if (ac instanceof AccessibleComponent) {
1394                    return ((AccessibleComponent) ac).getAccessibleAt(p);
1395                } else {
1396                    return null;
1397                }
1398            }
1399
1400            public boolean isFocusTraversable() {
1401                AccessibleContext ac = getCurrentAccessibleContext();
1402                if (ac instanceof AccessibleComponent) {
1403                    return ((AccessibleComponent) ac).isFocusTraversable();
1404                } else {
1405            Component c = getCurrentComponent();
1406            if (c != null) {
1407            return c.isFocusTraversable();
1408            } else {
1409            return false;
1410            }
1411                }
1412            }
1413
1414            public void requestFocus() {
1415                AccessibleContext ac = getCurrentAccessibleContext();
1416                if (ac instanceof AccessibleComponent) {
1417                    ((AccessibleComponent) ac).requestFocus();
1418                } else {
1419            Component c = getCurrentComponent();
1420            if (c != null) {
1421            c.requestFocus();
1422            }
1423        }
1424            }
1425
1426            public void addFocusListener(FocusListener l) {
1427                AccessibleContext ac = getCurrentAccessibleContext();
1428                if (ac instanceof AccessibleComponent) {
1429                    ((AccessibleComponent) ac).addFocusListener(l);
1430                } else {
1431            Component c = getCurrentComponent();
1432            if (c != null) {
1433            c.addFocusListener(l);
1434            }
1435        }
1436            }
1437
1438            public void removeFocusListener(FocusListener l) {
1439                AccessibleContext ac = getCurrentAccessibleContext();
1440                if (ac instanceof AccessibleComponent) {
1441                    ((AccessibleComponent) ac).removeFocusListener(l);
1442                } else {
1443                    Component c = getCurrentComponent();
1444                    if (c != null) {
1445                        c.removeFocusListener(l);
1446                    }
1447                }
1448            }
1449
1450        } // inner class AccessibleJTableHeaderElement
1451

1452    } // inner class AccessibleJTableHeader
1453

1454} // End of Class JTableHeader
1455

1456
Popular Tags