001: /*
002: * Copyright (c) 2004 JETA Software, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without modification,
005: * are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JETA Software nor the names of its contributors may
015: * be used to endorse or promote products derived from this software without
016: * specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
021: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
022: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
023: * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
024: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
025: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jeta.forms.gui.common;
031:
032: import java.io.Externalizable;
033: import java.io.IOException;
034: import java.io.StringReader;
035:
036: import com.jeta.forms.gui.common.parsers.FormSpecParser;
037: import com.jeta.forms.logger.FormsLogger;
038: import com.jgoodies.forms.layout.ColumnSpec;
039: import com.jgoodies.forms.layout.RowSpec;
040:
041: /**
042: * A <code>FormSpecAdapter</code> is used to correctly encode and decode a
043: * ColumnSpec and RowSpec to/from a String. This software depends on version
044: * 1.0.2 of the FormLayout, and that version does not provide support for
045: * serializing ColumnSpec and RowSpec. Furthermore, the FormLayout does not
046: * provide the ability to encode and decode a FormSpec to a String.
047: *
048: * @author Jeff Tassin
049: */
050: public class FormSpecAdapter implements FormSpecDefinition,
051: Externalizable {
052: /**
053: * The version of this class
054: */
055: private int m_version = VERSION_ID;
056:
057: /**
058: * The current version number.
059: */
060: public static final int VERSION_ID = 1;
061:
062: /**
063: * The alignment string depending on the specification type column: LEFT,
064: * CENTER, RIGHT, RILL row: TOP, CENTER, BOTTOM, RILL
065: */
066: private String m_alignment;
067:
068: /**
069: * Type size spec for the component: CONSTANT, COMPONENT, BOUNDED
070: */
071: private String m_size_type;
072:
073: /**
074: * If the size spec is constant, this is the value for the size.
075: */
076: private double m_constant_size;
077:
078: /**
079: * If the size spec is constant, this is the units: PX, PT, DLU IN, MM, CM
080: */
081: private String m_constant_units;
082:
083: /**
084: * If the size spec is component, this is the size: MIN, PREF, DEFAULT
085: */
086: private String m_component_size;
087:
088: /**
089: * If the size spec is bounded, this is the bound: MIN, MAX
090: */
091: private String m_bounded_size;
092:
093: /**
094: * The resize behavior: NONE, GROW
095: */
096: private String m_resize;
097:
098: /**
099: * If the resize behavior is GROW, this is the resize weight (0.0-1.0)
100: */
101: private double m_resize_weight = 1.0;
102:
103: /**
104: * ctor
105: */
106: public FormSpecAdapter(ColumnSpec spec) {
107: this (spec.toString());
108: }
109:
110: /**
111: * ctor
112: */
113: public FormSpecAdapter(RowSpec spec) {
114: this (spec.toString());
115: }
116:
117: /**
118: * ctor
119: */
120: public FormSpecAdapter(String enc) {
121: try {
122: StringReader reader = new StringReader(enc);
123: FormSpecParser parser = new FormSpecParser(reader);
124: parser.parse();
125:
126: m_alignment = parser.m_alignment;
127: m_size_type = parser.m_size_type;
128: m_constant_units = parser.m_constant_units;
129: m_component_size = parser.m_component_size;
130: m_bounded_size = parser.m_bounded_size;
131: m_resize = parser.m_resize;
132:
133: try {
134: m_constant_size = Double
135: .parseDouble(parser.m_constant_size);
136: } catch (Exception e) {
137: }
138:
139: try {
140: m_resize_weight = Double
141: .parseDouble(parser.m_resize_weight);
142: } catch (Exception e) {
143: }
144: } catch (Throwable t) {
145: FormsLogger.severe(t);
146: }
147: }
148:
149: /**
150: * Properly converts a stringified version of a FormSpec to its encoded
151: * version. FormLayout has a serious problem in that it stores the encoded
152: * string in a different form than it takes in the constructor. For example,
153: * the FormLayout stores dlu sizes as dluX or dluY. However, the FormSpec
154: * constructor cannot handle this form.
155: */
156: public static String fixup(String enc) {
157: try {
158: FormSpecAdapter adapter = new FormSpecAdapter(enc);
159: return FormUtils.toEncodedString(adapter);
160: } catch (Exception e) {
161: return enc;
162: }
163: }
164:
165: /**
166: * Converts a comma-separated-value set of specs for a rows or columns
167: * definition for a layout. (e.g spec1,spec2,spec3 )
168: */
169: public static String fixupSpecs(String specs) {
170: if (specs == null) {
171: System.out
172: .println("FormSpecAdapter.fixupSpecs specs=null!");
173: FormUtils.safeAssert(false);
174: }
175: // System.out.println( "FormSpecAdapter:fixupSpecs: " + specs );
176: StringBuffer sbuff = new StringBuffer();
177: java.util.StringTokenizer st = new java.util.StringTokenizer(
178: specs, ",");
179: while (st.hasMoreTokens()) {
180: String spec = st.nextToken();
181: sbuff.append(FormSpecAdapter.fixup(spec));
182: if (st.hasMoreTokens())
183: sbuff.append(",");
184: }
185: return sbuff.toString();
186: }
187:
188: /**
189: * @return the alignment string depending on the specification type column:
190: * LEFT, CENTER, RIGHT, RILL row: TOP, CENTER, BOTTOM, RILL
191: */
192: public String getAlignment() {
193: return m_alignment;
194: }
195:
196: /**
197: * @return CONSTANT, COMPONENT, BOUNDED
198: */
199: public String getSizeType() {
200: return m_size_type;
201: }
202:
203: /**
204: * @return the units (integer) (double) PX, PT, DLU IN, MM, CM
205: */
206: public String getConstantUnits() {
207: return m_constant_units;
208: }
209:
210: /**
211: * @return the size.
212: */
213: public double getConstantSize() {
214: return m_constant_size;
215: }
216:
217: /**
218: * @return the component size: MIN, PREF, DEFAULT
219: */
220: public String getComponentSize() {
221: return m_component_size;
222: }
223:
224: /**
225: * @return the bounded size MIN, MAX
226: */
227: public String getBoundedSize() {
228: return m_bounded_size;
229: }
230:
231: /**
232: * @return true if the bounded size is MIN
233: */
234: public boolean isBoundedMinimum() {
235: return "MIN".equalsIgnoreCase(m_bounded_size);
236: }
237:
238: /**
239: * @return true if the bounded size is MAX
240: */
241: public boolean isBoundedMaximum() {
242: return "MAX".equalsIgnoreCase(m_bounded_size);
243: }
244:
245: /**
246: * @return true if the units are PX, PT, or DLU
247: */
248: public boolean isIntegralUnits() {
249: return FormUtils.isIntegralUnits(m_constant_units);
250: }
251:
252: /**
253: * @return the resize behavior NONE, GROW
254: */
255: public String getResize() {
256: return m_resize;
257: }
258:
259: /**
260: * @return true if the resize behavior is NONE
261: */
262: public boolean isResizeNone() {
263: return "NONE".equalsIgnoreCase(m_resize);
264: }
265:
266: /**
267: * @return true if the resize behavior is GROW
268: */
269: public boolean isResizeGrow() {
270: return "GROW".equalsIgnoreCase(m_resize);
271: }
272:
273: /**
274: * @return the resize weight (0.0-1.0)
275: */
276: public double getResizeWeight() {
277: return m_resize_weight;
278: }
279:
280: /**
281: * @return true if the size type is bounded
282: */
283: public boolean isBoundedSize() {
284: return "BOUNDED".equals(m_size_type);
285: }
286:
287: /**
288: * @return true if the size type is component
289: */
290: public boolean isComponentSize() {
291: return "COMPONENT".equals(m_size_type);
292: }
293:
294: /**
295: * @return true if the size type is constant
296: */
297: public boolean isConstantSize() {
298: return "CONSTANT".equals(m_size_type);
299: }
300:
301: /**
302: * Externalizable Implementation
303: */
304: public void readExternal(java.io.ObjectInput in)
305: throws ClassNotFoundException, IOException {
306: m_version = in.readInt();
307: }
308:
309: /**
310: * Externalizable Implementation
311: */
312: public void writeExternal(java.io.ObjectOutput out)
313: throws IOException {
314: out.writeInt(m_version);
315: }
316:
317: }
|