001: /*--
002: Copyright (C) 2003-2007 Wolf Paulus.
003: All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions, and the following disclaimer.
011:
012: 2. Redistributions in binary form must reproduce the above copyright
013: notice, this list of conditions, and the disclaimer that follows
014: these conditions in the documentation and/or other materials provided
015: with the distribution.
016:
017: 3. The end-user documentation included with the redistribution,
018: if any, must include the following acknowledgment:
019: "This product includes software developed by the
020: SWIXML Project (http://www.swixml.org/)."
021: Alternately, this acknowledgment may appear in the software itself,
022: if and wherever such third-party acknowledgments normally appear.
023:
024: 4. The name "Swixml" must not be used to endorse or promote products
025: derived from this software without prior written permission. For
026: written permission, please contact <info_AT_swixml_DOT_org>
027:
028: 5. Products derived from this software may not be called "Swixml",
029: nor may "Swixml" appear in their name, without prior written
030: permission from the Swixml Project Management.
031:
032: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
033: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
034: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
035: DISCLAIMED. IN NO EVENT SHALL THE SWIXML PROJECT OR ITS
036: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
037: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
038: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
039: USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
040: ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
041: OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
042: OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
043: SUCH DAMAGE.
044: ====================================================================
045:
046: This software consists of voluntary contributions made by many
047: individuals on behalf of the Swixml Project and was originally
048: created by Wolf Paulus <wolf_AT_swixml_DOT_org>. For more information
049: on the Swixml Project, please see <http://www.swixml.org/>.
050: */
051:
052: package org.swixml.layoutconverters;
053:
054: import java.awt.GridBagLayout;
055: import java.awt.LayoutManager;
056: import java.lang.reflect.Field;
057: import java.util.StringTokenizer;
058:
059: import org.jdom.Attribute;
060: import org.jdom.Element;
061: import org.swixml.LayoutConverter;
062: import org.swixml.SwingEngine;
063: import org.swixml.converters.Util;
064:
065: /**
066: * A layout converter for <code>java.awt.GridBagLayout</code>.
067: *
068: * <p><b>Examples:</b></p>
069: * <pre>
070: * <panel layout="GridBagLayout">
071: * <button>
072: * <gridbagconstraints
073: * gridx="1" gridy="2" gridwidth="3" gridheight="4" weightx="0.1" weighty="1"
074: * anchor="GridBagConstraints.NORTH" fill="GridBagConstraints.HORIZONTAL"
075: * insets="1,2,3,4" ipadx="5" ipady="6"/>
076: * </button>
077: * <button>
078: * <gridbagconstraints gridx="2" gridy="3"/>
079: * </button>
080: * </panel>
081: * </pre>
082: *
083: * <pre>
084: * <panel>
085: * <layout type="GridBagLayout" columnWidths="200, 400, 150">
086: * <button>
087: * <gridbagconstraints gridx="1" gridy="2"/>
088: * </button>
089: * <button>
090: * <gridbagconstraints gridx="2" gridy="3"/>
091: * </button>
092: * </panel>
093: * </pre>
094: *
095: * @author Karl Tauber
096: * @author <a href="mailto:wolf@wolfpaulus.com">Wolf Paulus</a>
097: */
098: public class GridBagLayoutConverter implements LayoutConverter {
099:
100: /**
101: * Returns "gridbaglayout".
102: */
103: public String getID() {
104: return "gridbaglayout";
105: }
106:
107: /**
108: * <p>Creates a GridBagLayout instance.</p>
109: *
110: * <p><b>Examples for Valid XML attribute notations:</b></p>
111: * <ul>
112: * <li><code>layout="GridBagLayout"</code></li>
113: * <li><code>layout="GridBagLayout(rowWeights(0,0,1.0,0))"</code></li>
114: * <li><code>layout="GridBagLayout(columnWeights(0.5, 0.5, 1.0, 99.9))"</code></li>
115: * <li><code>layout="GridBagLayout(columnWidths(5, 5, 10, 33))"</code></li>
116: * <li><code>layout="GridBagLayout(rowHeights(5, 5, 10, 33))"</code></li>
117: * </ul>
118: */
119: public LayoutManager convertLayoutAttribute(final Attribute attr) {
120: StringTokenizer st = new StringTokenizer(attr.getValue(), "(,)");
121: st.nextToken(); // skip layout type
122:
123: //
124: // Gridbag Layouts have some public arrays, accept one but only one.
125: // public double[] rowWeights
126: // public double[] colWeights
127: //
128: GridBagLayout lm = new GridBagLayout();
129:
130: if (st.hasMoreTokens()) {
131: try {
132: String fieldname = st.nextToken();
133: Field field = GridBagLayout.class.getField(fieldname);
134: if (field != null) {
135: Class fieldtype = field.getType();
136:
137: if (int[].class.equals(fieldtype)) {
138: field.set(lm, Util.ia(st));
139: } else if (double[].class.equals(fieldtype)) {
140: field.set(lm, Util.da(st));
141: }
142:
143: }
144: } catch (NoSuchFieldException e) {
145: if (SwingEngine.DEBUG_MODE)
146: System.err.println(e.getMessage());
147: } catch (SecurityException e) {
148: if (SwingEngine.DEBUG_MODE)
149: System.err.println(e.getMessage());
150: } catch (IllegalArgumentException e) {
151: if (SwingEngine.DEBUG_MODE)
152: System.err.println(e.getMessage());
153: } catch (IllegalAccessException e) {
154: if (SwingEngine.DEBUG_MODE)
155: System.err.println(e.getMessage());
156: }
157: }
158: return lm;
159: }
160:
161: /**
162: * <p>Creates a GridBagLayout instance.</p>
163: *
164: * <p><b>Attributes:</b></p>
165: * <ul>
166: * <li><code>columnWidths</code> (optional): The minimum column widths.</li>
167: * <li><code>rowHeights</code> (optional): The minimum row heights.</li>
168: * <li><code>columnWeights</code> (optional): The column weights.</li>
169: * <li><code>rowWeights</code> (optional): The row weights.</li>
170: * </ul>
171: *
172: * <p><b>Examples for Valid XML element notations:</b></p>
173: * <ul>
174: * <li><code><layout type="GridBagLayout"/></code></li>
175: * <li><code><layout type="GridBagLayout" columnWidths="5, 5, 10, 33" rowWeights="0,0,1.0,0"/></code></li>
176: * </ul>
177: */
178: public LayoutManager convertLayoutElement(final Element element) {
179: String columnWidths = element.getAttributeValue("columnWidths");
180: String rowHeights = element.getAttributeValue("rowHeights");
181: String columnWeights = element
182: .getAttributeValue("columnWeights");
183: String rowWeights = element.getAttributeValue("rowWeights");
184:
185: GridBagLayout lm = new GridBagLayout();
186:
187: if (columnWidths != null)
188: lm.columnWidths = Util.ia(new StringTokenizer(columnWidths,
189: ","));
190: if (rowHeights != null)
191: lm.rowHeights = Util
192: .ia(new StringTokenizer(rowHeights, ","));
193: if (columnWeights != null)
194: lm.columnWeights = Util.da(new StringTokenizer(
195: columnWeights, ","));
196: if (rowWeights != null)
197: lm.rowWeights = Util
198: .da(new StringTokenizer(rowWeights, ","));
199:
200: return lm;
201: }
202:
203: /**
204: * Returns always <code>null</code>.
205: */
206: public Object convertConstraintsAttribute(final Attribute attr) {
207: return null;
208: }
209:
210: /**
211: * Returns always <code>null</code>.
212: */
213: public Object convertConstraintsElement(final Element element) {
214: return null;
215: }
216: }
|