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.awt.Component;
033: import java.awt.Container;
034:
035: import javax.swing.JComponent;
036: import javax.swing.JMenu;
037:
038: import com.jeta.open.registry.JETARegistry;
039: import com.jgoodies.forms.layout.CellConstraints;
040:
041: /**
042: * Utility methods for the form builder.
043: *
044: * @author Jeff Tassin
045: */
046: public class FormUtils {
047: /**
048: * temporary - for debugging only
049: */
050: private static int m_count = 0;
051:
052: static char[] letters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
053: 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
054: 'U', 'V', 'W', 'X', 'Y', 'Z' };
055:
056: /**
057: * Creates a unique 8 digit UI.
058: */
059: static String _createUID() {
060: java.rmi.server.UID uid = new java.rmi.server.UID();
061: // we need to strip off any non alphanumeric characters because we use
062: // the UID as
063: // file and directory names
064: StringBuffer sbuff = new StringBuffer(uid.toString());
065: for (int index = 0; index < sbuff.length(); index++) {
066: char c = sbuff.charAt(index);
067: if ((c < '0') || (c > '9' && c < 'A')
068: || (c > 'Z' && c < 'a') || (c > 'z')) {
069: int cindex = (int) (Math.random() * 26.0);
070: sbuff.setCharAt(index, letters[cindex]);
071: }
072: }
073:
074: char c = sbuff.charAt(0);
075: if (c < 65 || c > 90) {
076: int cindex = (int) (Math.random() * 26.0);
077: sbuff.setCharAt(0, letters[cindex]);
078: }
079: return sbuff.toString();
080: }
081:
082: /**
083: * Creates a unique id in the application.
084: *
085: * @return a unique id used to identify forms in the application.
086: */
087: public static String createUID() {
088: if (isDebug()) {
089: /**
090: * If we are debugging, return a user-friendly string that is easy
091: * to read.
092: */
093: m_count++;
094: java.util.Calendar c = java.util.Calendar.getInstance();
095: java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
096: "EEE, MMM d, yyyy - HH:mm:ss");
097: return String.valueOf(m_count) + "."
098: + format.format(c.getTime());
099: } else {
100: return _createUID();
101: }
102: }
103:
104: /**
105: * Converts any / or \ characters to the correct path separator character
106: * for the current operating system.
107: */
108: public static String fixPath(String path) {
109: if (path == null)
110: return null;
111:
112: char sep = '/';
113:
114: if (java.io.File.separatorChar == '/')
115: sep = '\\';
116:
117: return path.replace(sep, java.io.File.separatorChar);
118: }
119:
120: /**
121: * @return a reasonable default size for the given units
122: */
123: public static String getReasonableSize(String units) {
124: if ("DLU".equalsIgnoreCase(units))
125: return "12";
126: else if ("PX".equalsIgnoreCase(units))
127: return "24";
128: else if ("PT".equalsIgnoreCase(units))
129: return "24";
130: else if ("IN".equalsIgnoreCase(units))
131: return "0.5";
132: else if ("MM".equalsIgnoreCase(units))
133: return "10";
134: else if ("CM".equalsIgnoreCase(units))
135: return "1";
136: else {
137: safeAssert(false);
138: return "10";
139: }
140: }
141:
142: /**
143: * @return true if the environment is currently in design mode or runtime
144: * mode.
145: */
146: public static boolean isDesignMode() {
147: Boolean result = (Boolean) JETARegistry
148: .lookup("AbeilleForms.designMode");
149: return (Boolean.TRUE.equals(result));
150: }
151:
152: /**
153: * Debugging flag
154: */
155: public static boolean isDebug() {
156: try {
157: String result = System.getProperty("jeta1.debug");
158: return (result != null && result.equals("true"));
159: } catch (Exception e) {
160:
161: }
162: return false;
163: }
164:
165: /**
166: * Returns true if the forms are running outside the designer. That is, the
167: * forms libarary is being used by some other application other than the
168: * designer. If this value is true, then isDesignMode must be false.
169: */
170: public static boolean isRuntime() {
171: Boolean result = (Boolean) JETARegistry
172: .lookup("AbeilleForms.runTime");
173: if (result == null)
174: return true;
175: return (Boolean.TRUE.equals(result));
176: }
177:
178: /**
179: * @return true if the units are PX, PT, or DLU
180: */
181: public static boolean isIntegralUnits(String units) {
182: return ("PX".equalsIgnoreCase(units)
183: || "PT".equalsIgnoreCase(units) || "DLU"
184: .equalsIgnoreCase(units));
185: }
186:
187: /**
188: * @return true if the units are valid
189: */
190: public static boolean isValidUnits(String units) {
191: return ("DLU".equalsIgnoreCase(units)
192: || "PX".equalsIgnoreCase(units)
193: || "PT".equalsIgnoreCase(units)
194: || "IN".equalsIgnoreCase(units)
195: || "MM".equalsIgnoreCase(units) || "CM"
196: .equalsIgnoreCase(units));
197: }
198:
199: /**
200: * Sets the design mode flag for the environment. This should not be called
201: * outside of the designer. This flag changes momentarily to false when the
202: * designer shows a preview of a form.
203: *
204: * @param bdesign
205: * true if the environment should be set to to design mode. false
206: * if runtime mode.
207: */
208: public static void setDesignMode(boolean bdesign) {
209: JETARegistry.rebind("AbeilleForms.designMode", Boolean
210: .valueOf(bdesign));
211: }
212:
213: public static String fromAlignment(CellConstraints.Alignment align) {
214: if (align == CellConstraints.DEFAULT)
215: return "DEFAULT";
216: else if (align == CellConstraints.FILL)
217: return "FILL";
218: else if (align == CellConstraints.TOP)
219: return "TOP";
220: else if (align == CellConstraints.BOTTOM)
221: return "BOTTOM";
222: else if (align == CellConstraints.CENTER)
223: return "CENTER";
224: else if (align == CellConstraints.LEFT)
225: return "LEFT";
226: else if (align == CellConstraints.RIGHT)
227: return "RIGHT";
228: else
229: return "DEFAULT";
230: }
231:
232: /**
233: * Converts a string to an alignment value
234: */
235: public static CellConstraints.Alignment toAlignment(String val) {
236: if (val.equalsIgnoreCase("DEFAULT"))
237: return CellConstraints.DEFAULT;
238: else if (val.equalsIgnoreCase("FILL"))
239: return CellConstraints.FILL;
240: else if (val.equalsIgnoreCase("TOP"))
241: return CellConstraints.TOP;
242: else if (val.equalsIgnoreCase("BOTTOM"))
243: return CellConstraints.BOTTOM;
244: else if (val.equalsIgnoreCase("CENTER"))
245: return CellConstraints.CENTER;
246: else if (val.equalsIgnoreCase("LEFT"))
247: return CellConstraints.LEFT;
248: else if (val.equalsIgnoreCase("RIGHT"))
249: return CellConstraints.RIGHT;
250: else {
251: safeAssert(false);
252: return CellConstraints.DEFAULT;
253: }
254: }
255:
256: /**
257: * @return an encode string that represents the constant size params:
258: * <integer>integralUnit | <double>doubleUnit
259: */
260: public static String toConstantSize(FormSpecDefinition fspec) {
261: StringBuffer sbuff = new StringBuffer();
262: if (isIntegralUnits(fspec.getConstantUnits())) {
263: sbuff.append(Math.round(fspec.getConstantSize()));
264: } else {
265: java.text.DecimalFormat format = new java.text.DecimalFormat(
266: "###0.0");
267: String sz = format.format(fspec.getConstantSize());
268: /** temporary fix to handle european locales */
269: sz = sz.replace(',', '.');
270: sbuff.append(sz);
271: }
272: sbuff.append(fspec.getConstantUnits());
273: return sbuff.toString();
274: }
275:
276: /**
277: * @return a property encoded string for this form spec
278: */
279: public static String toEncodedString(FormSpecDefinition fspec) {
280: StringBuffer sbuff = new StringBuffer();
281: sbuff.append(fspec.getAlignment());
282: sbuff.append(":");
283: if ("CONSTANT".equalsIgnoreCase(fspec.getSizeType())) {
284: sbuff.append(toConstantSize(fspec));
285: } else if ("COMPONENT".equalsIgnoreCase(fspec.getSizeType())) {
286: sbuff.append(fspec.getComponentSize());
287: } else if ("BOUNDED".equalsIgnoreCase(fspec.getSizeType())) {
288: sbuff.append(fspec.getBoundedSize());
289: sbuff.append("(");
290: sbuff.append(toConstantSize(fspec));
291: sbuff.append(";");
292: sbuff.append(fspec.getComponentSize());
293: sbuff.append(")");
294: } else {
295: safeAssert(false);
296: }
297: sbuff.append(":");
298: sbuff.append(fspec.getResize());
299: if ("GROW".equalsIgnoreCase(fspec.getResize())) {
300: sbuff.append("(");
301: double weight = fspec.getResizeWeight();
302: if (weight > 1.0)
303: weight = 1.0;
304:
305: java.text.DecimalFormat format = new java.text.DecimalFormat(
306: "0.0");
307: String fw = format.format(weight);
308: /** temporary fix to handle european locales */
309: fw = fw.replace(',', '.');
310:
311: sbuff.append(fw);
312: sbuff.append(")");
313:
314: }
315: return sbuff.toString();
316: }
317:
318: /**
319: * Utility method that updates the look and feel for all components in a
320: * container.
321: */
322: public static void updateLookAndFeel(Component c) {
323: if (c == null)
324: return;
325:
326: c.invalidate();
327: c.validate();
328: c.repaint();
329:
330: if (c instanceof JComponent) {
331: ((JComponent) c).updateUI();
332: }
333:
334: Component[] children = null;
335: if (c instanceof JMenu) {
336: children = ((JMenu) c).getMenuComponents();
337: } else if (c instanceof Container) {
338: children = ((Container) c).getComponents();
339: }
340: if (children != null) {
341: for (int i = 0; i < children.length; i++) {
342: updateLookAndFeel(children[i]);
343: }
344: }
345: }
346:
347: public static void safeAssert(boolean assertvalue) {
348: if (FormUtils.isDebug()) {
349: assert (assertvalue);
350: }
351: }
352: }
|