KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*--
2  $Id: LayoutConverter.java,v 1.1 2004/03/01 07:56:01 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.Localizer;
59 import org.swixml.SwingEngine;
60
61 import java.awt.*;
62 import java.lang.reflect.Field JavaDoc;
63 import java.util.StringTokenizer JavaDoc;
64
65 /**
66  * The <code>LayoutConverter</code> class defines a converter that creates LayoutManager objects
67  * based on a provided String.
68  * <h3>Examples for Valid XML attribute notations:</h3>
69  * <pre>
70  * <p><b>FlowLayout</b><br>can be created with all 3 construtors:<br>
71  * <ul>
72  * <li>FlowLayout()</li>
73  * <li>FlowLayout(int align)</li>
74  * <li>FlowLayout(int align, int hgap, int vgap)</li>
75  * </ul>
76  *
77  * <ul>
78  * <li>BorderLayout()</li>
79  * <li>BorderLayout(int hgap, int vgap)</li>
80  * </ul>
81  *
82  * <ul>
83  * <li>GridBagLayout()</li>
84  * <li>GridBagLayout(rowWeights(0,0,1.0,0))</li>
85  * <li>GridBagLayout(colWeights(0.5, 0.5, 1.0, 99.9))</li>
86  * <li>GridBagLayout(columnWidths(5, 5, 10, 33))</li>
87  * <li>GridBagLayout(rowHeights(5, 5, 10, 33))</li>
88  * </ul>
89  *
90  * </pre>
91  *
92  * @author <a HREF="mailto:wolf@paulus.com">Wolf Paulus</a>
93  *
94  * @version $Revision: 1.1 $
95  *
96  * @see java.awt.LayoutManager
97  * @see org.swixml.ConverterLibrary
98
99  */

