001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.visualweb.propertyeditors;
042:
043: import java.beans.PropertyDescriptor;
044: import com.sun.rave.designtime.DesignProperty;
045: import com.sun.rave.propertyeditors.domains.AttachedDomain;
046: import com.sun.rave.propertyeditors.domains.Domain;
047: import com.sun.rave.propertyeditors.domains.Element;
048:
049: /**
050: * An abstract property editor base class, for building editors that allow
051: * a property's value to be selected from a pre-configured domain of values.
052: * A domain is a class that extends {@link com.sun.rave.propertyeditors.domains.Domain}.
053: * The editor's domain may be set in one of two ways:
054: * <ul>
055: * <li>A domain object may be specified as a constructor parameter.
056: * <li>The domain class may be specified as the value of the property
057: * descriptor attribute <code>DomainPropertyEditor.DOMAIN_CLASS</code>, e.g.
058: * <pre>
059: * propertyDescriptor.setValue(DomainPropertyEditor.DOMAIN_CLASS, MyEditor.class);
060: * </pre>
061: * </ul>
062: * If a domain is supplied via a constructor, the design property will not be
063: * searched for an attribute specifying a domain class name. If the domain is an
064: * instance of {@link com.sun.rave.propertyeditors.domains.AttachedDomain}, it's
065: * design property will be set as soon as the editor's design property is set.
066: *
067: * @author gjmurphy
068: * @see com.sun.rave.propertyeditors.domains.Domain
069: */
070: public abstract class DomainPropertyEditor extends PropertyEditorBase {
071:
072: /**
073: * Key used to specify a domain class within a property descriptor.
074: */
075: public final static String DOMAIN_CLASS = "com.sun.rave.propertyeditors.DOMAIN_CLASS"; //NOI18N
076:
077: // Used to represent a "null" or "empty" property value
078: static final Element EMPTY_ELEMENT = new Element(null, "");
079:
080: // The domain element that corresponds to this property's default or "unset"
081: // value. This is set by default to the empty element, but will be updated
082: // to reflect the property's unset value as soon as the property descriptor
083: // is passed in.
084: protected Element defaultElement = EMPTY_ELEMENT;
085:
086: Domain domain;
087:
088: DomainPropertyEditor() {
089: this .domain = null;
090: }
091:
092: DomainPropertyEditor(Domain domain) {
093: this .domain = domain;
094: }
095:
096: public void setDesignProperty(DesignProperty designProperty) {
097: super .setDesignProperty(designProperty);
098: PropertyDescriptor descriptor = designProperty
099: .getPropertyDescriptor();
100: if (this .domain == null) {
101: Object domainClassValue = descriptor
102: .getValue(this .DOMAIN_CLASS);
103: if (domainClassValue == null)
104: throw new IllegalArgumentException(bundle.getMessage(
105: "DomainPropertyEditor.domainMissing",
106: designProperty.getPropertyDescriptor()
107: .getDisplayName()));
108: if (!(domainClassValue instanceof Class))
109: throw new IllegalArgumentException(bundle.getMessage(
110: "DomainPropertyEditor.domainValueNotClass",
111: designProperty.getPropertyDescriptor()
112: .getDisplayName()));
113: Class domainClass = (Class) domainClassValue;
114: try {
115: Domain domain = (Domain) domainClass.newInstance();
116: this .domain = domain;
117: } catch (InstantiationException e) {
118: throw new IllegalArgumentException(bundle.getMessage(
119: "DomainPropertyEditor.domainError", //NOI18N
120: domainClass.toString(), designProperty
121: .getPropertyDescriptor()
122: .getDisplayName()));
123: } catch (IllegalAccessException e) {
124: throw new IllegalArgumentException(bundle.getMessage(
125: "DomainPropertyEditor.domainError", //NOI18N
126: domainClass.toString(), designProperty
127: .getPropertyDescriptor()
128: .getDisplayName()));
129: }
130: }
131: if (this .domain instanceof AttachedDomain)
132: ((AttachedDomain) this .domain)
133: .setDesignProperty(designProperty);
134: Object defaultValue = designProperty.getUnsetValue();
135: if (defaultValue != null) {
136: Element[] elements = domain.getElements();
137: for (int i = 0; i < elements.length; i++) {
138: if (defaultValue.equals(elements[i].getValue()))
139: defaultElement = elements[i];
140: }
141: }
142: }
143:
144: protected Domain getDomain() {
145: return this .domain;
146: }
147:
148: protected String getPropertyHelpId() {
149: if (this.domain != null)
150: return this.domain.getPropertyHelpId();
151: return null;
152: }
153:
154: }
|