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.util.Vector;
013:
014: import org.eclipse.jface.viewers.ICellEditorValidator;
015: import org.eclipse.jface.viewers.LabelProvider;
016: import org.eclipse.ui.views.properties.ComboBoxPropertyDescriptor;
017: import org.eclipse.ui.views.properties.IPropertyDescriptor;
018: import org.eclipse.ui.views.properties.IPropertySource;
019: import org.eclipse.ui.views.properties.PropertyDescriptor;
020: import org.eclipse.ui.views.properties.TextPropertyDescriptor;
021:
022: /**
023: * Example IPropertySource who itself is NOT editable, but whose children are.
024: * The values of the children determine the value of the address.
025: */
026: public class Address implements IPropertySource {
027:
028: //properties
029: private String city;
030:
031: private Integer province;
032:
033: private String postalCode;
034:
035: private StreetAddress street;
036:
037: //Property unique keys
038: public static final String P_ID_STREET = "Address.Street"; //$NON-NLS-1$
039:
040: public static final String P_ID_CITY = "Address.City"; //$NON-NLS-1$
041:
042: public static final String P_ID_PROVINCE = "Address.Province"; //$NON-NLS-1$
043:
044: public static final String P_ID_POSTALCODE = "Address.PostalCode"; //$NON-NLS-1$
045:
046: //Property display keys
047: public static final String P_STREET = MessageUtil
048: .getString("Street"); //$NON-NLS-1$
049:
050: public static final String P_CITY = MessageUtil.getString("City"); //$NON-NLS-1$
051:
052: public static final String P_PROVINCE = MessageUtil
053: .getString("Province"); //$NON-NLS-1$
054:
055: public static final String P_POSTALCODE = MessageUtil
056: .getString("PostalCode"); //$NON-NLS-1$
057:
058: public static final String P_DESCRIPTORS = "properties"; //$NON-NLS-1$
059:
060: //default values
061: private static final StreetAddress STREET_DEFAULT = new StreetAddress();
062:
063: private static final String CITY_DEFAULT = MessageUtil
064: .getString("unspecified_city"); //$NON-NLS-1$
065:
066: private static final Integer PROVINCE_DEFAULT = new Integer(0);
067:
068: private static final String POSTALCODE_DEFAULT = "A1B2C3"; //$NON-NLS-1$
069:
070: //
071: static private class ProvinceLabelProvider extends LabelProvider {
072: public String getText(Object element) {
073: String[] provinceValues = new String[] {
074: MessageUtil.getString("British_Columbia"), MessageUtil.getString("Alberta"), MessageUtil.getString("Saskatchewan"), MessageUtil.getString("Manitoba"), MessageUtil.getString("Ontario"), MessageUtil.getString("Quebec"), MessageUtil.getString("Newfoundland"), MessageUtil.getString("Prince_Edward_Island"), MessageUtil.getString("Nova_Scotia"), MessageUtil.getString("New_Brunswick"), MessageUtil.getString("Yukon"), MessageUtil.getString("North_West_Territories"), MessageUtil.getString("Nunavut") }; //$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$
075: return provinceValues[((Integer) element).intValue()];
076: }
077: }
078:
079: //
080: private static Vector descriptors;
081:
082: private static String[] provinceValues;
083: static {
084: descriptors = new Vector();
085: provinceValues = new String[] {
086: MessageUtil.getString("British_Columbia"), MessageUtil.getString("Alberta"), MessageUtil.getString("Saskatchewan"), MessageUtil.getString("Manitoba"), MessageUtil.getString("Ontario"), MessageUtil.getString("Quebec"), MessageUtil.getString("Newfoundland"), MessageUtil.getString("Prince_Edward_Island"), MessageUtil.getString("Nova_Scotia"), MessageUtil.getString("New_Brunswick"), MessageUtil.getString("Yukon"), MessageUtil.getString("North_West_Territories"), MessageUtil.getString("Nunavut") }; //$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$
087: descriptors.addElement(new PropertyDescriptor(P_ID_STREET,
088: P_STREET));
089: descriptors.addElement(new TextPropertyDescriptor(P_ID_CITY,
090: P_CITY));
091:
092: //PostalCode
093: PropertyDescriptor propertyDescriptor = new TextPropertyDescriptor(
094: P_ID_POSTALCODE, P_POSTALCODE);
095: //add custom validator to propertyDescriptor limiting postalcode
096: //values to XYXYXY, where X is a letter and Y is a digit
097: propertyDescriptor.setValidator(new ICellEditorValidator() {
098: public String isValid(Object value) {
099: if (value == null)
100: return MessageUtil
101: .getString("postal_code_is_incomplete"); //$NON-NLS-1$
102:
103: //
104: String testPostalCode = ((String) value).toUpperCase();
105: final int length = testPostalCode.length();
106: final char space = ' ';
107:
108: //removes white space
109: StringBuffer postalCodeBuffer = new StringBuffer(6);
110: char current;
111: for (int i = 0; i < length; i++) {
112: current = testPostalCode.charAt(i);
113: if (current != space)
114: postalCodeBuffer.append(current);
115: }
116: testPostalCode = postalCodeBuffer.toString();
117:
118: //check for proper length
119: if (testPostalCode.length() != 6) {
120: return MessageUtil
121: .getString("postal_code_is_incomplete"); //$NON-NLS-1$
122: }
123:
124: //check for proper format
125: if (testPostalCode.charAt(1) < '0'
126: || testPostalCode.charAt(1) > '9'
127: || testPostalCode.charAt(3) < '0'
128: || testPostalCode.charAt(3) > '9'
129: || testPostalCode.charAt(5) < '0'
130: || testPostalCode.charAt(5) > '9'
131: || testPostalCode.charAt(0) < 'A'
132: || testPostalCode.charAt(0) > 'Z'
133: || testPostalCode.charAt(2) < 'A'
134: || testPostalCode.charAt(2) > 'Z'
135: || testPostalCode.charAt(4) < 'A'
136: || testPostalCode.charAt(4) > 'Z') {
137: //fail
138: return MessageUtil
139: .format(
140: "_is_an_invalid_format_for_a_postal_code", new Object[] { testPostalCode }); //$NON-NLS-1$
141: }
142:
143: //all pass
144: return null;
145: }
146: });
147: descriptors.addElement(propertyDescriptor);
148:
149: //
150: ComboBoxPropertyDescriptor desc = new ComboBoxPropertyDescriptor(
151: P_ID_PROVINCE, P_PROVINCE, provinceValues);
152: desc.setLabelProvider(new ProvinceLabelProvider());
153: descriptors.addElement(desc);
154: }
155:
156: /**
157: * Address Default Constructor
158: */
159: Address() {
160: super ();
161: }
162:
163: /**
164: * Creates a new address.
165: *
166: * @param street the street
167: * @param city the city
168: * @param province the province
169: * @param postalCode has the form XYXYXY: where X is a letter and Y is a digit
170: * @exception IllegalArgumentException, if postalcode not in above form
171: */
172: public Address(StreetAddress street, String city, Integer province,
173: String postalCode) {
174: super ();
175: setStreet(street);
176: setCity(city);
177: setPostalCode(postalCode);
178: setProvince(province);
179: }
180:
181: /**
182: * Returns the city
183: */
184: private String getCity() {
185: if (city == null)
186: city = CITY_DEFAULT;
187: return city;
188: }
189:
190: /*
191: * Standard Accessor
192: */
193: private static Vector getDescriptors() {
194: return descriptors;
195: }
196:
197: /* (non-Javadoc)
198: * Method declared on IPropertySource
199: */
200: public Object getEditableValue() {
201: return this .toString();
202: }
203:
204: /**
205: * Returns the postal code
206: */
207: private String getPostalCode() {
208: if (postalCode == null)
209: postalCode = POSTALCODE_DEFAULT;
210: return postalCode;
211: }
212:
213: /* (non-Javadoc)
214: * Method declared on IPropertySource
215: */
216: public IPropertyDescriptor[] getPropertyDescriptors() {
217: return (IPropertyDescriptor[]) getDescriptors().toArray(
218: new IPropertyDescriptor[getDescriptors().size()]);
219: }
220:
221: /**
222: * The <code>Address</code> implementation of this
223: * <code>IPropertySource</code> method returns the following properties
224: *
225: * 1) P_CITY returns java.lang.String
226: * 2) P_POSTALCODE returns java.lang.String
227: * 3) P_PROVINCE returns java.lang.String
228: * 4) P_STREET returns StreetAddress
229: */
230: public Object getPropertyValue(Object propKey) {
231: if (propKey.equals(P_ID_PROVINCE))
232: return getProvince();
233: if (propKey.equals(P_ID_STREET))
234: return getStreet();
235: if (propKey.equals(P_ID_CITY))
236: return getCity();
237: if (propKey.equals(P_ID_POSTALCODE))
238: return getPostalCode();
239: return null;
240: }
241:
242: /**
243: * Returns the province
244: */
245: private Integer getProvince() {
246: if (province == null)
247: province = PROVINCE_DEFAULT;
248: return province;
249: }
250:
251: /**
252: * Returns the street
253: */
254: public StreetAddress getStreet() {
255: if (street == null)
256: street = new StreetAddress();
257: return street;
258: }
259:
260: /* (non-Javadoc)
261: * Method declared on IPropertySource
262: */
263: public boolean isPropertySet(Object property) {
264: if (property.equals(P_ID_PROVINCE))
265: return getProvince() != PROVINCE_DEFAULT;
266: if (property.equals(P_ID_STREET))
267: return !STREET_DEFAULT.equals(getStreet());
268: if (property.equals(P_ID_CITY))
269: return getCity() != CITY_DEFAULT;
270: if (property.equals(P_ID_POSTALCODE))
271: return getPostalCode() != POSTALCODE_DEFAULT;
272: return false;
273: }
274:
275: /* (non-Javadoc)
276: * Method declared on IPropertySource
277: */
278: public void resetPropertyValue(Object property) {
279: if (P_ID_POSTALCODE.equals(property)) {
280: setPostalCode(POSTALCODE_DEFAULT);
281: return;
282: }
283: if (P_ID_CITY.equals(property)) {
284: setCity(CITY_DEFAULT);
285: return;
286: }
287: if (P_ID_PROVINCE.equals(property)) {
288: setProvince(PROVINCE_DEFAULT);
289: return;
290: }
291: if (P_ID_STREET.equals(property)) {
292: setStreet(new StreetAddress());
293: return;
294: }
295: }
296:
297: /**
298: * Sets the city
299: */
300: private void setCity(String newCity) {
301: city = newCity;
302: }
303:
304: /**
305: * Sets the postal code
306: */
307: private void setPostalCode(String newPostalCode) {
308: //validation in ICellEditorValidator registered in PropertyDescriptor
309: this .postalCode = newPostalCode.toUpperCase();
310: }
311:
312: /**
313: * The <code>Address</code> implementation of this
314: * <code>IPropertySource</code> method
315: * defines the following Setable properties
316: *
317: * 1) P_CITY expects java.lang.String
318: * 2) P_POSTALCODE expects java.lang.String
319: * 3) P_PROVINCE expects java.lang.String
320: *
321: * <p>P_ID_STREET is not set here since it is referenced
322: * and set directly in StreetAddress.
323: * According to IPropertySource, StreetAddress.getEditableValue
324: * should return a String which will be passed to this method
325: * as the value. A new StreetAddress object should then be
326: * created from the string.
327: * An alternative would be to return the StreetAddress
328: * directly in StreetAddress.getEditableValue and define a
329: * cell editor for the StreetAddress property.
330: * This was ommitted for the sake of simplicity.
331: */
332: public void setPropertyValue(Object name, Object value) {
333: if (P_ID_POSTALCODE.equals(name)) {
334: setPostalCode((String) value);
335: return;
336: }
337: if (P_ID_CITY.equals(name)) {
338: setCity((String) value);
339: return;
340: }
341: if (P_ID_PROVINCE.equals(name)) {
342: setProvince((Integer) value);
343: return;
344: }
345: }
346:
347: /**
348: * Sets the province
349: */
350: private void setProvince(Integer newProvince) {
351: province = newProvince;
352: }
353:
354: /**
355: * Sets the street
356: */
357: private void setStreet(StreetAddress newStreet) {
358: street = newStreet;
359: }
360:
361: /**
362: * The value as displayed in the Property Sheet.
363: * @return java.lang.String
364: */
365: public String toString() {
366: StringBuffer outStringBuffer = new StringBuffer();
367: final String comma_space = ", "; //$NON-NLS-1$
368: final String space = " "; //$NON-NLS-1$
369: if (!getStreet().equals(STREET_DEFAULT)) {
370: outStringBuffer.append(getStreet());
371: outStringBuffer.append(comma_space);
372: }
373:
374: outStringBuffer.append(getCity());
375: outStringBuffer.append(space);
376: outStringBuffer
377: .append(provinceValues[getProvince().intValue()]);
378: outStringBuffer.append(comma_space);
379: outStringBuffer.append(getPostalCode());
380:
381: return outStringBuffer.toString();
382: }
383: }
|