ExtendedCalendarPropertyEditor.java
01 /*
02  * Copyright 2008-2016 the original author or authors.
03  *
04  * Licensed under the Apache License, Version 2.0 (the "License");
05  * you may not use this file except in compliance with the License.
06  * You may obtain a copy of the License at
07  *
08  *     http://www.apache.org/licenses/LICENSE-2.0
09  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package griffon.core.editors;
17 
18 import griffon.metadata.PropertyEditorFor;
19 
20 import java.time.LocalDate;
21 import java.time.LocalDateTime;
22 import java.time.LocalTime;
23 import java.util.Calendar;
24 import java.util.Date;
25 
26 /**
27  @author Andres Almiray
28  @since 2.4.0
29  */
30 @PropertyEditorFor(Calendar.class)
31 public class ExtendedCalendarPropertyEditor extends CalendarPropertyEditor {
32     @Override
33     protected void setValueInternal(Object value) {
34         if (value instanceof LocalDate) {
35             handleAsLocalDate((LocalDatevalue);
36         else if (value instanceof LocalDateTime) {
37             handleAsLocalDateTime(((LocalDateTimevalue));
38         else {
39             super.setValueInternal(value);
40         }
41     }
42 
43     protected void handleAsLocalDate(LocalDate value) {
44         Calendar c = Calendar.getInstance();
45         c.setTime(new Date(value.toEpochDay()));
46         super.setValueInternal(c);
47     }
48 
49     protected void handleAsLocalDateTime(LocalDateTime value) {
50         LocalDate localDate = value.toLocalDate();
51         LocalTime localTime = value.toLocalTime();
52 
53         Calendar c = Calendar.getInstance();
54         c.set(
55             localDate.getYear(),
56             localDate.getMonthValue() 1,
57             localDate.getDayOfMonth(),
58             localTime.getHour(),
59             localTime.getMinute(),
60             localTime.getSecond()
61         );
62 
63         super.setValueInternal(c);
64     }
65 }