Package org.onemind.swingweb

Source Code of org.onemind.swingweb.SwingWebAppManager

/*
* 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;

import java.awt.Toolkit;
import java.awt.Window;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Logger;
import org.onemind.awtbridge.input.InputException;
import org.onemind.awtbridge.render.RenderingException;
import org.onemind.commons.java.lang.ConfigurationException;
import org.onemind.swingweb.session.SwingWebSession;
import org.onemind.swingweb.util.SwingWebUtils;
/**
* The app manager encapsulate the handling of SwingWebToolkit and SwingWebContext and
* provide interfaces to start up a normal swing application in SwingWebContext easily.
* @author TiongHiang Lee
*
*/
public class SwingWebAppManager
{

    /** the logger **/
    private static final Logger _logger = Logger.getLogger(SwingWebAppManager.class.getName());

    /** the toolkit * */
    private static SwingWebToolkit _tk;

    /** the context * */
    private SwingWebContext _context;

    /**
     * Constructor
     * @param xml path to the xml config file
     * @throws ConfigurationException if there's configuration problem
     */
    public SwingWebAppManager(String xml) throws IOException, ConfigurationException
    {
        this(new FileInputStream(xml));
    }

    /**
     * Constructor
     * @param inStream
     * @throws ConfigurationException
     */
    public SwingWebAppManager(InputStream inStream) throws ConfigurationException
    {
        initContext(inStream);
    }

    /**
     * Init the context
     * @param xml the config
     * @throws ConfigurationException if there's configuration problem
     */
    private void initContext(InputStream inStream) throws ConfigurationException
    {
        try
        {
            _context = SwingWebContextFactory.createContext(inStream);
        } catch (Exception e)
        {
            throw new ConfigurationException("Cannot instantiate swingweb context", e);
        }
    }

    /**
     * Get the toolkit
     * @return the toolkit
     */
    private final SwingWebToolkit getToolkit()
    {
        if (_tk == null)
        {
            Toolkit tk = Toolkit.getDefaultToolkit();
            if (tk instanceof SwingWebToolkit)
            {
                _tk = (SwingWebToolkit) tk;
            } else
            {
                throw new IllegalArgumentException("Not in SwingWeb environment");
            }
        }
        return _tk;
    }

    /**
     * Get the context
     * @return the context
     */
    public final SwingWebContext getContext()
    {
        return _context;
    }

    /**
     * Start an application (using the appClass main method)
     * @param appClass the application class, or the applet class
     * @param args the arguments
     * @throws InvocationTargetException if there's problem invoking the main method
     * @throws IllegalAccessException if there's problem invoking the main method
     * @throws NoSuchMethodException if the application class does not have a main method
     */
    public Window startApp(Class appClass, String[] args) throws Exception
    {
        Window w = SwingWebUtils.startApp(appClass, args);
        getContext().getInputContext().dispatchEvents();
        return w;
    }

    /**
     * Init the current session. This will ensure the context will be set current to the toolkit as well.
     * If session==null, it will unsert the current session and context
     * @param session
     */
    public void setCurrentSession(SwingWebSession session)
    {
        if (session == null)
        {
            getToolkit().initContext(null);
            _context.setSession(null);
        } else
        {
            getToolkit().initContext(_context);
            _context.setSession(session);
        }
    }

    /**
     * Handle input for windows
     * @param window the windows
     * @param output the output object
     * @throws InputException if there's input problem
     */
    public void handleInput(Window window, Object input) throws InputException
    {
        _context.handleInput(window, input);
    }

    /**
     * Handle the input. Use the input context to handle it
     * @param input the input
     * @throws InputException if there's input problem
     */
    public void handleInput(Object input) throws InputException
    {
        _context.handleInput(input);
    }

    /**
     * Render the window to the output
     * @param window the window
     * @param output the output
     * @throws RenderingException if there's render problem
     */
    public void renderOutput(Window window, Object output) throws RenderingException
    {
        _context.renderOutput(window, output);
    }

    /**
     * Return the output to the writer
     * @param output the output object
     * @throws RenderingException if there's rendering problem
     */
    public void renderOutput(Object output) throws RenderingException
    {
        _context.renderOutput(output);
    }
}
TOP

Related Classes of org.onemind.swingweb.SwingWebAppManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.