100 public class LayoutConverter implements Converter {
101   /** converter's return type */
102   public static final Class JavaDoc TEMPLATE = LayoutManager.class;
103
104   public static final String JavaDoc FLOW_LAYOUT = "flowlayout";
105   public static final String JavaDoc BORDER_LAYOUT = "borderlayout";
106   public static final String JavaDoc GRID_LAYOUT = "gridlayout";
107   public static final String JavaDoc GRIDBAG_LAYOUT = "gridbaglayout";
108   public static final String JavaDoc CARD_LAYOUT = "cardlayout";
109
110   /**
111    * Convert the specified input object into an output object of the
112    * specified type.
113    * <pre>
114    * <p><b>FlowLayout</b><br>can be created with all 3 construtors:<br>
115    * <ul>
116    * <li>FlowLayout()</li>
117    * <li>FlowLayout(int align)</li>
118    * <li>FlowLayout(int align, int hgap, int vgap)</li>
119    * </ul>
120    *
121    * <ul>
122    * <li>BorderLayout()</li>
123    * <li>BorderLayout(int hgap, int vgap)</li>
124    * </ul>
125    *
126    * <ul>
127    * <li>BoxLayout: see Box tag</li>
128    * </ul>
129    *
130    * <ul>
131    * <li>GridLayout()</li>
132    * <li>GridLayout(int rows, int cols)</li>
133    * <li>GridLayout(int rows, int cols, int hgap, int vgap) </li>
134    * </ul>
135    *
136    * <ul>
137    * <li>GridBagLayout()</li>
138    * <li>GridBagLayout(rowWeights(0,0,1.0,0))</li>
139    * <li>GridBagLayout(colWeights(0.5, 0.5, 1.0, 99.9))</li>
140    * <li>GridBagLayout(columnWidths(5, 5, 10, 33))</li>
141    * <li>GridBagLayout(rowHeights(5, 5, 10, 33))</li>
142    * </ul>
143    *
144    * <ul>
145    * <li>CardLayout() - Creates a new card layout with gaps of size zero.</li>
146    * <li>CardLayout(int hgap, int vgap) - Creates a new card layout with the specified horizontal and vertical gaps.</li>
147    * </ul>
148    * Here is how to access the card layout manager of a component installed by SwixML
149    * (CardLayout)((Container)swingEngine.find("id_of_my_CLed_comp")).getLayout()
150    *
151    * </pre>
152    * @param type <code>Class</code> not used
153    * @param attr <code>Attribute</code> value is needed for conversion
154    * @return <code>Object</code>
155    */

156   public Object JavaDoc convert( final Class JavaDoc type, final Attribute attr, final Localizer localizer ) {
157     LayoutManager lm = null;
158     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc( attr.getValue(), "(,)" );
159     String JavaDoc s = st.nextToken().trim(); // here, s should contain the LayoutManager's name
160
if (s != null) {
161       s = s.toLowerCase();
162       if (s.equals( LayoutConverter.FLOW_LAYOUT )) {
163         //
164
// FLOW_LAYOUT
165
//
166
try {
167           if (st.hasMoreTokens()) {
168             //
169
// First FlowLayout parameter might be a pre-defined constant's name
170
//
171
Object JavaDoc o = PrimitiveConverter.conv( null, new Attribute( "NA", st.nextToken() ), localizer );
172             int[] para = Util.ia( st );
173             //
174
// Remaining paramters should be integer values
175
//
176
if (para.length < 2)
177               lm = new FlowLayout( Integer.valueOf( o.toString() ).intValue() );
178             else
179               lm = new FlowLayout( Integer.valueOf( o.toString() ).intValue(), para[ 0 ], para[ 1 ] );
180           }
181         } catch (Exception JavaDoc e) {
182         }
183         if (lm == null) {
184           lm = new FlowLayout();
185         }
186       } else if (s.equals( LayoutConverter.BORDER_LAYOUT )) {
187         //
188
// BORDER_LAYOUT
189
//
190
int[] para = Util.ia( st );
191         if (para.length < 2)
192           lm = new BorderLayout();
193         else
194           lm = new BorderLayout( para[ 0 ], para[ 1 ] );
195       } else if (s.equals( LayoutConverter.GRID_LAYOUT )) {
196         //
197
// GRID_LAYOUT
198
//
199
int[] para = Util.ia( st );
200         if (4 <= para.length)
201           lm = new GridLayout( para[ 0 ], para[ 1 ], para[ 2 ], para[ 3 ] );
202         else if (2 <= para.length)
203           lm = new GridLayout( para[ 0 ], para[ 1 ] );
204         else
205           lm = new GridLayout();
206       } else if (s.equals( LayoutConverter.CARD_LAYOUT )) {
207         //
208
// CARD_LAYOUT
209
//
210
//
211
int[] para = Util.ia( st );
212         if (para.length < 2)
213           lm = new CardLayout();
214         else
215           lm = new CardLayout( para[ 0 ], para[ 1 ] );
216       } else if (s.equals( LayoutConverter.GRIDBAG_LAYOUT )) {
217         //
218
// Gridbag Layouts have some public arrays, accept one but only one.
219
// public double[] rowWeights
220
// public double[] colWeights
221
//
222
lm = new GridBagLayout();
223
224         if (st.hasMoreTokens()) {
225           try {
226             String JavaDoc fieldname = st.nextToken();
227             Field JavaDoc field = GridBagLayout.class.getField( fieldname );
228             if (field != null) {
229               Class JavaDoc fieldtype = field.getType();
230
231               if (int[].class.equals( fieldtype )) {
232                 field.set( lm, Util.ia( st ) );
233               } else if (double[].class.equals( fieldtype )) {
234                 field.set( lm, Util.da( st ) );
235               }
236
237             }
238           } catch (NoSuchFieldException JavaDoc e) {
239             if (SwingEngine.DEBUG_MODE)
240               System.err.println( e.getMessage() );
241           } catch (SecurityException JavaDoc e) {
242             if (SwingEngine.DEBUG_MODE)
243               System.err.println( e.getMessage() );
244           } catch (IllegalArgumentException JavaDoc e) {
245             if (SwingEngine.DEBUG_MODE)
246               System.err.println( e.getMessage() );
247           } catch (IllegalAccessException JavaDoc e) {
248             if (SwingEngine.DEBUG_MODE)
249               System.err.println( e.getMessage() );
250           }
251         }
252       }
253     }
254     return lm;
255   }
256
257
258   /**
259    * A <code>Converters</code> conversTo method informs about the Class type the converter
260    * is returning when its <code>convert</code> method is called
261    * @return <code>Class</code> - the Class the converter is returning when its convert method is called
262    */

263   public Class JavaDoc convertsTo() {
264     return TEMPLATE;
265   }
266 }
267
Popular Tags