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.PropertyDescriptor;
017:
018: /**
019: * Example IPropertySource is editable and whose childern properties are itself not editable.
020: * The values of "userid" and "mailserver" are parsed from setting "email"
021: */
022: public class EmailAddress implements IPropertySource {
023:
024: //Property-Value
025: private String userid;
026:
027: private String domain;
028:
029: //Default Property-Value
030: private static final String USERID_DEFAULT = MessageUtil
031: .getString("unknownUser"); //$NON-NLS-1$
032:
033: private static final String DOMAIN_DEFAULT = MessageUtil
034: .getString("unknownDomain"); //$NON-NLS-1$
035:
036: //Property unique keys
037: public static final String P_ID_USERID = "EmailAddress.userid"; //$NON-NLS-1$
038:
039: public static final String P_ID_DOMAIN = "EmailAddress.domain"; //$NON-NLS-1$
040:
041: //Property display keys
042: public static final String P_USERID = MessageUtil
043: .getString("userid"); //$NON-NLS-1$
044:
045: public static final String P_DOMAIN = MessageUtil
046: .getString("domain"); //$NON-NLS-1$
047:
048: //Property-Descriptors
049: private static Vector descriptors;
050:
051: static {
052: descriptors = new Vector(2, 2);
053: //non-editable child properties --> provide no editors
054: descriptors.addElement(new PropertyDescriptor(P_ID_USERID,
055: P_USERID));
056: descriptors.addElement(new PropertyDescriptor(P_ID_DOMAIN,
057: P_DOMAIN));
058: }
059:
060: /**
061: * EmailAddress Default Constructor
062: */
063: public EmailAddress() {
064: super ();
065: }
066:
067: /**
068: * Convience EmailAddress constructor.
069: * Calls setEmailAddress() to parse emailAddress
070: * @param emailAddress java.lang.String, in the form userid@domain
071: * @throws java.lang.IllegalArgumentException, if does not subscribe to form
072: */
073: public EmailAddress(String emailAddress)
074: throws IllegalArgumentException {
075: super ();
076: setEmailAddress(emailAddress);
077:
078: }
079:
080: /**
081: * Returns the descriptors
082: */
083: private static Vector getDescriptors() {
084: return descriptors;
085: }
086:
087: /**
088: * Returns the domain
089: */
090: private String getDomain() {
091: if (domain == null)
092: domain = DOMAIN_DEFAULT;
093: return domain;
094: }
095:
096: /* (non-Javadoc)
097: * Method declared on IPropertySource
098: */
099: public Object getEditableValue() {
100: return this .toString();
101: }
102:
103: /* (non-Javadoc)
104: * Method declared on IPropertySource
105: */
106: public IPropertyDescriptor[] getPropertyDescriptors() {
107: return (IPropertyDescriptor[]) getDescriptors().toArray(
108: new IPropertyDescriptor[getDescriptors().size()]);
109: }
110:
111: /**
112: * The <code>EmailAddress</code> implementation of this
113: * <code>IPropertySource</code> method returns the following properties
114: *
115: * 1) P_USERID returns String, values before "@"
116: * 2) P_DOMAIN returns String, values after "@"
117: *
118: * Observe the available properties must always equal those listed
119: * in the property descriptors
120: */
121: public Object getPropertyValue(Object propKey) {
122: if (propKey.equals(P_ID_USERID))
123: return getUserid();
124: if (propKey.equals(P_ID_DOMAIN))
125: return getDomain();
126: return null;
127: }
128:
129: /**
130: * Returns the userid
131: */
132: private String getUserid() {
133: if (userid == null)
134: userid = USERID_DEFAULT;
135: return userid;
136: }
137:
138: /* (non-Javadoc)
139: * Method declared on IPropertySource
140: */
141: public boolean isPropertySet(Object property) {
142: return false;
143: }
144:
145: /* (non-Javadoc)
146: * Method declared on IPropertySource
147: */
148: public void resetPropertyValue(Object property) {
149: return;
150: }
151:
152: /**
153: * Sets the domain
154: */
155: private void setDomain(java.lang.String newDomain) {
156: domain = newDomain;
157: }
158:
159: /**
160: * Parses emailAddress into domain and userid. Throws SetPropertyVetoException
161: * if emailAddress does not contain an userid and domain seperated by '@'.
162: *
163: * @param emailAddress the email address
164: * @throws IllegalArgumentException
165: */
166: private void setEmailAddress(String emailAddress)
167: throws IllegalArgumentException {
168: if (emailAddress == null)
169: throw new IllegalArgumentException(MessageUtil
170: .getString("emailaddress_cannot_be_set_to_null")); //$NON-NLS-1$
171: int index = emailAddress.indexOf('@');
172: int length = emailAddress.length();
173: if (index > 0 && index < length) {
174: setUserid(emailAddress.substring(0, index));
175: setDomain(emailAddress.substring(index + 1));
176: return;
177: }
178: throw new IllegalArgumentException(
179: MessageUtil
180: .getString("invalid_email_address_format_should_have_been_validated")); //$NON-NLS-1$
181: }
182:
183: /**
184: * The <code>Address</code> implementation of this
185: * <code>IPropertySource</code> method
186: * defines the following Setable properties
187: *
188: * 1) P_USERID, expects String
189: * 2) P_DOMAIN, expects String
190: */
191: public void setPropertyValue(Object name, Object value) {
192: if (name.equals(P_ID_USERID)) {
193: setUserid((String) value);
194: return;
195: }
196: if (name.equals(P_ID_DOMAIN)) {
197: setDomain((String) value);
198: return;
199: }
200: }
201:
202: /**
203: * Sets the userid
204: */
205: private void setUserid(String newUserid) {
206: userid = newUserid;
207: }
208:
209: /**
210: * The value as displayed in the Property Sheet.
211: * @return java.lang.String
212: */
213: public String toString() {
214: StringBuffer strbuffer = new StringBuffer(getUserid());
215: strbuffer.append('@');
216: strbuffer.append(getDomain());
217: return strbuffer.toString();
218: }
219: }
|