KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > testapp > interactive > InteractiveApp


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.testapp.interactive;
31
32 import nextapp.echo2.app.ApplicationInstance;
33 import nextapp.echo2.app.TaskQueueHandle;
34 import nextapp.echo2.app.Window;
35 import nextapp.echo2.webcontainer.ContainerContext;
36 import nextapp.echo2.webrender.ClientConfiguration;
37
38 /**
39  * Interactive Test Application Instance.
40  * <p>
41  * <b>WARNING TO DEVELOPERS:</b>
42  * <p>
43  * Beware that the Interactive Test Application is not a good example of an
44  * Echo application. The intent of this application is to serve as a running
45  * testbed for the Echo framework. As such, the requirements of this
46  * application are dramatically different from a production internet
47  * application. There is stuff in this code of this application that is
48  * downright absurd. Please do not look to this application to see good design
49  * practices for building your own Echo applications--you will not find them
50  * here.
51  */

52 public class InteractiveApp extends ApplicationInstance {
53
54     private static final String JavaDoc[] GHOST_SCRIPT_1 = new String JavaDoc[] {
55         "EnterTestApplication",
56         "StartTest:ButtonTest",
57         "StartTest:SplitPaneNestedTest",
58         "StartTest:ListBoxTest",
59         "StartTest:TextComponentTest",
60         "StartTest:StyleSheetTest",
61         "StartTest:ContainerContextTest",
62         "StartTest:GridTest",
63         "StartTest:HierarchyTest",
64         "ExitTestApplication"
65     };
66     
67     /**
68      * A boolean flag indicating whether the application is running on a live
69      * demo server. This flag is used to disable certain tests based on
70      * whether the <code>nextapp.echo2.demoserver</code> system property is
71      * set.
72      */

73     public static final boolean LIVE_DEMO_SERVER;
74     static {
75         boolean liveDemoServer;
76         try {
77             if ("true".equals(System.getProperties().getProperty("nextapp.echo2.demoserver"))) {
78                 liveDemoServer = true;
79             } else {
80                 liveDemoServer = false;
81             }
82         } catch (SecurityException JavaDoc ex) {
83             liveDemoServer = false;
84         }
85         LIVE_DEMO_SERVER = liveDemoServer;
86     }
87     
88     /**
89      * Convenience method to return the <code>ThreadLocal</code> instance
90      * precast to the appropriate type.
91      *
92      * @return the active <code>InteractiveApp</code>
93      * @see #getActive()
94      */

95     public static InteractiveApp getApp() {
96         return (InteractiveApp) ApplicationInstance.getActive();
97     }
98
99     private TaskQueueHandle ghostTaskQueue;
100     private Window mainWindow;
101     private ConsoleWindowPane console;
102     
103     /**
104      * Writes a message to a pop-up debugging console.
105      * The console is used for displaying information such as
106      * fired events.
107      *
108      * @param message the message to write to the console
109      */

110     public void consoleWrite(String JavaDoc message) {
111         if (console == null) {
112             console = new ConsoleWindowPane();
113             getDefaultWindow().getContent().add(console);
114         } else if (console.getParent() == null) {
115             getDefaultWindow().getContent().add(console);
116         }
117         console.writeMessage(message);
118     }
119     
120     /**
121      * Displays a <code>TestPane</code> from which the user may select an
122      * interactive test to run.
123      */

124     public void displayTestPane() {
125         mainWindow.setContent(new TestPane());
126     }
127     
128     /**
129      * Displays the <code>WelcomePane</code> which greets new users visiting
130      * the application.
131      */

132     public void displayWelcomePane() {
133         mainWindow.setContent(new WelcomePane());
134     }
135     
136     private String JavaDoc getRequestParameter(String JavaDoc parameterName) {
137         ContainerContext cc = (ContainerContext) getContextProperty(ContainerContext.CONTEXT_PROPERTY_NAME);
138         Object JavaDoc parameterValue = cc.getInitialRequestParameterMap().get(parameterName);
139         if (parameterValue instanceof String JavaDoc) {
140             return (String JavaDoc) parameterValue;
141         } else if (parameterValue instanceof String JavaDoc[]) {
142             return ((String JavaDoc[]) parameterValue).length == 0 ? null : ((String JavaDoc[]) parameterValue)[0];
143         } else {
144             return parameterValue == null ? null : parameterValue.toString();
145         }
146     }
147
148     /**
149      * @see nextapp.echo2.app.ApplicationInstance#init()
150      */

151     public Window init() {
152         setStyleSheet(Styles.DEFAULT_STYLE_SHEET);
153         mainWindow = new Window();
154         mainWindow.setTitle("NextApp Echo2 Test Application");
155         mainWindow.setContent(new WelcomePane());
156         
157         ContainerContext cc = (ContainerContext) getContextProperty(ContainerContext.CONTEXT_PROPERTY_NAME);
158         if (!LIVE_DEMO_SERVER) {
159             if (cc.getInitialRequestParameterMap().containsKey("ghost")) {
160                 GhostTask ghostTask = new GhostTask();
161                 
162                 if ("1".equals(getRequestParameter("script"))) {
163                     ghostTask.setScript(GHOST_SCRIPT_1);
164                 }
165                 if (cc.getInitialRequestParameterMap().containsKey("clicks")) {
166                     ghostTask.setClicksPerIteration(Integer.parseInt(getRequestParameter("clicks")));
167                 }
168                 if (cc.getInitialRequestParameterMap().containsKey("iterations")) {
169                     ghostTask.setTotalIterations(Integer.parseInt(getRequestParameter("iterations")));
170                 }
171                 startGhostTask(ghostTask, 0);
172             }
173         }
174         
175         ClientConfiguration clientConfiguration = new ClientConfiguration();
176         clientConfiguration.setProperty(ClientConfiguration.PROPERTY_SERVER_ERROR_MESSAGE,
177                 "KA-BOOM! (test non-default server error message)");
178         cc.setClientConfiguration(clientConfiguration);
179         
180         return mainWindow;
181     }
182     
183     /**
184      * Sets the window title to display the current iteration of the ghost
185      * test.
186      *
187      * @param iteration the iteration number (negative values will restore
188      * the window title to its normal state)
189      */

190     public void setGhostIterationWindowTitle(int iteration) {
191         if (iteration < 0) {
192             mainWindow.setTitle("NextApp Echo2 Test Application");
193         } else {
194             mainWindow.setTitle("Iteration #" + iteration);
195         }
196     }
197     
198     /**
199      * Starts the ghost test with the specified callback interval, run-time,
200      * and clicks-per-iteration.
201      *
202      * @param interval the callback interval between ghost actions, in
203      * milliseconds
204      */

205     public void startGhostTask(GhostTask ghostTask, int interval) {
206         if (ghostTaskQueue != null) {
207             return;
208         }
209         ghostTaskQueue = createTaskQueue();
210         ContainerContext containerContext =
211                 (ContainerContext) getContextProperty(ContainerContext.CONTEXT_PROPERTY_NAME);
212         containerContext.setTaskQueueCallbackInterval(ghostTaskQueue, interval);
213         ghostTask.startTask(this, ghostTaskQueue);
214     }
215     
216     /**
217      * Stops the currently running ghost test.
218      */

219     public void stopGhostTest() {
220         removeTaskQueue(ghostTaskQueue);
221         ghostTaskQueue = null;
222     }
223 }
224
Popular Tags