-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathYearMonthSpinner.java
More file actions
210 lines (191 loc) · 6.25 KB
/
YearMonthSpinner.java
File metadata and controls
210 lines (191 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
* @(#)YearMonthSpinner.java 1.0 2015/02/17
*/
package darrylbu.component;
import java.awt.ComponentOrientation;
import java.awt.FlowLayout;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.event.ChangeListener;
import darrylbu.editor.SpinnerTemporalEditor;
import darrylbu.model.SpinnerTemporalModel;
/**
* YearMonthSpinner is a composite of two spinners, one for the month and the other for the year,
* used to select a java.time.MonthYear.
* <P>
* <B>Note that compiling or using this class requires Java 8</B>
*
* @author Darryl
* @see YearMonth
*
*/
public class YearMonthSpinner extends JPanel {
private boolean programmaticChange;
private SpinnerTemporalModel<YearMonth> monthModel;
private SpinnerTemporalModel<YearMonth> yearModel;
private JSpinner monthSpinner;
private JSpinner yearSpinner;
/**
* Constructs a YearMonthSpinner with the current year and month, with no lower or upper limits.
*
*/
public YearMonthSpinner() {
this(YearMonth.now());
}
/**
* Constructs a YearMonthSpinner with the supplied year and month, with no lower or upper limits.
*
* @param value the initial value
*/
public YearMonthSpinner(YearMonth value) {
this(value, null, null);
}
/**
* Constructs a YearMonthSpinner with the supplied year and month, lower (earliest) and upper
* (latest) dates.
*
* @param value the initial value
* @param min the minimum (earliest) value
* @param max the maximum (latest) value
*/
public YearMonthSpinner(YearMonth value, YearMonth min, YearMonth max) {
super(new FlowLayout(FlowLayout.CENTER, 0, 0));
if (value == null) {
value = YearMonth.now();
}
monthModel = new SpinnerTemporalModel<>(value, min, max, ChronoUnit.MONTHS);
monthSpinner = new JSpinner(monthModel) {
@Override
public ComponentOrientation getComponentOrientation() {
return ComponentOrientation.RIGHT_TO_LEFT;
}
};
SpinnerTemporalEditor monthEditor = new SpinnerTemporalEditor(monthSpinner,
DateTimeFormatter.ofPattern("MMMM"));
monthEditor.getTextField().setColumns(7);
monthSpinner.setEditor(monthEditor);
yearModel = new SpinnerTemporalModel<>(value, min, max, ChronoUnit.YEARS);
yearSpinner = new JSpinner(yearModel) {
@Override
public ComponentOrientation getComponentOrientation() {
return ComponentOrientation.LEFT_TO_RIGHT;
}
};
SpinnerTemporalEditor yearEditor
= new SpinnerTemporalEditor(yearSpinner, DateTimeFormatter.ofPattern("uuuu"));
yearEditor.getTextField().setColumns(3);
yearEditor.getTextField().setHorizontalAlignment(JTextField.RIGHT);
yearSpinner.setEditor(yearEditor);
monthSpinner.addChangeListener(ce -> {
if (!programmaticChange) {
programmaticChange = true;
yearSpinner.setValue(monthSpinner.getValue());//value);
programmaticChange = false;
}
});
yearSpinner.addChangeListener(ce -> {
if (!programmaticChange) {
programmaticChange = true;
monthSpinner.setValue(yearSpinner.getValue());//value);
programmaticChange = false;
}
});
add(monthSpinner);
add(yearSpinner);
}
/**
* Adds a listener that is notified each time a change occurs. The source of the
* <code>ChangeEvent</code> will be the contained month spinner.
*
* @param listener the <code>ChangeListeners</code> to add
* @see #removeChangeListener
*/
public void addChangeListener(ChangeListener listener) {
monthSpinner.addChangeListener(listener);
}
/**
* Removes a <code>ChangeListener</code>.
*
* @param listener the <code>ChangeListener</code> to remove
*/
public void removeChangeListener(ChangeListener listener) {
monthSpinner.removeChangeListener(listener);
}
/**
* {@inheritDoc }
*/
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
monthSpinner.setEnabled(enabled);
yearSpinner.setEnabled(enabled);
}
/**
* Returns the current value
*
* @return the current value
*/
public YearMonth getValue() {
return monthModel.getTemporalValue();
}
/**
* Sets the current value
* <P>
* This class does not attempt to verify that min <= value <= max. It is the responsibility of
* client code to supply a sane value.
*
* @param value The value to set
*/
public void setValue(YearMonth value) {
programmaticChange = true;
monthSpinner.setValue(value);
yearSpinner.setValue(value);
programmaticChange = false;
}
/**
* Returns the minimum value (earliest year/month), or <CODE>null</CODE> if no limit is set.
*
* @return The earliest year/month that can be selected.
*/
public YearMonth getMin() {
return monthModel.getMin();
}
/**
* Sets the minimum value (earliest year/month). Call this method with a <CODE>null</CODE> value
* for no limit.
* <P>
* This class does not attempt to verify that min <= value <= max. It is the responsibility of
* client code to supply a sane value.
*
* @param min The earliest year/month that can be selected, or <CODE>null</CODE> for no limit.
*/
public void setMin(YearMonth min) {
monthModel.setMin(min);
yearModel.setMin(min);
}
/**
* Returns the maximum value (latest year/month), or <CODE>null</CODE> if no limit is set.
*
* @return The latest year/month that can be selected.
*/
public YearMonth getMax() {
return monthModel.getMax();
}
/**
* Sets the maximum value (latest year/month). Call this method with a <CODE>null</CODE> value for
* no limit.
* <P>
* This class does not attempt to verify that min <= value <= max. It is the responsibility of
* client code to supply a sane value.
*
* @param max The latest year/month that can be selected, or <CODE>null</CODE> for no limit.
*/
public void setMax(YearMonth max) {
monthModel.setMax(max);
yearModel.setMax(max);
}
}