2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.propertysheet
;
12 import java
.beans
.PropertyChangeListener
;
13 import java
.beans
.PropertyChangeSupport
;
14 import java
.text
.DateFormat
;
15 import java
.text
.SimpleDateFormat
;
16 import java
.util
.Vector
;
18 import org
.apache
.log4j
.Logger
;
19 import org
.eclipse
.ui
.views
.properties
.ComboBoxPropertyDescriptor
;
20 import org
.eclipse
.ui
.views
.properties
.IPropertyDescriptor
;
21 import org
.eclipse
.ui
.views
.properties
.IPropertySource
;
22 import org
.eclipse
.ui
.views
.properties
.PropertyDescriptor
;
23 import org
.eclipse
.ui
.views
.properties
.TextPropertyDescriptor
;
25 import eu
.etaxonomy
.cdm
.model
.common
.TimePeriod
;
26 import eu
.etaxonomy
.taxeditor
.ITaxEditorConstants
;
33 public class TimePeriodPropertySource
implements IPropertySource
{
34 private static final Logger logger
= Logger
35 .getLogger(TimePeriodPropertySource
.class);
37 private TimePeriod timePeriod
;
39 // Property unique keys
40 public static final String P_ID_STARTYEAR
= "P_ID_STARTYEAR";
41 public static final String P_ID_STARTMONTH
= "P_ID_STARTMONTH";
42 public static final String P_ID_STARTDAY
= "P_ID_STARTDAY";
43 public static final String P_ID_ENDYEAR
= "P_ID_ENDYEAR";
44 public static final String P_ID_ENDMONTH
= "P_ID_ENDMONTH";
45 public static final String P_ID_ENDDAY
= "P_ID_ENDDAY";
47 // Property display keys
48 public static final String P_STARTYEAR
= "00:Year (Start)";
49 public static final String P_STARTMONTH
= "01:Month (Start)";
50 public static final String P_STARTDAY
= "02:Day (Start)";
51 public static final String P_ENDYEAR
= "03:Year (End)";
52 public static final String P_ENDMONTH
= "04:Month (End)";
53 public static final String P_ENDDAY
= "05:Day (End)";
55 protected static final String
[] TOP_LEVEL_PROPERTIES
=
56 new String
[] {P_ID_STARTYEAR
, P_ID_STARTMONTH
, P_ID_STARTDAY
,
57 P_ID_ENDYEAR
, P_ID_ENDMONTH
, P_ID_ENDDAY
};
59 private static final String
[] P_MONTH_MENU
= new String
[] {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
61 public TimePeriodPropertySource(TimePeriod timePeriod
) {
64 if (timePeriod
== null) {
65 timePeriod
= TimePeriod
.NewInstance();
67 this.timePeriod
= timePeriod
;
69 // Add property sheet descriptors
70 for (String key
: TOP_LEVEL_PROPERTIES
) {
75 //static date formatter
76 private static final DateFormat formatter
= new SimpleDateFormat(
77 "EEEE, MMMM d, yyyy"); //$NON-NLS-1$
79 private Vector
<PropertyDescriptor
> descriptors
= new Vector
<PropertyDescriptor
>();
81 private void addDescriptor(String id
) {
83 if (id
.equals(P_ID_STARTYEAR
)) {
84 PropertyDescriptor descriptor
= new TextPropertyDescriptor(P_ID_STARTYEAR
, P_STARTYEAR
);
85 descriptor
.setValidator(new YearValidator());
86 descriptors
.addElement(descriptor
);
89 if (id
.equals(P_ID_STARTMONTH
)) {
90 descriptors
.addElement(new ComboBoxPropertyDescriptor(
91 P_ID_STARTMONTH
, P_STARTMONTH
, P_MONTH_MENU
));
94 if (id
.equals(P_ID_STARTDAY
)) {
95 PropertyDescriptor descriptor
= new TextPropertyDescriptor(P_ID_STARTDAY
, P_STARTDAY
);
96 descriptor
.setValidator(new DayValidator());
97 descriptors
.addElement(descriptor
);
100 if (id
.equals(P_ID_ENDYEAR
)) {
101 PropertyDescriptor descriptor
= new TextPropertyDescriptor(P_ID_ENDYEAR
, P_ENDYEAR
);
102 descriptor
.setValidator(new YearValidator());
103 descriptors
.addElement(descriptor
);
106 if (id
.equals(P_ID_ENDMONTH
)) {
107 descriptors
.addElement(new ComboBoxPropertyDescriptor(
108 P_ID_ENDMONTH
, P_ENDMONTH
, P_MONTH_MENU
));
111 if (id
.equals(P_ID_ENDDAY
)) {
112 PropertyDescriptor descriptor
= new TextPropertyDescriptor(P_ID_ENDDAY
, P_ENDDAY
);
113 descriptor
.setValidator(new DayValidator());
114 descriptors
.addElement(descriptor
);
119 * Method declared on IPropertySource
121 public IPropertyDescriptor
[] getPropertyDescriptors() {
122 return (IPropertyDescriptor
[]) descriptors
.toArray(
123 new IPropertyDescriptor
[descriptors
.size()]);
126 public Object
getPropertyValue(Object id
) {
128 if (id
.equals(P_ID_STARTYEAR
)) {
129 Integer startYear
= timePeriod
.getStartYear();
130 return (startYear
== null) ?
"" : String
.valueOf(startYear
);
133 if (id
.equals(P_ID_STARTMONTH
)) {
134 Integer startMonth
= timePeriod
.getStartMonth();
135 return (startMonth
== null) ?
0 : startMonth
;
138 if (id
.equals(P_ID_STARTDAY
)) {
139 Integer startDay
= timePeriod
.getStartDay();
140 return (startDay
== null) ?
"" : String
.valueOf(startDay
);
143 if (id
.equals(P_ID_ENDYEAR
)) {
144 Integer endYear
= timePeriod
.getEndYear();
145 return (endYear
== null) ?
"" : String
.valueOf(endYear
);
148 if (id
.equals(P_ID_ENDMONTH
)) {
149 Integer endMonth
= timePeriod
.getEndMonth();
150 return (endMonth
== null) ?
0 : endMonth
;
153 if (id
.equals(P_ID_ENDDAY
)) {
154 Integer endDay
= timePeriod
.getEndDay();
155 return (endDay
== null) ?
"" : String
.valueOf(endDay
);
162 * Method declared on IPropertySource
164 public boolean isPropertySet(Object property
) {
169 * Method declared on IPropertySource
171 public void resetPropertyValue(Object property
) {}
173 public void setPropertyValue(Object id
, Object value
) {
175 if (id
.equals(P_ID_STARTYEAR
)) {
176 timePeriod
.setStartYear(castToInteger(value
));
179 if (id
.equals(P_ID_STARTMONTH
)) {
180 timePeriod
.setStartMonth(castToInteger(value
));
183 if (id
.equals(P_ID_STARTDAY
)) {
184 timePeriod
.setStartDay(castToInteger(value
));
187 if (id
.equals(P_ID_ENDYEAR
)) {
188 timePeriod
.setEndYear(castToInteger(value
));
191 if (id
.equals(P_ID_ENDMONTH
)) {
192 timePeriod
.setEndMonth(castToInteger(value
));
195 if (id
.equals(P_ID_ENDDAY
)) {
196 timePeriod
.setEndDay(castToInteger(value
));
199 propertyChangeSupport
.firePropertyChange(ITaxEditorConstants
.PROPERTY_SHEET_CHANGE
, null, timePeriod
);
202 private Integer
castToInteger(Object value
) {
204 // Dropdown lists return an Integer index
205 if (value
instanceof Integer
) {
207 // First entry in dropdown is empty
208 if (((Integer
) value
).equals(0)) {
211 return (Integer
) value
;
215 Integer integer
= null;
217 integer
= new Integer((String
) value
);
219 } catch (ClassCastException e
) {
220 } catch (NumberFormatException e
) {
226 * Method declared on IPropertySource
228 public Object
getEditableValue() {
229 if (timePeriod
== null) {
232 return timePeriod
.getYear();
236 // * The value as displayed in the Property Sheet.
237 // * @return java.lang.String
239 // public String toString() {
240 // if (timePeriod == null) {
243 // return timePeriod.getYear();
247 private PropertyChangeSupport propertyChangeSupport
= new PropertyChangeSupport(this);
249 public void addPropertyChangeListener(
250 PropertyChangeListener listener
) {
251 propertyChangeSupport
.addPropertyChangeListener(listener
);