001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2004 TOPP - www.openplans.org
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.validation;
018:
019: import java.beans.IntrospectionException;
020: import java.beans.PropertyDescriptor;
021: import java.beans.PropertyEditorManager;
022: import java.beans.PropertyEditorSupport;
023: import java.beans.SimpleBeanInfo;
024: import java.net.MalformedURLException;
025: import java.net.URL;
026: import java.util.Locale;
027: import java.util.MissingResourceException;
028: import java.util.ResourceBundle;
029:
030: /**
031: * Utility class extending SimpleBeanInfo with our own helper functions.
032: *
033: * @author David Zwiers, Refractions Research, Inc.
034: * @author $Author: jive $ (last modification)
035: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/validation/src/main/java/org/geotools/validation/ValidationBeanInfo.java $
036: * @version $Id: ValidationBeanInfo.java 22266 2006-10-19 11:30:55Z acuster $
037: */
038: public class ValidationBeanInfo extends SimpleBeanInfo {
039: /**
040: * ValidationBeanInfo constructor.
041: *
042: * <p>
043: * Description
044: * </p>
045: */
046: public ValidationBeanInfo() {
047: super ();
048: PropertyEditorManager.registerEditor(URL.class,
049: URLPropertyEditor.class);
050: }
051:
052: /**
053: * Implementation of getPropertyDescriptors. This method should be called
054: * by all overriding sub-class methods. Property names 'name',
055: * 'description', 'typeNames'
056: *
057: *
058: * @see java.beans.BeanInfo#getPropertyDescriptors()
059: */
060: public PropertyDescriptor[] getPropertyDescriptors() {
061: try {
062: PropertyDescriptor[] pd = new PropertyDescriptor[2];
063: ResourceBundle resourceBundle = getResourceBundle(Validation.class);
064:
065: pd[0] = createPropertyDescriptor("name", Validation.class,
066: resourceBundle);
067: pd[0].setExpert(false);
068: pd[1] = createPropertyDescriptor("description",
069: Validation.class, resourceBundle);
070: pd[1].setExpert(false);
071:
072: return pd;
073: } catch (IntrospectionException e) {
074: // TODO error, log here
075: e.printStackTrace();
076:
077: return new PropertyDescriptor[0];
078: }
079: }
080:
081: /** Based on getCLass().getName() return target Bean type */
082: protected Class beanType() {
083: Class type = getClass();
084: String typeName = type.getName();
085: if (typeName.endsWith("BeanInfo")) {
086: typeName = typeName.substring(0, typeName.length() - 8);
087: try {
088: return Class.forName(typeName);
089: } catch (ClassNotFoundException e) {
090: return null;
091: }
092: }
093: return null;
094: }
095:
096: /** Return bundle for the property file for out beanType */
097: protected ResourceBundle getResourceBundle() {
098: return getResourceBundle(beanType());
099: }
100:
101: /** Return bundle for the property file for the provided class */
102: protected ResourceBundle getResourceBundle(Class cls) {
103: Locale locale = Locale.getDefault();
104:
105: try {
106: return ResourceBundle.getBundle(cls.getName(), locale);
107: } catch (MissingResourceException mre) {
108: return ResourceBundle.getBundle(cls.getName());
109: }
110: }
111:
112: protected PropertyDescriptor createPropertyDescriptor(String name,
113: ResourceBundle bundle) throws IntrospectionException {
114: return createPropertyDescriptor(name, beanType(), bundle);
115: }
116:
117: protected PropertyDescriptor createPropertyDescriptor(String name,
118: Class cls, ResourceBundle resourceBundle)
119: throws IntrospectionException {
120: PropertyDescriptor pd = new PropertyDescriptor(name, cls);
121: String s = resourceBundle.getString(pd.getName()
122: + ".DisplayName");
123:
124: if ((s == null) || (s == "")) {
125: s = pd.getDisplayName();
126: }
127:
128: pd.setDisplayName(s);
129: s = resourceBundle.getString(pd.getName() + ".Description");
130:
131: if ((s == null) || (s == "")) {
132: s = pd.getShortDescription();
133: }
134:
135: pd.setShortDescription(s);
136:
137: return pd;
138: }
139:
140: /**
141: * URLPropertyEditor purpose.
142: *
143: * <p>
144: * Used to support java.net.URL properties in BeanInfo's
145: * </p>
146: *
147: * @author dzwiers, Refractions Research, Inc.
148: * @author $Author: jive $ (last modification)
149: * @version $Id: ValidationBeanInfo.java 22266 2006-10-19 11:30:55Z acuster $
150: */
151: class URLPropertyEditor extends PropertyEditorSupport {
152: /** the editor's data */
153: URL url;
154:
155: /**
156: * Implementation of getAsText.
157: *
158: *
159: * @see java.beans.PropertyEditor#getAsText()
160: */
161: public String getAsText() {
162: return url.toString();
163: }
164:
165: /**
166: * Implementation of setAsText.
167: *
168: * @param text
169: *
170: * @throws IllegalArgumentException
171: *
172: * @see java.beans.PropertyEditor#setAsText(java.lang.String)
173: */
174: public void setAsText(String text)
175: throws IllegalArgumentException {
176: try {
177: url = new URL(text);
178: } catch (MalformedURLException e) {
179: throw new IllegalArgumentException("Invalid URL: "
180: + text);
181: }
182: }
183:
184: /**
185: * Implementation of supportsCustomEditor.
186: *
187: *
188: * @see java.beans.PropertyEditor#supportsCustomEditor()
189: */
190: public boolean supportsCustomEditor() {
191: return false;
192: }
193:
194: /**
195: * Implementation of getJavaInitializationString.
196: *
197: *
198: * @see java.beans.PropertyEditor#getJavaInitializationString()
199: */
200: public String getJavaInitializationString() {
201: return "new URL(\"" + url.toString() + "\")";
202: }
203:
204: /**
205: * Implementation of isPaintable.
206: *
207: *
208: * @see java.beans.PropertyEditor#isPaintable()
209: */
210: public boolean isPaintable() {
211: return false;
212: }
213:
214: /**
215: * Implementation of toString.
216: *
217: *
218: * @see java.lang.Object#toString()
219: */
220: public String toString() {
221: return url.toString();
222: }
223:
224: /**
225: * Implementation of setValue.
226: *
227: * @param value
228: *
229: * @see java.beans.PropertyEditor#setValue(java.lang.Object)
230: */
231: public void setValue(Object value) {
232: try {
233: url = new URL(value.toString());
234: } catch (MalformedURLException e) {
235: //TODO error, log this
236: e.printStackTrace();
237: }
238: }
239:
240: /**
241: * Implementation of getValue.
242: *
243: *
244: * @see java.beans.PropertyEditor#getValue()
245: */
246: public Object getValue() {
247: return url;
248: }
249: }
250: }
|