fce92a9ebea0a7143c8a28ab54919084e0538ebf
[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.text.DateFormat;
15 import java.text.SimpleDateFormat;
16 import java.util.Vector;
17
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;
24
25 import eu.etaxonomy.cdm.model.common.TimePeriod;
26 import eu.etaxonomy.taxeditor.store.model.Resources;
27
28 /**
29 * @author p.ciardelli
30 * @created 10.11.2008
31 * @version 1.0
32 */
33 public class TimePeriodPropertySource implements IPropertySource {
34 private static final Logger logger = Logger
35 .getLogger(TimePeriodPropertySource.class);
36
37 private TimePeriod timePeriod;
38
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";
46
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)";
54
55 protected static final String[] TOP_LEVEL_PROPERTIES = new String[] {
56 P_ID_STARTYEAR,
57 P_ID_STARTMONTH,
58 P_ID_STARTDAY,
59 P_ID_ENDYEAR,
60 P_ID_ENDMONTH,
61 P_ID_ENDDAY};
62
63 private static final String[] P_MONTH_MENU = new String[] {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
64
65 public TimePeriodPropertySource(TimePeriod timePeriod) {
66 super();
67
68 if (timePeriod == null) {
69 timePeriod = TimePeriod.NewInstance();
70 }
71 this.timePeriod = timePeriod;
72
73 // Add property sheet descriptors
74 for (String key : TOP_LEVEL_PROPERTIES) {
75 addDescriptor(key);
76 }
77 }
78
79 //static date formatter
80 private static final DateFormat formatter = new SimpleDateFormat(
81 "EEEE, MMMM d, yyyy"); //$NON-NLS-1$
82
83 private Vector<PropertyDescriptor> descriptors = new Vector<PropertyDescriptor>();
84
85 private void addDescriptor(String id) {
86
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);
91 }
92
93 if (id.equals(P_ID_STARTMONTH)) {
94 descriptors.addElement(new ComboBoxPropertyDescriptor(
95 P_ID_STARTMONTH, P_STARTMONTH, P_MONTH_MENU));
96 }
97
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);
102 }
103
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);
108 }
109
110 if (id.equals(P_ID_ENDMONTH)) {
111 descriptors.addElement(new ComboBoxPropertyDescriptor(
112 P_ID_ENDMONTH, P_ENDMONTH, P_MONTH_MENU));
113 }
114
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);
119 }
120 }
121
122 /* (non-Javadoc)
123 * Method declared on IPropertySource
124 */
125 public IPropertyDescriptor[] getPropertyDescriptors() {
126 return (IPropertyDescriptor[]) descriptors.toArray(
127 new IPropertyDescriptor[descriptors.size()]);
128 }
129
130 public Object getPropertyValue(Object id) {
131
132 if (id.equals(P_ID_STARTYEAR)) {
133 Integer startYear = timePeriod.getStartYear();
134 return (startYear == null) ? "" : String.valueOf(startYear);
135 }
136
137 if (id.equals(P_ID_STARTMONTH)) {
138 Integer startMonth = timePeriod.getStartMonth();
139 return (startMonth == null) ? 0 : startMonth;
140 }
141
142 if (id.equals(P_ID_STARTDAY)) {
143 Integer startDay = timePeriod.getStartDay();
144 return (startDay == null) ? "" : String.valueOf(startDay);
145 }
146
147 if (id.equals(P_ID_ENDYEAR)) {
148 Integer endYear = timePeriod.getEndYear();
149 return (endYear == null) ? "" : String.valueOf(endYear);
150 }
151
152 if (id.equals(P_ID_ENDMONTH)) {
153 Integer endMonth = timePeriod.getEndMonth();
154 return (endMonth == null) ? 0 : endMonth;
155 }
156
157 if (id.equals(P_ID_ENDDAY)) {
158 Integer endDay = timePeriod.getEndDay();
159 return (endDay == null) ? "" : String.valueOf(endDay);
160 }
161
162 return "";
163 }
164
165 /* (non-Javadoc)
166 * Method declared on IPropertySource
167 */
168 public boolean isPropertySet(Object property) {
169 return false;
170 }
171
172 /* (non-Javadoc)
173 * Method declared on IPropertySource
174 */
175 public void resetPropertyValue(Object property) {}
176
177 public void setPropertyValue(Object id, Object value) {
178
179 if (id.equals(P_ID_STARTYEAR)) {
180 timePeriod.setStartYear(castToInteger(value));
181 }
182
183 if (id.equals(P_ID_STARTMONTH)) {
184 timePeriod.setStartMonth(castToInteger(value));
185 }
186
187 if (id.equals(P_ID_STARTDAY)) {
188 timePeriod.setStartDay(castToInteger(value));
189 }
190
191 if (id.equals(P_ID_ENDYEAR)) {
192 timePeriod.setEndYear(castToInteger(value));
193 }
194
195 if (id.equals(P_ID_ENDMONTH)) {
196 timePeriod.setEndMonth(castToInteger(value));
197 }
198
199 if (id.equals(P_ID_ENDDAY)) {
200 timePeriod.setEndDay(castToInteger(value));
201 }
202
203 propertyChangeSupport.firePropertyChange(Resources.PROPERTY_SHEET_CHANGE, null, timePeriod);
204 }
205
206 private Integer castToInteger(Object value) {
207
208 // Dropdown lists return an Integer index
209 if (value instanceof Integer) {
210
211 // First entry in dropdown is empty
212 if (((Integer) value).equals(0)) {
213 return null;
214 } else {
215 return (Integer) value;
216 }
217 }
218
219 Integer integer = null;
220 try {
221 integer = new Integer((String) value);
222
223 } catch (ClassCastException e) {
224 } catch (NumberFormatException e) {
225 }
226 return integer;
227 }
228
229 /* (non-Javadoc)
230 * Method declared on IPropertySource
231 */
232 public Object getEditableValue() {
233 if (timePeriod == null) {
234 return "";
235 }
236 return timePeriod.getYear();
237 }
238
239 // /**
240 // * The value as displayed in the Property Sheet.
241 // * @return java.lang.String
242 // */
243 // public String toString() {
244 // if (timePeriod == null) {
245 // return "";
246 // }
247 // return timePeriod.getYear();
248 //
249 // }
250
251 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
252
253 public void addPropertyChangeListener(
254 PropertyChangeListener listener) {
255 propertyChangeSupport.addPropertyChangeListener(listener);
256 }
257 }