|
1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.util.swing; |
|
27 |
|
28 import java.awt.Dimension; |
|
29 import java.util.Vector; |
|
30 import javax.swing.*; |
|
31 |
|
32 @SuppressWarnings({"serial"}) |
|
33 public class ExtList extends JList { |
|
34 // |
|
35 // Instance data |
|
36 // |
|
37 |
|
38 private boolean fills = false; |
|
39 private boolean tracksWidth = true; |
|
40 private boolean visibleRowCountStatic = true; |
|
41 private int vWidth = -1; |
|
42 |
|
43 // |
|
44 // Constructors |
|
45 // |
|
46 |
|
47 public ExtList() { |
|
48 } |
|
49 |
|
50 public ExtList(ListModel dataModel) { |
|
51 super(dataModel); |
|
52 } |
|
53 |
|
54 public ExtList(Object[] listData) { |
|
55 super(listData); |
|
56 } |
|
57 |
|
58 public ExtList(Vector<?> listData) { |
|
59 super(listData); |
|
60 } |
|
61 |
|
62 // |
|
63 // Scrollable methods |
|
64 // |
|
65 |
|
66 /** |
|
67 * Partially override the hard-coded values returned by the superclass when |
|
68 * the model is empty, orientation is {@code VERTICAL}, and no fixed |
|
69 * width/height has been set. In this case, a zero width {@code Dimension} |
|
70 * is returned. |
|
71 */ |
|
72 @Override |
|
73 public Dimension getPreferredScrollableViewportSize() { |
|
74 Dimension d = super.getPreferredScrollableViewportSize(); |
|
75 |
|
76 if (vWidth != -1) { |
|
77 d.width = vWidth; |
|
78 } else if (getLayoutOrientation() == VERTICAL && |
|
79 getFixedCellWidth() <= 0 && |
|
80 getFixedCellHeight() <= 0 && |
|
81 getModel().getSize() == 0) { |
|
82 |
|
83 d.width = 0; |
|
84 } |
|
85 |
|
86 return d; |
|
87 } |
|
88 |
|
89 @Override |
|
90 public boolean getScrollableTracksViewportHeight() { |
|
91 return fills && super.getScrollableTracksViewportHeight(); |
|
92 } |
|
93 |
|
94 @Override |
|
95 public boolean getScrollableTracksViewportWidth() { |
|
96 return tracksWidth || super.getScrollableTracksViewportWidth(); |
|
97 } |
|
98 |
|
99 // |
|
100 // JList methods |
|
101 // |
|
102 |
|
103 @Override |
|
104 public int getVisibleRowCount() { |
|
105 int vrc = super.getVisibleRowCount(); |
|
106 if (vrc > 0 && !isVisibleRowCountStatic()) { |
|
107 return Math.min(getModel().getSize(), vrc); |
|
108 } |
|
109 return vrc; |
|
110 } |
|
111 |
|
112 // |
|
113 // ExtList methods |
|
114 // |
|
115 |
|
116 /** |
|
117 * Gets whether or not this list is always made large enough to fill the |
|
118 * height of an enclosing viewport. By default this property is {@code |
|
119 * false}. |
|
120 */ |
|
121 public boolean getFillsViewportHeight() { |
|
122 return fills; |
|
123 } |
|
124 |
|
125 /** |
|
126 * Gets the preferred width of a viewport containing this list, or -1 if the |
|
127 * width should be calculated automatically. |
|
128 */ |
|
129 public int getVisibleWidth() { |
|
130 return vWidth; |
|
131 } |
|
132 |
|
133 /** |
|
134 * Gets whether the visible row count, if set, should determine the height |
|
135 * of an enclosing viewport even if there are fewer rows in the data model. |
|
136 * The default value is {@code true}. |
|
137 */ |
|
138 public boolean isVisibleRowCountStatic() { |
|
139 return visibleRowCountStatic; |
|
140 } |
|
141 |
|
142 /** |
|
143 * Sets whether or not this list is always made large enough to fill the |
|
144 * height of an enclosing viewport. By default this property is {@code |
|
145 * false}. |
|
146 */ |
|
147 public void setFillsViewportHeight(boolean fills) { |
|
148 this.fills = fills; |
|
149 } |
|
150 |
|
151 /** |
|
152 * Set whether to resize the list horizontally instead of scrolling, when in |
|
153 * a scroll pane. By default this property is {@code true}. |
|
154 * |
|
155 * @param tracksWidth |
|
156 * {@code true} to resize, {@code false} otherwise |
|
157 */ |
|
158 public void setScrollableTracksViewportWidth(boolean tracksWidth) { |
|
159 this.tracksWidth = tracksWidth; |
|
160 } |
|
161 |
|
162 /** |
|
163 * Sets the preferred width of a viewport containing this list, or -1 if the |
|
164 * width should be calculated automatically. |
|
165 */ |
|
166 public void setVisibleWidth(int vWidth) { |
|
167 this.vWidth = vWidth; |
|
168 } |
|
169 |
|
170 /** |
|
171 * Sets whether the visible row count, if set, should determine the height |
|
172 * of an enclosing viewport even if there are fewer rows in the data model. |
|
173 * The default value is {@code true}. |
|
174 */ |
|
175 public void setVisibleRowCountStatic(boolean visibleRowCountStatic) { |
|
176 this.visibleRowCountStatic = visibleRowCountStatic; |
|
177 } |
|
178 } |