001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.examples.propertysheet;
011:
012: import java.text.DateFormat;
013: import java.text.SimpleDateFormat;
014: import java.util.Date;
015: import java.util.GregorianCalendar;
016: import java.util.Vector;
017:
018: import org.eclipse.jface.viewers.LabelProvider;
019: import org.eclipse.ui.views.properties.ComboBoxPropertyDescriptor;
020: import org.eclipse.ui.views.properties.IPropertyDescriptor;
021: import org.eclipse.ui.views.properties.IPropertySource;
022: import org.eclipse.ui.views.properties.TextPropertyDescriptor;
023:
024: /**
025: * Example IPropertySource who itself is NOT editable, but whose children are.
026: * The values of the children determine the value of the birthday.
027: */
028: public class Birthday implements IPropertySource {
029:
030: //Properties
031: private Integer day, month, year;
032:
033: //Property unique keys
034: public static final String P_ID_DAY = "Birthday.day"; //$NON-NLS-1$
035:
036: public static final String P_ID_MONTH = "Birthday.month"; //$NON-NLS-1$
037:
038: public static final String P_ID_YEAR = "Birthday.year"; //$NON-NLS-1$
039:
040: //Property display keys
041: public static final String P_DAY = MessageUtil.getString("day"); //$NON-NLS-1$
042:
043: public static final String P_MONTH = MessageUtil.getString("month"); //$NON-NLS-1$
044:
045: public static final String P_YEAR = MessageUtil.getString("year"); //$NON-NLS-1$
046:
047: //default values
048: private static final Integer DAY_DEFAULT = new Integer(1);
049:
050: private static final Integer MONTH_DEFAULT = new Integer(1);
051:
052: private static final Integer YEAR_DEFAULT = new Integer(2000);
053:
054: //static date formater
055: private static final DateFormat formatter = new SimpleDateFormat(
056: "EEEE, MMMM d, yyyy"); //$NON-NLS-1$
057:
058: static private class DayLabelProvider extends LabelProvider {
059: public String getText(Object element) {
060: String[] dayValues = new String[] {
061: "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }; //$NON-NLS-31$ //$NON-NLS-30$ //$NON-NLS-29$ //$NON-NLS-28$ //$NON-NLS-27$ //$NON-NLS-26$ //$NON-NLS-25$ //$NON-NLS-24$ //$NON-NLS-23$ //$NON-NLS-22$ //$NON-NLS-21$ //$NON-NLS-20$ //$NON-NLS-19$ //$NON-NLS-18$ //$NON-NLS-17$ //$NON-NLS-16$ //$NON-NLS-15$ //$NON-NLS-14$ //$NON-NLS-13$ //$NON-NLS-12$ //$NON-NLS-11$ //$NON-NLS-10$ //$NON-NLS-9$ //$NON-NLS-8$ //$NON-NLS-7$ //$NON-NLS-6$ //$NON-NLS-5$ //$NON-NLS-4$ //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$
062: return dayValues[((Integer) element).intValue()];
063: }
064: }
065:
066: static private class MonthLabelProvider extends LabelProvider {
067: public String getText(Object element) {
068: String[] monthValues = new String[] {
069: MessageUtil.getString("January"), MessageUtil.getString("February"), MessageUtil.getString("March"), MessageUtil.getString("April"), MessageUtil.getString("May"), MessageUtil.getString("June"), MessageUtil.getString("July"), MessageUtil.getString("August"), MessageUtil.getString("September"), MessageUtil.getString("October"), MessageUtil.getString("November"), MessageUtil.getString("December") }; //$NON-NLS-12$ //$NON-NLS-11$ //$NON-NLS-10$ //$NON-NLS-9$ //$NON-NLS-8$ //$NON-NLS-7$ //$NON-NLS-6$ //$NON-NLS-5$ //$NON-NLS-4$ //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$
070: return monthValues[((Integer) element).intValue()];
071: }
072: }
073:
074: //
075: private static Vector descriptors;
076: static {
077: descriptors = new Vector();
078:
079: ///
080: String[] dayValues = new String[] {
081: "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }; //$NON-NLS-31$ //$NON-NLS-30$ //$NON-NLS-29$ //$NON-NLS-28$ //$NON-NLS-27$ //$NON-NLS-26$ //$NON-NLS-25$ //$NON-NLS-24$ //$NON-NLS-23$ //$NON-NLS-22$ //$NON-NLS-21$ //$NON-NLS-20$ //$NON-NLS-19$ //$NON-NLS-18$ //$NON-NLS-17$ //$NON-NLS-16$ //$NON-NLS-15$ //$NON-NLS-14$ //$NON-NLS-13$ //$NON-NLS-12$ //$NON-NLS-11$ //$NON-NLS-10$ //$NON-NLS-9$ //$NON-NLS-8$ //$NON-NLS-7$ //$NON-NLS-6$ //$NON-NLS-5$ //$NON-NLS-4$ //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$
082: ComboBoxPropertyDescriptor days = new ComboBoxPropertyDescriptor(
083: P_ID_DAY, P_DAY, dayValues);
084: days.setLabelProvider(new DayLabelProvider());
085: descriptors.addElement(days);
086:
087: ///
088: String[] monthValues = new String[] {
089: MessageUtil.getString("January"), MessageUtil.getString("February"), MessageUtil.getString("March"), MessageUtil.getString("April"), MessageUtil.getString("May"), MessageUtil.getString("June"), MessageUtil.getString("July"), MessageUtil.getString("August"), MessageUtil.getString("September"), MessageUtil.getString("October"), MessageUtil.getString("November"), MessageUtil.getString("December") }; //$NON-NLS-12$ //$NON-NLS-11$ //$NON-NLS-10$ //$NON-NLS-9$ //$NON-NLS-8$ //$NON-NLS-7$ //$NON-NLS-6$ //$NON-NLS-5$ //$NON-NLS-4$ //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$
090: ComboBoxPropertyDescriptor months = new ComboBoxPropertyDescriptor(
091: P_ID_MONTH, P_MONTH, monthValues);
092: months.setLabelProvider(new MonthLabelProvider());
093: descriptors.addElement(months);
094:
095: ///
096: descriptors.addElement(new TextPropertyDescriptor(P_ID_YEAR,
097: P_YEAR));
098: }
099:
100: /**
101: * Address Default Constructor
102: */
103: Birthday() {
104: super ();
105: }
106:
107: /**
108: * Convenience Address Constructor
109: */
110: public Birthday(int day, int month, int year) {
111: super ();
112: setDay(new Integer(day));
113: setMonth(new Integer(month));
114: setYear(new Integer(year));
115: }
116:
117: /**
118: * Returns the day
119: */
120: private Integer getDay() {
121: if (day == null)
122: day = DAY_DEFAULT;
123: return day;
124: }
125:
126: /**
127: * Standard Accessor
128: */
129: private static Vector getDescriptors() {
130: return descriptors;
131: }
132:
133: /* (non-Javadoc)
134: * Method declared on IPropertySource
135: */
136: public Object getEditableValue() {
137: return this .toString();
138: }
139:
140: /**
141: * Returns the month
142: */
143: private Integer getMonth() {
144: if (month == null)
145: month = MONTH_DEFAULT;
146: return month;
147: }
148:
149: /* (non-Javadoc)
150: * Method declared on IPropertySource
151: */
152: public IPropertyDescriptor[] getPropertyDescriptors() {
153: return (IPropertyDescriptor[]) getDescriptors().toArray(
154: new IPropertyDescriptor[getDescriptors().size()]);
155: }
156:
157: /**
158: * The <code>Birthday</code> implementation of this
159: * <code>IPropertySource</code> method returns the following properties
160: *
161: * 1) P_DAY returns java.lang.Integer
162: * 2) P_MONTH returns java.lang.Integer
163: * 3) P_YEAR returns java.lang.Integer
164: * 4) P_STREET returns java.lang.String
165: */
166: public Object getPropertyValue(Object propKey) {
167: if (propKey.equals(P_ID_DAY))
168: return new Integer(getDay().intValue() - 1);
169: if (propKey.equals(P_ID_MONTH))
170: return new Integer(getMonth().intValue() - 1);
171: if (propKey.equals(P_ID_YEAR))
172: return getYear().toString();
173: return null;
174: }
175:
176: /**
177: * Returns the year
178: */
179: private Integer getYear() {
180: if (year == null)
181: year = YEAR_DEFAULT;
182: return year;
183: }
184:
185: /* (non-Javadoc)
186: * Method declared on IPropertySource
187: */
188: public boolean isPropertySet(Object property) {
189: if (P_ID_DAY.equals(property))
190: return getDay() != DAY_DEFAULT;
191: if (P_ID_MONTH.equals(property))
192: return getMonth() != MONTH_DEFAULT;
193: if (P_ID_YEAR.equals(property))
194: return getYear() != YEAR_DEFAULT;
195: return false;
196: }
197:
198: /* (non-Javadoc)
199: * Method declared on IPropertySource
200: */
201: public void resetPropertyValue(Object property) {
202: if (P_ID_DAY.equals(property)) {
203: setDay(DAY_DEFAULT);
204: return;
205: }
206: if (P_ID_MONTH.equals(property)) {
207: setMonth(MONTH_DEFAULT);
208: return;
209: }
210: if (P_ID_YEAR.equals(property)) {
211: setYear(YEAR_DEFAULT);
212: return;
213: }
214: }
215:
216: /**
217: * Sets the day
218: */
219: private void setDay(Integer newDay) {
220: day = newDay;
221: }
222:
223: /**
224: * Sets the month
225: */
226: private void setMonth(Integer newMonth) {
227: month = newMonth;
228: }
229:
230: /**
231: * The <code>Birthday</code> implementation of this
232: * <code>IPropertySource</code> method
233: * defines the following Setable properties
234: *
235: * 1) P_DAY expects java.lang.Integer
236: * 2) P_MONTH expects java.lang.Integer
237: * 3) P_YEAR expects java.lang.Integer
238: */
239: public void setPropertyValue(Object name, Object value) {
240: if (P_ID_DAY.equals(name)) {
241: setDay(new Integer(((Integer) value).intValue() + 1));
242: return;
243: }
244: if (P_ID_MONTH.equals(name)) {
245: setMonth(new Integer(((Integer) value).intValue() + 1));
246: return;
247: }
248: if (P_ID_YEAR.equals(name)) {
249: try {
250: setYear(new Integer((String) value));
251: } catch (NumberFormatException e) {
252: setYear(YEAR_DEFAULT);
253: }
254: return;
255: }
256: }
257:
258: /**
259: * Sets the year
260: */
261: private void setYear(Integer newYear) {
262: year = newYear;
263: }
264:
265: /**
266: * The value as displayed in the Property Sheet.
267: * @return java.lang.String
268: */
269: public String toString() {
270: Date bday = (new GregorianCalendar(getYear().intValue(),
271: getMonth().intValue() - 1, getDay().intValue()))
272: .getTime();
273: return formatter.format(bday);
274: }
275: }
|