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.FlowLayout;
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.PrimitiveConverter;
062: import org.swixml.converters.Util;
063:
064: /**
065: * A layout converter for <code>java.awt.FlowLayout</code>.
066: *
067: * <p><b>Examples:</b></p>
068: * <pre>
069: * <panel layout="FlowLayout">
070: * <panel ... />
071: * <panel ... />
072: * </panel>
073: * </pre>
074: *
075: * <pre>
076: * <panel layout="FlowLayout(FlowLayout.RIGHT)">
077: * ...
078: * </panel>
079: * </pre>
080: *
081: * <pre>
082: * <panel layout="FlowLayout(FlowLayout.LEFT, 1, 2)">
083: * ...
084: * </panel>
085: * </pre>
086: *
087: * <pre>
088: * <panel>
089: * <layout type="FlowLayout" alignment="FlowLayout.RIGHT"/>
090: * ...
091: * </panel>
092: * </pre>
093: *
094: * <pre>
095: * <panel>
096: * <layout type="FlowLayout" alignment="FlowLayout.LEFT" hgap="10" vgap="20"/>
097: * ...
098: * </panel>
099: * </pre>
100: *
101: * @author Karl Tauber
102: * @author <a href="mailto:wolf@wolfpaulus.com">Wolf Paulus</a>
103: */
104: public class FlowLayoutConverter implements LayoutConverter {
105:
106: /**
107: * Returns "flowlayout".
108: */
109: public String getID() {
110: return "flowlayout";
111: }
112:
113: /**
114: * <p>Creates a FlowLayout instance.</p>
115: *
116: * <p><b>Examples for Valid XML attribute notations:</b></p>
117: * <ul>
118: * <li><code>layout="FlowLayout"</code></li>
119: * <li><code>layout="FlowLayout(int align)"</code></li>
120: * <li><code>layout="FlowLayout(int align, int hgap, int vgap)"</code></li>
121: * </ul>
122: */
123: public LayoutManager convertLayoutAttribute(final Attribute attr) {
124: StringTokenizer st = new StringTokenizer(attr.getValue(), "(,)");
125: st.nextToken(); // skip layout type
126:
127: try {
128: if (st.hasMoreTokens()) {
129: //
130: // First FlowLayout parameter might be a pre-defined constant's name
131: //
132: Object o = PrimitiveConverter.conv(null, new Attribute(
133: "NA", st.nextToken()), null);
134: int[] para = Util.ia(st);
135: //
136: // Remaining paramters should be integer values
137: //
138: if (para.length < 2)
139: return new FlowLayout(Integer.valueOf(o.toString())
140: .intValue());
141: else
142: return new FlowLayout(Integer.valueOf(o.toString())
143: .intValue(), para[0], para[1]);
144: }
145: } catch (Exception e) {
146: }
147: return new FlowLayout();
148: }
149:
150: /**
151: * <p>Creates a FlowLayout instance.</p>
152: *
153: * <p><b>Attributes:</b></p>
154: * <ul>
155: * <li><code>alignment</code> (optional): The horizontal alignment.</li>
156: * <li><code>hgap</code> (optional): The horizontal gap.</li>
157: * <li><code>vgap</code> (optional): The vertical gap.</li>
158: * </ul>
159: *
160: * <p><b>Examples for Valid XML element notations:</b></p>
161: * <ul>
162: * <li><code><layout type="FlowLayout"/></code></li>
163: * <li><code><layout type="FlowLayout" alignment="FlowLayout.LEFT"/></code></li>
164: * <li><code><layout type="FlowLayout" alignment="FlowLayout.LEFT" hgap="10" vgap="20"/></code></li>
165: * </ul>
166: */
167: public LayoutManager convertLayoutElement(final Element element) {
168: int align = FlowLayout.CENTER;
169: String value = element.getAttributeValue("alignment");
170: if (value != null) {
171: try {
172: Object o = PrimitiveConverter.conv(null, new Attribute(
173: "NA", value), null);
174: align = Integer.valueOf(o.toString()).intValue();
175: } catch (Exception ex) {
176: }
177: }
178: int hgap = Util.getInteger(element, "hgap", 5);
179: int vgap = Util.getInteger(element, "vgap", 5);
180: return new FlowLayout(align, hgap, vgap);
181: }
182:
183: /**
184: * Returns always <code>null</code>.
185: */
186: public Object convertConstraintsAttribute(final Attribute attr) {
187: return null;
188: }
189:
190: /**
191: * Returns always <code>null</code>.
192: */
193: public Object convertConstraintsElement(final Element element) {
194: return null;
195: }
196: }
|