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.swt.graphics.RGB;
017: import org.eclipse.ui.views.properties.ColorPropertyDescriptor;
018: import org.eclipse.ui.views.properties.ComboBoxPropertyDescriptor;
019: import org.eclipse.ui.views.properties.IPropertyDescriptor;
020: import org.eclipse.ui.views.properties.IPropertySheetEntry;
021: import org.eclipse.ui.views.properties.PropertyDescriptor;
022: import org.eclipse.ui.views.properties.TextPropertyDescriptor;
023:
024: /**
025: * A User Element
026: */
027: public class UserElement extends OrganizationElement {
028:
029: // Properties
030: private Name fullName;
031:
032: private Address address;
033:
034: private String phoneNumber;
035:
036: private EmailAddress emailAddress;
037:
038: private Boolean coop;
039:
040: private Birthday birthday;
041:
042: private Float salary;
043:
044: private RGB hairColor;
045:
046: private RGB eyeColor;
047:
048: // Property default values
049: private Name fullName_Default;
050:
051: private Address address_Default;
052:
053: private String phoneNumber_Default = "555-1111"; //$NON-NLS-1$
054:
055: private EmailAddress emailAddress_Default;
056:
057: private Boolean coop_Default;
058:
059: private Birthday birthday_Default;
060:
061: private Float salary_Default;
062:
063: private RGB hairColor_Default;
064:
065: private RGB eyeColor_Default;
066:
067: // Property unique keys
068: public static final String P_ID_PHONENUMBER = "User.phonenumber"; //$NON-NLS-1$
069:
070: public static final String P_ID_ADDRESS = "User.address"; //$NON-NLS-1$
071:
072: public static final String P_ID_FULLNAME = "User.fullname"; //$NON-NLS-1$
073:
074: public static final String P_ID_EMAIL = "User.email"; //$NON-NLS-1$
075:
076: public static final String P_ID_COOP = "User.coop student"; //$NON-NLS-1$
077:
078: public static final String P_ID_BDAY = "User.birthday"; //$NON-NLS-1$
079:
080: public static final String P_ID_SALARY = "User.salary"; //$NON-NLS-1$
081:
082: public static final String P_ID_HAIRCOLOR = "User.haircolor"; //$NON-NLS-1$
083:
084: public static final String P_ID_EYECOLOR = "User.eyecolor"; //$NON-NLS-1$
085:
086: // Property display keys
087: public static final String P_PHONENUMBER = MessageUtil
088: .getString("phonenumber"); //$NON-NLS-1$
089:
090: public static final String P_ADDRESS = MessageUtil
091: .getString("address"); //$NON-NLS-1$
092:
093: public static final String P_FULLNAME = MessageUtil
094: .getString("fullname"); //$NON-NLS-1$
095:
096: public static final String P_EMAIL = MessageUtil.getString("email"); //$NON-NLS-1$
097:
098: public static final String P_COOP = MessageUtil
099: .getString("coop_student"); //$NON-NLS-1$
100:
101: public static final String P_BDAY = MessageUtil
102: .getString("birthday"); //$NON-NLS-1$
103:
104: public static final String P_SALARY = MessageUtil
105: .getString("salary"); //$NON-NLS-1$
106:
107: public static final String P_HAIRCOLOR = MessageUtil
108: .getString("haircolor"); //$NON-NLS-1$
109:
110: public static final String P_EYECOLOR = MessageUtil
111: .getString("eyecolor"); //$NON-NLS-1$
112:
113: // Help context ids
114: private static final String PHONE_NUMBER_CONTEXT = "org.eclipse.ui.examples.propertysheet.phone_number_context"; //$NON-NLS-1$
115:
116: private static final String ADDRESS_CONTEXT = "org.eclipse.ui.examples.propertysheet.address_context"; //$NON-NLS-1$
117:
118: private static final String FULL_NAME_CONTEXT = "org.eclipse.ui.examples.propertysheet.full_name_context"; //$NON-NLS-1$
119:
120: private static final String EMAIL_CONTEXT = "org.eclipse.ui.examples.propertysheet.email_context"; //$NON-NLS-1$
121:
122: private static final String COOP_CONTEXT = "org.eclipse.ui.examples.propertysheet.coop_context"; //$NON-NLS-1$
123:
124: private static final String BIRTHDAY_CONTEXT = "org.eclipse.ui.examples.propertysheet.birthday_context"; //$NON-NLS-1$
125:
126: private static final String SALARY_CONTEXT = "org.eclipse.ui.examples.propertysheet.salary_context"; //$NON-NLS-1$
127:
128: private static final String HAIR_COLOR__CONTEXT = "org.eclipse.ui.examples.propertysheet.hair_color_context"; //$NON-NLS-1$
129:
130: private static final String EYE_COLOR_CONTEXT = "org.eclipse.ui.examples.propertysheet.eye_color_context"; //$NON-NLS-1$
131:
132: // Property Category
133: public static final String P_CONTACTINFO = MessageUtil
134: .getString("contact"); //$NON-NLS-1$
135:
136: public static final String P_PERSONELINFO = MessageUtil
137: .getString("personel"); //$NON-NLS-1$
138:
139: public static final String P_PERSONALINFO = MessageUtil
140: .getString("personal"); //$NON-NLS-1$
141:
142: // Property Values
143: public static final Integer P_VALUE_TRUE = new Integer(0);
144:
145: public static final Integer P_VALUE_FALSE = new Integer(1);
146:
147: public static final String P_VALUE_TRUE_LABEL = MessageUtil
148: .getString("true"); //$NON-NLS-1$
149:
150: public static final String P_VALUE_FALSE_LABEL = MessageUtil
151: .getString("false"); //$NON-NLS-1$
152:
153: static private class BooleanLabelProvider extends LabelProvider {
154: public String getText(Object element) {
155: String[] values = new String[] { P_VALUE_TRUE_LABEL,
156: P_VALUE_FALSE_LABEL };
157: return values[((Integer) element).intValue()];
158: }
159: }
160:
161: //
162: static private Vector descriptors;
163: static {
164: descriptors = new Vector();
165: PropertyDescriptor propertyDescriptor;
166:
167: ///
168: propertyDescriptor = new TextPropertyDescriptor(
169: P_ID_PHONENUMBER, P_PHONENUMBER);
170: propertyDescriptor.setCategory(P_CONTACTINFO);
171: propertyDescriptor.setHelpContextIds(PHONE_NUMBER_CONTEXT);
172: descriptors.addElement(propertyDescriptor);
173:
174: ///
175: propertyDescriptor = new PropertyDescriptor(P_ID_ADDRESS,
176: P_ADDRESS);
177: propertyDescriptor.setCategory(P_CONTACTINFO);
178: propertyDescriptor.setHelpContextIds(ADDRESS_CONTEXT);
179: descriptors.addElement(propertyDescriptor);
180:
181: ///
182: propertyDescriptor = new TextPropertyDescriptor(P_ID_EMAIL,
183: P_EMAIL);
184: propertyDescriptor.setCategory(P_CONTACTINFO);
185: propertyDescriptor.setHelpContextIds(EMAIL_CONTEXT);
186: propertyDescriptor.setValidator(new EmailAddressValidator());
187: descriptors.addElement(propertyDescriptor);
188:
189: ///
190: propertyDescriptor = new TextPropertyDescriptor(P_ID_FULLNAME,
191: P_FULLNAME);
192: propertyDescriptor.setCategory(P_PERSONELINFO);
193: propertyDescriptor.setHelpContextIds(FULL_NAME_CONTEXT);
194: descriptors.addElement(propertyDescriptor);
195:
196: ///
197: propertyDescriptor = new PropertyDescriptor(P_ID_BDAY, P_BDAY);
198: propertyDescriptor.setCategory(P_PERSONELINFO);
199: propertyDescriptor.setHelpContextIds(BIRTHDAY_CONTEXT);
200: descriptors.addElement(propertyDescriptor);
201:
202: ///
203: propertyDescriptor = new ComboBoxPropertyDescriptor(P_ID_COOP,
204: P_COOP, new String[] { P_VALUE_TRUE_LABEL,
205: P_VALUE_FALSE_LABEL });
206: propertyDescriptor.setCategory(P_PERSONELINFO);
207: propertyDescriptor.setHelpContextIds(COOP_CONTEXT);
208: propertyDescriptor.setLabelProvider(new BooleanLabelProvider());
209: descriptors.addElement(propertyDescriptor);
210:
211: ///
212: propertyDescriptor = new TextPropertyDescriptor(P_ID_SALARY,
213: P_SALARY);
214: //add custom validator to propertyDescriptor limiting salary values to be
215: //greator than zero
216: propertyDescriptor
217: .setHelpContextIds(new Object[] { SALARY_CONTEXT });
218: propertyDescriptor
219: .setFilterFlags(new String[] { IPropertySheetEntry.FILTER_ID_EXPERT });
220: propertyDescriptor.setValidator(new ICellEditorValidator() {
221: public String isValid(Object value) {
222: if (value == null)
223: return MessageUtil.getString("salary_is_invalid"); //$NON-NLS-1$
224: //
225: Float trySalary;
226: try {
227: trySalary = new Float(Float
228: .parseFloat((String) value));
229: } catch (NumberFormatException e) {
230: return MessageUtil.getString("salary_is_invalid"); //$NON-NLS-1$
231: }
232: if (trySalary.floatValue() < 0.0)
233: return MessageUtil
234: .getString("salary_must_be_greator_than_zero"); //$NON-NLS-1$
235: return null;
236: }
237: });
238: propertyDescriptor.setCategory(P_PERSONELINFO);
239: descriptors.addElement(propertyDescriptor);
240:
241: /// HairColor
242: propertyDescriptor = new ColorPropertyDescriptor(
243: P_ID_HAIRCOLOR, P_HAIRCOLOR);
244: propertyDescriptor.setCategory(P_PERSONALINFO);
245: propertyDescriptor.setHelpContextIds(HAIR_COLOR__CONTEXT);
246: descriptors.addElement(propertyDescriptor);
247:
248: /// EyeColor
249: propertyDescriptor = new ColorPropertyDescriptor(P_ID_EYECOLOR,
250: P_EYECOLOR);
251: propertyDescriptor.setCategory(P_PERSONALINFO);
252: propertyDescriptor.setHelpContextIds(EYE_COLOR_CONTEXT);
253: descriptors.addElement(propertyDescriptor);
254:
255: //gets descriptors from parent, warning name-space collision may occur
256: Vector parentDescriptors = OrganizationElement.getDescriptors();
257: for (int i = 0; i < parentDescriptors.size(); i++) {
258: descriptors.addElement(parentDescriptors.elementAt(i));
259: }
260: }
261:
262: /**
263: * Constructor. Default visibility only called from GroupElement.createSubGroup()
264: * Creates a new UserElement within the passed parent GroupElement,
265: * *
266: * @param name the name
267: * @param parent the parent
268: */
269: UserElement(String name, GroupElement parent) {
270: super (name, parent);
271: }
272:
273: /**
274: * Returns the address
275: */
276: private Address getAddress() {
277: if (address == null)
278: address = new Address();
279: return address;
280: }
281:
282: /**
283: * Returns the birthday
284: */
285: private Birthday getBirthday() {
286: if (birthday == null)
287: birthday = new Birthday();
288: return birthday;
289: }
290:
291: /* (non-Javadoc)
292: * Method declared on IWorkbenchAdapter
293: */
294: public Object[] getChildren(Object o) {
295: return new Object[0];
296: }
297:
298: /**
299: * Returns the coop
300: */
301: private Boolean getCoop() {
302: if (coop == null)
303: coop = new Boolean(false);
304: return coop;
305: }
306:
307: /**
308: * Returns the descriptors
309: */
310: static Vector getDescriptors() {
311: return descriptors;
312: }
313:
314: /* (non-Javadoc)
315: * Method declared on IPropertySource
316: */
317: public Object getEditableValue() {
318: return this .toString();
319: }
320:
321: /**
322: * Returns the email address
323: */
324: EmailAddress getEmailAddress() {
325: if (emailAddress == null)
326: emailAddress = new EmailAddress();
327: return emailAddress;
328: }
329:
330: /**
331: * Returns the eye color
332: */
333: private RGB getEyeColor() {
334: if (eyeColor == null)
335: eyeColor = new RGB(60, 60, 60);
336: return eyeColor;
337: }
338:
339: /**
340: * Returns the full name
341: */
342: private Name getFullName() {
343: if (fullName == null)
344: fullName = new Name(getName());
345: return fullName;
346: }
347:
348: /**
349: * Returns the hair color
350: */
351: private RGB getHairColor() {
352: if (hairColor == null)
353: hairColor = new RGB(255, 255, 255);
354: return hairColor;
355: }
356:
357: /**
358: * Returns the phone number
359: */
360: private String getPhoneNumber() {
361: return phoneNumber;
362: }
363:
364: /* (non-Javadoc)
365: * Method declared on IPropertySource
366: */
367: public IPropertyDescriptor[] getPropertyDescriptors() {
368: return (IPropertyDescriptor[]) getDescriptors().toArray(
369: new IPropertyDescriptor[getDescriptors().size()]);
370: }
371:
372: /**
373: * The <code>Userment</code> implementation of this
374: * <code>IPropertySource</code> method returns the following properties
375: *
376: * 1) P_ADDRESS returns Address (IPropertySource), the address of this user
377: * 2) P_FULLNAME returns Name (IPropertySource), the full name of this user
378: * 3) P_PHONENUMBER returns String, the phone number of this user
379: * 4) P_EMAIL returns EmailAddress (IPropertySource), the email address of this user
380: * 5) P_COOP returns Boolean, whether the individual is a coop student or not
381: * 6) P_BDAY returns Birthday
382: * 7) P_SALARY return java.lang.Float
383: * 8) P_HAIRCOLOR, expects RGB
384: * 9) P_EYECOLOR, expects RGB
385: */
386: public Object getPropertyValue(Object propKey) {
387: if (propKey.equals(P_ID_ADDRESS))
388: return getAddress();
389: if (propKey.equals(P_ID_FULLNAME))
390: return getFullName();
391: if (propKey.equals(P_ID_PHONENUMBER))
392: return getPhoneNumber();
393: if (propKey.equals(P_ID_EMAIL))
394: return getEmailAddress();
395: if (propKey.equals(P_ID_COOP))
396: return getCoop().equals(Boolean.TRUE) ? P_VALUE_TRUE
397: : P_VALUE_FALSE;
398: if (propKey.equals(P_ID_BDAY))
399: return getBirthday();
400: if (propKey.equals(P_ID_SALARY))
401: return getSalary().toString();
402: if (propKey.equals(P_ID_HAIRCOLOR))
403: return getHairColor();
404: if (propKey.equals(P_ID_EYECOLOR))
405: return getEyeColor();
406: return super .getPropertyValue(propKey);
407: }
408:
409: /**
410: * Returns the salary
411: */
412: private Float getSalary() {
413: if (salary == null)
414: salary = new Float(0);
415: return salary;
416: }
417:
418: /* (non-Javadoc)
419: * Method declared on IPropertySource
420: */
421: public boolean isPropertySet(Object property) {
422: if (property.equals(P_ID_ADDRESS))
423: return getAddress() != address_Default;
424: if (property.equals(P_ID_FULLNAME))
425: return getFullName() != fullName_Default;
426: if (property.equals(P_ID_PHONENUMBER))
427: return getPhoneNumber() != phoneNumber_Default;
428: if (property.equals(P_ID_EMAIL))
429: return getEmailAddress() != emailAddress_Default;
430: if (property.equals(P_ID_COOP))
431: return getCoop() != coop_Default;
432: if (property.equals(P_ID_BDAY))
433: return getBirthday() != birthday_Default;
434: if (property.equals(P_ID_SALARY))
435: return getSalary() != salary_Default;
436: if (property.equals(P_ID_HAIRCOLOR))
437: return getHairColor() != hairColor_Default;
438: if (property.equals(P_ID_EYECOLOR))
439: return getEyeColor() != eyeColor_Default;
440: return false;
441: }
442:
443: /* (non-Javadoc)
444: * Method declared on OrganizationElement
445: */
446: public boolean isUser() {
447: return true;
448: }
449:
450: /* (non-Javadoc)
451: * Method declared on IPropertySource
452: */
453: public void resetPropertyValue(Object property) {
454: if (property.equals(P_ID_ADDRESS)) {
455: setAddress(address_Default);
456: return;
457: }
458: if (property.equals(P_ID_FULLNAME)) {
459: setFullName(fullName_Default);
460: return;
461: }
462: if (property.equals(P_ID_PHONENUMBER)) {
463: setPhoneNumber(phoneNumber_Default);
464: return;
465: }
466: if (property.equals(P_ID_EMAIL)) {
467: setEmailAddress(emailAddress_Default);
468: return;
469: }
470: if (property.equals(P_ID_COOP)) {
471: setCoop(coop_Default);
472: }
473: if (property.equals(P_ID_BDAY)) {
474: setBirthday(birthday_Default);
475: return;
476: }
477: if (property.equals(P_ID_SALARY)) {
478: setSalary(salary_Default);
479: return;
480: }
481: if (property.equals(P_ID_HAIRCOLOR)) {
482: setHairColor(hairColor_Default);
483: return;
484: }
485: if (property.equals(P_ID_EYECOLOR)) {
486: setEyeColor(eyeColor_Default);
487: return;
488: }
489: super .resetPropertyValue(property);
490: }
491:
492: /**
493: * Sets the address
494: */
495: public void setAddress(Address newAddress) {
496: address = newAddress;
497: }
498:
499: /**
500: * Sets the birthday
501: */
502: public void setBirthday(Birthday newBirthday) {
503: birthday = new Birthday();
504: }
505:
506: /**
507: * Sets the coop
508: */
509: public void setCoop(Boolean newCoop) {
510: coop = newCoop;
511: }
512:
513: /**
514: *Sets the email address
515: */
516: public void setEmailAddress(EmailAddress newEmailAddress) {
517: emailAddress = newEmailAddress;
518: }
519:
520: /**
521: * Sets eye color
522: */
523: public void setEyeColor(RGB newEyeColor) {
524: eyeColor = newEyeColor;
525: }
526:
527: /**
528: * Sets full name
529: */
530: public void setFullName(Name newFullName) {
531: fullName = newFullName;
532: }
533:
534: /**
535: * Sets hair color
536: */
537: public void setHairColor(RGB newHairColor) {
538: hairColor = newHairColor;
539: }
540:
541: /**
542: * Sets phone number
543: */
544: public void setPhoneNumber(String newPhoneNumber) {
545: phoneNumber = newPhoneNumber;
546: }
547:
548: /**
549: * The <code>OrganizationElement</code> implementation of this
550: * <code>IPropertySource</code> method
551: * defines the following Setable properties
552: *
553: * 1) P_ADDRESS, expects Address
554: * 2) P_FULLNAME, expects Name
555: * 3) P_PHONENUMBER, expects String
556: * 4) P_EMAIL, expects EmailAddress.
557: * 5) P_BOOLEAN, expects Boolean, whether the individual is a coop student or not
558: * 6) P_BDAY, expects Birthday
559: * 7) P_SALARY, expects java.lang.Float
560: * 8) P_HAIRCOLOR, expects RGB
561: * 9) P_EYECOLOR, expects RGB
562: */
563: public void setPropertyValue(Object propKey, Object val) {
564: // need to convert from String to domain objects
565: if (propKey.equals(P_ID_ADDRESS)) {
566: //setAddress((Address)val);
567: return;
568: }
569: if (propKey.equals(P_ID_FULLNAME)) {
570: setFullName(new Name((String) val));
571: return;
572: }
573: if (propKey.equals(P_ID_PHONENUMBER)) {
574: setPhoneNumber((String) val);
575: return;
576: }
577: if (propKey.equals(P_ID_EMAIL)) {
578: setEmailAddress(new EmailAddress((String) val));
579: return;
580: }
581: if (propKey.equals(P_ID_COOP)) {
582: setCoop(((Integer) val).equals(P_VALUE_TRUE) ? Boolean.TRUE
583: : Boolean.FALSE);
584: }
585: if (propKey.equals(P_ID_BDAY)) {
586: //setBirthday((Birthday)val);
587: return;
588: }
589: if (propKey.equals(P_ID_SALARY)) {
590: try {
591: setSalary(new Float(Float.parseFloat((String) val)));
592: } catch (NumberFormatException e) {
593: setSalary(salary_Default);
594: }
595: return;
596: }
597: if (propKey.equals(P_ID_HAIRCOLOR)) {
598: setHairColor((RGB) val);
599: return;
600: }
601: if (propKey.equals(P_ID_EYECOLOR)) {
602: setEyeColor((RGB) val);
603: return;
604: }
605: super .setPropertyValue(propKey, val);
606: }
607:
608: /**
609: * Sets the salary
610: */
611: void setSalary(Float newSalary) {
612: salary = newSalary;
613: }
614:
615: /* (non-Javadoc)
616: * @see org.eclipse.ui.model.IWorkbenchAdapter#getForeground(java.lang.Object)
617: */
618: public RGB getForeground(Object element) {
619: return null;
620: }
621:
622: /* (non-Javadoc)
623: * @see org.eclipse.ui.model.IWorkbenchAdapter#getBackground(java.lang.Object)
624: */
625: public RGB getBackground(Object element) {
626: return null;
627: }
628: }
|