-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathLocalDateCombo.java
More file actions
249 lines (227 loc) · 7.8 KB
/
LocalDateCombo.java
File metadata and controls
249 lines (227 loc) · 7.8 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/**
* @(#)LocalDateCombo.java 1.0 2015/02/19
*/
package darrylbu.component;
import java.awt.Component;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
/**
* LocalDateCombo is a Swing date picker for selecting a java.time.LocalDate from a drop-down
* {@link MonthView}.
* <P>
* <B>Note that compiling or using this class requires Java 8</B>
*
* @author Darryl
* @see LocalDate
*/
public class LocalDateCombo extends JComboBox<LocalDate> {
private final DefaultComboBoxModel<LocalDate> comboModel = new DefaultComboBoxModel<>();
private LocalDate min;
private LocalDate max;
private final MonthView monthView;
private final JPopupMenu popupMenu = new JPopupMenu();
/**
* Constructs a LocalDateCombo with today's date, no upper or lower limits, formatted in a medium
* style.
*
* @see FormatStyle#MEDIUM
*/
public LocalDateCombo() {
this(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM));
}
/**
* Constructs a LocalDateCombo with today's date and no upper or lower limits, formatted according
* to the provided formatter.
*
* @param formatter Formats the date for display
*
* @see DateTimeFormatter
*/
public LocalDateCombo(DateTimeFormatter formatter) {
this(LocalDate.now(), formatter);
}
/**
* Constructs a LocalDateCombo with the date provided and no lower or upper limits, formatted in a
* medium style.
*
* @param value The initial value
*
* @see FormatStyle#MEDIUM
*/
public LocalDateCombo(LocalDate value) {
this(value, DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM));
}
/**
* Constructs a LocalDateCombo with the date provided and no lower or upper limits, formatted
* according to the provided formatter.
*
* @param value The initial value
* @param formatter Formats the date for display
*/
public LocalDateCombo(LocalDate value, DateTimeFormatter formatter) {
this(value, null, null, formatter);
}
/**
* Constructs a LocalDateCombo with the date, lower (earliest) and upper (latest) limits provided,
* formatted in a medium style.
* <P>
* Dates outside the specified range are not displayed.
* <P>
* This class does not attempt to verify that minDate <= value <= maxDate. It is the
* responsibility of client code to supply sane values.
*
* @param value The initial value
* @param minDate The minimum value (earliest date); <CODE>null</CODE> for no limit.
* @param maxDate The maximum value (latest date); <CODE>null</CODE> for no limit.
*
* @see FormatStyle#MEDIUM
*/
public LocalDateCombo(LocalDate value, LocalDate minDate, LocalDate maxDate) {
this(value, minDate, maxDate, DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM));
}
/**
* Constructs a LocalDateCombo with the date, lower (earliest) and upper (latest) limits provided,
* formatted according to the provided formatter.
* <P>
* Dates outside the specified range are not displayed.
* <P>
* This class does not attempt to verify that minDate <= value <= maxDate. It is the
* responsibility of client code to supply sane values.
*
* @param value The initial value
* @param minDate The minimum value (earliest date); <CODE>null</CODE> for no limit.
* @param maxDate The maximum value (latest date); <CODE>null</CODE> for no limit.
* @param formatter Formats the date for display
*/
public LocalDateCombo(LocalDate value, LocalDate minDate, LocalDate maxDate,
DateTimeFormatter formatter) {
// Thursday after 24 of September
LocalDate longNameDate = LocalDate.now().withDayOfMonth(24).withMonth(9);
longNameDate = longNameDate.plusDays(4 - longNameDate.getDayOfWeek().getValue());
setPrototypeDisplayValue(longNameDate);
monthView = new MonthView(value, minDate, maxDate);
comboModel.addElement(value);
setModel(comboModel);
setRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value,
int index, boolean isSelected, boolean hasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, hasFocus);
setText(formatter.format((TemporalAccessor) value));
return this;
}
});
min = minDate;
max = maxDate;
popupMenu.add(monthView);
addPopupMenuListener(new PopupMenuListener() {
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent pme) {
final boolean popupShown = popupMenu.isShowing();
SwingUtilities.invokeLater(() -> {
hidePopup();
if (popupShown) {
popupMenu.setVisible(false);
} else {
monthView.setValue(getValue());
popupMenu.show(LocalDateCombo.this, 0, getHeight());
}
});
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent pme) {
}
@Override
public void popupMenuCanceled(PopupMenuEvent pme) {
}
});
monthView.addPropertyChangeListener("Confirm", pce -> {
popupMenu.setVisible(false);
});
monthView.addPropertyChangeListener("Value", pce -> {
setValue((LocalDate) pce.getNewValue());
firePropertyChange("Value", pce.getOldValue(), pce.getNewValue());
});
}
/**
* Returns the current value
*
* @return the current value
*/
public LocalDate getValue() {
return comboModel.getElementAt(0);//value;
}
/**
* Sets the current value, adjusted to be within any specified min/max range.
*
* @param value The value to set
*/
public void setValue(LocalDate value) {
if (getSelectedItem().equals(value)) {
return;
}
if (min != null && value.isBefore(min)) {
value = min;
}
if (max != null && value.isAfter(max)) {
value = max;
}
comboModel.removeAllElements();
comboModel.addElement(value);
if (!monthView.getValue().equals(value)) {
monthView.setValue(value);
}
}
/**
* Returns the minimum value (earliest date), or <CODE>null</CODE> if no limit is set.
*
* @return The earliest date that can be selected.
*/
public LocalDate getMin() {
return min;
}
/**
* Sets the minimum value (earliest date). 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 date that can be selected, or <CODE>null</CODE> for no limit.
*/
public void setMin(LocalDate min) {
this.min = min;
monthView.setMin(min);
}
/**
* Returns the maximum value (latest date), or <CODE>null</CODE> if no limit is set.
*
* @return The latest date that can be selected.
*/
public LocalDate getMax() {
return max;
}
/**
* Sets the maximum value (latest date). 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 date that can be selected, or <CODE>null</CODE> for no limit.
*/
public void setMax(LocalDate max) {
this.max = max;
monthView.setMax(max);
}
}