001: /*
002: * Copyright (C) 2004 TiongHiang Lee
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Email: thlee@onemindsoft.org
019: */
020:
021: package org.onemind.swingweb.util;
022:
023: import java.applet.Applet;
024: import java.awt.*;
025: import java.lang.reflect.InvocationTargetException;
026: import java.util.*;
027: import java.util.List;
028: import java.util.logging.Level;
029: import java.util.logging.Logger;
030: import javax.swing.JComponent;
031: import javax.swing.RepaintManager;
032: import org.onemind.awtbridge.util.AwtBridgeUtils;
033: import org.onemind.commons.java.lang.reflect.ReflectUtils;
034: import org.onemind.commons.java.util.LogUtils;
035: import org.onemind.swingweb.*;
036: import org.onemind.swingweb.awt.EmptyAppletStub;
037: import org.onemind.swingweb.session.URLLocal;
038:
039: /**
040: * Provides some utility methods
041: * @author TiongHiang Lee (thlee@onemindsoft.org)
042: *
043: */
044: public class SwingWebUtils extends AwtBridgeUtils {
045:
046: private static final boolean _swingwebMode = Toolkit
047: .getDefaultToolkit() instanceof SwingWebToolkit;
048: /** the logger **/
049: private static final Logger _logger = Logger
050: .getLogger(SwingWebUtils.class.getName());
051:
052: /** the argument to main static method **/
053: private static final Object[] MAIN_ARGS = new Object[] { new String[0] };
054:
055: /** for storing url localst in non-swingweb mode **/
056: private static final List _fakeURLLocals = new ArrayList();
057:
058: private static class FixLayoutEvent extends AWTEvent implements
059: ActiveEvent {
060:
061: private JComponent _com;
062:
063: public FixLayoutEvent(JComponent source) {
064: super (source, 0);
065: _com = source;
066: // TODO Auto-generated constructor stub
067: }
068:
069: public void dispatch() {
070: _com.invalidate();
071: RepaintManager.currentManager(_com).addInvalidComponent(
072: _com);
073: _com.validate();
074:
075: }
076: }
077:
078: /**
079: * Get current swingweb context
080: * @return the context
081: */
082: public static SwingWebContext getSwingWebContext() {
083: return (SwingWebContext) ((SwingWebToolkit) Toolkit
084: .getDefaultToolkit()).getContext();
085: }
086:
087: /**
088: * Start the applet class as a window application by
089: * adding it to a window, call the init method and make it to show
090: * @param appletClass the applet class
091: * @return the containing window of the applet
092: * @throws InstantiationException if the applet cannot be instantiated
093: * @throws IllegalAccessException if the applet cannot be instantiated
094: */
095: public static Window startAppletApp(Class appletClass)
096: throws InstantiationException, IllegalAccessException {
097: //try starting as applet
098: if (Applet.class.isAssignableFrom(appletClass)) {
099: Applet app = (Applet) appletClass.newInstance();
100: Window win = new Frame();
101: win.add(app);
102: app.setStub(new EmptyAppletStub());
103: app.init();
104: win.pack();
105: win.show();
106: return win; //successful, so return
107: } else {
108: throw new IllegalArgumentException("The given argument "
109: + appletClass + " is not an applet class");
110: }
111: }
112:
113: /**
114: * Start the window class as a window application
115: * by instantiate it with given arguments and make it to show
116: * @param windowClass
117: * @return the window started
118: * @throws NoSuchMethodException if there's no appropriate constructor
119: * @throws InvocationTargetException if there's problem calling the constructor
120: * @throws InstantiationException if there's problem calling the constructor
121: * @throws IllegalAccessException if there's problem calling the constructor
122: */
123: public static Window startWindowApp(Class windowClass, Object[] args)
124: throws IllegalAccessException, InstantiationException,
125: InvocationTargetException, NoSuchMethodException {
126: // try starting as window
127: if (Window.class.isAssignableFrom(windowClass)) {
128: Window w = (Window) ReflectUtils.newInstance(windowClass,
129: args);
130: w.pack();
131: w.show();
132: return w;
133: } else {
134: throw new IllegalArgumentException("The given argument "
135: + windowClass + " is not an applet class");
136: }
137: }
138:
139: /**
140: * Start the appClass by calling the static main() method
141: * @param appClass the app class
142: * @param args the arguments
143: * @throws InvocationTargetException if there's problem calling the constructor
144: * @throws IllegalAccessException if there's problem calling the constructor
145: * @throws NoSuchMethodException if there's no main method
146: */
147: public static void startMainApp(Class appClass, String[] args)
148: throws NoSuchMethodException, IllegalAccessException,
149: InvocationTargetException {
150: Object[] mainArgs;
151: if (args == null) {
152: mainArgs = MAIN_ARGS;
153: } else {
154: mainArgs = new Object[] { args };
155: }
156: ReflectUtils.invoke(appClass, "main", mainArgs);
157: }
158:
159: public static Window startSwApp(Class appClass, String[] args)
160: throws NoSuchMethodException, IllegalAccessException,
161: InvocationTargetException {
162: Object[] mainArgs;
163: if (args == null) {
164: mainArgs = MAIN_ARGS;
165: } else {
166: mainArgs = new Object[] { args };
167: }
168: return (Window) ReflectUtils.invoke(appClass, "swMain",
169: mainArgs);
170: }
171:
172: /**
173: * Start an application. First try to using the main() method, if failed it will
174: * try to start the appClass as applet or window depending on the appClass class hierachy.
175: * @param appClass the app class
176: * @param args the arguments
177: * @return the window if the app is starting as a window or applet, or null if main() is called
178: * @throws Exception if the application cannot be started
179: *
180: */
181: public static Window startApp(Class appClass, String[] args)
182: throws Exception {
183: java.util.List exceptions = new ArrayList();
184: //start with sw main method
185: try {
186: if (ReflectUtils.getMethod(appClass, "swMain", MAIN_ARGS) != null) {
187: try {
188: return SwingWebUtils.startSwApp(appClass, args);
189: } catch (Exception e1) {
190: _logger
191: .warning("Cannot start "
192: + appClass
193: + " as applet. Will try to start as application");
194: exceptions.add(e1);
195: }
196: }
197: } catch (NoSuchMethodException e) {
198: //do nothing
199: }
200: //start with the main method
201: try {
202: if (ReflectUtils.getMethod(appClass, "main", MAIN_ARGS) != null) {
203: try {
204: SwingWebUtils.startMainApp(appClass, args);
205: return null;
206: } catch (Exception e1) {
207: _logger
208: .warning("Cannot start "
209: + appClass
210: + " as applet. Will try to start as application");
211: exceptions.add(e1);
212: }
213: }
214: } catch (NoSuchMethodException e) {
215: //do nothing
216: }
217: //try starting as applet
218: if (Applet.class.isAssignableFrom(appClass)) {
219: if (_logger.isLoggable(Level.FINEST)) {
220: _logger.finest("Try to start as applet");
221: }
222: try {
223: return SwingWebUtils.startAppletApp(appClass);
224: } catch (Exception e) {
225: _logger
226: .warning("Cannot start "
227: + appClass
228: + " as applet. Will try to start as application");
229: exceptions.add(e);
230: }
231: }
232: //try starting as window
233: if (Window.class.isAssignableFrom(appClass)) {
234: try {
235: return SwingWebUtils.startWindowApp(appClass, args);
236: } catch (Exception e) {
237: _logger
238: .warning("Cannot start "
239: + appClass
240: + "as window. Will try to start as application");
241: exceptions.add(e);
242: }
243: }
244: _logger.severe("Cannot start application " + appClass);
245: for (Iterator it = exceptions.iterator(); it.hasNext();) {
246: _logger.severe(LogUtils.getTrace((Exception) it.next()));
247: }
248: throw new Exception("Cannot start application " + appClass);
249: }
250:
251: /**
252: * Indicate whether this is in swingweb mode
253: * @return true if is in swingweb mode
254: */
255: public static boolean isInSwingWebMode() {
256: return _swingwebMode;
257: }
258:
259: public static void registerURLLocal(URLLocal local) {
260: if (isInSwingWebMode()) {
261: ((SwingWebComponentManager) getSwingWebContext()
262: .getSession().getComponentManager())
263: .registerURLLocal(local);
264: } else {
265: _fakeURLLocals.add(local);
266: }
267: }
268:
269: public static void fixLayout(JComponent component) {
270: if (isInSwingWebMode()) {
271: getSwingWebContext().getEventQueue().postEvent(
272: new FixLayoutEvent(component));
273: }
274: }
275: }
|