001: /*
002: * Beryl - A web platform based on XML, XSLT and Java
003: * This file is part of the Beryl XML GUI
004: *
005: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011:
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
020: */
021:
022: package org.beryl.gui;
023:
024: import java.awt.Component;
025: import java.awt.EventQueue;
026: import java.awt.Toolkit;
027: import java.io.PrintWriter;
028: import java.io.Writer;
029: import java.lang.ref.WeakReference;
030: import java.net.URL;
031: import java.util.ArrayList;
032: import java.util.Iterator;
033: import java.util.Locale;
034: import java.util.Properties;
035:
036: import javax.swing.LookAndFeel;
037: import javax.swing.SwingUtilities;
038: import javax.swing.UIManager;
039:
040: import org.apache.log4j.PropertyConfigurator;
041:
042: /**
043: * Miscellaneous functions to get the XML GUI ready for use and to perform common tasks
044: */
045: public class GUIUtils {
046: public static final String KUNSTSTOFF_LNF = "com.incors.plaf.kunststoff.KunststoffLookAndFeel";
047: private static ArrayList widgets = new ArrayList();
048:
049: private static class ExceptionWriter extends Writer {
050: public String exception = new String();
051:
052: /**
053: * Overwritten IO function (from <tt>java.io.Writer</tt>)
054: */
055: public void close() {
056: }
057:
058: /**
059: * Overwritten IO function (from <tt>java.io.Writer</tt>)
060: */
061: public void flush() {
062: }
063:
064: /**
065: * Overwritten IO function (from <tt>java.io.Writer</tt>)
066: */
067: public void write(String string) {
068: }
069:
070: /**
071: * Overwritten IO function (from <tt>java.io.Writer</tt>)
072: */
073: public void write(char[] cbuf, int off, int len) {
074: String str = new String(cbuf, off, len);
075: exception += str + "\n";
076: }
077:
078: };
079:
080: /**
081: * Convert a <tt>Throwable</tt> into a <tt>String</tt>
082: * @param throwable The <tt>Throwable</tt> to convert
083: * @return A <tt>String</tt> describing the <tt>Throwable</tt>
084: */
085: public static String getStringForThrowable(Throwable throwable) {
086: ExceptionWriter writer = new ExceptionWriter();
087: throwable.printStackTrace(new PrintWriter(writer));
088: return writer.exception;
089: }
090:
091: /**
092: * Add the core internationalization & icons to the search path
093: */
094: public static void initializeBase() throws GUIException {
095: ImageIconFactory.addSearchPath("resources/xmlgui/icons");
096: InternationalizationManager
097: .addLanguageFile("resources/xmlgui/base");
098: }
099:
100: /**
101: * MacOS X specific initialization
102: */
103: public static void initializeOSX() {
104: System.setProperty("apple.laf.useScreenMenuBar", "true");
105: }
106:
107: /**
108: * Initialize the event queue
109: */
110: public static void initializeEventQueue() {
111: EventQueue waitQueue = new WaitCursorEventQueue(500);
112: Toolkit.getDefaultToolkit().getSystemEventQueue().push(
113: waitQueue);
114: }
115:
116: /**
117: * Initialize log4j with a basic console appender
118: */
119: public static void initializeLogging() throws GUIException {
120: try {
121:
122: URL url = GUIUtils.class
123: .getResource("/resources/xmlgui/log4j.properties");
124: Properties properties = new Properties();
125: properties.load(url.openStream());
126: PropertyConfigurator.configure(properties);
127: } catch (Exception e) {
128: throw new GUIException("Error while initializing logging",
129: e);
130: }
131: }
132:
133: /**
134: * Initialize a custom look and feel
135: */
136: public static void initializeLookAndFeel(String className)
137: throws GUIException {
138: try {
139: Class lnfClass = Class.forName(className);
140:
141: UIManager.setLookAndFeel((LookAndFeel) lnfClass
142: .newInstance());
143: UIManager.getLookAndFeelDefaults().put("ClassLoader",
144: GUIUtils.class.getClassLoader());
145: } catch (Exception e) {
146: throw new GUIException(
147: "Error while initializing LookAndFeel");
148: }
149: }
150:
151: /**
152: * Initialize everything using the given locale
153: */
154: public static void defaultInitialization(Locale locale)
155: throws GUIException {
156: if (locale != null)
157: Locale.setDefault(locale);
158: initializeLogging();
159: initializeBase();
160: initializeEventQueue();
161:
162: if (System.getProperty("os.name").equals("Mac OS X")) {
163: initializeOSX();
164: } else {
165: try {
166: initializeLookAndFeel(KUNSTSTOFF_LNF);
167: } catch (GUIException e) {
168: /* Ignore */
169: }
170: }
171: }
172:
173: /**
174: * Initialize everything while taking the default locale
175: */
176: public static void defaultInitialization() throws GUIException {
177: defaultInitialization(null);
178: }
179:
180: /**
181: * Dynamically update the look and feel of all widgets
182: */
183: public static void updateAllWidgets() {
184: synchronized (widgets) {
185: for (int i = 0; i < widgets.size(); i++) {
186: WeakReference ref = (WeakReference) widgets.get(i);
187: Widget referent = (Widget) ref.get();
188:
189: if (referent != null) {
190: Component component = referent.getWidget();
191: if (component != null)
192: SwingUtilities.updateComponentTreeUI(component);
193: }
194: }
195: }
196: }
197:
198: /**
199: * Internally used by Frame/Dialogs to register themselves
200: * when they are created. Needed for dynamic Look And Feel
201: * changes
202: */
203: public static void register(Widget widget) {
204: WeakReference reference = new WeakReference(widget);
205: reference.enqueue();
206: synchronized (widgets) {
207: widgets.add(reference);
208: }
209: System.gc();
210: }
211:
212: /**
213: * Internally used by Frame/Dialogs to unregister themselves
214: * when they are finalized. Needed for dynamic Look And Feel
215: * changes
216: */
217: public static void unregister(Widget widget) {
218: synchronized (widgets) {
219: for (Iterator i = widgets.iterator(); i.hasNext();) {
220: WeakReference ref = (WeakReference) i.next();
221: Object referent = ref.get();
222:
223: if (referent == widget || referent == null) {
224: i.remove();
225: }
226: }
227: }
228: }
229: }
|