-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
JTableHeader.java
1520 lines (1366 loc) · 53.2 KB
/
JTableHeader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.swing.table;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.beans.BeanProperty;
import java.beans.PropertyChangeListener;
import java.beans.Transient;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serial;
import java.util.Locale;
import javax.accessibility.Accessible;
import javax.accessibility.AccessibleAction;
import javax.accessibility.AccessibleComponent;
import javax.accessibility.AccessibleContext;
import javax.accessibility.AccessibleRole;
import javax.accessibility.AccessibleSelection;
import javax.accessibility.AccessibleState;
import javax.accessibility.AccessibleStateSet;
import javax.accessibility.AccessibleText;
import javax.accessibility.AccessibleValue;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.TableColumnModelListener;
import javax.swing.plaf.TableHeaderUI;
import sun.awt.AWTAccessor;
import sun.awt.AWTAccessor.MouseEventAccessor;
import sun.swing.table.DefaultTableCellHeaderRenderer;
/**
* This is the object which manages the header of the <code>JTable</code>.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans
* has been added to the <code>java.beans</code> package.
* Please see {@link java.beans.XMLEncoder}.
*
* @author Alan Chung
* @author Philip Milne
* @see javax.swing.JTable
*/
@SuppressWarnings("serial") // Same-version serialization only
public class JTableHeader extends JComponent implements TableColumnModelListener, Accessible
{
/**
* @see #getUIClassID
* @see #readObject
*/
private static final String uiClassID = "TableHeaderUI";
//
// Instance Variables
//
/**
* The table for which this object is the header;
* the default is <code>null</code>.
*/
protected JTable table;
/**
* The <code>TableColumnModel</code> of the table header.
*/
protected TableColumnModel columnModel;
/**
* If true, reordering of columns are allowed by the user;
* the default is true.
*/
protected boolean reorderingAllowed;
/**
* If true, resizing of columns are allowed by the user;
* the default is true.
*/
protected boolean resizingAllowed;
/**
* Obsolete as of Java 2 platform v1.3. Real time repaints, in response
* to column dragging or resizing, are now unconditional.
*/
/*
* If this flag is true, then the header will repaint the table as
* a column is dragged or resized; the default is true.
*/
protected boolean updateTableInRealTime;
/** The index of the column being resized. <code>null</code> if not resizing. */
protected transient TableColumn resizingColumn;
/** The index of the column being dragged. <code>null</code> if not dragging. */
protected transient TableColumn draggedColumn;
/** The distance from its original position the column has been dragged. */
protected transient int draggedDistance;
/**
* The default renderer to be used when a <code>TableColumn</code>
* does not define a <code>headerRenderer</code>.
*/
private TableCellRenderer defaultRenderer;
/**
* Flag to indicate UI update is in progress
*/
private transient boolean updateInProgress;
//
// Constructors
//
/**
* Constructs a <code>JTableHeader</code> with a default
* <code>TableColumnModel</code>.
*
* @see #createDefaultColumnModel
*/
public JTableHeader() {
this(null);
}
/**
* Constructs a <code>JTableHeader</code> which is initialized with
* <code>cm</code> as the column model. If <code>cm</code> is
* <code>null</code> this method will initialize the table header
* with a default <code>TableColumnModel</code>.
*
* @param cm the column model for the table
* @see #createDefaultColumnModel
*/
public JTableHeader(TableColumnModel cm) {
super();
//setFocusable(false); // for strict win/mac compatibility mode,
// this method should be invoked
if (cm == null)
cm = createDefaultColumnModel();
setColumnModel(cm);
// Initialize local ivars
initializeLocalVars();
// Get UI going
updateUI();
}
//
// Local behavior attributes
//
/**
* Sets the table associated with this header.
* @param table the new table
*/
@BeanProperty(description
= "The table associated with this header.")
public void setTable(JTable table) {
JTable old = this.table;
this.table = table;
firePropertyChange("table", old, table);
}
/**
* Returns the table associated with this header.
* @return the <code>table</code> property
*/
public JTable getTable() {
return table;
}
/**
* Sets whether the user can drag column headers to reorder columns.
*
* @param reorderingAllowed true if the table view should allow
* reordering; otherwise false
* @see #getReorderingAllowed
*/
@BeanProperty(description
= "Whether the user can drag column headers to reorder columns.")
public void setReorderingAllowed(boolean reorderingAllowed) {
boolean old = this.reorderingAllowed;
this.reorderingAllowed = reorderingAllowed;
firePropertyChange("reorderingAllowed", old, reorderingAllowed);
}
/**
* Returns true if the user is allowed to rearrange columns by
* dragging their headers, false otherwise. The default is true. You can
* rearrange columns programmatically regardless of this setting.
*
* @return the <code>reorderingAllowed</code> property
* @see #setReorderingAllowed
*/
public boolean getReorderingAllowed() {
return reorderingAllowed;
}
/**
* Sets whether the user can resize columns by dragging between headers.
*
* @param resizingAllowed true if table view should allow
* resizing
* @see #getResizingAllowed
*/
@BeanProperty(description
= "Whether the user can resize columns by dragging between headers.")
public void setResizingAllowed(boolean resizingAllowed) {
boolean old = this.resizingAllowed;
this.resizingAllowed = resizingAllowed;
firePropertyChange("resizingAllowed", old, resizingAllowed);
}
/**
* Returns true if the user is allowed to resize columns by dragging
* between their headers, false otherwise. The default is true. You can
* resize columns programmatically regardless of this setting.
*
* @return the <code>resizingAllowed</code> property
* @see #setResizingAllowed
*/
public boolean getResizingAllowed() {
return resizingAllowed;
}
/**
* Returns the dragged column, if and only if, a drag is in
* process, otherwise returns <code>null</code>.
*
* @return the dragged column, if a drag is in
* process, otherwise returns <code>null</code>
* @see #getDraggedDistance
*/
public TableColumn getDraggedColumn() {
return draggedColumn;
}
/**
* Returns the column's horizontal distance from its original
* position, if and only if, a drag is in process. Otherwise, the
* the return value is meaningless.
*
* @return the column's horizontal distance from its original
* position, if a drag is in process, otherwise the return
* value is meaningless
* @see #getDraggedColumn
*/
public int getDraggedDistance() {
return draggedDistance;
}
/**
* Returns the resizing column. If no column is being
* resized this method returns <code>null</code>.
*
* @return the resizing column, if a resize is in process, otherwise
* returns <code>null</code>
*/
public TableColumn getResizingColumn() {
return resizingColumn;
}
/**
* Obsolete as of Java 2 platform v1.3. Real time repaints, in response to
* column dragging or resizing, are now unconditional.
* @param flag true if tableView should update the body of the
* table in real time
*/
public void setUpdateTableInRealTime(boolean flag) {
updateTableInRealTime = flag;
}
/**
* Obsolete as of Java 2 platform v1.3. Real time repaints, in response to
* column dragging or resizing, are now unconditional.
* @return true if the table updates in real time
*/
public boolean getUpdateTableInRealTime() {
return updateTableInRealTime;
}
/**
* Sets the default renderer to be used when no <code>headerRenderer</code>
* is defined by a <code>TableColumn</code>.
* @param defaultRenderer the default renderer
* @since 1.3
*/
public void setDefaultRenderer(TableCellRenderer defaultRenderer) {
this.defaultRenderer = defaultRenderer;
}
/**
* Returns the default renderer used when no <code>headerRenderer</code>
* is defined by a <code>TableColumn</code>.
* @return the default renderer
* @since 1.3
*/
@Transient
public TableCellRenderer getDefaultRenderer() {
return defaultRenderer;
}
/**
* Returns the index of the column that <code>point</code> lies in, or -1 if it
* lies out of bounds.
*
* @param point if this <code>point</code> lies within a column, the index of
* that column will be returned; otherwise it is out of bounds
* and -1 is returned
*
* @return the index of the column that <code>point</code> lies in, or -1 if it
* lies out of bounds
*/
public int columnAtPoint(Point point) {
int x = point.x;
if (!getComponentOrientation().isLeftToRight()) {
x = getWidthInRightToLeft() - x - 1;
}
return getColumnModel().getColumnIndexAtX(x);
}
/**
* Returns the rectangle containing the header tile at <code>column</code>.
* When the <code>column</code> parameter is out of bounds this method uses the
* same conventions as the <code>JTable</code> method <code>getCellRect</code>.
*
* @param column index of the column
*
* @return the rectangle containing the header tile at <code>column</code>
* @see JTable#getCellRect
*/
public Rectangle getHeaderRect(int column) {
Rectangle r = new Rectangle();
TableColumnModel cm = getColumnModel();
r.height = getHeight();
if (column < 0) {
// x = width = 0;
if( !getComponentOrientation().isLeftToRight() ) {
r.x = getWidthInRightToLeft();
}
}
else if (column >= cm.getColumnCount()) {
if( getComponentOrientation().isLeftToRight() ) {
r.x = getWidth();
}
}
else {
for(int i = 0; i < column; i++) {
r.x += cm.getColumn(i).getWidth();
}
if( !getComponentOrientation().isLeftToRight() ) {
r.x = getWidthInRightToLeft() - r.x - cm.getColumn(column).getWidth();
}
r.width = cm.getColumn(column).getWidth();
}
return r;
}
/**
* Allows the renderer's tips to be used if there is text set.
* @param event the location of the event identifies the proper
* renderer and, therefore, the proper tip
* @return the tool tip for this component
*/
@SuppressWarnings("deprecation")
public String getToolTipText(MouseEvent event) {
String tip = null;
Point p = event.getPoint();
int column;
// Locate the renderer under the event location
if ((column = columnAtPoint(p)) != -1) {
TableColumn aColumn = columnModel.getColumn(column);
TableCellRenderer renderer = aColumn.getHeaderRenderer();
if (renderer == null) {
renderer = defaultRenderer;
}
Component component = renderer.getTableCellRendererComponent(
getTable(), aColumn.getHeaderValue(), false, false,
-1, column);
// Now have to see if the component is a JComponent before
// getting the tip
if (component instanceof JComponent) {
// Convert the event to the renderer's coordinate system
MouseEvent newEvent;
Rectangle cellRect = getHeaderRect(column);
p.translate(-cellRect.x, -cellRect.y);
newEvent = new MouseEvent(component, event.getID(),
event.getWhen(), event.getModifiers(),
p.x, p.y, event.getXOnScreen(), event.getYOnScreen(),
event.getClickCount(),
event.isPopupTrigger(), MouseEvent.NOBUTTON);
MouseEventAccessor meAccessor = AWTAccessor.getMouseEventAccessor();
meAccessor.setCausedByTouchEvent(newEvent,
meAccessor.isCausedByTouchEvent(event));
tip = ((JComponent)component).getToolTipText(newEvent);
}
}
// No tip from the renderer get our own tip
if (tip == null)
tip = getToolTipText();
return tip;
}
/**
* Returns the preferred size of the table header.
* This is the size required to display the header and requested for
* the viewport.
* The returned {@code Dimension} {@code width} will always be calculated by
* the underlying TableHeaderUI, regardless of any width specified by
* {@link JComponent#setPreferredSize(java.awt.Dimension)}
*
* @return the size
*/
@Override
public Dimension getPreferredSize() {
Dimension preferredSize = super.getPreferredSize();
if (isPreferredSizeSet() && ui != null) {
Dimension size = ui.getPreferredSize(this);
if (size != null) preferredSize.width = size.width;
}
return preferredSize;
}
//
// Managing TableHeaderUI
//
/**
* Returns the look and feel (L&F) object that renders this component.
*
* @return the <code>TableHeaderUI</code> object that renders this component
*/
public TableHeaderUI getUI() {
return (TableHeaderUI)ui;
}
/**
* Sets the look and feel (L&F) object that renders this component.
*
* @param ui the <code>TableHeaderUI</code> L&F object
* @see UIDefaults#getUI
*/
public void setUI(TableHeaderUI ui){
if (this.ui != ui) {
super.setUI(ui);
repaint();
}
}
/**
* Notification from the <code>UIManager</code> that the look and feel
* (L&F) has changed.
* Replaces the current UI object with the latest version from the
* <code>UIManager</code>.
*
* @see JComponent#updateUI
*/
public void updateUI(){
if (!updateInProgress) {
updateInProgress = true;
try {
setUI((TableHeaderUI)UIManager.getUI(this));
TableCellRenderer renderer = getDefaultRenderer();
if (renderer instanceof Component) {
SwingUtilities.updateComponentTreeUI((Component)renderer);
}
} finally {
updateInProgress = false;
}
}
}
/**
* Returns the suffix used to construct the name of the look and feel
* (L&F) class used to render this component.
*
* @return the string "TableHeaderUI"
* @see JComponent#getUIClassID
* @see UIDefaults#getUI
*/
public String getUIClassID() {
return uiClassID;
}
//
// Managing models
//
/**
* Sets the column model for this table to <code>newModel</code> and registers
* for listener notifications from the new column model.
*
* @param columnModel the new data source for this table
* @throws IllegalArgumentException
* if <code>newModel</code> is <code>null</code>
* @see #getColumnModel
*/
@BeanProperty(description
= "The object governing the way columns appear in the view.")
public void setColumnModel(TableColumnModel columnModel) {
if (columnModel == null) {
throw new IllegalArgumentException("Cannot set a null ColumnModel");
}
TableColumnModel old = this.columnModel;
if (columnModel != old) {
if (old != null) {
old.removeColumnModelListener(this);
}
this.columnModel = columnModel;
columnModel.addColumnModelListener(this);
firePropertyChange("columnModel", old, columnModel);
resizeAndRepaint();
}
}
/**
* Returns the <code>TableColumnModel</code> that contains all column information
* of this table header.
*
* @return the <code>columnModel</code> property
* @see #setColumnModel
*/
public TableColumnModel getColumnModel() {
return columnModel;
}
//
// Implementing TableColumnModelListener interface
//
/**
* Invoked when a column is added to the table column model.
* <p>
* Application code will not use these methods explicitly, they
* are used internally by <code>JTable</code>.
*
* @param e the event received
* @see TableColumnModelListener
*/
public void columnAdded(TableColumnModelEvent e) { resizeAndRepaint(); }
/**
* Invoked when a column is removed from the table column model.
* <p>
* Application code will not use these methods explicitly, they
* are used internally by <code>JTable</code>.
*
* @param e the event received
* @see TableColumnModelListener
*/
public void columnRemoved(TableColumnModelEvent e) { resizeAndRepaint(); }
/**
* Invoked when a column is repositioned.
* <p>
* Application code will not use these methods explicitly, they
* are used internally by <code>JTable</code>.
*
* @param e the event received
* @see TableColumnModelListener
*/
public void columnMoved(TableColumnModelEvent e) { repaint(); }
/**
* Invoked when a column is moved due to a margin change.
* <p>
* Application code will not use these methods explicitly, they
* are used internally by <code>JTable</code>.
*
* @param e the event received
* @see TableColumnModelListener
*/
public void columnMarginChanged(ChangeEvent e) { resizeAndRepaint(); }
// --Redrawing the header is slow in cell selection mode.
// --Since header selection is ugly and it is always clear from the
// --view which columns are selected, don't redraw the header.
/**
* Invoked when the selection model of the <code>TableColumnModel</code>
* is changed. This method currently has no effect (the header is not
* redrawn).
* <p>
* Application code will not use these methods explicitly, they
* are used internally by <code>JTable</code>.
*
* @param e the event received
* @see TableColumnModelListener
*/
public void columnSelectionChanged(ListSelectionEvent e) { } // repaint(); }
//
// Package Methods
//
/**
* Returns the default column model object which is
* a <code>DefaultTableColumnModel</code>. A subclass can override this
* method to return a different column model object
*
* @return the default column model object
*/
protected TableColumnModel createDefaultColumnModel() {
return new DefaultTableColumnModel();
}
/**
* Returns a default renderer to be used when no header renderer
* is defined by a <code>TableColumn</code>.
*
* @return the default table column renderer
* @since 1.3
*/
protected TableCellRenderer createDefaultRenderer() {
return new DefaultTableCellHeaderRenderer();
}
/**
* Initializes the local variables and properties with default values.
* Used by the constructor methods.
*/
protected void initializeLocalVars() {
setOpaque(true);
table = null;
reorderingAllowed = true;
resizingAllowed = true;
draggedColumn = null;
draggedDistance = 0;
resizingColumn = null;
updateTableInRealTime = true;
// I'm registered to do tool tips so we can draw tips for the
// renderers
ToolTipManager toolTipManager = ToolTipManager.sharedInstance();
toolTipManager.registerComponent(this);
setDefaultRenderer(createDefaultRenderer());
}
/**
* Sizes the header and marks it as needing display. Equivalent
* to <code>revalidate</code> followed by <code>repaint</code>.
*/
public void resizeAndRepaint() {
revalidate();
repaint();
}
/**
* Sets the header's <code>draggedColumn</code> to <code>aColumn</code>.
* <p>
* Application code will not use this method explicitly, it is used
* internally by the column dragging mechanism.
*
* @param aColumn the column being dragged, or <code>null</code> if
* no column is being dragged
*/
public void setDraggedColumn(TableColumn aColumn) {
draggedColumn = aColumn;
}
/**
* Sets the header's <code>draggedDistance</code> to <code>distance</code>.
* @param distance the distance dragged
*/
public void setDraggedDistance(int distance) {
draggedDistance = distance;
}
/**
* Sets the header's <code>resizingColumn</code> to <code>aColumn</code>.
* <p>
* Application code will not use this method explicitly, it
* is used internally by the column sizing mechanism.
*
* @param aColumn the column being resized, or <code>null</code> if
* no column is being resized
*/
public void setResizingColumn(TableColumn aColumn) {
resizingColumn = aColumn;
}
private int getWidthInRightToLeft() {
if ((table != null) &&
(table.getAutoResizeMode() != JTable.AUTO_RESIZE_OFF)) {
return table.getWidth();
}
return super.getWidth();
}
/**
* Returns a string representation of this <code>JTableHeader</code>. This method
* is intended to be used only for debugging purposes, and the
* content and format of the returned string may vary between
* implementations. The returned string may be empty but may not
* be <code>null</code>.
* <P>
* Overriding <code>paramString</code> to provide information about the
* specific new aspects of the JFC components.
*
* @return a string representation of this <code>JTableHeader</code>
*/
protected String paramString() {
String reorderingAllowedString = (reorderingAllowed ?
"true" : "false");
String resizingAllowedString = (resizingAllowed ?
"true" : "false");
String updateTableInRealTimeString = (updateTableInRealTime ?
"true" : "false");
return super.paramString() +
",draggedDistance=" + draggedDistance +
",reorderingAllowed=" + reorderingAllowedString +
",resizingAllowed=" + resizingAllowedString +
",updateTableInRealTime=" + updateTableInRealTimeString;
}
/////////////////
// Accessibility support
////////////////
/**
* Gets the AccessibleContext associated with this JTableHeader.
* For JTableHeaders, the AccessibleContext takes the form of an
* AccessibleJTableHeader.
* A new AccessibleJTableHeader instance is created if necessary.
*
* @return an AccessibleJTableHeader that serves as the
* AccessibleContext of this JTableHeader
*/
public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessibleJTableHeader();
}
return accessibleContext;
}
//
// *** should also implement AccessibleSelection?
// *** and what's up with keyboard navigation/manipulation?
//
/**
* This class implements accessibility support for the
* <code>JTableHeader</code> class. It provides an implementation of the
* Java Accessibility API appropriate to table header user-interface
* elements.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans
* has been added to the <code>java.beans</code> package.
* Please see {@link java.beans.XMLEncoder}.
*/
@SuppressWarnings("serial") // Same-version serialization only
protected class AccessibleJTableHeader extends AccessibleJComponent {
/**
* Constructs an {@code AccessibleJTableHeader}.
*/
protected AccessibleJTableHeader() {}
/**
* Get the role of this object.
*
* @return an instance of AccessibleRole describing the role of the
* object
* @see AccessibleRole
*/
public AccessibleRole getAccessibleRole() {
return AccessibleRole.PANEL;
}
/**
* Returns the Accessible child, if one exists, contained at the local
* coordinate Point.
*
* @param p The point defining the top-left corner of the Accessible,
* given in the coordinate space of the object's parent.
* @return the Accessible, if it exists, at the specified location;
* else null
*/
public Accessible getAccessibleAt(Point p) {
int column;
// Locate the renderer under the Point
if ((column = JTableHeader.this.columnAtPoint(p)) != -1) {
TableColumn aColumn = JTableHeader.this.columnModel.getColumn(column);
TableCellRenderer renderer = aColumn.getHeaderRenderer();
if (renderer == null) {
if (defaultRenderer != null) {
renderer = defaultRenderer;
} else {
return null;
}
}
Component component = renderer.getTableCellRendererComponent(
JTableHeader.this.getTable(),
aColumn.getHeaderValue(), false, false,
-1, column);
return new AccessibleJTableHeaderEntry(column, JTableHeader.this, JTableHeader.this.table);
} else {
return null;
}
}
/**
* Returns the number of accessible children in the object. If all
* of the children of this object implement Accessible, than this
* method should return the number of children of this object.
*
* @return the number of accessible children in the object.
*/
public int getAccessibleChildrenCount() {
return JTableHeader.this.columnModel.getColumnCount();
}
/**
* Return the nth Accessible child of the object.
*
* @param i zero-based index of child
* @return the nth Accessible child of the object
*/
public Accessible getAccessibleChild(int i) {
if (i < 0 || i >= getAccessibleChildrenCount()) {
return null;
} else {
TableColumn aColumn = JTableHeader.this.columnModel.getColumn(i)
;
TableCellRenderer renderer = aColumn.getHeaderRenderer();
if (renderer == null) {
if (defaultRenderer != null) {
renderer = defaultRenderer;
} else {
return null;
}
}
Component component = renderer.getTableCellRendererComponent(
JTableHeader.this.getTable(),
aColumn.getHeaderValue(), false, false,
-1, i);
return new AccessibleJTableHeaderEntry(i, JTableHeader.this, JTableHeader.this.table);
}
}
/**
* This class provides an implementation of the Java Accessibility
* API appropriate for JTableHeader entries.
*/
protected class AccessibleJTableHeaderEntry extends AccessibleContext
implements Accessible, AccessibleComponent {
private JTableHeader parent;
private int column;
private JTable table;
/**
* Constructs an AccessibleJTableHeaderEntry
* @since 1.4
*
* @param c the column index
* @param p the parent <code>JTableHeader</code>
* @param t the table <code>JTable</code>
*/
public AccessibleJTableHeaderEntry(int c, JTableHeader p, JTable t) {
parent = p;
column = c;
table = t;
this.setAccessibleParent(parent);
}
/**
* Get the AccessibleContext associated with this object.
* In the implementation of the Java Accessibility API
* for this class, returns this object, which serves as
* its own AccessibleContext.
*
* @return this object
*/
public AccessibleContext getAccessibleContext() {
return this;
}
private AccessibleContext getCurrentAccessibleContext() {
TableColumnModel tcm = table.getColumnModel();
if (tcm != null) {
// Fixes 4772355 - ArrayOutOfBoundsException in
// JTableHeader
if (column < 0 || column >= tcm.getColumnCount()) {
return null;
}
TableColumn aColumn = tcm.getColumn(column);
TableCellRenderer renderer = aColumn.getHeaderRenderer();
if (renderer == null) {
if (defaultRenderer != null) {
renderer = defaultRenderer;
} else {
return null;
}
}
Component c = renderer.getTableCellRendererComponent(
JTableHeader.this.getTable(),
aColumn.getHeaderValue(), false, false,
-1, column);
if (c instanceof Accessible) {
return ((Accessible) c).getAccessibleContext();
}
}
return null;
}
private Component getCurrentComponent() {
TableColumnModel tcm = table.getColumnModel();
if (tcm != null) {
// Fixes 4772355 - ArrayOutOfBoundsException in
// JTableHeader
if (column < 0 || column >= tcm.getColumnCount()) {
return null;
}
TableColumn aColumn = tcm.getColumn(column);
TableCellRenderer renderer = aColumn.getHeaderRenderer();
if (renderer == null) {
if (defaultRenderer != null) {
renderer = defaultRenderer;
} else {
return null;
}
}
return renderer.getTableCellRendererComponent(
JTableHeader.this.getTable(),
aColumn.getHeaderValue(), false, false,
-1, column);
} else {