KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > SPagingBoundedRangeModel


1 /*
2  * $Id: SPagingBoundedRangeModel.java,v 1.5 2005/02/25 14:36:14 blueshift Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings;
15
16 import javax.swing.event.ChangeEvent JavaDoc;
17 import javax.swing.event.ChangeListener JavaDoc;
18 import javax.swing.event.EventListenerList JavaDoc;
19 import java.util.EventListener JavaDoc;
20
21
22 /**
23  * SPagingBoundedRangeModel.java
24  * <p/>
25  * <p/>
26  * Created: Tue Nov 19 12:39:33 2002
27  *
28  * @author <a HREF="mailto:armin.haaf@mercatis.de">Armin Haaf</a>
29  * @version $Revision: 1.5 $
30  */

31 public class SPagingBoundedRangeModel implements SBoundedRangeModel {
32
33     /**
34      * Only one <code>ChangeEvent</code> is needed per model instance since the
35      * event's only (read-only) state is the source property. The source
36      * of events generated here is always "this".
37      */

38     protected transient ChangeEvent JavaDoc changeEvent = null;
39
40     /**
41      * The listeners waiting for model changes.
42      */

43     protected EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
44
45     protected int value = 0;
46     protected int extent = 0;
47     protected int min = 0;
48     protected int max = 100;
49     protected boolean isAdjusting = false;
50
51     /**
52      * indicates if we should fire event immediately when they arise, or if we
53      * should collect them for a later delivery
54      */

55     private boolean delayEvents = false;
56
57     /**
58      * got a delayed Event?
59      */

60     protected boolean gotDelayedEvent = false;
61
62
63     public SPagingBoundedRangeModel() {
64         super();
65     }
66
67     public SPagingBoundedRangeModel(int value, int extent, int min, int max) {
68         setRangeProperties(value, extent, min, max, false);
69     }
70
71     /**
72      * Returns the model's current value.
73      *
74      * @return the model's current value
75      * @see #setValue
76      * @see javax.swing.BoundedRangeModel#getValue
77      */

78     public int getValue() {
79         return value;
80     }
81
82
83     /**
84      * Returns the model's extent.
85      *
86      * @return the model's extent
87      * @see #setExtent
88      * @see javax.swing.BoundedRangeModel#getExtent
89      */

90     public int getExtent() {
91         return extent;
92     }
93
94
95     /**
96      * Returns the model's minimum.
97      *
98      * @return the model's minimum
99      * @see #setMinimum
100      * @see javax.swing.BoundedRangeModel#getMinimum
101      */

102     public int getMinimum() {
103         return min;
104     }
105
106
107     /**
108      * Returns the model's maximum.
109      *
110      * @return the model's maximum
111      * @see #setMaximum
112      * @see javax.swing.BoundedRangeModel#getMaximum
113      */

114     public int getMaximum() {
115         return max;
116     }
117
118     /**
119      * Sets the current value of the model. For a slider, that
120      * determines where the knob appears. Ensures that the new
121      * value, <I>n</I> falls within the model's constraints:
122      * <pre>
123      * minimum <= value <= maximum
124      * </pre>
125      *
126      * @see javax.swing.BoundedRangeModel#setValue
127      */

128     public void setValue(int n) {
129         setRangeProperties(n, extent, min, max, isAdjusting);
130     }
131
132
133     /**
134      * Sets the extent to <I>n</I> after ensuring that <I>n</I>
135      * is greater than or equal to zero and falls within the model's
136      * constraints:
137      * <pre>
138      * minimum <= value <= maximum
139      * </pre>
140      *
141      * @see javax.swing.BoundedRangeModel#setExtent
142      */

143     public void setExtent(int n) {
144         setRangeProperties(value, n, min, max, isAdjusting);
145     }
146
147
148     /**
149      * Sets the minimum to <I>n</I> after ensuring that <I>n</I>
150      * that the other three properties obey the model's constraints:
151      * <pre>
152      * minimum <= value <= maximum
153      * </pre>
154      *
155      * @see #getMinimum
156      * @see javax.swing.BoundedRangeModel#setMinimum
157      */

158     public void setMinimum(int n) {
159         setRangeProperties(Math.max(value, n), extent, n, max, isAdjusting);
160     }
161
162
163     /**
164      * Sets the maximum to <I>n</I> after ensuring that <I>n</I>
165      * that the other three properties obey the model's constraints:
166      * <pre>
167      * minimum <= value <= maximum
168      * </pre>
169      *
170      * @see javax.swing.BoundedRangeModel#setMaximum
171      */

172     public void setMaximum(int n) {
173         setRangeProperties(Math.min(value, n), extent, min, n, isAdjusting);
174     }
175
176
177     /**
178      * Sets the <code>valueIsAdjusting</code> property.
179      *
180      * @see #getValueIsAdjusting
181      * @see #setValue
182      * @see javax.swing.BoundedRangeModel#setValueIsAdjusting
183      */

184     public void setValueIsAdjusting(boolean b) {
185         setRangeProperties(value, extent, min, max, b);
186     }
187
188     /**
189      * Returns true if the value is in the process of changing
190      * as a result of actions being taken by the user.
191      *
192      * @return the value of the <code>valueIsAdjusting</code> property
193      * @see #setValue
194      * @see javax.swing.BoundedRangeModel#getValueIsAdjusting
195      */

196     public boolean getValueIsAdjusting() {
197         return isAdjusting;
198     }
199
200     /**
201      * Sets all of the <code>BoundedRangeModel</code> properties after forcing
202      * the arguments to obey the usual constraints:
203      * <pre>
204      * minimum <= value <= maximum
205      * </pre>
206      * <p/>
207      * At most, one <code>ChangeEvent</code> is generated.
208      *
209      * @see javax.swing.BoundedRangeModel#setRangeProperties
210      * @see #setValue
211      * @see #setExtent
212      * @see #setMinimum
213      * @see #setMaximum
214      * @see #setValueIsAdjusting
215      */

216     public void setRangeProperties(int newValue,
217                                    int newExtent,
218                                    int newMin,
219                                    int newMax,
220                                    boolean adjusting) {
221         if (newMin > newMax) {
222             newMin = newMax;
223         }
224         if (newValue > newMax) {
225             newMax = newValue;
226         }
227         if (newValue < newMin) {
228             newMin = newValue;
229         }
230         if (newExtent < 0) {
231             newExtent = 0;
232         }
233
234         boolean isChange =
235                 (newValue != value) ||
236                 (newExtent != extent) ||
237                 (newMin != min) ||
238                 (newMax != max) ||
239                 (adjusting != isAdjusting);
240
241         if (isChange) {
242             value = newValue;
243             extent = newExtent;
244             min = newMin;
245             max = newMax;
246             isAdjusting = adjusting;
247
248             fireStateChanged();
249         }
250     }
251
252     public boolean getDelayEvents() {
253         return delayEvents;
254     }
255
256     public void setDelayEvents(boolean b) {
257         delayEvents = b;
258     }
259
260
261     /**
262      * Adds a <code>ChangeListener</code>. The change listeners are run each
263      * time any one of the Bounded Range model properties changes.
264      *
265      * @param l the ChangeListener to add
266      * @see #removeChangeListener
267      * @see javax.swing.BoundedRangeModel#addChangeListener
268      */

269     public void addChangeListener(ChangeListener JavaDoc l) {
270         listenerList.add(ChangeListener JavaDoc.class, l);
271     }
272
273
274     /**
275      * Removes a <code>ChangeListener</code>.
276      *
277      * @param l the <code>ChangeListener</code> to remove
278      * @see #addChangeListener
279      * @see javax.swing.BoundedRangeModel#removeChangeListener
280      */

281     public void removeChangeListener(ChangeListener JavaDoc l) {
282         listenerList.remove(ChangeListener JavaDoc.class, l);
283     }
284
285
286     /**
287      * Returns an array of all the change listeners
288      * registered on this <code>DefaultBoundedRangeModel</code>.
289      *
290      * @return all of this model's <code>ChangeListener</code>s
291      * or an empty
292      * array if no change listeners are currently registered
293      * @see #addChangeListener
294      * @see #removeChangeListener
295      * @since 1.4
296      */

297     public ChangeListener JavaDoc[] getChangeListeners() {
298         return (ChangeListener JavaDoc[]) listenerList.getListeners(ChangeListener JavaDoc.class);
299     }
300
301
302     /**
303      * Runs each <code>ChangeListener</code>'s <code>stateChanged</code> method.
304      *
305      * @see #setRangeProperties
306      * @see EventListenerList
307      */

308     protected void fireStateChanged() {
309         if (delayEvents) {
310             gotDelayedEvent = true;
311         } else {
312             Object JavaDoc[] listeners = listenerList.getListenerList();
313             for (int i = listeners.length - 2; i >= 0; i -= 2) {
314                 if (listeners[i] == ChangeListener JavaDoc.class) {
315                     if (changeEvent == null) {
316                         changeEvent = new ChangeEvent JavaDoc(this);
317                     }
318                     ((ChangeListener JavaDoc) listeners[i + 1]).stateChanged(changeEvent);
319                 }
320             }
321         }
322     }
323
324
325     /**
326      * Returns a string that displays all of the
327      * <code>BoundedRangeModel</code> properties.
328      */

329     public String JavaDoc toString() {
330         String JavaDoc modelString =
331                 "value=" + getValue() + ", " +
332                 "extent=" + getExtent() + ", " +
333                 "min=" + getMinimum() + ", " +
334                 "max=" + getMaximum() + ", " +
335                 "adj=" + getValueIsAdjusting();
336
337         return getClass().getName() + "[" + modelString + "]";
338     }
339
340     /**
341      * Returns an array of all the objects currently registered as
342      * <code><em>Foo</em>Listener</code>s
343      * upon this model.
344      * <code><em>Foo</em>Listener</code>s
345      * are registered using the <code>add<em>Foo</em>Listener</code> method.
346      * <p/>
347      * You can specify the <code>listenerType</code> argument
348      * with a class literal, such as <code><em>Foo</em>Listener.class</code>.
349      * For example, you can query a <code>DefaultBoundedRangeModel</code>
350      * instance <code>m</code>
351      * for its change listeners
352      * with the following code:
353      * <p/>
354      * <pre>ChangeListener[] cls = (ChangeListener[])(m.getListeners(ChangeListener.class));</pre>
355      * <p/>
356      * If no such listeners exist,
357      * this method returns an empty array.
358      *
359      * @param listenerType the type of listeners requested;
360      * this parameter should specify an interface
361      * that descends from <code>java.util.EventListener</code>
362      * @return an array of all objects registered as
363      * <code><em>Foo</em>Listener</code>s
364      * on this model,
365      * or an empty array if no such
366      * listeners have been added
367      * @throws ClassCastException if <code>listenerType</code> doesn't
368      * specify a class or interface that implements
369      * <code>java.util.EventListener</code>
370      * @see #getChangeListeners
371      * @since 1.3
372      */

373     public EventListener JavaDoc[] getListeners(Class JavaDoc listenerType) {
374         return listenerList.getListeners(listenerType);
375     }
376
377
378     /**
379      * fire event with isValueIsAdjusting true
380      */

381     public void fireDelayedIntermediateEvents() {
382     }
383
384     public void fireDelayedFinalEvents() {
385         if (!delayEvents && gotDelayedEvent) {
386             fireStateChanged();
387             gotDelayedEvent = false;
388         }
389     }
390
391 }// SPagingBoundedRangeModel
392
Popular Tags