KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.objectweb.proactive.p2p.peerconfiguration;
2 import javax.swing.*;
3 import java.awt.Container JavaDoc;
4 import java.util.Calendar JavaDoc;
5
6 public class SpinnerSet extends JPanel {
7     protected JFormattedTextField ftf = null;
8     protected JSpinner spinnerMinutes = null;
9     protected JSpinner spinnerHour = null;
10     protected JSpinner spinnerDay = null;
11     protected JSpinner spinnerMonth = null;
12     protected JSpinner spinnerYear = null;
13
14     public SpinnerSet(boolean cycleMonths) {
15         super(new SpringLayout());
16
17         String JavaDoc[] labels = {"Day: ", "Month: ", "Year: "};
18         int numPairs = labels.length;
19         Calendar JavaDoc calendar = Calendar.getInstance();
20
21 // // Minutes
22
// int currentMinutes = calendar.get(Calendar.MINUTE);
23
// String[] minutes = {"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20",
24
// "21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40",
25
// "41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59"};
26
// SpinnerModel minutesModel = new CyclingSpinnerListModel(minutes);
27
// spinnerMinutes = addLabeledSpinner(this, labels[0], minutesModel);
28
// ftf = getTextField(spinnerMinutes);
29
// if (ftf != null ) {
30
// ftf.setColumns(8); //specify more width than we need
31
// ftf.setHorizontalAlignment(JTextField.RIGHT);
32
// }
33
//
34
// // Hour
35
// int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
36
// String[] hour = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24"};
37
// SpinnerModel hourModel = new CyclingSpinnerListModel(hour);
38
// spinnerHour = addLabeledSpinner(this, labels[1], hourModel);
39
// ftf = getTextField(spinnerHour);
40
// if (ftf != null ) {
41
// ftf.setColumns(8); //specify more width than we need
42
// ftf.setHorizontalAlignment(JTextField.RIGHT);
43
// }
44

45         // Day
46
int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
47         String JavaDoc[] days = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15",
48                        "16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
49         SpinnerModel dayModel = new CyclingSpinnerListModel(days);
50         spinnerDay = addLabeledSpinner(this, labels[0], dayModel);
51         ftf = getTextField(spinnerDay);
52         if (ftf != null ) {
53            ftf.setColumns(8); //specify more width than we need
54
ftf.setHorizontalAlignment(JTextField.RIGHT);
55         }
56
57         //Month
58
String JavaDoc[] monthStrings = {"1","2","3","4","5","6","7","8","9","10","11","12"};
59         SpinnerListModel monthModel = null;
60         if (cycleMonths) { //use custom model
61
monthModel = new CyclingSpinnerListModel(monthStrings);
62         } else { //use standard model
63
monthModel = new SpinnerListModel(monthStrings);
64         }
65         spinnerMonth = addLabeledSpinner(this,
66                                     labels[1],
67                                     monthModel);
68         ftf = getTextField(spinnerMonth);
69         if (ftf != null ) {
70             ftf.setColumns(8); //specify more width than we need
71
ftf.setHorizontalAlignment(JTextField.RIGHT);
72         }
73
74
75         //Year
76
int currentYear = calendar.get(Calendar.YEAR);
77         SpinnerModel yearModel = new SpinnerNumberModel(currentYear, //initial value
78
currentYear - 100, //min
79
currentYear + 100, //max
80
1); //step
81
//If we're cycling, hook this model up to the month model.
82
if (monthModel instanceof CyclingSpinnerListModel) {
83
84 // ((CyclingSpinnerListModel)minutesModel).setLinkedModel(hourModel);
85
// ((CyclingSpinnerListModel)hourModel).setLinkedModel(dayModel);
86

87         ((CyclingSpinnerListModel)dayModel).setLinkedModel(monthModel);
88             ((CyclingSpinnerListModel)monthModel).setLinkedModel(yearModel);
89         }
90         spinnerYear = addLabeledSpinner(this, labels[2], yearModel);
91         //Make the year be formatted without a thousands separator.
92
spinnerYear.setEditor(new JSpinner.NumberEditor(spinnerYear, "#"));
93
94         //Lay out the panel.
95
SpringUtilities.makeCompactGrid(this,
96                                         numPairs, 2, //rows, cols
97
10, 10, //initX, initY
98
6, 10); //xPad, yPad
99
}
100
101     /**
102      * Return the formatted text field used by the editor, or
103      * null if the editor doesn't descend from JSpinner.DefaultEditor.
104      */

105     public JFormattedTextField getTextField(JSpinner spinner) {
106         JComponent editor = spinner.getEditor();
107         if (editor instanceof JSpinner.DefaultEditor) {
108             return ((JSpinner.DefaultEditor)editor).getTextField();
109         } else {
110             System.err.println("Unexpected editor type: "
111                                + spinner.getEditor().getClass()
112                                + " isn't a descendant of DefaultEditor");
113             return null;
114         }
115     }
116
117     /**
118      * DateFormatSymbols returns an extra, empty value at the
119      * end of the array of months. Remove it.
120      */

121     static protected String JavaDoc[] getMonthStrings() {
122         String JavaDoc[] months = new java.text.DateFormatSymbols JavaDoc().getMonths();
123         int lastIndex = months.length - 1;
124
125         if (months[lastIndex] == null
126            || months[lastIndex].length() <= 0) { //last item empty
127
String JavaDoc[] monthStrings = new String JavaDoc[lastIndex];
128             System.arraycopy(months, 0,
129                              monthStrings, 0, lastIndex);
130             return monthStrings;
131         } else { //last item not empty
132
return months;
133         }
134     }
135
136     static protected JSpinner addLabeledSpinner(Container JavaDoc c,
137                                                 String JavaDoc label,
138                                                 SpinnerModel model) {
139         JLabel l = new JLabel(label);
140         c.add(l);
141
142         JSpinner spinner = new JSpinner(model);
143         l.setLabelFor(spinner);
144         c.add(spinner);
145
146         return spinner;
147     }
148
149     /**
150      * Create the GUI and show it. For thread safety,
151      * this method should be invoked from the
152      * event-dispatching thread.
153      */

154     private static void createAndShowGUI() {
155         //Make sure we have nice window decorations.
156
JFrame.setDefaultLookAndFeelDecorated(true);
157
158         //Create and set up the window.
159
JFrame frame = new JFrame("SpinnerSet");
160         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
161
162         //Create and set up the content pane.
163
JComponent newContentPane = new SpinnerSet(false);
164         newContentPane.setOpaque(true); //content panes must be opaque
165
frame.setContentPane(newContentPane);
166
167         //Display the window.
168
frame.pack();
169         frame.setVisible(true);
170     }
171
172     public int getYear() {
173            return (((Integer JavaDoc) spinnerYear.getValue()).intValue());
174     }
175     public int getMonth() {
176        String JavaDoc monthString = (String JavaDoc) spinnerMonth.getValue();
177        if (monthString.compareTo("January")==0) return (1);
178        if (monthString.compareTo("February")==0) return (2);
179        if (monthString.compareTo("March")==0) return (3);
180        if (monthString.compareTo("April")==0) return (4);
181        if (monthString.compareTo("May")==0) return (5);
182        if (monthString.compareTo("June")==0) return (6);
183        if (monthString.compareTo("July")==0) return (7);
184        if (monthString.compareTo("August")==0) return (8);
185        if (monthString.compareTo("September")==0) return (9);
186        if (monthString.compareTo("October")==0) return (10);
187        if (monthString.compareTo("November")==0) return (11);
188            if (monthString.compareTo("December")==0) return (12);
189        return(1);
190     }
191     public int getDay() {
192            return ((new Integer JavaDoc((String JavaDoc) spinnerDay.getValue())).intValue());
193     }
194
195     public JSpinner[] getSpinners() {
196             JSpinner[] result = {spinnerDay, spinnerMonth, spinnerYear};
197             return (result);
198     }
199
200 }
Popular Tags