author | yan |
Mon, 19 May 2014 10:43:09 +0400 | |
changeset 24500 | 399b77799d66 |
parent 22574 | 7f8ce0c8c20a |
child 25201 | 4adc75e0c4e5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
22574
7f8ce0c8c20a
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents:
20458
diff
changeset
|
2 |
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package javax.swing; |
|
27 |
||
28 |
import javax.swing.event.*; |
|
29 |
import java.io.Serializable; |
|
30 |
import java.util.EventListener; |
|
31 |
||
32 |
/** |
|
33 |
* A generic implementation of BoundedRangeModel. |
|
34 |
* <p> |
|
35 |
* <strong>Warning:</strong> |
|
36 |
* Serialized objects of this class will not be compatible with |
|
37 |
* future Swing releases. The current serialization support is |
|
38 |
* appropriate for short term storage or RMI between applications running |
|
39 |
* the same version of Swing. As of 1.4, support for long term storage |
|
20458 | 40 |
* of all JavaBeans™ |
2 | 41 |
* has been added to the <code>java.beans</code> package. |
42 |
* Please see {@link java.beans.XMLEncoder}. |
|
43 |
* |
|
44 |
* @author David Kloba |
|
45 |
* @author Hans Muller |
|
46 |
* @see BoundedRangeModel |
|
47 |
*/ |
|
22574
7f8ce0c8c20a
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents:
20458
diff
changeset
|
48 |
@SuppressWarnings("serial") // Same-version serialization only |
2 | 49 |
public class DefaultBoundedRangeModel implements BoundedRangeModel, Serializable |
50 |
{ |
|
51 |
/** |
|
52 |
* Only one <code>ChangeEvent</code> is needed per model instance since the |
|
53 |
* event's only (read-only) state is the source property. The source |
|
54 |
* of events generated here is always "this". |
|
55 |
*/ |
|
56 |
protected transient ChangeEvent changeEvent = null; |
|
57 |
||
58 |
/** The listeners waiting for model changes. */ |
|
59 |
protected EventListenerList listenerList = new EventListenerList(); |
|
60 |
||
61 |
private int value = 0; |
|
62 |
private int extent = 0; |
|
63 |
private int min = 0; |
|
64 |
private int max = 100; |
|
65 |
private boolean isAdjusting = false; |
|
66 |
||
67 |
||
68 |
/** |
|
69 |
* Initializes all of the properties with default values. |
|
70 |
* Those values are: |
|
71 |
* <ul> |
|
72 |
* <li><code>value</code> = 0 |
|
73 |
* <li><code>extent</code> = 0 |
|
74 |
* <li><code>minimum</code> = 0 |
|
75 |
* <li><code>maximum</code> = 100 |
|
76 |
* <li><code>adjusting</code> = false |
|
77 |
* </ul> |
|
78 |
*/ |
|
79 |
public DefaultBoundedRangeModel() { |
|
80 |
} |
|
81 |
||
82 |
||
83 |
/** |
|
84 |
* Initializes value, extent, minimum and maximum. Adjusting is false. |
|
85 |
* Throws an <code>IllegalArgumentException</code> if the following |
|
86 |
* constraints aren't satisfied: |
|
87 |
* <pre> |
|
88 |
* min <= value <= value+extent <= max |
|
89 |
* </pre> |
|
90 |
*/ |
|
91 |
public DefaultBoundedRangeModel(int value, int extent, int min, int max) |
|
92 |
{ |
|
93 |
if ((max >= min) && |
|
94 |
(value >= min) && |
|
95 |
((value + extent) >= value) && |
|
96 |
((value + extent) <= max)) { |
|
97 |
this.value = value; |
|
98 |
this.extent = extent; |
|
99 |
this.min = min; |
|
100 |
this.max = max; |
|
101 |
} |
|
102 |
else { |
|
103 |
throw new IllegalArgumentException("invalid range properties"); |
|
104 |
} |
|
105 |
} |
|
106 |
||
107 |
||
108 |
/** |
|
109 |
* Returns the model's current value. |
|
110 |
* @return the model's current value |
|
111 |
* @see #setValue |
|
112 |
* @see BoundedRangeModel#getValue |
|
113 |
*/ |
|
114 |
public int getValue() { |
|
115 |
return value; |
|
116 |
} |
|
117 |
||
118 |
||
119 |
/** |
|
120 |
* Returns the model's extent. |
|
121 |
* @return the model's extent |
|
122 |
* @see #setExtent |
|
123 |
* @see BoundedRangeModel#getExtent |
|
124 |
*/ |
|
125 |
public int getExtent() { |
|
126 |
return extent; |
|
127 |
} |
|
128 |
||
129 |
||
130 |
/** |
|
131 |
* Returns the model's minimum. |
|
132 |
* @return the model's minimum |
|
133 |
* @see #setMinimum |
|
134 |
* @see BoundedRangeModel#getMinimum |
|
135 |
*/ |
|
136 |
public int getMinimum() { |
|
137 |
return min; |
|
138 |
} |
|
139 |
||
140 |
||
141 |
/** |
|
142 |
* Returns the model's maximum. |
|
143 |
* @return the model's maximum |
|
144 |
* @see #setMaximum |
|
145 |
* @see BoundedRangeModel#getMaximum |
|
146 |
*/ |
|
147 |
public int getMaximum() { |
|
148 |
return max; |
|
149 |
} |
|
150 |
||
151 |
||
152 |
/** |
|
153 |
* Sets the current value of the model. For a slider, that |
|
154 |
* determines where the knob appears. Ensures that the new |
|
155 |
* value, <I>n</I> falls within the model's constraints: |
|
156 |
* <pre> |
|
157 |
* minimum <= value <= value+extent <= maximum |
|
158 |
* </pre> |
|
159 |
* |
|
160 |
* @see BoundedRangeModel#setValue |
|
161 |
*/ |
|
162 |
public void setValue(int n) { |
|
163 |
n = Math.min(n, Integer.MAX_VALUE - extent); |
|
164 |
||
165 |
int newValue = Math.max(n, min); |
|
166 |
if (newValue + extent > max) { |
|
167 |
newValue = max - extent; |
|
168 |
} |
|
169 |
setRangeProperties(newValue, extent, min, max, isAdjusting); |
|
170 |
} |
|
171 |
||
172 |
||
173 |
/** |
|
174 |
* Sets the extent to <I>n</I> after ensuring that <I>n</I> |
|
175 |
* is greater than or equal to zero and falls within the model's |
|
176 |
* constraints: |
|
177 |
* <pre> |
|
178 |
* minimum <= value <= value+extent <= maximum |
|
179 |
* </pre> |
|
180 |
* @see BoundedRangeModel#setExtent |
|
181 |
*/ |
|
182 |
public void setExtent(int n) { |
|
183 |
int newExtent = Math.max(0, n); |
|
184 |
if(value + newExtent > max) { |
|
185 |
newExtent = max - value; |
|
186 |
} |
|
187 |
setRangeProperties(value, newExtent, min, max, isAdjusting); |
|
188 |
} |
|
189 |
||
190 |
||
191 |
/** |
|
192 |
* Sets the minimum to <I>n</I> after ensuring that <I>n</I> |
|
193 |
* that the other three properties obey the model's constraints: |
|
194 |
* <pre> |
|
195 |
* minimum <= value <= value+extent <= maximum |
|
196 |
* </pre> |
|
197 |
* @see #getMinimum |
|
198 |
* @see BoundedRangeModel#setMinimum |
|
199 |
*/ |
|
200 |
public void setMinimum(int n) { |
|
201 |
int newMax = Math.max(n, max); |
|
202 |
int newValue = Math.max(n, value); |
|
203 |
int newExtent = Math.min(newMax - newValue, extent); |
|
204 |
setRangeProperties(newValue, newExtent, n, newMax, isAdjusting); |
|
205 |
} |
|
206 |
||
207 |
||
208 |
/** |
|
209 |
* Sets the maximum to <I>n</I> after ensuring that <I>n</I> |
|
210 |
* that the other three properties obey the model's constraints: |
|
211 |
* <pre> |
|
212 |
* minimum <= value <= value+extent <= maximum |
|
213 |
* </pre> |
|
214 |
* @see BoundedRangeModel#setMaximum |
|
215 |
*/ |
|
216 |
public void setMaximum(int n) { |
|
217 |
int newMin = Math.min(n, min); |
|
218 |
int newExtent = Math.min(n - newMin, extent); |
|
219 |
int newValue = Math.min(n - newExtent, value); |
|
220 |
setRangeProperties(newValue, newExtent, newMin, n, isAdjusting); |
|
221 |
} |
|
222 |
||
223 |
||
224 |
/** |
|
225 |
* Sets the <code>valueIsAdjusting</code> property. |
|
226 |
* |
|
227 |
* @see #getValueIsAdjusting |
|
228 |
* @see #setValue |
|
229 |
* @see BoundedRangeModel#setValueIsAdjusting |
|
230 |
*/ |
|
231 |
public void setValueIsAdjusting(boolean b) { |
|
232 |
setRangeProperties(value, extent, min, max, b); |
|
233 |
} |
|
234 |
||
235 |
||
236 |
/** |
|
237 |
* Returns true if the value is in the process of changing |
|
238 |
* as a result of actions being taken by the user. |
|
239 |
* |
|
240 |
* @return the value of the <code>valueIsAdjusting</code> property |
|
241 |
* @see #setValue |
|
242 |
* @see BoundedRangeModel#getValueIsAdjusting |
|
243 |
*/ |
|
244 |
public boolean getValueIsAdjusting() { |
|
245 |
return isAdjusting; |
|
246 |
} |
|
247 |
||
248 |
||
249 |
/** |
|
250 |
* Sets all of the <code>BoundedRangeModel</code> properties after forcing |
|
251 |
* the arguments to obey the usual constraints: |
|
252 |
* <pre> |
|
253 |
* minimum <= value <= value+extent <= maximum |
|
254 |
* </pre> |
|
255 |
* <p> |
|
256 |
* At most, one <code>ChangeEvent</code> is generated. |
|
257 |
* |
|
258 |
* @see BoundedRangeModel#setRangeProperties |
|
259 |
* @see #setValue |
|
260 |
* @see #setExtent |
|
261 |
* @see #setMinimum |
|
262 |
* @see #setMaximum |
|
263 |
* @see #setValueIsAdjusting |
|
264 |
*/ |
|
265 |
public void setRangeProperties(int newValue, int newExtent, int newMin, int newMax, boolean adjusting) |
|
266 |
{ |
|
267 |
if (newMin > newMax) { |
|
268 |
newMin = newMax; |
|
269 |
} |
|
270 |
if (newValue > newMax) { |
|
271 |
newMax = newValue; |
|
272 |
} |
|
273 |
if (newValue < newMin) { |
|
274 |
newMin = newValue; |
|
275 |
} |
|
276 |
||
277 |
/* Convert the addends to long so that extent can be |
|
278 |
* Integer.MAX_VALUE without rolling over the sum. |
|
279 |
* A JCK test covers this, see bug 4097718. |
|
280 |
*/ |
|
281 |
if (((long)newExtent + (long)newValue) > newMax) { |
|
282 |
newExtent = newMax - newValue; |
|
283 |
} |
|
284 |
||
285 |
if (newExtent < 0) { |
|
286 |
newExtent = 0; |
|
287 |
} |
|
288 |
||
289 |
boolean isChange = |
|
290 |
(newValue != value) || |
|
291 |
(newExtent != extent) || |
|
292 |
(newMin != min) || |
|
293 |
(newMax != max) || |
|
294 |
(adjusting != isAdjusting); |
|
295 |
||
296 |
if (isChange) { |
|
297 |
value = newValue; |
|
298 |
extent = newExtent; |
|
299 |
min = newMin; |
|
300 |
max = newMax; |
|
301 |
isAdjusting = adjusting; |
|
302 |
||
303 |
fireStateChanged(); |
|
304 |
} |
|
305 |
} |
|
306 |
||
307 |
||
308 |
/** |
|
309 |
* Adds a <code>ChangeListener</code>. The change listeners are run each |
|
310 |
* time any one of the Bounded Range model properties changes. |
|
311 |
* |
|
312 |
* @param l the ChangeListener to add |
|
313 |
* @see #removeChangeListener |
|
314 |
* @see BoundedRangeModel#addChangeListener |
|
315 |
*/ |
|
316 |
public void addChangeListener(ChangeListener l) { |
|
317 |
listenerList.add(ChangeListener.class, l); |
|
318 |
} |
|
319 |
||
320 |
||
321 |
/** |
|
322 |
* Removes a <code>ChangeListener</code>. |
|
323 |
* |
|
324 |
* @param l the <code>ChangeListener</code> to remove |
|
325 |
* @see #addChangeListener |
|
326 |
* @see BoundedRangeModel#removeChangeListener |
|
327 |
*/ |
|
328 |
public void removeChangeListener(ChangeListener l) { |
|
329 |
listenerList.remove(ChangeListener.class, l); |
|
330 |
} |
|
331 |
||
332 |
||
333 |
/** |
|
334 |
* Returns an array of all the change listeners |
|
335 |
* registered on this <code>DefaultBoundedRangeModel</code>. |
|
336 |
* |
|
337 |
* @return all of this model's <code>ChangeListener</code>s |
|
338 |
* or an empty |
|
339 |
* array if no change listeners are currently registered |
|
340 |
* |
|
341 |
* @see #addChangeListener |
|
342 |
* @see #removeChangeListener |
|
343 |
* |
|
344 |
* @since 1.4 |
|
345 |
*/ |
|
346 |
public ChangeListener[] getChangeListeners() { |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
2
diff
changeset
|
347 |
return listenerList.getListeners(ChangeListener.class); |
2 | 348 |
} |
349 |
||
350 |
||
351 |
/** |
|
352 |
* Runs each <code>ChangeListener</code>'s <code>stateChanged</code> method. |
|
353 |
* |
|
354 |
* @see #setRangeProperties |
|
355 |
* @see EventListenerList |
|
356 |
*/ |
|
357 |
protected void fireStateChanged() |
|
358 |
{ |
|
359 |
Object[] listeners = listenerList.getListenerList(); |
|
360 |
for (int i = listeners.length - 2; i >= 0; i -=2 ) { |
|
361 |
if (listeners[i] == ChangeListener.class) { |
|
362 |
if (changeEvent == null) { |
|
363 |
changeEvent = new ChangeEvent(this); |
|
364 |
} |
|
365 |
((ChangeListener)listeners[i+1]).stateChanged(changeEvent); |
|
366 |
} |
|
367 |
} |
|
368 |
} |
|
369 |
||
370 |
||
371 |
/** |
|
372 |
* Returns a string that displays all of the |
|
373 |
* <code>BoundedRangeModel</code> properties. |
|
374 |
*/ |
|
375 |
public String toString() { |
|
376 |
String modelString = |
|
377 |
"value=" + getValue() + ", " + |
|
378 |
"extent=" + getExtent() + ", " + |
|
379 |
"min=" + getMinimum() + ", " + |
|
380 |
"max=" + getMaximum() + ", " + |
|
381 |
"adj=" + getValueIsAdjusting(); |
|
382 |
||
383 |
return getClass().getName() + "[" + modelString + "]"; |
|
384 |
} |
|
385 |
||
386 |
/** |
|
387 |
* Returns an array of all the objects currently registered as |
|
388 |
* <code><em>Foo</em>Listener</code>s |
|
389 |
* upon this model. |
|
390 |
* <code><em>Foo</em>Listener</code>s |
|
391 |
* are registered using the <code>add<em>Foo</em>Listener</code> method. |
|
392 |
* <p> |
|
393 |
* You can specify the <code>listenerType</code> argument |
|
394 |
* with a class literal, such as <code><em>Foo</em>Listener.class</code>. |
|
395 |
* For example, you can query a <code>DefaultBoundedRangeModel</code> |
|
396 |
* instance <code>m</code> |
|
397 |
* for its change listeners |
|
398 |
* with the following code: |
|
399 |
* |
|
400 |
* <pre>ChangeListener[] cls = (ChangeListener[])(m.getListeners(ChangeListener.class));</pre> |
|
401 |
* |
|
402 |
* If no such listeners exist, |
|
403 |
* this method returns an empty array. |
|
404 |
* |
|
405 |
* @param listenerType the type of listeners requested; |
|
406 |
* this parameter should specify an interface |
|
407 |
* that descends from <code>java.util.EventListener</code> |
|
408 |
* @return an array of all objects registered as |
|
409 |
* <code><em>Foo</em>Listener</code>s |
|
410 |
* on this model, |
|
411 |
* or an empty array if no such |
|
412 |
* listeners have been added |
|
413 |
* @exception ClassCastException if <code>listenerType</code> doesn't |
|
414 |
* specify a class or interface that implements |
|
415 |
* <code>java.util.EventListener</code> |
|
416 |
* |
|
417 |
* @see #getChangeListeners |
|
418 |
* |
|
419 |
* @since 1.3 |
|
420 |
*/ |
|
421 |
public <T extends EventListener> T[] getListeners(Class<T> listenerType) { |
|
422 |
return listenerList.getListeners(listenerType); |
|
423 |
} |
|
424 |
} |