KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > p2p > peerconfiguration > CyclingSpinnerListModel


1 package org.objectweb.proactive.p2p.peerconfiguration;
2 import javax.swing.SpinnerModel JavaDoc;
3 import javax.swing.SpinnerListModel JavaDoc;
4
5 /**
6  * This 1.4 example is used by the various SpinnerDemos.
7  * It implements a SpinnerListModel that works only with
8  * an Object array and that implements cycling (the next
9  * value and previous value are never null). It also
10  * lets you optionally associate a spinner model that's
11  * linked to this one, so that when a cycle occurs the
12  * linked spinner model is updated.
13
14  * The SpinnerDemos use the CyclingSpinnerListModel for
15  * a month spinner that (in SpinnerDemo3) is tied to the
16  * year spinner, so that -- for example -- when the month
17  * changes from December to January, the year increases.
18  */

19 public class CyclingSpinnerListModel extends SpinnerListModel JavaDoc {
20     Object JavaDoc firstValue, lastValue;
21     SpinnerModel JavaDoc linkedModel = null;
22
23     public CyclingSpinnerListModel(Object JavaDoc[] values) {
24         super(values);
25         firstValue = values[0];
26         lastValue = values[values.length - 1];
27     }
28
29     public void setLinkedModel(SpinnerModel JavaDoc linkedModel) {
30         this.linkedModel = linkedModel;
31     }
32
33     public Object JavaDoc getNextValue() {
34         Object JavaDoc value = super.getNextValue();
35         if (value == null) {
36             value = firstValue;
37             if (linkedModel != null) {
38                 linkedModel.setValue(linkedModel.getNextValue());
39             }
40         }
41         return value;
42     }
43
44     public Object JavaDoc getPreviousValue() {
45         Object JavaDoc value = super.getPreviousValue();
46         if (value == null) {
47             value = lastValue;
48             if (linkedModel != null) {
49                 linkedModel.setValue(linkedModel.getPreviousValue());
50             }
51         }
52         return value;
53     }
54 }
55
Popular Tags