KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > propertyeditor > ClassArrayEditor


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.util.propertyeditor;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26 import java.beans.PropertyEditorSupport JavaDoc;
27
28 /** A property editor for Class[].
29  *
30  * @version $Revision: 1958 $
31  * @author Scott.Stark@jboss.org
32  */

33 public class ClassArrayEditor extends PropertyEditorSupport JavaDoc
34 {
35    /** Build a Class[] from a comma/whitespace seperated list of classes
36     * @param text - the class name list
37     */

38    public void setAsText(final String JavaDoc text) throws IllegalArgumentException JavaDoc
39    {
40       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
41       StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(text, ", \t\r\n");
42       ArrayList JavaDoc classes = new ArrayList JavaDoc();
43       while( tokenizer.hasMoreTokens() == true )
44       {
45          String JavaDoc name = tokenizer.nextToken();
46          try
47          {
48             Class JavaDoc c = loader.loadClass(name);
49             classes.add(c);
50          }
51          catch(ClassNotFoundException JavaDoc e)
52          {
53             throw new IllegalArgumentException JavaDoc("Failed to find class: "+name);
54          }
55       }
56
57       Class JavaDoc[] theValue = new Class JavaDoc[classes.size()];
58       classes.toArray(theValue);
59       setValue(theValue);
60    }
61
62    /**
63     * @return a comma seperated string of the class array
64     */

65    public String JavaDoc getAsText()
66    {
67       Class JavaDoc[] theValue = (Class JavaDoc[]) getValue();
68       StringBuffer JavaDoc text = new StringBuffer JavaDoc();
69       int length = theValue == null ? 0 : theValue.length;
70       for(int n = 0; n < length; n ++)
71       {
72          text.append(theValue[n].getName());
73          text.append(',');
74       }
75       // Remove the trailing ','
76
text.setLength(text.length()-1);
77       return text.toString();
78    }
79 }
80
Popular Tags