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;
022:
023: import java.awt.Toolkit;
024: import java.awt.Window;
025: import java.io.*;
026: import java.lang.reflect.InvocationTargetException;
027: import java.util.logging.Logger;
028: import org.onemind.awtbridge.input.InputException;
029: import org.onemind.awtbridge.render.RenderingException;
030: import org.onemind.commons.java.lang.ConfigurationException;
031: import org.onemind.swingweb.session.SwingWebSession;
032: import org.onemind.swingweb.util.SwingWebUtils;
033:
034: /**
035: * The app manager encapsulate the handling of SwingWebToolkit and SwingWebContext and
036: * provide interfaces to start up a normal swing application in SwingWebContext easily.
037: * @author TiongHiang Lee
038: *
039: */
040: public class SwingWebAppManager {
041:
042: /** the logger **/
043: private static final Logger _logger = Logger
044: .getLogger(SwingWebAppManager.class.getName());
045:
046: /** the toolkit * */
047: private static SwingWebToolkit _tk;
048:
049: /** the context * */
050: private SwingWebContext _context;
051:
052: /**
053: * Constructor
054: * @param xml path to the xml config file
055: * @throws ConfigurationException if there's configuration problem
056: */
057: public SwingWebAppManager(String xml) throws IOException,
058: ConfigurationException {
059: this (new FileInputStream(xml));
060: }
061:
062: /**
063: * Constructor
064: * @param inStream
065: * @throws ConfigurationException
066: */
067: public SwingWebAppManager(InputStream inStream)
068: throws ConfigurationException {
069: initContext(inStream);
070: }
071:
072: /**
073: * Init the context
074: * @param xml the config
075: * @throws ConfigurationException if there's configuration problem
076: */
077: private void initContext(InputStream inStream)
078: throws ConfigurationException {
079: try {
080: _context = SwingWebContextFactory.createContext(inStream);
081: } catch (Exception e) {
082: throw new ConfigurationException(
083: "Cannot instantiate swingweb context", e);
084: }
085: }
086:
087: /**
088: * Get the toolkit
089: * @return the toolkit
090: */
091: private final SwingWebToolkit getToolkit() {
092: if (_tk == null) {
093: Toolkit tk = Toolkit.getDefaultToolkit();
094: if (tk instanceof SwingWebToolkit) {
095: _tk = (SwingWebToolkit) tk;
096: } else {
097: throw new IllegalArgumentException(
098: "Not in SwingWeb environment");
099: }
100: }
101: return _tk;
102: }
103:
104: /**
105: * Get the context
106: * @return the context
107: */
108: public final SwingWebContext getContext() {
109: return _context;
110: }
111:
112: /**
113: * Start an application (using the appClass main method)
114: * @param appClass the application class, or the applet class
115: * @param args the arguments
116: * @throws InvocationTargetException if there's problem invoking the main method
117: * @throws IllegalAccessException if there's problem invoking the main method
118: * @throws NoSuchMethodException if the application class does not have a main method
119: */
120: public Window startApp(Class appClass, String[] args)
121: throws Exception {
122: Window w = SwingWebUtils.startApp(appClass, args);
123: getContext().getInputContext().dispatchEvents();
124: return w;
125: }
126:
127: /**
128: * Init the current session. This will ensure the context will be set current to the toolkit as well.
129: * If session==null, it will unsert the current session and context
130: * @param session
131: */
132: public void setCurrentSession(SwingWebSession session) {
133: if (session == null) {
134: getToolkit().initContext(null);
135: _context.setSession(null);
136: } else {
137: getToolkit().initContext(_context);
138: _context.setSession(session);
139: }
140: }
141:
142: /**
143: * Handle input for windows
144: * @param window the windows
145: * @param output the output object
146: * @throws InputException if there's input problem
147: */
148: public void handleInput(Window window, Object input)
149: throws InputException {
150: _context.handleInput(window, input);
151: }
152:
153: /**
154: * Handle the input. Use the input context to handle it
155: * @param input the input
156: * @throws InputException if there's input problem
157: */
158: public void handleInput(Object input) throws InputException {
159: _context.handleInput(input);
160: }
161:
162: /**
163: * Render the window to the output
164: * @param window the window
165: * @param output the output
166: * @throws RenderingException if there's render problem
167: */
168: public void renderOutput(Window window, Object output)
169: throws RenderingException {
170: _context.renderOutput(window, output);
171: }
172:
173: /**
174: * Return the output to the writer
175: * @param output the output object
176: * @throws RenderingException if there's rendering problem
177: */
178: public void renderOutput(Object output) throws RenderingException {
179: _context.renderOutput(output);
180: }
181: }
|