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.GridLayout;
055: import java.awt.LayoutManager;
056: import java.util.StringTokenizer;
057:
058: import org.jdom.Attribute;
059: import org.jdom.Element;
060: import org.swixml.LayoutConverter;
061: import org.swixml.converters.Util;
062:
063: /**
064: * A layout converter for <code>java.awt.GridLayout</code>.
065: *
066: * <p><b>Examples:</b></p>
067: * <pre>
068: * <panel layout="GridLayout">
069: * <panel ... />
070: * <panel ... />
071: * </panel>
072: * </pre>
073: *
074: * <pre>
075: * <panel layout="GridLayout(2,4)">
076: * ...
077: * </panel>
078: * </pre>
079: *
080: * <pre>
081: * <panel layout="GridLayout(2,4,10,20)">
082: * ...
083: * </panel>
084: * </pre>
085: *
086: * <pre>
087: * <panel>
088: * <layout type="GridLayout" rows="2" columns="4"/>
089: * ...
090: * </panel>
091: * </pre>
092: *
093: * <pre>
094: * <panel>
095: * <layout type="GridLayout" rows="2" columns="4" hgap="10" vgap="20"/>
096: * ...
097: * </panel>
098: * </pre>
099: *
100: * @author Karl Tauber
101: * @author <a href="mailto:wolf@wolfpaulus.com">Wolf Paulus</a>
102: */
103: public class GridLayoutConverter implements LayoutConverter {
104:
105: /**
106: * Returns "gridlayout".
107: */
108: public String getID() {
109: return "gridlayout";
110: }
111:
112: /**
113: * <p>Creates a GridLayout instance.</p>
114: *
115: * <p><b>Examples for Valid XML attribute notations:</b></p>
116: * <ul>
117: * <li><code>layout="GridLayout"</code></li>
118: * <li><code>layout="GridLayout(int rows, int cols)"</code></li>
119: * <li><code>layout="GridLayout(int rows, int cols, int hgap, int vgap)"</code></li>
120: * </ul>
121: */
122: public LayoutManager convertLayoutAttribute(final Attribute attr) {
123: StringTokenizer st = new StringTokenizer(attr.getValue(), "(,)");
124: st.nextToken(); // skip layout type
125:
126: int[] para = Util.ia(st);
127: if (4 <= para.length)
128: return new GridLayout(para[0], para[1], para[2], para[3]);
129: else if (2 <= para.length)
130: return new GridLayout(para[0], para[1]);
131: else
132: return new GridLayout();
133: }
134:
135: /**
136: * <p>Creates a GridLayout instance.</p>
137: *
138: * <p><b>Attributes:</b></p>
139: * <ul>
140: * <li><code>rows</code> (optional): The number of rows.</li>
141: * <li><code>columns</code> (optional): The number of columns.</li>
142: * <li><code>hgap</code> (optional): The horizontal gap.</li>
143: * <li><code>vgap</code> (optional): The vertical gap.</li>
144: * </ul>
145: *
146: * <p><b>Examples for Valid XML element notations:</b></p>
147: * <ul>
148: * <li><code><layout type="GridLayout"/></code></li>
149: * <li><code><layout type="GridLayout" rows="4" columns="5"/></code></li>
150: * <li><code><layout type="GridLayout" rows="2" columns="4" hgap="10" vgap="20"/></code></li>
151: * </ul>
152: */
153: public LayoutManager convertLayoutElement(final Element element) {
154: int rows = Util.getInteger(element, "rows", 1);
155: int cols = Util.getInteger(element, "columns", 0);
156: int hgap = Util.getInteger(element, "hgap", 0);
157: int vgap = Util.getInteger(element, "vgap", 0);
158: return new GridLayout(rows, cols, hgap, vgap);
159: }
160:
161: /**
162: * Returns always <code>null</code>.
163: */
164: public Object convertConstraintsAttribute(final Attribute attr) {
165: return null;
166: }
167:
168: /**
169: * Returns always <code>null</code>.
170: */
171: public Object convertConstraintsElement(final Element element) {
172: return null;
173: }
174: }
|