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: * Example IPropertySource whose value as well as its children are editable.
020: */
021: public class Name implements IPropertySource {
022: private String firstName = ""; //$NON-NLS-1$
023:
024: private String lastName = ""; //$NON-NLS-1$
025:
026: private String initial = ""; //$NON-NLS-1$
027:
028: // property unique keys
029: public static String P_ID_FIRSTNAME = "Name.FirstName"; //$NON-NLS-1$
030:
031: public static String P_ID_LASTNAME = "Name.LastName"; //$NON-NLS-1$
032:
033: public static String P_ID_MIDDLENAME = "Name.Middle"; //$NON-NLS-1$
034:
035: // property display keys
036: public static String P_FIRSTNAME = MessageUtil
037: .getString("FirstName"); //$NON-NLS-1$
038:
039: public static String P_LASTNAME = MessageUtil.getString("LastName"); //$NON-NLS-1$
040:
041: public static String P_MIDDLENAME = MessageUtil.getString("Middle"); //$NON-NLS-1$
042:
043: // default values
044: //
045: private static final String FIRSTNAME_DEFAULT = null;
046:
047: private static final String LASTNAME_DEFAULT = null;
048:
049: private static final String MIDDLENAME_DEFAULT = null;
050:
051: public static final String P_DESCRIPTORS = "properties"; //$NON-NLS-1$
052:
053: static private Vector descriptors;
054: static {
055: descriptors = new Vector();
056: descriptors.addElement(new TextPropertyDescriptor(
057: P_ID_FIRSTNAME, P_FIRSTNAME));
058: descriptors.addElement(new TextPropertyDescriptor(
059: P_ID_LASTNAME, P_LASTNAME));
060: descriptors.addElement(new TextPropertyDescriptor(
061: P_ID_MIDDLENAME, P_MIDDLENAME));
062: }
063:
064: /**
065: * Creates a new Name.
066: * @param name String in the form "firstname initial lastname"
067: */
068: public Name(String name) {
069: int index1, index2;
070: index1 = name.indexOf(' ');
071: if (index1 < 0)
072: index1 = name.length();
073: index2 = name.lastIndexOf(' ');
074: if (index2 > 0)
075: lastName = name.substring(index2 + 1);
076: firstName = name.substring(0, index1);
077: if (index1 < index2)
078: initial = name.substring(index1 + 1, index2);
079: }
080:
081: /**
082: * Returns the descriptors
083: */
084: private static Vector getDescriptors() {
085: return descriptors;
086: }
087:
088: /* (non-Javadoc)
089: * Method declared on IPropertySource
090: */
091: public Object getEditableValue() {
092: return this .toString();
093: }
094:
095: /**
096: * Returns the first name
097: */
098: private String getFirstName() {
099: return firstName;
100: }
101:
102: /**
103: * Returns the initial
104: */
105: private String getInitial() {
106: return initial;
107: }
108:
109: /**
110: * Returns the last name
111: */
112: private String getLastName() {
113: return lastName;
114: }
115:
116: /* (non-Javadoc)
117: * Method declared on IPropertySource
118: */
119: public IPropertyDescriptor[] getPropertyDescriptors() {
120: return (IPropertyDescriptor[]) getDescriptors().toArray(
121: new IPropertyDescriptor[getDescriptors().size()]);
122: }
123:
124: /**
125: * The <code>Name</code> implementation of this
126: * <code>IPropertySource</code> method returns the following properties
127: *
128: * 1) P_FIRSTNAME returns String, firstname
129: * 2) P_LASTNAME returns String, lastname
130: * 3) P_MIDDLENAME returns String, middle
131: */
132: public Object getPropertyValue(Object propKey) {
133: if (P_ID_FIRSTNAME.equals(propKey))
134: return getFirstName();
135: if (P_ID_LASTNAME.equals(propKey))
136: return getLastName();
137: if (P_ID_MIDDLENAME.equals(propKey))
138: return getInitial();
139: return null;
140: }
141:
142: /* (non-Javadoc)
143: * Method declared on IPropertySource
144: */
145: public boolean isPropertySet(Object key) {
146: if (key.equals(P_ID_FIRSTNAME))
147: return getFirstName() != FIRSTNAME_DEFAULT;
148: if (key.equals(P_ID_LASTNAME))
149: return getLastName() != LASTNAME_DEFAULT;
150: if (key.equals(P_ID_MIDDLENAME))
151: return getInitial() != MIDDLENAME_DEFAULT;
152: return false;
153: }
154:
155: /**
156: * Implemented as part of IPropertySource framework. Sets the specified property
157: * to its default value.
158: *
159: * @see IPropertySource#resetPropertyValue(Object)
160: * @param property The property to reset.
161: */
162: public void resetPropertyValue(Object property) {
163: if (P_ID_FIRSTNAME.equals(property)) {
164: setFirstName(FIRSTNAME_DEFAULT);
165: return;
166: }
167: if (P_ID_LASTNAME.equals(property)) {
168: setLastName(LASTNAME_DEFAULT);
169: return;
170: }
171: if (P_ID_MIDDLENAME.equals(property)) {
172: setInitial(MIDDLENAME_DEFAULT);
173: return;
174: }
175: }
176:
177: /**
178: * Sets the first name
179: */
180: private void setFirstName(String newFirstName) {
181: firstName = newFirstName;
182: }
183:
184: /**
185: * Sets the initial
186: */
187: private void setInitial(String newInitial) {
188: initial = newInitial;
189: }
190:
191: /**
192: * Sets the last name
193: */
194: private void setLastName(String newLastName) {
195: lastName = newLastName;
196: }
197:
198: /**
199: * The <code>Name</code> implementation of this
200: * <code>IPropertySource</code> method
201: * defines the following Setable properties
202: *
203: * 1) P_FIRST, expects String, sets the firstname of this OrganizationElement
204: * 2) P_MIDDLENAME, expects String, sets middlename of this OrganizationElement
205: * 3) P_LASTNAME, expects String, sets lastname of this OrganizationElement
206: */
207: public void setPropertyValue(Object propName, Object val) {
208: if (P_ID_FIRSTNAME.equals(propName)) {
209: setFirstName((String) val);
210: return;
211: }
212: if (P_ID_LASTNAME.equals(propName)) {
213: setLastName((String) val);
214: return;
215: }
216: if (P_ID_MIDDLENAME.equals(propName)) {
217: setInitial((String) val);
218: return;
219: }
220: }
221:
222: /**
223: * The value as displayed in the Property Sheet. Will not print default values
224: * @return java.lang.String
225: */
226: public String toString() {
227: StringBuffer outStringBuffer = new StringBuffer();
228: if (getFirstName() != FIRSTNAME_DEFAULT) {
229: outStringBuffer.append(getFirstName());
230: outStringBuffer.append(" "); //$NON-NLS-1$
231: }
232: if (getInitial() != MIDDLENAME_DEFAULT) {
233: outStringBuffer.append(getInitial());
234: outStringBuffer.append(" "); //$NON-NLS-1$
235: }
236: if (getLastName() != LASTNAME_DEFAULT) {
237: outStringBuffer.append(getLastName());
238: }
239:
240: return outStringBuffer.toString();
241: }
242: }
|