1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.sourceforge.jdatebutton;
24
25 import java.time.LocalDate;
26 import java.time.format.DateTimeFormatter;
27 import java.util.Locale;
28
29
30
31
32
33 public abstract class JDateBaseConfig<T extends JDateBaseButton> {
34
35
36
37 public static final String PROPERTY_DATE = "SelectedDate";
38
39
40
41
42 private Locale theLocale = null;
43
44
45
46
47 private String theFormat = null;
48
49
50
51
52 private JDateFormatter theFormatter = null;
53
54
55
56
57 private DateTimeFormatter theDateFormat = null;
58
59
60
61
62 private Boolean doShowNarrowDays = false;
63
64
65
66
67 private boolean allowNullDateSelection = false;
68
69
70
71
72 private LocalDate theEarliest = null;
73
74
75
76
77 private LocalDate theLatest = null;
78
79
80
81
82 private LocalDate theMonth = null;
83
84
85
86
87 private T theOwner = null;
88
89
90
91
92 protected JDateBaseConfig() {
93
94 setLocale(Locale.getDefault());
95 setFormat("dd-MMM-yyyy");
96 }
97
98
99
100
101
102 protected JDateBaseConfig(final JDateFormatter pFormatter) {
103
104 setFormatter(pFormatter);
105 }
106
107
108
109
110
111 public Locale getLocale() {
112 return theLocale;
113 }
114
115
116
117
118
119 public T getOwner() {
120 return theOwner;
121 }
122
123
124
125
126
127 public abstract LocalDate getSelectedDate();
128
129
130
131
132
133 public LocalDate getEarliestDate() {
134 return theEarliest;
135 }
136
137
138
139
140
141 public LocalDate getLatestDate() {
142 return theLatest;
143 }
144
145
146
147
148
149 public LocalDate getCurrentMonth() {
150 return theMonth;
151 }
152
153
154
155
156
157 public boolean allowNullDateSelection() {
158 return allowNullDateSelection;
159 }
160
161
162
163
164
165 public boolean showNarrowDays() {
166 return doShowNarrowDays;
167 }
168
169
170
171
172 protected final void checkNoFormatter() {
173
174 if (theFormatter != null) {
175 throw new UnsupportedOperationException();
176 }
177 }
178
179
180
181
182
183 public final void setLocale(final Locale pLocale) {
184
185 checkNoFormatter();
186
187
188 setTheLocale(pLocale);
189 }
190
191
192
193
194
195 public void setTheLocale(final Locale pLocale) {
196
197 theLocale = pLocale;
198
199
200 if (theFormat != null) {
201 setFormat(theFormat);
202 }
203
204
205 rebuildNames();
206 }
207
208
209
210
211
212 public final void setFormat(final String pFormat) {
213
214 checkNoFormatter();
215
216
217 theFormat = pFormat;
218
219
220 theDateFormat = DateTimeFormatter.ofPattern(theFormat, theLocale);
221
222
223 refreshText();
224 }
225
226
227
228
229 protected final void refreshText() {
230
231 if (theOwner != null) {
232 theOwner.refreshText();
233 }
234 }
235
236
237
238
239
240 public final void setFormatter(final JDateFormatter pFormatter) {
241
242 theFormatter = pFormatter;
243
244
245 theFormat = null;
246 theDateFormat = null;
247
248
249 setTheLocale(pFormatter.getLocale());
250 }
251
252
253
254
255
256
257 public void setAllowNullDateSelection(final boolean pAllowNullDateSelection) {
258 allowNullDateSelection = pAllowNullDateSelection;
259 }
260
261
262
263
264
265 public void setShowNarrowDays(final boolean pShowNarrowDays) {
266
267 doShowNarrowDays = pShowNarrowDays;
268
269
270 rebuildNames();
271 }
272
273
274
275
276 protected void rebuildNames() {
277
278 if (theOwner != null) {
279 theOwner.reBuildNames();
280 }
281 }
282
283
284
285
286
287 public void setOwner(final T pOwner) {
288 theOwner = pOwner;
289 }
290
291
292
293
294
295
296 public final void setEarliestDate(final LocalDate pEarliest) {
297
298 theEarliest = null;
299
300
301 if (pEarliest != null) {
302
303 theEarliest = pEarliest;
304
305
306 if ((theLatest != null)
307 && (theLatest.compareTo(theEarliest) < 0)) {
308 theLatest = theEarliest;
309 }
310 }
311 }
312
313
314
315
316
317
318 public final void setLatestDate(final LocalDate pLatest) {
319
320 theLatest = null;
321
322
323 if (pLatest != null) {
324
325 theLatest = pLatest;
326
327
328 if ((theEarliest != null)
329 && (theLatest.compareTo(theEarliest) < 0)) {
330 theEarliest = theLatest;
331 }
332 }
333 }
334
335
336
337
338
339
340 public String formatDate(final LocalDate pDate) {
341
342 if (pDate == null) {
343 return null;
344 }
345
346
347 if (theFormatter != null) {
348
349 return theFormatter.formatLocalDate(pDate);
350 }
351
352
353 return pDate.format(theDateFormat);
354 }
355
356
357
358
359
360
361 public String capitaliseString(final String pValue) {
362 String myValue = pValue;
363
364
365 if (Character.isLowerCase(pValue.codePointAt(0))) {
366
367 int iCharLen = pValue.offsetByCodePoints(0, 1);
368
369
370 myValue = pValue.substring(0, iCharLen).toUpperCase(theLocale)
371 + pValue.substring(iCharLen);
372 }
373
374
375 return myValue;
376 }
377
378
379
380
381
382 public LocalDate currentDate() {
383 return LocalDate.now();
384 }
385
386
387
388
389
390 public int getCurrentDay() {
391 LocalDate myDate = currentDate();
392 return isSameMonth(myDate, theMonth)
393 ? myDate.getDayOfMonth()
394 : 0;
395 }
396
397
398
399
400
401 public int getSelectedDay() {
402 LocalDate myDate = getSelectedDate();
403 return isSameMonth(myDate, theMonth)
404 ? myDate.getDayOfMonth()
405 : 0;
406 }
407
408
409
410
411
412 public int getEarliestDay() {
413 return isSameMonth(theEarliest, theMonth)
414 ? theEarliest.getDayOfMonth()
415 : 0;
416 }
417
418
419
420
421
422 public int getLatestDay() {
423 return isSameMonth(theLatest, theMonth)
424 ? theLatest.getDayOfMonth()
425 : 0;
426 }
427
428
429
430
431 public void previousMonth() {
432 theMonth = theMonth.minusMonths(1);
433 }
434
435
436
437
438 public void nextMonth() {
439 theMonth = theMonth.plusMonths(1);
440 }
441
442
443
444
445 public void previousYear() {
446 theMonth = theMonth.minusYears(1);
447 if ((theEarliest != null)
448 && (theMonth.compareTo(theEarliest) < 0)) {
449 theMonth = theEarliest.withDayOfMonth(1);
450 }
451 }
452
453
454
455
456 public void nextYear() {
457 theMonth = theMonth.plusYears(1);
458 if ((theLatest != null)
459 && (theMonth.compareTo(theLatest) > 0)) {
460 theMonth = theLatest.withDayOfMonth(1);
461 }
462 }
463
464
465
466
467
468 public final void setSelectedDate(final LocalDate pDate) {
469 LocalDate myOld = getSelectedDate();
470
471
472 if (!isDateChanged(myOld, pDate)) {
473 return;
474 }
475
476
477 storeSelectedDate(pDate);
478 if (theOwner != null) {
479 theOwner.refreshText();
480 }
481 }
482
483
484
485
486
487 protected abstract void storeSelectedDate(final LocalDate pDate);
488
489
490
491
492
493 protected void storeExplicitDate(final LocalDate pDate) {
494 storeSelectedDate(pDate);
495 }
496
497
498
499
500
501 public void setSelectedDay(final int pDay) {
502 LocalDate myOld = getSelectedDate();
503 LocalDate myNew = null;
504
505
506 if (pDay > 0) {
507
508 myNew = theMonth.withDayOfMonth(pDay);
509 }
510
511
512 if (!isDateChanged(myOld, myNew)) {
513 return;
514 }
515
516
517 storeExplicitDate(myNew);
518 if (theOwner != null) {
519 theOwner.refreshText();
520 }
521 }
522
523
524
525
526 public void initialiseCurrent() {
527
528 LocalDate myDate = getInitialDate();
529
530
531 if ((theEarliest != null)
532 && (myDate.compareTo(theEarliest) < 0)) {
533 myDate = theEarliest;
534 }
535
536
537 if ((theLatest != null)
538 && (myDate.compareTo(theLatest) > 0)) {
539 myDate = theLatest;
540 }
541
542
543 theMonth = myDate.withDayOfMonth(1);
544 }
545
546
547
548
549
550 protected abstract LocalDate getInitialDate();
551
552
553
554
555
556
557
558 public static boolean isDateChanged(final LocalDate pFirst,
559 final LocalDate pSecond) {
560 if (pFirst == null) {
561 return pSecond != null;
562 } else {
563 return !pFirst.equals(pSecond);
564 }
565 }
566
567
568
569
570
571
572
573 public static boolean isSameMonth(final LocalDate pFirst,
574 final LocalDate pSecond) {
575 if (!isSameYear(pFirst, pSecond)) {
576 return false;
577 } else {
578 return pFirst.getMonthValue() == pSecond.getMonthValue();
579 }
580 }
581
582
583
584
585
586
587
588 public static boolean isSameYear(final LocalDate pFirst,
589 final LocalDate pSecond) {
590 if (pFirst == null) {
591 return false;
592 }
593 return pFirst.getYear() == pSecond.getYear();
594 }
595 }