Source Code Cross Referenced for SwingWebUtils.java in  » Web-Framework » swingweb » org » onemind » swingweb » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » swingweb » org.onemind.swingweb.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.