.
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / propertysheet / TimePeriodPropertySource.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.taxeditor.propertysheet;
11
12 import java.beans.PropertyChangeListener;
13 import java.beans.PropertyChangeSupport;
14 import java.util.Vector;
15
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;
21
22 import eu.etaxonomy.cdm.model.common.TimePeriod;
23 import eu.etaxonomy.taxeditor.model.Resources;
24
25 /**
26 * @author p.ciardelli
27 * @created 10.11.2008
28 * @version 1.0
29 */
30 public class TimePeriodPropertySource implements ICdmBasePropertySource {
31
32 private TimePeriod timePeriod;
33
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";
41
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)";
49
50 protected static final String[] TOP_LEVEL_PROPERTIES = new String[] {
51 P_ID_STARTYEAR,
52 P_ID_STARTMONTH,
53 P_ID_STARTDAY,
54 P_ID_ENDYEAR,
55 P_ID_ENDMONTH,
56 P_ID_ENDDAY};
57
58 private static final String[] P_MONTH_MENU = new String[] {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
59
60 public TimePeriodPropertySource(TimePeriod timePeriod) {
61 super();
62
63 if (timePeriod == null) {
64 timePeriod = TimePeriod.NewInstance();
65 }
66 this.timePeriod = timePeriod;
67
68 // Add property sheet descriptors
69 for (String key : TOP_LEVEL_PROPERTIES) {
70 addDescriptor(key);
71 }
72 }
73
74 private Vector<PropertyDescriptor> descriptors = new Vector<PropertyDescriptor>();
75
76 private void addDescriptor(String id) {
77
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);
82 }
83
84 if (id.equals(P_ID_STARTMONTH)) {
85 descriptors.addElement(new ComboBoxPropertyDescriptor(
86 P_ID_STARTMONTH, P_STARTMONTH, P_MONTH_MENU));
87 }
88
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);
93 }
94
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);
99 }
100
101 if (id.equals(P_ID_ENDMONTH)) {
102 descriptors.addElement(new ComboBoxPropertyDescriptor(
103 P_ID_ENDMONTH, P_ENDMONTH, P_MONTH_MENU));
104 }
105
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);
110 }
111 }
112
113 /* (non-Javadoc)
114 * Method declared on IPropertySource
115 */
116 public IPropertyDescriptor[] getPropertyDescriptors() {
117 return (IPropertyDescriptor[]) descriptors.toArray(
118 new IPropertyDescriptor[descriptors.size()]);
119 }
120
121 public Object getPropertyValue(Object id) {
122
123 if (id.equals(P_ID_STARTYEAR)) {
124 Integer startYear = timePeriod.getStartYear();
125 return (startYear == null) ? "" : String.valueOf(startYear);
126 }
127
128 if (id.equals(P_ID_STARTMONTH)) {
129 Integer startMonth = timePeriod.getStartMonth();
130 return (startMonth == null) ? 0 : startMonth;
131 }
132
133 if (id.equals(P_ID_STARTDAY)) {
134 Integer startDay = timePeriod.getStartDay();
135 return (startDay == null) ? "" : String.valueOf(startDay);
136 }
137
138 if (id.equals(P_ID_ENDYEAR)) {
139 Integer endYear = timePeriod.getEndYear();
140 return (endYear == null) ? "" : String.valueOf(endYear);
141 }
142
143 if (id.equals(P_ID_ENDMONTH)) {
144 Integer endMonth = timePeriod.getEndMonth();
145 return (endMonth == null) ? 0 : endMonth;
146 }
147
148 if (id.equals(P_ID_ENDDAY)) {
149 Integer endDay = timePeriod.getEndDay();
150 return (endDay == null) ? "" : String.valueOf(endDay);
151 }
152
153 return "";
154 }
155
156 /* (non-Javadoc)
157 * Method declared on IPropertySource
158 */
159 public boolean isPropertySet(Object property) {
160 return false;
161 }
162
163 /* (non-Javadoc)
164 * Method declared on IPropertySource
165 */
166 public void resetPropertyValue(Object property) {}
167
168 public void setPropertyValue(Object id, Object value) {
169
170 if (id.equals(P_ID_STARTYEAR)) {
171 timePeriod.setStartYear(castToInteger(value));
172 }
173
174 if (id.equals(P_ID_STARTMONTH)) {
175 timePeriod.setStartMonth(castToInteger(value));
176 }
177
178 if (id.equals(P_ID_STARTDAY)) {
179 timePeriod.setStartDay(castToInteger(value));
180 }
181
182 if (id.equals(P_ID_ENDYEAR)) {
183 timePeriod.setEndYear(castToInteger(value));
184 }
185
186 if (id.equals(P_ID_ENDMONTH)) {
187 timePeriod.setEndMonth(castToInteger(value));
188 }
189
190 if (id.equals(P_ID_ENDDAY)) {
191 timePeriod.setEndDay(castToInteger(value));
192 }
193
194 propertyChangeSupport.firePropertyChange(Resources.PROPERTY_SHEET_CHANGE, null, timePeriod);
195 }
196
197 private Integer castToInteger(Object value) {
198
199 // Dropdown lists return an Integer index
200 if (value instanceof Integer) {
201
202 // First entry in dropdown is empty
203 if (((Integer) value).equals(0)) {
204 return null;
205 } else {
206 return (Integer) value;
207 }
208 }
209
210 Integer integer = null;
211 try {
212 integer = new Integer((String) value);
213
214 } catch (ClassCastException e) {
215 } catch (NumberFormatException e) {
216 }
217 return integer;
218 }
219
220 /* (non-Javadoc)
221 * Method declared on IPropertySource
222 */
223 public Object getEditableValue() {
224 if (timePeriod == null) {
225 return "";
226 }
227 return timePeriod.getYear();
228 }
229
230 // /**
231 // * The value as displayed in the Property Sheet.
232 // * @return java.lang.String
233 // */
234 // public String toString() {
235 // if (timePeriod == null) {
236 // return "";
237 // }
238 // return timePeriod.getYear();
239 //
240 // }
241
242 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
243
244 public void addPropertyChangeListener(
245 PropertyChangeListener listener) {
246 propertyChangeSupport.addPropertyChangeListener(listener);
247 }
248 }