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.ui.views.properties.IPropertyDescriptor;
015: import org.eclipse.ui.views.properties.IPropertySource;
016: import org.eclipse.ui.views.properties.TextPropertyDescriptor;
017:
018: /**
019: * PropertySource containing street information pertenant to Address
020: */
021: public class StreetAddress implements IPropertySource {
022:
023: //properties
024: private Integer buildNo;
025:
026: private String aptBox;
027:
028: private String streetName;
029:
030: //default property values
031: private static final Integer BUILD_NO_DEFAULT = new Integer(0);
032:
033: private static final String APTBOX_DEFAULT = MessageUtil
034: .getString("unspecified"); //$NON-NLS-1$
035:
036: private static final String STREETNAME_DEFAULT = MessageUtil
037: .getString("unspecified"); //$NON-NLS-1$
038:
039: //property unique keys
040: public static final String P_ID_BUILD_NO = "Street.buildingNo"; //$NON-NLS-1$
041:
042: public static final String P_ID_APTBOX = "Street.aptNo"; //$NON-NLS-1$
043:
044: public static final String P_ID_STREET = "Street.street"; //$NON-NLS-1$
045:
046: //property display keys
047: public static final String P_BUILD_NO = MessageUtil
048: .getString("building_number"); //$NON-NLS-1$
049:
050: public static final String P_APTBOX = MessageUtil
051: .getString("apt.no_or_box.no"); //$NON-NLS-1$
052:
053: public static final String P_STREET = MessageUtil
054: .getString("street"); //$NON-NLS-1$
055:
056: //
057: private static Vector descriptors;
058:
059: static {
060: descriptors = new Vector();
061: descriptors.addElement(new TextPropertyDescriptor(
062: P_ID_BUILD_NO, P_BUILD_NO));
063: descriptors.addElement(new TextPropertyDescriptor(P_ID_APTBOX,
064: P_APTBOX));
065: descriptors.addElement(new TextPropertyDescriptor(P_ID_STREET,
066: P_STREET));
067: }
068:
069: /**
070: * Street Default Constructor.
071: */
072: public StreetAddress() {
073: super ();
074: }
075:
076: /**
077: * Convenience Street constructor. AptBox set to default
078: */
079: public StreetAddress(int buildNo, String streetName) {
080: super ();
081: setBuildNo(new Integer(buildNo));
082: setStreetName(streetName);
083: }
084:
085: /**
086: * Convenience Street constructor.
087: */
088: public StreetAddress(int buildNo, String aptBox, String streetName) {
089: super ();
090: setBuildNo(new Integer(buildNo));
091: setAptBox(aptBox);
092: setStreetName(streetName);
093: }
094:
095: /* (non-Javadoc)
096: * Method declared on Object
097: */
098: public boolean equals(Object ob) {
099: return toString().equals(ob.toString());
100: }
101:
102: /**
103: * the appartment number
104: */
105: private String getAptBox() {
106: if (aptBox == null)
107: aptBox = APTBOX_DEFAULT;
108: return aptBox;
109: }
110:
111: /**
112: * Returns the building number
113: */
114: private Integer getBuildNo() {
115: if (buildNo == null)
116: buildNo = BUILD_NO_DEFAULT;
117: return buildNo;
118: }
119:
120: /**
121: * Returns the descriptors
122: */
123: private static Vector getDescriptors() {
124: return descriptors;
125: }
126:
127: /* (non-Javadoc)
128: * Method declared on IPropertySource
129: */
130: public Object getEditableValue() {
131: return this .toString();
132: }
133:
134: /* (non-Javadoc)
135: * Method declared on IPropertySource
136: */
137: public IPropertyDescriptor[] getPropertyDescriptors() {
138: return (IPropertyDescriptor[]) getDescriptors().toArray(
139: new IPropertyDescriptor[getDescriptors().size()]);
140: }
141:
142: /**
143: * The <code>Name</code> implementation of this
144: * <code>IPropertySource</code> method returns the following properties
145: *
146: * 1) P_BUILD_NO returns java.lang.Integer
147: * 2) P_APTBOX returns java.lang.String
148: * 3) P_STREET returns java.lang.String
149: */
150: public Object getPropertyValue(Object propKey) {
151: if (propKey.equals(P_ID_BUILD_NO))
152: return getBuildNo().toString();
153: if (propKey.equals(P_ID_APTBOX))
154: return getAptBox();
155: if (propKey.equals(P_ID_STREET))
156: return getStreetName();
157: return null;
158: }
159:
160: /**
161: * Returns the street name
162: */
163: private String getStreetName() {
164: if (streetName == null)
165: streetName = STREETNAME_DEFAULT;
166: return streetName;
167: }
168:
169: /* (non-Javadoc)
170: * Method declared on Object
171: */
172: public int hashCode() {
173: return toString().hashCode();
174: }
175:
176: /* (non-Javadoc)
177: * Method declared on IPropertySource
178: */
179: public boolean isPropertySet(Object property) {
180: if (property.equals(P_ID_BUILD_NO))
181: return getBuildNo() != BUILD_NO_DEFAULT;
182: if (property.equals(P_ID_APTBOX))
183: return getAptBox() != APTBOX_DEFAULT;
184: if (property.equals(P_ID_STREET))
185: return getStreetName() != STREETNAME_DEFAULT;
186: return false;
187: }
188:
189: /* (non-Javadoc)
190: * Method declared on IPropertySource
191: */
192: public void resetPropertyValue(Object property) {
193: if (property.equals(P_ID_BUILD_NO)) {
194: setBuildNo(BUILD_NO_DEFAULT);
195: return;
196: }
197: if (property.equals(P_ID_APTBOX)) {
198: setAptBox(APTBOX_DEFAULT);
199: return;
200: }
201: if (property.equals(P_ID_STREET)) {
202: setStreetName(STREETNAME_DEFAULT);
203: return;
204: }
205: }
206:
207: /**
208: * Sets the appartment number
209: */
210: private void setAptBox(String newAptBox) {
211: aptBox = newAptBox;
212: }
213:
214: /**
215: * Sets the building number
216: */
217: private void setBuildNo(Integer newBuildNo) {
218: buildNo = newBuildNo;
219: }
220:
221: /**
222: * The <code>Name</code> implementation of this
223: * <code>IPropertySource</code> method
224: * defines the following Setable properties
225: *
226: * 1) P_BUILD_NO expects java.lang.Integer
227: * 2) P_APTBOX expects java.lang.String
228: * 3) P_STREET expects java.lang.String
229: */
230: public void setPropertyValue(Object name, Object value) {
231: if (name.equals(P_ID_BUILD_NO)) {
232: try {
233: setBuildNo(new Integer(Integer.parseInt((String) value)));
234: } catch (NumberFormatException e) {
235: setBuildNo(BUILD_NO_DEFAULT);
236: }
237: return;
238: }
239: if (name.equals(P_ID_APTBOX)) {
240: setAptBox((String) value);
241: return;
242: }
243: if (name.equals(P_ID_STREET)) {
244: setStreetName((String) value);
245: return;
246: }
247: }
248:
249: /**
250: * Sets the street name
251: */
252: private void setStreetName(String newStreetName) {
253: streetName = newStreetName;
254: }
255:
256: /**
257: * The value as displayed in the Property Sheet. Will not print default values
258: * @return java.lang.String
259: */
260: public String toString() {
261: StringBuffer outStringBuffer = new StringBuffer();
262: if (!getAptBox().equals(APTBOX_DEFAULT)) {
263: outStringBuffer.append(getAptBox());
264: outStringBuffer.append(", "); //$NON-NLS-1$
265: }
266: if (!getBuildNo().equals(BUILD_NO_DEFAULT)) {
267: outStringBuffer.append(getBuildNo());
268: outStringBuffer.append(" "); //$NON-NLS-1$
269: }
270: if (!getStreetName().equals(STREETNAME_DEFAULT)) {
271: outStringBuffer.append(getStreetName());
272: }
273: return outStringBuffer.toString();
274: }
275: }
|