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.LayoutManager;
055: import java.util.StringTokenizer;
056:
057: import org.jdom.Attribute;
058: import org.jdom.Element;
059: import org.swixml.LayoutConverter;
060: import org.swixml.converters.Util;
061:
062: import com.jgoodies.forms.layout.CellConstraints;
063: import com.jgoodies.forms.layout.FormLayout;
064:
065: /**
066: * A layout converter for <code>com.jgoodies.forms.layout.FormLayout</code>.
067: *
068: * <p><b>Examples:</b></p>
069: * <pre>
070: * <panel>
071: * <layout type="FormLayout"
072: * columns="p, 3dlu, p:grow"
073: * rows="p, 3dlu, p"/>
074: *
075: * <label constraints="1,1" text="Company"/>
076: * <textfield constraints="3,1"/>
077: * <label constraints="1,3" text="Contact"/>
078: * <textfield constraints="3,3"/>
079: * </panel>
080: * </pre>
081: *
082: * <pre>
083: * <panel>
084: * <layout type="FormLayout"
085: * columns="right:max(40dlu;pref), 3dlu, d:grow, 7dlu, right:pref, 3dlu, d:grow"
086: * rows="p, 3dlu, p, 9dlu, p, 3dlu, p"
087: * columnGroups="1,5; 3,7"/>
088: *
089: * <label constraints="1,1" text="Company"/>
090: * <textfield constraints="3,1,5,1"/>
091: * <label constraints="1,3" text="Contact"/>
092: * <textfield constraints="3,3,5,1"/>
093: *
094: * <label constraints="1,5" text="PTI [kW]"/>
095: * <textfield constraints="3,5"/>
096: * <label constraints="5,5" text="Power [kW]"/>
097: * <textfield constraints="7,5"/>
098: * <label constraints="1,7" text="R [mm]"/>
099: * <textfield constraints="3,7"/>
100: * <label constraints="5,7" text="D [mm]"/>
101: * <textfield constraints="7,7"/>
102: * </panel>
103: * </pre>
104: *
105: * @author Karl Tauber
106: */
107: public class FormLayoutConverter implements LayoutConverter {
108:
109: /**
110: * Returns "formlayout".
111: */
112: public String getID() {
113: return "formlayout";
114: }
115:
116: /**
117: * Returns always <code>null</code>.
118: */
119: public LayoutManager convertLayoutAttribute(final Attribute attr) {
120: return null;
121: }
122:
123: /**
124: * <p>Creates a FormLayout instance.</p>
125: *
126: * <p><b>Attributes:</b></p>
127: * <ul>
128: * <li><code>columns</code> (required): The column specifications as documented in JGoodies FormLayout.</li>
129: * <li><code>row</code> (required): The row specifications as documented in JGoodies FormLayout.</li>
130: * <li><code>columnGroups</code> (optional): The column groups, where each column
131: * in a group gets the same group wide width. Groups are separated by semicolons,
132: * column indices in a group are separated by colons. E.g. "1,5; 3,7,9" defines
133: * two groups, where first group contains columns 1 and 5; and second group
134: * contains columns 3, 7 and 9. Note that column indices are 1-based.</li>
135: * <li><code>rowGroups</code> (optional): The row groups, where each row
136: * in a group gets the same group wide height. Groups are separated by semicolons,
137: * row indices in a group are separated by colons. E.g. "1,5; 3,7,9" defines
138: * two groups, where first group contains rows 1 and 5; and second group
139: * contains rows 3, 7 and 9. Note that row indices are 1-based.</li>
140: * </ul>
141: *
142: * <p><b>Examples for Valid XML element notations:</b></p>
143: * <ul>
144: * <li><code><layout type="FormLayout" columns="p, 3dlu, p" rows="p, 3dlu, p"/></code></li>
145: * <li><code><layout type="FormLayout" columns="p, 3dlu, p, 3dlu, p, 3dlu, p" rows="p, 3dlu, p"
146: * columnGroups="1,5; 3,7" rowGroups="1,3"/></code></li>
147: * </ul>
148: */
149: public LayoutManager convertLayoutElement(final Element element) {
150: String encodedColumnSpecs = element
151: .getAttributeValue("columns");
152: String encodedRowSpecs = element.getAttributeValue("rows");
153: int[][] columnGroupIndices = convertGroupIndices(element
154: .getAttributeValue("columnGroups"));
155: int[][] rowGroupIndices = convertGroupIndices(element
156: .getAttributeValue("rowGroups"));
157:
158: FormLayout lm = new FormLayout(encodedColumnSpecs,
159: encodedRowSpecs);
160: if (columnGroupIndices != null)
161: lm.setColumnGroups(columnGroupIndices);
162: if (rowGroupIndices != null)
163: lm.setRowGroups(rowGroupIndices);
164:
165: return lm;
166: }
167:
168: /**
169: * <p>Creates a CellConstraints instance.</p>
170: *
171: * <p>Allowed syntaxes of attribute value:</p>
172: * <ul>
173: * <li><code>"x, y"</code></li>
174: * <li><code>"x, y, w, h"</code></li>
175: * <li><code>"x, y, hAlign, vAlign"</code></li>
176: * <li><code>"x, y, w, h, hAlign, vAlign"</code></li>
177: * </ul>
178: * <p>See JGoodies FormLayout for details.</p>
179: *
180: * <p><b>Examples for Valid XML attribute notations:</b></p>
181: * <ul>
182: * <li><code>constraints="1, 3"</code></li>
183: * <li><code>constraints="1, 3, 2, 1"</code></li>
184: * <li><code>constraints="1, 3, left, bottom"</code></li>
185: * <li><code>constraints="1, 3, 2, 1, l, b"</code></li>
186: * </ul>
187: */
188: public Object convertConstraintsAttribute(final Attribute attr) {
189: return new CellConstraints(attr.getValue());
190: }
191:
192: /**
193: * Returns always <code>null</code>.
194: */
195: public Object convertConstraintsElement(final Element element) {
196: return null;
197: }
198:
199: private int[][] convertGroupIndices(final String groups) {
200: if (groups == null)
201: return null;
202: StringTokenizer st = new StringTokenizer(groups, ";");
203: int[][] groupIndices = new int[st.countTokens()][];
204: int i = 0;
205: while (st.hasMoreTokens()) {
206: String group = st.nextToken();
207: groupIndices[i++] = Util
208: .ia(new StringTokenizer(group, ","));
209: }
210: return groupIndices;
211: }
212: }
|