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
.util
.Vector
;
16 import org
.eclipse
.ui
.views
.properties
.ComboBoxPropertyDescriptor
;
17 import org
.eclipse
.ui
.views
.properties
.IPropertyDescriptor
;
18 import org
.eclipse
.ui
.views
.properties
.IPropertySource
;
19 import org
.eclipse
.ui
.views
.properties
.PropertyDescriptor
;
20 import org
.eclipse
.ui
.views
.properties
.TextPropertyDescriptor
;
22 import eu
.etaxonomy
.cdm
.model
.common
.TimePeriod
;
23 import eu
.etaxonomy
.taxeditor
.model
.Resources
;
30 public class TimePeriodPropertySource
implements ICdmBasePropertySource
{
32 private TimePeriod timePeriod
;
34 // Property unique keys
35 public static final String P_ID_STARTYEAR
= "P_ID_STARTYEAR";
36 public static final String P_ID_STARTMONTH
= "P_ID_STARTMONTH";
37 public static final String P_ID_STARTDAY
= "P_ID_STARTDAY";
38 public static final String P_ID_ENDYEAR
= "P_ID_ENDYEAR";
39 public static final String P_ID_ENDMONTH
= "P_ID_ENDMONTH";
40 public static final String P_ID_ENDDAY
= "P_ID_ENDDAY";
42 // Property display keys
43 public static final String P_STARTYEAR
= "Year (Start)";
44 public static final String P_STARTMONTH
= "Month (Start)";
45 public static final String P_STARTDAY
= "Day (Start)";
46 public static final String P_ENDYEAR
= "Year (End)";
47 public static final String P_ENDMONTH
= "Month (End)";
48 public static final String P_ENDDAY
= "Day (End)";
50 protected static final String
[] TOP_LEVEL_PROPERTIES
= new String
[] {
58 private static final String
[] P_MONTH_MENU
= new String
[] {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
60 public TimePeriodPropertySource(TimePeriod timePeriod
) {
63 if (timePeriod
== null) {
64 timePeriod
= TimePeriod
.NewInstance();
66 this.timePeriod
= timePeriod
;
68 // Add property sheet descriptors
69 for (String key
: TOP_LEVEL_PROPERTIES
) {
74 private Vector
<PropertyDescriptor
> descriptors
= new Vector
<PropertyDescriptor
>();
76 private void addDescriptor(String id
) {
78 if (id
.equals(P_ID_STARTYEAR
)) {
79 PropertyDescriptor descriptor
= new TextPropertyDescriptor(P_ID_STARTYEAR
, P_STARTYEAR
);
80 descriptor
.setValidator(new YearValidator());
81 descriptors
.addElement(descriptor
);
84 if (id
.equals(P_ID_STARTMONTH
)) {
85 descriptors
.addElement(new ComboBoxPropertyDescriptor(
86 P_ID_STARTMONTH
, P_STARTMONTH
, P_MONTH_MENU
));
89 if (id
.equals(P_ID_STARTDAY
)) {
90 PropertyDescriptor descriptor
= new TextPropertyDescriptor(P_ID_STARTDAY
, P_STARTDAY
);
91 descriptor
.setValidator(new DayValidator());
92 descriptors
.addElement(descriptor
);
95 if (id
.equals(P_ID_ENDYEAR
)) {
96 PropertyDescriptor descriptor
= new TextPropertyDescriptor(P_ID_ENDYEAR
, P_ENDYEAR
);
97 descriptor
.setValidator(new YearValidator());
98 descriptors
.addElement(descriptor
);
101 if (id
.equals(P_ID_ENDMONTH
)) {
102 descriptors
.addElement(new ComboBoxPropertyDescriptor(
103 P_ID_ENDMONTH
, P_ENDMONTH
, P_MONTH_MENU
));
106 if (id
.equals(P_ID_ENDDAY
)) {
107 PropertyDescriptor descriptor
= new TextPropertyDescriptor(P_ID_ENDDAY
, P_ENDDAY
);
108 descriptor
.setValidator(new DayValidator());
109 descriptors
.addElement(descriptor
);
114 * Method declared on IPropertySource
116 public IPropertyDescriptor
[] getPropertyDescriptors() {
117 return (IPropertyDescriptor
[]) descriptors
.toArray(
118 new IPropertyDescriptor
[descriptors
.size()]);
121 public Object
getPropertyValue(Object id
) {
123 if (id
.equals(P_ID_STARTYEAR
)) {
124 Integer startYear
= timePeriod
.getStartYear();
125 return (startYear
== null) ?
"" : String
.valueOf(startYear
);
128 if (id
.equals(P_ID_STARTMONTH
)) {
129 Integer startMonth
= timePeriod
.getStartMonth();
130 return (startMonth
== null) ?
0 : startMonth
;
133 if (id
.equals(P_ID_STARTDAY
)) {
134 Integer startDay
= timePeriod
.getStartDay();
135 return (startDay
== null) ?
"" : String
.valueOf(startDay
);
138 if (id
.equals(P_ID_ENDYEAR
)) {
139 Integer endYear
= timePeriod
.getEndYear();
140 return (endYear
== null) ?
"" : String
.valueOf(endYear
);
143 if (id
.equals(P_ID_ENDMONTH
)) {
144 Integer endMonth
= timePeriod
.getEndMonth();
145 return (endMonth
== null) ?
0 : endMonth
;
148 if (id
.equals(P_ID_ENDDAY
)) {
149 Integer endDay
= timePeriod
.getEndDay();
150 return (endDay
== null) ?
"" : String
.valueOf(endDay
);
157 * Method declared on IPropertySource
159 public boolean isPropertySet(Object property
) {
164 * Method declared on IPropertySource
166 public void resetPropertyValue(Object property
) {}
168 public void setPropertyValue(Object id
, Object value
) {
170 if (id
.equals(P_ID_STARTYEAR
)) {
171 timePeriod
.setStartYear(castToInteger(value
));
174 if (id
.equals(P_ID_STARTMONTH
)) {
175 timePeriod
.setStartMonth(castToInteger(value
));
178 if (id
.equals(P_ID_STARTDAY
)) {
179 timePeriod
.setStartDay(castToInteger(value
));
182 if (id
.equals(P_ID_ENDYEAR
)) {
183 timePeriod
.setEndYear(castToInteger(value
));
186 if (id
.equals(P_ID_ENDMONTH
)) {
187 timePeriod
.setEndMonth(castToInteger(value
));
190 if (id
.equals(P_ID_ENDDAY
)) {
191 timePeriod
.setEndDay(castToInteger(value
));
194 propertyChangeSupport
.firePropertyChange(Resources
.PROPERTY_SHEET_CHANGE
, null, timePeriod
);
197 private Integer
castToInteger(Object value
) {
199 // Dropdown lists return an Integer index
200 if (value
instanceof Integer
) {
202 // First entry in dropdown is empty
203 if (((Integer
) value
).equals(0)) {
206 return (Integer
) value
;
210 Integer integer
= null;
212 integer
= new Integer((String
) value
);
214 } catch (ClassCastException e
) {
215 } catch (NumberFormatException e
) {
221 * Method declared on IPropertySource
223 public Object
getEditableValue() {
224 if (timePeriod
== null) {
227 return timePeriod
.getYear();
231 // * The value as displayed in the Property Sheet.
232 // * @return java.lang.String
234 // public String toString() {
235 // if (timePeriod == null) {
238 // return timePeriod.getYear();
242 private PropertyChangeSupport propertyChangeSupport
= new PropertyChangeSupport(this);
244 public void addPropertyChangeListener(
245 PropertyChangeListener listener
) {
246 propertyChangeSupport
.addPropertyChangeListener(listener
);