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
.store
.model
.Resources
;
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
= "Year (Start)";
49 public static final String P_STARTMONTH
= "Month (Start)";
50 public static final String P_STARTDAY
= "Day (Start)";
51 public static final String P_ENDYEAR
= "Year (End)";
52 public static final String P_ENDMONTH
= "Month (End)";
53 public static final String P_ENDDAY
= "Day (End)";
55 protected static final String
[] TOP_LEVEL_PROPERTIES
= new String
[] {
63 private static final String
[] P_MONTH_MENU
= new String
[] {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
65 public TimePeriodPropertySource(TimePeriod timePeriod
) {
68 if (timePeriod
== null) {
69 timePeriod
= TimePeriod
.NewInstance();
71 this.timePeriod
= timePeriod
;
73 // Add property sheet descriptors
74 for (String key
: TOP_LEVEL_PROPERTIES
) {
79 //static date formatter
80 private static final DateFormat formatter
= new SimpleDateFormat(
81 "EEEE, MMMM d, yyyy"); //$NON-NLS-1$
83 private Vector
<PropertyDescriptor
> descriptors
= new Vector
<PropertyDescriptor
>();
85 private void addDescriptor(String id
) {
87 if (id
.equals(P_ID_STARTYEAR
)) {
88 PropertyDescriptor descriptor
= new TextPropertyDescriptor(P_ID_STARTYEAR
, P_STARTYEAR
);
89 descriptor
.setValidator(new YearValidator());
90 descriptors
.addElement(descriptor
);
93 if (id
.equals(P_ID_STARTMONTH
)) {
94 descriptors
.addElement(new ComboBoxPropertyDescriptor(
95 P_ID_STARTMONTH
, P_STARTMONTH
, P_MONTH_MENU
));
98 if (id
.equals(P_ID_STARTDAY
)) {
99 PropertyDescriptor descriptor
= new TextPropertyDescriptor(P_ID_STARTDAY
, P_STARTDAY
);
100 descriptor
.setValidator(new DayValidator());
101 descriptors
.addElement(descriptor
);
104 if (id
.equals(P_ID_ENDYEAR
)) {
105 PropertyDescriptor descriptor
= new TextPropertyDescriptor(P_ID_ENDYEAR
, P_ENDYEAR
);
106 descriptor
.setValidator(new YearValidator());
107 descriptors
.addElement(descriptor
);
110 if (id
.equals(P_ID_ENDMONTH
)) {
111 descriptors
.addElement(new ComboBoxPropertyDescriptor(
112 P_ID_ENDMONTH
, P_ENDMONTH
, P_MONTH_MENU
));
115 if (id
.equals(P_ID_ENDDAY
)) {
116 PropertyDescriptor descriptor
= new TextPropertyDescriptor(P_ID_ENDDAY
, P_ENDDAY
);
117 descriptor
.setValidator(new DayValidator());
118 descriptors
.addElement(descriptor
);
123 * Method declared on IPropertySource
125 public IPropertyDescriptor
[] getPropertyDescriptors() {
126 return (IPropertyDescriptor
[]) descriptors
.toArray(
127 new IPropertyDescriptor
[descriptors
.size()]);
130 public Object
getPropertyValue(Object id
) {
132 if (id
.equals(P_ID_STARTYEAR
)) {
133 Integer startYear
= timePeriod
.getStartYear();
134 return (startYear
== null) ?
"" : String
.valueOf(startYear
);
137 if (id
.equals(P_ID_STARTMONTH
)) {
138 Integer startMonth
= timePeriod
.getStartMonth();
139 return (startMonth
== null) ?
0 : startMonth
;
142 if (id
.equals(P_ID_STARTDAY
)) {
143 Integer startDay
= timePeriod
.getStartDay();
144 return (startDay
== null) ?
"" : String
.valueOf(startDay
);
147 if (id
.equals(P_ID_ENDYEAR
)) {
148 Integer endYear
= timePeriod
.getEndYear();
149 return (endYear
== null) ?
"" : String
.valueOf(endYear
);
152 if (id
.equals(P_ID_ENDMONTH
)) {
153 Integer endMonth
= timePeriod
.getEndMonth();
154 return (endMonth
== null) ?
0 : endMonth
;
157 if (id
.equals(P_ID_ENDDAY
)) {
158 Integer endDay
= timePeriod
.getEndDay();
159 return (endDay
== null) ?
"" : String
.valueOf(endDay
);
166 * Method declared on IPropertySource
168 public boolean isPropertySet(Object property
) {
173 * Method declared on IPropertySource
175 public void resetPropertyValue(Object property
) {}
177 public void setPropertyValue(Object id
, Object value
) {
179 if (id
.equals(P_ID_STARTYEAR
)) {
180 timePeriod
.setStartYear(castToInteger(value
));
183 if (id
.equals(P_ID_STARTMONTH
)) {
184 timePeriod
.setStartMonth(castToInteger(value
));
187 if (id
.equals(P_ID_STARTDAY
)) {
188 timePeriod
.setStartDay(castToInteger(value
));
191 if (id
.equals(P_ID_ENDYEAR
)) {
192 timePeriod
.setEndYear(castToInteger(value
));
195 if (id
.equals(P_ID_ENDMONTH
)) {
196 timePeriod
.setEndMonth(castToInteger(value
));
199 if (id
.equals(P_ID_ENDDAY
)) {
200 timePeriod
.setEndDay(castToInteger(value
));
203 propertyChangeSupport
.firePropertyChange(Resources
.PROPERTY_SHEET_CHANGE
, null, timePeriod
);
206 private Integer
castToInteger(Object value
) {
208 // Dropdown lists return an Integer index
209 if (value
instanceof Integer
) {
211 // First entry in dropdown is empty
212 if (((Integer
) value
).equals(0)) {
215 return (Integer
) value
;
219 Integer integer
= null;
221 integer
= new Integer((String
) value
);
223 } catch (ClassCastException e
) {
224 } catch (NumberFormatException e
) {
230 * Method declared on IPropertySource
232 public Object
getEditableValue() {
233 if (timePeriod
== null) {
236 return timePeriod
.getYear();
240 // * The value as displayed in the Property Sheet.
241 // * @return java.lang.String
243 // public String toString() {
244 // if (timePeriod == null) {
247 // return timePeriod.getYear();
251 private PropertyChangeSupport propertyChangeSupport
= new PropertyChangeSupport(this);
253 public void addPropertyChangeListener(
254 PropertyChangeListener listener
) {
255 propertyChangeSupport
.addPropertyChangeListener(listener
);