/*
* Copyright (C) 2004 TiongHiang Lee
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Email: thlee@onemindsoft.org
*/
package org.onemind.swingweb.util;
import java.applet.Applet;
import java.awt.*;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.RepaintManager;
import org.onemind.awtbridge.util.AwtBridgeUtils;
import org.onemind.commons.java.lang.reflect.ReflectUtils;
import org.onemind.commons.java.util.LogUtils;
import org.onemind.swingweb.*;
import org.onemind.swingweb.awt.EmptyAppletStub;
import org.onemind.swingweb.session.URLLocal;
/**
* Provides some utility methods
* @author TiongHiang Lee (thlee@onemindsoft.org)
*
*/
public class SwingWebUtils extends AwtBridgeUtils
{
private static final boolean _swingwebMode = Toolkit.getDefaultToolkit() instanceof SwingWebToolkit;
/** the logger **/
private static final Logger _logger = Logger.getLogger(SwingWebUtils.class.getName());
/** the argument to main static method **/
private static final Object[] MAIN_ARGS = new Object[]{new String[0]};
/** for storing url localst in non-swingweb mode **/
private static final List _fakeURLLocals = new ArrayList();
private static class FixLayoutEvent extends AWTEvent implements ActiveEvent{
private JComponent _com;
public FixLayoutEvent(JComponent source)
{
super(source, 0);
_com = source;
// TODO Auto-generated constructor stub
}
public void dispatch(){
_com.invalidate();
RepaintManager.currentManager(_com).addInvalidComponent(_com);
_com.validate();
}
}
/**
* Get current swingweb context
* @return the context
*/
public static SwingWebContext getSwingWebContext()
{
return (SwingWebContext) ((SwingWebToolkit) Toolkit.getDefaultToolkit()).getContext();
}
/**
* Start the applet class as a window application by
* adding it to a window, call the init method and make it to show
* @param appletClass the applet class
* @return the containing window of the applet
* @throws InstantiationException if the applet cannot be instantiated
* @throws IllegalAccessException if the applet cannot be instantiated
*/
public static Window startAppletApp(Class appletClass) throws InstantiationException, IllegalAccessException
{
//try starting as applet
if (Applet.class.isAssignableFrom(appletClass))
{
Applet app = (Applet) appletClass.newInstance();
Window win = new Frame();
win.add(app);
app.setStub(new EmptyAppletStub());
app.init();
win.pack();
win.show();
return win; //successful, so return
} else
{
throw new IllegalArgumentException("The given argument " + appletClass + " is not an applet class");
}
}
/**
* Start the window class as a window application
* by instantiate it with given arguments and make it to show
* @param windowClass
* @return the window started
* @throws NoSuchMethodException if there's no appropriate constructor
* @throws InvocationTargetException if there's problem calling the constructor
* @throws InstantiationException if there's problem calling the constructor
* @throws IllegalAccessException if there's problem calling the constructor
*/
public static Window startWindowApp(Class windowClass, Object[] args) throws IllegalAccessException, InstantiationException,
InvocationTargetException, NoSuchMethodException
{
// try starting as window
if (Window.class.isAssignableFrom(windowClass))
{
Window w = (Window) ReflectUtils.newInstance(windowClass, args);
w.pack();
w.show();
return w;
} else
{
throw new IllegalArgumentException("The given argument " + windowClass + " is not an applet class");
}
}
/**
* Start the appClass by calling the static main() method
* @param appClass the app class
* @param args the arguments
* @throws InvocationTargetException if there's problem calling the constructor
* @throws IllegalAccessException if there's problem calling the constructor
* @throws NoSuchMethodException if there's no main method
*/
public static void startMainApp(Class appClass, String[] args) throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException
{
Object[] mainArgs;
if (args == null)
{
mainArgs = MAIN_ARGS;
} else
{
mainArgs = new Object[]{args};
}
ReflectUtils.invoke(appClass, "main", mainArgs);
}
public static Window startSwApp(Class appClass, String[] args) throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException
{
Object[] mainArgs;
if (args == null)
{
mainArgs = MAIN_ARGS;
} else
{
mainArgs = new Object[]{args};
}
return (Window) ReflectUtils.invoke(appClass, "swMain", mainArgs);
}
/**
* Start an application. First try to using the main() method, if failed it will
* try to start the appClass as applet or window depending on the appClass class hierachy.
* @param appClass the app class
* @param args the arguments
* @return the window if the app is starting as a window or applet, or null if main() is called
* @throws Exception if the application cannot be started
*
*/
public static Window startApp(Class appClass, String[] args) throws Exception
{
java.util.List exceptions = new ArrayList();
//start with sw main method
try
{
if (ReflectUtils.getMethod(appClass, "swMain", MAIN_ARGS) != null)
{
try
{
return SwingWebUtils.startSwApp(appClass, args);
} catch (Exception e1)
{
_logger.warning("Cannot start " + appClass + " as applet. Will try to start as application");
exceptions.add(e1);
}
}
} catch (NoSuchMethodException e)
{
//do nothing
}
//start with the main method
try
{
if (ReflectUtils.getMethod(appClass, "main", MAIN_ARGS) != null)
{
try
{
SwingWebUtils.startMainApp(appClass, args);
return null;
} catch (Exception e1)
{
_logger.warning("Cannot start " + appClass + " as applet. Will try to start as application");
exceptions.add(e1);
}
}
} catch (NoSuchMethodException e)
{
//do nothing
}
//try starting as applet
if (Applet.class.isAssignableFrom(appClass))
{
if (_logger.isLoggable(Level.FINEST))
{
_logger.finest("Try to start as applet");
}
try
{
return SwingWebUtils.startAppletApp(appClass);
} catch (Exception e)
{
_logger.warning("Cannot start " + appClass + " as applet. Will try to start as application");
exceptions.add(e);
}
}
//try starting as window
if (Window.class.isAssignableFrom(appClass))
{
try
{
return SwingWebUtils.startWindowApp(appClass, args);
} catch (Exception e)
{
_logger.warning("Cannot start " + appClass + "as window. Will try to start as application");
exceptions.add(e);
}
}
_logger.severe("Cannot start application " + appClass);
for (Iterator it = exceptions.iterator(); it.hasNext();)
{
_logger.severe(LogUtils.getTrace((Exception) it.next()));
}
throw new Exception("Cannot start application " + appClass);
}
/**
* Indicate whether this is in swingweb mode
* @return true if is in swingweb mode
*/
public static boolean isInSwingWebMode()
{
return _swingwebMode;
}
public static void registerURLLocal(URLLocal local)
{
if (isInSwingWebMode())
{
((SwingWebComponentManager) getSwingWebContext().getSession().getComponentManager()).registerURLLocal(local);
} else
{
_fakeURLLocals.add(local);
}
}
public static void fixLayout(JComponent component)
{
if (isInSwingWebMode())
{
getSwingWebContext().getEventQueue().postEvent(new FixLayoutEvent(component));
}
}
}