001: /**
002: * L2FProd.com Common Components 7.3 License.
003: *
004: * Copyright 2005-2007 L2FProd.com
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package com.l2fprod.common.beans;
018:
019: import com.l2fprod.common.util.ResourceManager;
020:
021: import java.awt.Image;
022: import java.beans.BeanDescriptor;
023: import java.beans.IntrospectionException;
024: import java.beans.PropertyDescriptor;
025: import java.beans.SimpleBeanInfo;
026: import java.util.ArrayList;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.MissingResourceException;
030:
031: /**
032: * A convenient class to build beaninfos by adding and removing
033: * properties. <br>
034: */
035: public class BaseBeanInfo extends SimpleBeanInfo {
036:
037: private Class type;
038:
039: private BeanDescriptor beanDescriptor;
040:
041: private List properties = new ArrayList(0);
042:
043: public BaseBeanInfo(Class type) {
044: this .type = type;
045: }
046:
047: public final Class getType() {
048: return type;
049: }
050:
051: public ResourceManager getResources() {
052: return ResourceManager.get(getType());
053: }
054:
055: public BeanDescriptor getBeanDescriptor() {
056: if (beanDescriptor == null) {
057: beanDescriptor = new DefaultBeanDescriptor(this );
058: }
059: return beanDescriptor;
060: }
061:
062: public PropertyDescriptor[] getPropertyDescriptors() {
063: return (PropertyDescriptor[]) properties
064: .toArray(new PropertyDescriptor[0]);
065: }
066:
067: public int getPropertyDescriptorCount() {
068: return properties.size();
069: }
070:
071: public PropertyDescriptor getPropertyDescriptor(int index) {
072: return (PropertyDescriptor) properties.get(index);
073: }
074:
075: protected PropertyDescriptor addPropertyDescriptor(
076: PropertyDescriptor property) {
077: properties.add(property);
078: return property;
079: }
080:
081: public ExtendedPropertyDescriptor addProperty(String propertyName) {
082: ExtendedPropertyDescriptor descriptor;
083: try {
084: if (propertyName == null
085: || propertyName.trim().length() == 0) {
086: throw new IntrospectionException("bad property name");
087: }
088:
089: descriptor = ExtendedPropertyDescriptor
090: .newPropertyDescriptor(propertyName, getType());
091:
092: try {
093: descriptor.setDisplayName(getResources().getString(
094: propertyName));
095: } catch (MissingResourceException e) {
096: // ignore, the resource may not be provided
097: }
098: try {
099: descriptor.setShortDescription(getResources()
100: .getString(propertyName + ".shortDescription"));
101: } catch (MissingResourceException e) {
102: // ignore, the resource may not be provided
103: }
104: addPropertyDescriptor(descriptor);
105: return descriptor;
106: } catch (IntrospectionException e) {
107: throw new RuntimeException(e);
108: }
109: }
110:
111: /**
112: * Removes the first occurrence of the property named <code>propertyName</code>
113: * @param propertyName
114: * @return the removed PropertyDescriptor or null if not found.
115: */
116: public PropertyDescriptor removeProperty(String propertyName) {
117: if (propertyName == null) {
118: throw new IllegalArgumentException(
119: "Property name can not be null");
120: }
121: for (Iterator iter = properties.iterator(); iter.hasNext();) {
122: PropertyDescriptor property = (PropertyDescriptor) iter
123: .next();
124: if (propertyName.equals(property.getName())) {
125: // remove the property from the list
126: iter.remove();
127: return property;
128: }
129: }
130: return null;
131: }
132:
133: /**
134: * Get the icon for displaying this bean.
135: *
136: * @param kind Kind of icon.
137: * @return Image for bean, or null if none.
138: */
139: public Image getIcon(int kind) {
140: return null;
141: }
142:
143: /**
144: * Return a text describing the object.
145: *
146: * @param value an <code>Object</code> value
147: * @return a text describing the object.
148: */
149: public String getText(Object value) {
150: return value.toString();
151: }
152:
153: /**
154: * Return a text describing briefly the object. The text will be used
155: * whereever a explanation is needed to give to the user
156: *
157: * @param value an <code>Object</code> value
158: * @return a <code>String</code> value
159: */
160: public String getDescription(Object value) {
161: return getText(value);
162: }
163:
164: /**
165: * Return a text describing the object. The text will be displayed in a
166: * tooltip.
167: *
168: * @param value an <code>Object</code> value
169: * @return a <code>String</code> value
170: */
171: public String getToolTipText(Object value) {
172: return getText(value);
173: }
174:
175: }
|