001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017:
018: package java.beans;
019:
020: import java.util.Collections;
021: import java.util.Enumeration;
022: import java.util.HashMap;
023: import java.util.LinkedList;
024: import java.util.Map;
025:
026: /**
027: * Common base class for Descriptors.
028: */
029: public class FeatureDescriptor {
030:
031: private Map<String, Object> values;
032:
033: boolean preferred, hidden, expert;
034:
035: String shortDescription;
036:
037: String name;
038:
039: String displayName;
040:
041: /**
042: * <p>
043: * Constructs an instance.
044: * </p>
045: */
046: public FeatureDescriptor() {
047: this .values = new HashMap<String, Object>();
048: }
049:
050: /**
051: * <p>
052: * Sets the value for the named attribute.
053: * </p>
054: *
055: * @param attributeName
056: * The name of the attribute to set a value with.
057: * @param value
058: * The value to set.
059: */
060: public void setValue(String attributeName, Object value) {
061: if (attributeName == null || value == null) {
062: throw new NullPointerException();
063: }
064: values.put(attributeName, value);
065: }
066:
067: /**
068: * <p>
069: * Gets the value associated with the named attribute.
070: * </p>
071: *
072: * @param attributeName
073: * The name of the attribute to get a value for.
074: * @return The attribute's value.
075: */
076: public Object getValue(String attributeName) {
077: Object result = null;
078: if (attributeName != null) {
079: result = values.get(attributeName);
080: }
081: return result;
082: }
083:
084: /**
085: * <p>
086: * Enumerates the attribute names.
087: * </p>
088: *
089: * @return An instance of {@link Enumeration}.
090: */
091: public Enumeration<String> attributeNames() {
092: // Create a new list, so that the references are copied
093: return Collections.enumeration(new LinkedList<String>(values
094: .keySet()));
095: }
096:
097: /**
098: * <p>
099: * Sets the short description.
100: * </p>
101: *
102: * @param text
103: * The description to set.
104: */
105: public void setShortDescription(String text) {
106: this .shortDescription = text;
107: }
108:
109: /**
110: * <p>
111: * Sets the name.
112: * </p>
113: *
114: * @param name
115: * The name to set.
116: */
117: public void setName(String name) {
118: this .name = name;
119: }
120:
121: /**
122: * <p>
123: * Sets the display name.
124: * </p>
125: *
126: * @param displayName
127: * The display name to set.
128: */
129: public void setDisplayName(String displayName) {
130: this .displayName = displayName;
131: }
132:
133: /**
134: * <p>
135: * Gets the short description or {@link #getDisplayName()} if not set.
136: * </p>
137: *
138: * @return The description.
139: */
140: public String getShortDescription() {
141: return shortDescription == null ? getDisplayName()
142: : shortDescription;
143: }
144:
145: /**
146: * <p>
147: * Gets the name.
148: * </p>
149: *
150: * @return The name.
151: */
152: public String getName() {
153: return name;
154: }
155:
156: /**
157: * <p>
158: * Gets the display name or {@link #getName()} if not set.
159: * </p>
160: *
161: * @return The display name.
162: */
163: public String getDisplayName() {
164: return displayName == null ? getName() : displayName;
165: }
166:
167: /**
168: * <p>
169: * Sets the preferred indicator.
170: * </p>
171: *
172: * @param preferred
173: * <code>true</code> if preferred, <code>false</code>
174: * otherwise.
175: */
176: public void setPreferred(boolean preferred) {
177: this .preferred = preferred;
178: }
179:
180: /**
181: * <p>
182: * Sets the hidden indicator.
183: * </p>
184: *
185: * @param hidden
186: * <code>true</code> if hidden, <code>false</code> otherwise.
187: */
188: public void setHidden(boolean hidden) {
189: this .hidden = hidden;
190: }
191:
192: /**
193: * <p>
194: * Sets the expert indicator.
195: * </p>
196: *
197: * @param expert
198: * <code>true</code> if expert, <code>false</code> otherwise.
199: */
200: public void setExpert(boolean expert) {
201: this .expert = expert;
202: }
203:
204: /**
205: * <p>
206: * Indicates if this feature is preferred.
207: * </p>
208: *
209: * @return <code>true</code> if preferred, <code>false</code> otherwise.
210: */
211: public boolean isPreferred() {
212: return preferred;
213: }
214:
215: /**
216: * <p>
217: * Indicates if this feature is hidden.
218: * </p>
219: *
220: * @return <code>true</code> if hidden, <code>false</code> otherwise.
221: */
222: public boolean isHidden() {
223: return hidden;
224: }
225:
226: /**
227: * <p>
228: * Indicates if this feature is an expert feature.
229: * </p>
230: *
231: * @return <code>true</code> if hidden, <code>false</code> otherwise.
232: */
233: public boolean isExpert() {
234: return expert;
235: }
236:
237: void merge(FeatureDescriptor feature) {
238: assert (name.equals(feature.name));
239: expert |= feature.expert;
240: hidden |= feature.hidden;
241: preferred |= feature.preferred;
242: if (shortDescription == null) {
243: shortDescription = feature.shortDescription;
244: }
245: if (name == null) {
246: name = feature.name;
247: }
248: if (displayName == null) {
249: displayName = feature.displayName;
250: }
251: }
252: }
|