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-2006 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.vmd.api.model;
042:
043: /**
044: * This immutable class represents a property descriptor used in component descriptor.
045: * <p>
046: * This class holds information about name, type id, default value or factory for default values, serializable and readonly ability.
047: *
048: * @author David Kaspar
049: */
050: public final class PropertyDescriptor {
051:
052: private final String name;
053: private final TypeID type;
054: private final DefaultValueFactory defaultValueFactory;
055: private final PropertyValue defaultValue;
056: private final boolean allowNull;
057: private final boolean allowUserCode;
058: private final Versionable versionable;
059: private final boolean useForSerialization;
060: private final boolean readOnly;
061:
062: /**
063: * Creates a new property descriptor using name, type id, and default value. It will be serializable and read-write by default.
064: * @param name the property name
065: * @param type the property type id
066: * @param defaultValue the default property value
067: * @param allowNull the property allow null values
068: * @param allowUserCode the property allow user code values
069: * @param versionable the property version
070: */
071: public PropertyDescriptor(String name, TypeID type,
072: PropertyValue defaultValue, boolean allowNull,
073: boolean allowUserCode, Versionable versionable) {
074: this (name, type, defaultValue, allowNull, allowUserCode,
075: versionable, true, false);
076: }
077:
078: /**
079: * Creates a new property descriptor using name, type id, default value, serializable and read-only ability.
080: * @param name the property name
081: * @param type the property type id
082: * @param defaultValue the default property value
083: * @param allowNull the property allow null values
084: * @param allowUserCode the property allow user code values
085: * @param versionable the property version
086: * @param useForSerialization true if serializable
087: * @param readonly true if the property is read-only
088: */
089: public PropertyDescriptor(String name, TypeID type,
090: PropertyValue defaultValue, boolean allowNull,
091: boolean allowUserCode, Versionable versionable,
092: boolean useForSerialization, boolean readonly) {
093: this (name, type, null, defaultValue, allowNull, allowUserCode,
094: versionable, useForSerialization, readonly);
095: }
096:
097: /**
098: * Creates a new property descriptor using name, type id, factory for default values, serializable and read-only ability.
099: * @param name the property name
100: * @param type the property type id
101: * @param defaultValueFactory the factory for default property values
102: * @param allowNull the property allow null values
103: * @param allowUserCode the property allow user code values
104: * @param versionable the property version
105: * @param useForSerialization true if serializable
106: * @param readonly true if the property is read-only
107: */
108: public PropertyDescriptor(String name, TypeID type,
109: DefaultValueFactory defaultValueFactory, boolean allowNull,
110: boolean allowUserCode, Versionable versionable,
111: boolean useForSerialization, boolean readonly) {
112: this (name, type, defaultValueFactory, null, allowNull,
113: allowUserCode, versionable, useForSerialization,
114: readonly);
115: }
116:
117: private PropertyDescriptor(String name, TypeID type,
118: DefaultValueFactory defaultValueFactory,
119: PropertyValue defaultValue, boolean allowNull,
120: boolean allowUserCode, Versionable versionable,
121: boolean useForSerialization, boolean readonly) {
122: assert name != null
123: && type != null
124: && (defaultValueFactory != null || defaultValue != null) : "Name, type, defaultValueFactory or defaultValue could be null in PropertyDescriptor : "
125: + this ; // NOI18N
126: this .name = name;
127: this .type = type;
128: this .defaultValueFactory = defaultValueFactory;
129: this .defaultValue = defaultValue;
130: this .allowNull = allowNull;
131: this .allowUserCode = allowUserCode;
132: this .versionable = versionable;
133: this .useForSerialization = useForSerialization;
134: this .readOnly = readonly;
135: }
136:
137: /**
138: * Returns a property name.
139: * @return the property name
140: */
141: public String getName() {
142: return name;
143: }
144:
145: /**
146: * Returns a property type id.
147: * @return the property type id
148: */
149: public TypeID getType() {
150: return type;
151: }
152:
153: /**
154: * Returns a default value.
155: * @return the default value, null if a default-value-factory is used
156: */
157: public PropertyValue getDefaultValue() {
158: return defaultValue;
159: }
160:
161: /**
162: * Returns whether the property allows null values.
163: * @return true, if allows null
164: */
165: public boolean isAllowNull() {
166: return allowNull;
167: }
168:
169: /**
170: * Returns whether the property allows user code values.
171: * @return true, if allows null
172: */
173: public boolean isAllowUserCode() {
174: return allowUserCode;
175: }
176:
177: /**
178: * Returns a property version.
179: * @return the property version
180: */
181: public Versionable getVersionable() {
182: return versionable;
183: }
184:
185: /**
186: * Returns a state of serialization ability.
187: * @return true if a property is serializable
188: */
189: public boolean isUseForSerialization() {
190: return useForSerialization;
191: }
192:
193: /**
194: * Returns a state of read-only ability.
195: * @return true if a property is read-only, false if a property is read-write
196: */
197: public boolean isReadOnly() {
198: return readOnly;
199: }
200:
201: /**
202: * Creates a new default property value.
203: * @param component the component where the default value should be stored
204: * @param propertyName the property name of a property where the default value should be stored
205: * @return the property value
206: */
207: public PropertyValue createDefaultValue(DesignComponent component,
208: String propertyName) {
209: return defaultValueFactory != null ? defaultValueFactory
210: .createDefaultValue(component, propertyName)
211: : defaultValue;
212: }
213:
214: /**
215: * This interface describes a factory of default values. Use this if you would like to create default values dynamically
216: * based on the component and property name where the return default property value should be stored.
217: */
218: public interface DefaultValueFactory {
219:
220: /**
221: * Create a property value that will be taken as a default value of a specified property name in a specified component.
222: * <p>
223: * Note: This method is called when a component is not fully initialized, therefore use only
224: * component.getComponentID (), component.getType () and component.getComponentDescriptor () methods there.
225: * @param component the component
226: * @param propertyName the property name
227: * @return the default property value
228: */
229: public PropertyValue createDefaultValue(
230: DesignComponent component, String propertyName);
231:
232: }
233:
234: }
|