- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathJList.java
3853 lines (3513 loc) · 146 KB
/
JList.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.
*/
packagejavax.swing;
importjava.awt.Color;
importjava.awt.Component;
importjava.awt.Container;
importjava.awt.Cursor;
importjava.awt.Dimension;
importjava.awt.Font;
importjava.awt.FontMetrics;
importjava.awt.GraphicsEnvironment;
importjava.awt.HeadlessException;
importjava.awt.IllegalComponentStateException;
importjava.awt.Insets;
importjava.awt.Point;
importjava.awt.Rectangle;
importjava.awt.event.FocusListener;
importjava.awt.event.MouseEvent;
importjava.beans.BeanProperty;
importjava.beans.JavaBean;
importjava.beans.PropertyChangeEvent;
importjava.beans.PropertyChangeListener;
importjava.beans.Transient;
importjava.io.IOException;
importjava.io.ObjectOutputStream;
importjava.io.Serial;
importjava.io.Serializable;
importjava.util.ArrayList;
importjava.util.Collections;
importjava.util.List;
importjava.util.Locale;
importjava.util.Vector;
importjavax.accessibility.Accessible;
importjavax.accessibility.AccessibleAction;
importjavax.accessibility.AccessibleComponent;
importjavax.accessibility.AccessibleContext;
importjavax.accessibility.AccessibleIcon;
importjavax.accessibility.AccessibleRole;
importjavax.accessibility.AccessibleSelection;
importjavax.accessibility.AccessibleState;
importjavax.accessibility.AccessibleStateSet;
importjavax.accessibility.AccessibleText;
importjavax.accessibility.AccessibleValue;
importjavax.swing.event.EventListenerList;
importjavax.swing.event.ListDataEvent;
importjavax.swing.event.ListDataListener;
importjavax.swing.event.ListSelectionEvent;
importjavax.swing.event.ListSelectionListener;
importjavax.swing.plaf.ListUI;
importjavax.swing.text.Position;
importsun.awt.AWTAccessor;
importsun.awt.AWTAccessor.MouseEventAccessor;
importsun.swing.SwingUtilities2;
importsun.swing.SwingUtilities2.Section;
importstaticsun.swing.SwingUtilities2.Section.LEADING;
importstaticsun.swing.SwingUtilities2.Section.TRAILING;
/**
* A component that displays a list of objects and allows the user to select
* one or more items. A separate model, {@code ListModel}, maintains the
* contents of the list.
* <p>
* It's easy to display an array or Vector of objects, using the {@code JList}
* constructor that automatically builds a read-only {@code ListModel} instance
* for you:
* <pre>
* {@code
* // Create a JList that displays strings from an array
*
* String[] data = {"one", "two", "three", "four"};
* JList<String> myList = new JList<String>(data);
*
* // Create a JList that displays the superclasses of JList.class, by
* // creating it with a Vector populated with this data
*
* Vector<Class<?>> superClasses = new Vector<Class<?>>();
* Class<JList> rootClass = javax.swing.JList.class;
* for(Class<?> cls = rootClass; cls != null; cls = cls.getSuperclass()) {
* superClasses.addElement(cls);
* }
* JList<Class<?>> myList = new JList<Class<?>>(superClasses);
*
* // The automatically created model is stored in JList's "model"
* // property, which you can retrieve
*
* ListModel<Class<?>> model = myList.getModel();
* for(int i = 0; i < model.getSize(); i++) {
* System.out.println(model.getElementAt(i));
* }
* }
* </pre>
* <p>
* A {@code ListModel} can be supplied directly to a {@code JList} by way of a
* constructor or the {@code setModel} method. The contents need not be static -
* the number of items, and the values of items can change over time. A correct
* {@code ListModel} implementation notifies the set of
* {@code javax.swing.event.ListDataListener}s that have been added to it, each
* time a change occurs. These changes are characterized by a
* {@code javax.swing.event.ListDataEvent}, which identifies the range of list
* indices that have been modified, added, or removed. {@code JList}'s
* {@code ListUI} is responsible for keeping the visual representation up to
* date with changes, by listening to the model.
* <p>
* Simple, dynamic-content, {@code JList} applications can use the
* {@code DefaultListModel} class to maintain list elements. This class
* implements the {@code ListModel} interface and also provides a
* <code>java.util.Vector</code>-like API. Applications that need a more
* custom <code>ListModel</code> implementation may instead wish to subclass
* {@code AbstractListModel}, which provides basic support for managing and
* notifying listeners. For example, a read-only implementation of
* {@code AbstractListModel}:
* <pre>
* {@code
* // This list model has about 2^16 elements. Enjoy scrolling.
*
* ListModel<String> bigData = new AbstractListModel<String>() {
* public int getSize() { return Short.MAX_VALUE; }
* public String getElementAt(int index) { return "Index " + index; }
* };
* }
* </pre>
* <p>
* The selection state of a {@code JList} is managed by another separate
* model, an instance of {@code ListSelectionModel}. {@code JList} is
* initialized with a selection model on construction, and also contains
* methods to query or set this selection model. Additionally, {@code JList}
* provides convenient methods for easily managing the selection. These methods,
* such as {@code setSelectedIndex} and {@code getSelectedValue}, are cover
* methods that take care of the details of interacting with the selection
* model. By default, {@code JList}'s selection model is configured to allow any
* combination of items to be selected at a time; selection mode
* {@code MULTIPLE_INTERVAL_SELECTION}. The selection mode can be changed
* on the selection model directly, or via {@code JList}'s cover method.
* Responsibility for updating the selection model in response to user gestures
* lies with the list's {@code ListUI}.
* <p>
* A correct {@code ListSelectionModel} implementation notifies the set of
* {@code javax.swing.event.ListSelectionListener}s that have been added to it
* each time a change to the selection occurs. These changes are characterized
* by a {@code javax.swing.event.ListSelectionEvent}, which identifies the range
* of the selection change.
* <p>
* The preferred way to listen for changes in list selection is to add
* {@code ListSelectionListener}s directly to the {@code JList}. {@code JList}
* then takes care of listening to the selection model and notifying your
* listeners of change.
* <p>
* Responsibility for listening to selection changes in order to keep the list's
* visual representation up to date lies with the list's {@code ListUI}.
* <p>
* <a id="renderer"></a>
* Painting of cells in a {@code JList} is handled by a delegate called a
* cell renderer, installed on the list as the {@code cellRenderer} property.
* The renderer provides a {@code java.awt.Component} that is used
* like a "rubber stamp" to paint the cells. Each time a cell needs to be
* painted, the list's {@code ListUI} asks the cell renderer for the component,
* moves it into place, and has it paint the contents of the cell by way of its
* {@code paint} method. A default cell renderer, which uses a {@code JLabel}
* component to render, is installed by the lists's {@code ListUI}. You can
* substitute your own renderer using code like this:
* <pre>
* {@code
* // Display an icon and a string for each object in the list.
*
* class MyCellRenderer extends JLabel implements ListCellRenderer<Object> {
* static final ImageIcon longIcon = new ImageIcon("long.gif");
* static final ImageIcon shortIcon = new ImageIcon("short.gif");
*
* // This is the only method defined by ListCellRenderer.
* // We just reconfigure the JLabel each time we're called.
*
* public Component getListCellRendererComponent(
* JList<?> list, // the list
* Object value, // value to display
* int index, // cell index
* boolean isSelected, // is the cell selected
* boolean cellHasFocus) // does the cell have focus
* {
* String s = value.toString();
* setText(s);
* setIcon((s.length() > 10) ? longIcon : shortIcon);
* if (isSelected) {
* setBackground(list.getSelectionBackground());
* setForeground(list.getSelectionForeground());
* } else {
* setBackground(list.getBackground());
* setForeground(list.getForeground());
* }
* setEnabled(list.isEnabled());
* setFont(list.getFont());
* setOpaque(true);
* return this;
* }
* }
*
* myList.setCellRenderer(new MyCellRenderer());
* }
* </pre>
* <p>
* Another job for the cell renderer is in helping to determine sizing
* information for the list. By default, the list's {@code ListUI} determines
* the size of cells by asking the cell renderer for its preferred
* size for each list item. This can be expensive for large lists of items.
* To avoid these calculations, you can set a {@code fixedCellWidth} and
* {@code fixedCellHeight} on the list, or have these values calculated
* automatically based on a single prototype value:
* <a id="prototype_example"></a>
* <pre>
* {@code
* JList<String> bigDataList = new JList<String>(bigData);
*
* // We don't want the JList implementation to compute the width
* // or height of all of the list cells, so we give it a string
* // that's as big as we'll need for any cell. It uses this to
* // compute values for the fixedCellWidth and fixedCellHeight
* // properties.
*
* bigDataList.setPrototypeCellValue("Index 1234567890");
* }
* </pre>
* <p>
* {@code JList} doesn't implement scrolling directly. To create a list that
* scrolls, make it the viewport view of a {@code JScrollPane}. For example:
* <pre>
* JScrollPane scrollPane = new JScrollPane(myList);
*
* // Or in two steps:
* JScrollPane scrollPane = new JScrollPane();
* scrollPane.getViewport().setView(myList);
* </pre>
* <p>
* {@code JList} doesn't provide any special handling of double or triple
* (or N) mouse clicks, but it's easy to add a {@code MouseListener} if you
* wish to take action on these events. Use the {@code locationToIndex}
* method to determine what cell was clicked. For example:
* <pre>
* MouseListener mouseListener = new MouseAdapter() {
* public void mouseClicked(MouseEvent e) {
* if (e.getClickCount() == 2) {
* int index = list.locationToIndex(e.getPoint());
* System.out.println("Double clicked on Item " + index);
* }
* }
* };
* list.addMouseListener(mouseListener);
* </pre>
* <p>
* <strong>Warning:</strong> Swing is not thread safe. For more
* information see <a
* href="package-summary.html#threading">Swing's Threading
* Policy</a>.
* <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}.
* <p>
* See <a href="https://docs.oracle.com/javase/tutorial/uiswing/components/list.html">How to Use Lists</a>
* in <a href="https://docs.oracle.com/javase/tutorial/"><em>The Java Tutorial</em></a>
* for further documentation.
*
* @see ListModel
* @see AbstractListModel
* @see DefaultListModel
* @see ListSelectionModel
* @see DefaultListSelectionModel
* @see ListCellRenderer
* @see DefaultListCellRenderer
*
* @param <E> the type of the elements of this list
*
* @author Hans Muller
* @since 1.2
*/
@JavaBean(defaultProperty = "UI", description = "A component which allows for the selection of one or more objects from a list.")
@SwingContainer(false)
@SuppressWarnings("serial") // Same-version serialization only
publicclassJList<E> extendsJComponentimplementsScrollable, Accessible
{
/**
* @see #getUIClassID
* @see #readObject
*/
privatestaticfinalStringuiClassID = "ListUI";
/**
* Indicates a vertical layout of cells, in a single column;
* the default layout.
* @see #setLayoutOrientation
* @since 1.4
*/
publicstaticfinalintVERTICAL = 0;
/**
* Indicates a "newspaper style" layout with cells flowing vertically
* then horizontally.
* @see #setLayoutOrientation
* @since 1.4
*/
publicstaticfinalintVERTICAL_WRAP = 1;
/**
* Indicates a "newspaper style" layout with cells flowing horizontally
* then vertically.
* @see #setLayoutOrientation
* @since 1.4
*/
publicstaticfinalintHORIZONTAL_WRAP = 2;
privateintfixedCellWidth = -1;
privateintfixedCellHeight = -1;
privateinthorizontalScrollIncrement = -1;
privateEprototypeCellValue;
privateintvisibleRowCount = 8;
privateColorselectionForeground;
privateColorselectionBackground;
privatebooleandragEnabled;
privateListSelectionModelselectionModel;
privateListModel<E> dataModel;
privateListCellRenderer<? superE> cellRenderer;
privateListSelectionListenerselectionListener;
/**
* How to lay out the cells; defaults to <code>VERTICAL</code>.
*/
privateintlayoutOrientation;
/**
* The drop mode for this component.
*/
privateDropModedropMode = DropMode.USE_SELECTION;
/**
* The drop location.
*/
privatetransientDropLocationdropLocation;
/**
* Flag to indicate UI update is in progress
*/
privatetransientbooleanupdateInProgress;
/**
* A subclass of <code>TransferHandler.DropLocation</code> representing
* a drop location for a <code>JList</code>.
*
* @see #getDropLocation
* @since 1.6
*/
publicstaticfinalclassDropLocationextendsTransferHandler.DropLocation {
privatefinalintindex;
privatefinalbooleanisInsert;
privateDropLocation(Pointp, intindex, booleanisInsert) {
super(p);
this.index = index;
this.isInsert = isInsert;
}
/**
* Returns the index where dropped data should be placed in the
* list. Interpretation of the value depends on the drop mode set on
* the associated component. If the drop mode is either
* <code>DropMode.USE_SELECTION</code> or <code>DropMode.ON</code>,
* the return value is an index of a row in the list. If the drop mode is
* <code>DropMode.INSERT</code>, the return value refers to the index
* where the data should be inserted. If the drop mode is
* <code>DropMode.ON_OR_INSERT</code>, the value of
* <code>isInsert()</code> indicates whether the index is an index
* of a row, or an insert index.
* <p>
* <code>-1</code> indicates that the drop occurred over empty space,
* and no index could be calculated.
*
* @return the drop index
*/
publicintgetIndex() {
returnindex;
}
/**
* Returns whether or not this location represents an insert
* location.
*
* @return whether or not this is an insert location
*/
publicbooleanisInsert() {
returnisInsert;
}
/**
* Returns a string representation of this drop location.
* This method is intended to be used for debugging purposes,
* and the content and format of the returned string may vary
* between implementations.
*
* @return a string representation of this drop location
*/
publicStringtoString() {
returngetClass().getName()
+ "[dropPoint=" + getDropPoint() + ","
+ "index=" + index + ","
+ "insert=" + isInsert + "]";
}
}
/**
* Constructs a {@code JList} that displays elements from the specified,
* {@code non-null}, model. All {@code JList} constructors delegate to
* this one.
* <p>
* This constructor registers the list with the {@code ToolTipManager},
* allowing for tooltips to be provided by the cell renderers.
*
* @param dataModel the model for the list
* @throws IllegalArgumentException if the model is {@code null}
*/
publicJList(ListModel<E> dataModel)
{
if (dataModel == null) {
thrownewIllegalArgumentException("dataModel must be non null");
}
// Register with the ToolTipManager so that tooltips from the
// renderer show through.
ToolTipManagertoolTipManager = ToolTipManager.sharedInstance();
toolTipManager.registerComponent(this);
layoutOrientation = VERTICAL;
this.dataModel = dataModel;
selectionModel = createSelectionModel();
setAutoscrolls(true);
updateUI();
}
/**
* Constructs a <code>JList</code> that displays the elements in
* the specified array. This constructor creates a read-only model
* for the given array, and then delegates to the constructor that
* takes a {@code ListModel}.
* <p>
* Attempts to pass a {@code null} value to this method results in
* undefined behavior and, most likely, exceptions. The created model
* references the given array directly. Attempts to modify the array
* after constructing the list results in undefined behavior.
*
* @param listData the array of Objects to be loaded into the data model,
* {@code non-null}
*/
publicJList(finalE[] listData)
{
this (
newAbstractListModel<E>() {
publicintgetSize() { returnlistData.length; }
publicEgetElementAt(inti) { returnlistData[i]; }
}
);
}
/**
* Constructs a <code>JList</code> that displays the elements in
* the specified <code>Vector</code>. This constructor creates a read-only
* model for the given {@code Vector}, and then delegates to the constructor
* that takes a {@code ListModel}.
* <p>
* Attempts to pass a {@code null} value to this method results in
* undefined behavior and, most likely, exceptions. The created model
* references the given {@code Vector} directly. Attempts to modify the
* {@code Vector} after constructing the list results in undefined behavior.
*
* @param listData the <code>Vector</code> to be loaded into the
* data model, {@code non-null}
*/
publicJList(finalVector<? extendsE> listData) {
this (
newAbstractListModel<E>() {
publicintgetSize() { returnlistData.size(); }
publicEgetElementAt(inti) { returnlistData.elementAt(i); }
}
);
}
/**
* Constructs a <code>JList</code> with an empty, read-only, model.
*/
publicJList() {
this (
newAbstractListModel<E>() {
publicintgetSize() { return0; }
publicEgetElementAt(inti) { thrownewIndexOutOfBoundsException("No Data Model"); }
}
);
}
/**
* Returns the {@code ListUI}, the look and feel object that
* renders this component.
*
* @return the <code>ListUI</code> object that renders this component
*/
publicListUIgetUI() {
return (ListUI)ui;
}
/**
* Sets the {@code ListUI}, the look and feel object that
* renders this component.
*
* @param ui the <code>ListUI</code> object
* @see UIDefaults#getUI
*/
@BeanProperty(hidden = true, visualUpdate = true, description
= "The UI object that implements the Component's LookAndFeel.")
publicvoidsetUI(ListUIui) {
super.setUI(ui);
}
/**
* Resets the {@code ListUI} property by setting it to the value provided
* by the current look and feel. If the current cell renderer was installed
* by the developer (rather than the look and feel itself), this also causes
* the cell renderer and its children to be updated, by calling
* {@code SwingUtilities.updateComponentTreeUI} on it.
*
* @see UIManager#getUI
* @see SwingUtilities#updateComponentTreeUI
*/
publicvoidupdateUI() {
if (!updateInProgress) {
updateInProgress = true;
try {
setUI((ListUI)UIManager.getUI(this));
ListCellRenderer<? superE> renderer = getCellRenderer();
if (rendererinstanceofComponent) {
SwingUtilities.updateComponentTreeUI((Component)renderer);
}
} finally {
updateInProgress = false;
}
}
}
/**
* Returns {@code "ListUI"}, the <code>UIDefaults</code> key used to look
* up the name of the {@code javax.swing.plaf.ListUI} class that defines
* the look and feel for this component.
*
* @return the string "ListUI"
* @see JComponent#getUIClassID
* @see UIDefaults#getUI
*/
@BeanProperty(bound = false)
publicStringgetUIClassID() {
returnuiClassID;
}
/* -----private-----
* This method is called by setPrototypeCellValue and setCellRenderer
* to update the fixedCellWidth and fixedCellHeight properties from the
* current value of prototypeCellValue (if it's non null).
* <p>
* This method sets fixedCellWidth and fixedCellHeight but does <b>not</b>
* generate PropertyChangeEvents for them.
*
* @see #setPrototypeCellValue
* @see #setCellRenderer
*/
privatevoidupdateFixedCellSize()
{
ListCellRenderer<? superE> cr = getCellRenderer();
Evalue = getPrototypeCellValue();
if ((cr != null) && (value != null)) {
Componentc = cr.getListCellRendererComponent(this, value, 0, false, false);
/* The ListUI implementation will add Component c to its private
* CellRendererPane however we can't assume that's already
* been done here. So we temporarily set the one "inherited"
* property that may affect the renderer components preferred size:
* its font.
*/
Fontf = c.getFont();
c.setFont(getFont());
Dimensiond = c.getPreferredSize();
fixedCellWidth = d.width;
fixedCellHeight = d.height;
c.setFont(f);
}
}
/**
* Returns the "prototypical" cell value -- a value used to calculate a
* fixed width and height for cells. This can be {@code null} if there
* is no such value.
*
* @return the value of the {@code prototypeCellValue} property
* @see #setPrototypeCellValue
*/
publicEgetPrototypeCellValue() {
returnprototypeCellValue;
}
/**
* Sets the {@code prototypeCellValue} property, and then (if the new value
* is {@code non-null}), computes the {@code fixedCellWidth} and
* {@code fixedCellHeight} properties by requesting the cell renderer
* component for the given value (and index 0) from the cell renderer, and
* using that component's preferred size.
* <p>
* This method is useful when the list is too long to allow the
* {@code ListUI} to compute the width/height of each cell, and there is a
* single cell value that is known to occupy as much space as any of the
* others, a so-called prototype.
* <p>
* While all three of the {@code prototypeCellValue},
* {@code fixedCellHeight}, and {@code fixedCellWidth} properties may be
* modified by this method, {@code PropertyChangeEvent} notifications are
* only sent when the {@code prototypeCellValue} property changes.
* <p>
* To see an example which sets this property, see the
* <a href="#prototype_example">class description</a> above.
* <p>
* The default value of this property is <code>null</code>.
* <p>
* This is a JavaBeans bound property.
*
* @param prototypeCellValue the value on which to base
* <code>fixedCellWidth</code> and
* <code>fixedCellHeight</code>
* @see #getPrototypeCellValue
* @see #setFixedCellWidth
* @see #setFixedCellHeight
* @see JComponent#addPropertyChangeListener
*/
@BeanProperty(visualUpdate = true, description
= "The cell prototype value, used to compute cell width and height.")
publicvoidsetPrototypeCellValue(EprototypeCellValue) {
EoldValue = this.prototypeCellValue;
this.prototypeCellValue = prototypeCellValue;
/* If the prototypeCellValue has changed and is non-null,
* then recompute fixedCellWidth and fixedCellHeight.
*/
if ((prototypeCellValue != null) && !prototypeCellValue.equals(oldValue)) {
updateFixedCellSize();
}
firePropertyChange("prototypeCellValue", oldValue, prototypeCellValue);
}
/**
* Returns the value of the {@code fixedCellWidth} property.
*
* @return the fixed cell width
* @see #setFixedCellWidth
*/
publicintgetFixedCellWidth() {
returnfixedCellWidth;
}
/**
* Sets a fixed value to be used for the width of every cell in the list.
* If {@code width} is -1, cell widths are computed in the {@code ListUI}
* by applying <code>getPreferredSize</code> to the cell renderer component
* for each list element.
* <p>
* The default value of this property is {@code -1}.
* <p>
* This is a JavaBeans bound property.
*
* @param width the width to be used for all cells in the list
* @see #setPrototypeCellValue
* @see #getFixedCellWidth
* @see JComponent#addPropertyChangeListener
*/
@BeanProperty(visualUpdate = true, description
= "Defines a fixed cell width when greater than zero.")
publicvoidsetFixedCellWidth(intwidth) {
intoldValue = fixedCellWidth;
fixedCellWidth = width;
firePropertyChange("fixedCellWidth", oldValue, fixedCellWidth);
}
/**
* Returns the value of the {@code fixedCellHeight} property.
*
* @return the fixed cell height
* @see #setFixedCellHeight
*/
publicintgetFixedCellHeight() {
returnfixedCellHeight;
}
/**
* Sets a fixed value to be used for the height of every cell in the list.
* If {@code height} is -1, cell heights are computed in the {@code ListUI}
* by applying <code>getPreferredSize</code> to the cell renderer component
* for each list element.
* <p>
* The default value of this property is {@code -1}.
* <p>
* This is a JavaBeans bound property.
*
* @param height the height to be used for all cells in the list
* @see #setPrototypeCellValue
* @see #setFixedCellWidth
* @see JComponent#addPropertyChangeListener
*/
@BeanProperty(visualUpdate = true, description
= "Defines a fixed cell height when greater than zero.")
publicvoidsetFixedCellHeight(intheight) {
intoldValue = fixedCellHeight;
fixedCellHeight = height;
firePropertyChange("fixedCellHeight", oldValue, fixedCellHeight);
}
/**
* Returns the object responsible for painting list items.
*
* @return the value of the {@code cellRenderer} property
* @see #setCellRenderer
*/
@Transient
publicListCellRenderer<? superE> getCellRenderer() {
returncellRenderer;
}
/**
* Sets the delegate that is used to paint each cell in the list.
* The job of a cell renderer is discussed in detail in the
* <a href="#renderer">class level documentation</a>.
* <p>
* If the {@code prototypeCellValue} property is {@code non-null},
* setting the cell renderer also causes the {@code fixedCellWidth} and
* {@code fixedCellHeight} properties to be re-calculated. Only one
* <code>PropertyChangeEvent</code> is generated however -
* for the <code>cellRenderer</code> property.
* <p>
* The default value of this property is provided by the {@code ListUI}
* delegate, i.e. by the look and feel implementation.
* <p>
* This is a JavaBeans bound property.
*
* @param cellRenderer the <code>ListCellRenderer</code>
* that paints list cells
* @see #getCellRenderer
*/
@BeanProperty(visualUpdate = true, description
= "The component used to draw the cells.")
publicvoidsetCellRenderer(ListCellRenderer<? superE> cellRenderer) {
ListCellRenderer<? superE> oldValue = this.cellRenderer;
this.cellRenderer = cellRenderer;
/* If the cellRenderer has changed and prototypeCellValue
* was set, then recompute fixedCellWidth and fixedCellHeight.
*/
if ((cellRenderer != null) && !cellRenderer.equals(oldValue)) {
updateFixedCellSize();
}
firePropertyChange("cellRenderer", oldValue, cellRenderer);
}
/**
* Returns the color used to draw the foreground of selected items.
* {@code DefaultListCellRenderer} uses this color to draw the foreground
* of items in the selected state, as do the renderers installed by most
* {@code ListUI} implementations.
*
* @return the color to draw the foreground of selected items
* @see #setSelectionForeground
* @see DefaultListCellRenderer
*/
publicColorgetSelectionForeground() {
returnselectionForeground;
}
/**
* Sets the color used to draw the foreground of selected items, which
* cell renderers can use to render text and graphics.
* {@code DefaultListCellRenderer} uses this color to draw the foreground
* of items in the selected state, as do the renderers installed by most
* {@code ListUI} implementations.
* <p>
* The default value of this property is defined by the look and feel
* implementation.
* <p>
* This is a JavaBeans bound property.
*
* @param selectionForeground the {@code Color} to use in the foreground
* for selected list items
* @see #getSelectionForeground
* @see #setSelectionBackground
* @see #setForeground
* @see #setBackground
* @see #setFont
* @see DefaultListCellRenderer
*/
@BeanProperty(visualUpdate = true, description
= "The foreground color of selected cells.")
publicvoidsetSelectionForeground(ColorselectionForeground) {
ColoroldValue = this.selectionForeground;
this.selectionForeground = selectionForeground;
firePropertyChange("selectionForeground", oldValue, selectionForeground);
}
/**
* Returns the color used to draw the background of selected items.
* {@code DefaultListCellRenderer} uses this color to draw the background
* of items in the selected state, as do the renderers installed by most
* {@code ListUI} implementations.
*
* @return the color to draw the background of selected items
* @see #setSelectionBackground
* @see DefaultListCellRenderer
*/
publicColorgetSelectionBackground() {
returnselectionBackground;
}
/**
* Sets the color used to draw the background of selected items, which
* cell renderers can use fill selected cells.
* {@code DefaultListCellRenderer} uses this color to fill the background
* of items in the selected state, as do the renderers installed by most
* {@code ListUI} implementations.
* <p>
* The default value of this property is defined by the look
* and feel implementation.
* <p>
* This is a JavaBeans bound property.
*
* @param selectionBackground the {@code Color} to use for the
* background of selected cells
* @see #getSelectionBackground
* @see #setSelectionForeground
* @see #setForeground
* @see #setBackground
* @see #setFont
* @see DefaultListCellRenderer
*/
@BeanProperty(visualUpdate = true, description
= "The background color of selected cells.")
publicvoidsetSelectionBackground(ColorselectionBackground) {
ColoroldValue = this.selectionBackground;
this.selectionBackground = selectionBackground;
firePropertyChange("selectionBackground", oldValue, selectionBackground);
}
/**
* Returns the value of the {@code visibleRowCount} property. See the
* documentation for {@link #setVisibleRowCount} for details on how to
* interpret this value.
*
* @return the value of the {@code visibleRowCount} property.
* @see #setVisibleRowCount
*/
publicintgetVisibleRowCount() {
returnvisibleRowCount;
}
/**
* Sets the {@code visibleRowCount} property, which has different meanings
* depending on the layout orientation: For a {@code VERTICAL} layout
* orientation, this sets the preferred number of rows to display without
* requiring scrolling; for other orientations, it affects the wrapping of
* cells.
* <p>
* In {@code VERTICAL} orientation:<br>
* Setting this property affects the return value of the
* {@link #getPreferredScrollableViewportSize} method, which is used to
* calculate the preferred size of an enclosing viewport. See that method's
* documentation for more details.
* <p>
* In {@code HORIZONTAL_WRAP} and {@code VERTICAL_WRAP} orientations:<br>
* This affects how cells are wrapped. See the documentation of
* {@link #setLayoutOrientation} for more details.
* <p>
* The default value of this property is {@code 8}.
* <p>
* Calling this method with a negative value results in the property
* being set to {@code 0}.
* <p>
* This is a JavaBeans bound property.
*
* @param visibleRowCount an integer specifying the preferred number of
* rows to display without requiring scrolling
* @see #getVisibleRowCount
* @see #getPreferredScrollableViewportSize
* @see #setLayoutOrientation
* @see JComponent#getVisibleRect
* @see JViewport
*/
@BeanProperty(visualUpdate = true, description
= "The preferred number of rows to display without requiring scrolling")
publicvoidsetVisibleRowCount(intvisibleRowCount) {
intoldValue = this.visibleRowCount;
this.visibleRowCount = Math.max(0, visibleRowCount);
firePropertyChange("visibleRowCount", oldValue, visibleRowCount);
}
/**
* Returns the layout orientation property for the list: {@code VERTICAL}
* if the layout is a single column of cells, {@code VERTICAL_WRAP} if the
* layout is "newspaper style" with the content flowing vertically then
* horizontally, or {@code HORIZONTAL_WRAP} if the layout is "newspaper
* style" with the content flowing horizontally then vertically.
*
* @return the value of the {@code layoutOrientation} property
* @see #setLayoutOrientation
* @since 1.4
*/
publicintgetLayoutOrientation() {
returnlayoutOrientation;
}
/**
* Defines the way list cells are laid out. Consider a {@code JList}
* with five cells. Cells can be laid out in one of the following ways:
*
* <pre>
* VERTICAL: 0
* 1
* 2
* 3
* 4
*
* HORIZONTAL_WRAP: 0 1 2
* 3 4
*
* VERTICAL_WRAP: 0 3
* 1 4
* 2
* </pre>
* <p>
* A description of these layouts follows:
*
* <table class="striped">
* <caption>Describes layouts VERTICAL,HORIZONTAL_WRAP, and VERTICAL_WRAP
* </caption>
* <thead>
* <tr>
* <th scope="col">Value
* <th scope="col">Description