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.BorderLayout;
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.converters.Util;
063:
064: /**
065: * A layout converter for <code>java.awt.BorderLayout</code>.
066: *
067: * <p><b>Examples:</b></p>
068: * <pre>
069: * <panel layout="BorderLayout">
070: * <panel constraints="BorderLayout.NORTH" />
071: * <panel constraints="BorderLayout.CENTER" />
072: * </panel>
073: * </pre>
074: *
075: * <pre>
076: * <panel layout="BorderLayout(10,20)">
077: * <panel constraints="NORTH" />
078: * <panel constraints="CENTER" />
079: * </panel>
080: * </pre>
081: *
082: * <pre>
083: * <panel>
084: * <layout type="BorderLayout" hgap="10" vgap="20"/>
085: * <panel constraints="NORTH" />
086: * <panel constraints="CENTER" />
087: * </panel>
088: * </pre>
089: *
090: * @author Karl Tauber
091: * @author <a href="mailto:wolf@wolfpaulus.com">Wolf Paulus</a>
092: */
093: public class BorderLayoutConverter implements LayoutConverter {
094:
095: /**
096: * Returns "borderlayout".
097: */
098: public String getID() {
099: return "borderlayout";
100: }
101:
102: /**
103: * <p>Creates a BorderLayout instance.</p>
104: *
105: * <p><b>Examples for Valid XML attribute notations:</b></p>
106: * <ul>
107: * <li><code>layout="BorderLayout"</code></li>
108: * <li><code>layout="BorderLayout(int hgap, int vgap)"</code></li>
109: * </ul>
110: */
111: public LayoutManager convertLayoutAttribute(final Attribute attr) {
112: StringTokenizer st = new StringTokenizer(attr.getValue(), "(,)");
113: st.nextToken(); // skip layout type
114:
115: int[] para = Util.ia(st);
116: if (para.length < 2)
117: return new BorderLayout();
118: else
119: return new BorderLayout(para[0], para[1]);
120: }
121:
122: /**
123: * <p>Creates a BorderLayout instance.</p>
124: *
125: * <p><b>Attributes:</b></p>
126: * <ul>
127: * <li><code>hgap</code> (optional): The horizontal gap.</li>
128: * <li><code>vgap</code> (optional): The vertical gap.</li>
129: * </ul>
130: *
131: * <p><b>Examples for Valid XML element notations:</b></p>
132: * <ul>
133: * <li><code><layout type="BorderLayout"/></code></li>
134: * <li><code><layout type="BorderLayout" hgap="10" vgap="20"/></code></li>
135: * </ul>
136: */
137: public LayoutManager convertLayoutElement(final Element element) {
138: int hgap = Util.getInteger(element, "hgap", 0);
139: int vgap = Util.getInteger(element, "vgap", 0);
140: return new BorderLayout(hgap, vgap);
141: }
142:
143: /**
144: * Converts BorderLayout constraints.
145: *
146: * <p><b>Examples for Valid XML attribute notations:</b></p>
147: * <ul>
148: * <li><code>constraints="BorderLayout.CENTER"</code></li>
149: * <li><code>constraints="BorderLayout.NORTH"</code></li>
150: * <li><code>constraints="EAST"</code></li>
151: * </ul>
152: */
153: public Object convertConstraintsAttribute(final Attribute attr) {
154: String value = attr.getValue();
155: Field[] fields = BorderLayout.class.getFields();
156: for (int i = 0; i < fields.length; i++) {
157: if (value.endsWith(fields[i].getName())) {
158: try {
159: return fields[i].get(BorderLayout.class);
160: } catch (Exception e) {
161: }
162: break;
163: }
164: }
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: }
|