KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > swixml > converters > BorderConverter


1 /*--
2  $Id: BorderConverter.java,v 1.1 2004/03/01 07:55:58 wolfpaulus Exp $
3
4  Copyright (C) 2003-2004 Wolf Paulus.
5  All rights reserved.
6
7  Redistribution and use in source and binary forms, with or without
8  modification, are permitted provided that the following conditions
9  are met:
10
11  1. Redistributions of source code must retain the above copyright
12  notice, this list of conditions, and the following disclaimer.
13
14  2. Redistributions in binary form must reproduce the above copyright
15  notice, this list of conditions, and the disclaimer that follows
16  these conditions in the documentation and/or other materials provided
17  with the distribution.
18
19  3. The end-user documentation included with the redistribution,
20  if any, must include the following acknowledgment:
21         "This product includes software developed by the
22          SWIXML Project (http://www.swixml.org/)."
23  Alternately, this acknowledgment may appear in the software itself,
24  if and wherever such third-party acknowledgments normally appear.
25
26  4. The name "Swixml" must not be used to endorse or promote products
27  derived from this software without prior written permission. For
28  written permission, please contact <info_AT_swixml_DOT_org>
29
30  5. Products derived from this software may not be called "Swixml",
31  nor may "Swixml" appear in their name, without prior written
32  permission from the Swixml Project Management.
33
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  DISCLAIMED. IN NO EVENT SHALL THE SWIXML PROJECT OR ITS
38  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
41  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
42  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
43  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
44  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45  SUCH DAMAGE.
46  ====================================================================
47
48  This software consists of voluntary contributions made by many
49  individuals on behalf of the Swixml Project and was originally
50  created by Wolf Paulus <wolf_AT_swixml_DOT_org>. For more information
51  on the Swixml Project, please see <http://www.swixml.org/>.
52 */

53
54 package org.swixml.converters;
55
56 import org.jdom.Attribute;
57 import org.swixml.Converter;
58 import org.swixml.ConverterLibrary;
59 import org.swixml.Localizer;
60 import org.swixml.SwingEngine;
61
62 import javax.swing.*;
63 import javax.swing.border.Border JavaDoc;
64 import java.lang.reflect.Method JavaDoc;
65 import java.util.StringTokenizer JavaDoc;
66
67 /**
68  * The <code>BorderConverter</code> class defines a converter that creates Border objects based on a provided String.
69  * The BorderConverter internally uses the <code>javax.swing.BorderFactory</code> and its static <i>create</i>.. methods
70  * to instatiate differnt kinds of borders, based on the given String.<br>
71  * Additional parameters to create a border need to be comma separated and enclosed in parentheses.<br><br>
72  * Parameter types get converted through the <code>ConverterLibrary</code>. Therefore, only parameter classes are supported that
73  * have registered converters in the ConverLibrary.
74  * <h3>Examples for Valid XML attribute notations:</h3>
75  * <pre>
76  * <ul>
77  * <li>border="MatteBorder(4,4,4,4,red)"</li>
78  * <li>border="EmptyBorder(5,5,5,5)"</li>
79  * <li>border="TitledBorder(My Title)"</li>
80  * <li>border="RaisedBevelBorder"</li>
81  * </ul>
82  * </pre>
83  *
84  * @author <a HREF="mailto:wolf@paulus.com">Wolf Paulus</a>
85  * @version $Revision: 1.1 $
86  *
87  * @see javax.swing.BorderFactory
88  * @see javax.swing.border.AbstractBorder
89  * @see org.swixml.ConverterLibrary
90  */

91 public class BorderConverter implements Converter {
92   /** converter's return type */
93   public static final Class JavaDoc TEMPLATE = Border JavaDoc.class;
94
95   /** all methods the BorderFactory provides */
96   private static final Method JavaDoc[] METHODS = BorderFactory.class.getMethods();
97
98   /**
99    * Returns a <code>javax.swing Border</code>
100    * @param type <code>Class</code> not used
101    * @param attr <code>Attribute</code> value needs to provide Border type name and optional parameter
102    * @return <code>Object</code> runtime type is subclass of <code>AbstractBorder</code>
103    */

104   public Object JavaDoc convert( final Class JavaDoc type, final Attribute attr, Localizer localizer ) {
105     Border JavaDoc border = null;
106     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc( attr.getValue(), "(,)" ); // border type + parameters
107
int n = st.countTokens() - 1; // number of parameter to create a border
108
String JavaDoc borderType = st.nextToken().trim();
109     Method JavaDoc method = null;
110     ConverterLibrary cvtlib = ConverterLibrary.getInstance();
111     //
112
// Special case for single parameter construction, give priority to String Type
113
//
114

115     if (n == 1) {
116       try {
117         method = BorderFactory.class.getMethod( "create" + borderType, new Class JavaDoc[]{String JavaDoc.class} );
118       } catch (Exception JavaDoc e) {
119         // no need to do anything here.
120
}
121     }
122     for (int i = 0; method == null && i < METHODS.length; i++) {
123       if (METHODS[i].getParameterTypes().length == n && METHODS[i].getName().endsWith( borderType )) {
124         method = METHODS[i];
125
126         for (int j = 0; j < method.getParameterTypes().length; j++) {
127           if (String JavaDoc.class.equals( method.getParameterTypes()[j] )) {
128             continue;
129           }
130           if (null == cvtlib.getConverter( method.getParameterTypes()[j] )) {
131             method = null;
132             break;
133           }
134         }
135       }
136     }
137     try {
138       Object JavaDoc[] args = new Object JavaDoc[n];
139       for (int i = 0; i < n; i++) { // fill argument array
140
Converter converter = cvtlib.getConverter( method.getParameterTypes()[i] );
141         Attribute attrib = new Attribute( String JavaDoc.class.equals(converter.convertsTo()) ? "title" : "NA", st.nextToken().trim(), Attribute.CDATA_TYPE );
142         if (converter != null) {
143           args[i] = converter.convert( method.getParameterTypes()[i], attrib, localizer );
144         } else {
145           args[i] = attrib.getValue();
146         }
147       }
148       border = (Border JavaDoc) method.invoke( null, args );
149     } catch (Exception JavaDoc e) {
150        if (SwingEngine.DEBUG_MODE) System.err.println( "Couldn't create border, " + attr.getValue() + "\n" + e.getMessage() );
151     }
152     return border;
153   }
154
155   /**
156    * A <code>Converters</code> conversTo method informs about the Class type the converter
157    * is returning when its <code>convert</code> method is called
158    * @return <code>Class</code> - the Class the converter is returning when its convert method is called
159    */

160   public Class JavaDoc convertsTo() {
161     return TEMPLATE;
162   }
163 }
164
Popular Tags