001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.testapp.interactive.testscreen;
031:
032: import nextapp.echo2.app.ApplicationInstance;
033: import nextapp.echo2.app.Button;
034: import nextapp.echo2.app.Extent;
035: import nextapp.echo2.app.Insets;
036: import nextapp.echo2.app.Label;
037: import nextapp.echo2.app.TaskQueueHandle;
038: import nextapp.echo2.app.Column;
039: import nextapp.echo2.app.event.ActionEvent;
040: import nextapp.echo2.app.event.ActionListener;
041: import nextapp.echo2.app.layout.SplitPaneLayoutData;
042:
043: /**
044: * Test for asynchronous (server push) operations.
045: */
046: public class PushTest extends Column {
047:
048: /**
049: * Thread to simulate long-running operation on server.
050: * Note that threading is not allowed by J2EE containers
051: * conforming to the 1.3 (or earlier) J2EE specification.
052: * If you plan on deploying an Echo2 user interface to such
053: * a container, please refrain from using this class as
054: * an example.
055: */
056: private class SimulatedServerOperation extends Thread {
057:
058: private int percentComplete = 0;
059:
060: public void run() {
061: while (percentComplete < 100) {
062: percentComplete += (Math.random() * 20);
063: if (percentComplete > 100) {
064: percentComplete = 100;
065: }
066: ApplicationInstance app = getApplicationInstance();
067: if (app != null) {
068: app.enqueueTask(taskQueue, new ProgressUpdateTask(
069: percentComplete));
070: try {
071: Thread.sleep((long) (Math.random() * 1000));
072: } catch (InterruptedException ex) {
073: }
074: }
075: }
076: }
077: }
078:
079: private class ProgressUpdateTask implements Runnable {
080:
081: private int percentComplete;
082:
083: private ProgressUpdateTask(int percentComplete) {
084: this .percentComplete = percentComplete;
085: }
086:
087: /**
088: * @see java.lang.Runnable#run()
089: */
090: public void run() {
091: if (percentComplete < 100) {
092: statusLabel
093: .setText("Asynchronous operation in progress; "
094: + percentComplete + "% complete.");
095: } else {
096: statusLabel.setText("Asynchronous operation complete.");
097: getApplicationInstance().removeTaskQueue(taskQueue);
098: taskQueue = null;
099: }
100: }
101: }
102:
103: private TaskQueueHandle taskQueue;
104: private Label statusLabel;
105:
106: public PushTest() {
107: super ();
108:
109: SplitPaneLayoutData splitPaneLayoutData = new SplitPaneLayoutData();
110: splitPaneLayoutData.setInsets(new Insets(10));
111: setLayoutData(splitPaneLayoutData);
112:
113: setCellSpacing(new Extent(20));
114:
115: statusLabel = new Label("Asynchronous operation not active.");
116: add(statusLabel);
117:
118: Button startButton = new Button(
119: "Start Asynchronous (Server Push) Operation");
120: startButton.setStyleName("Default");
121: startButton.addActionListener(new ActionListener() {
122: public void actionPerformed(ActionEvent e) {
123: if (taskQueue == null) {
124: // Only start new operation if taskQueue is null, indicating that last operation has completed.
125: taskQueue = getApplicationInstance()
126: .createTaskQueue();
127: new SimulatedServerOperation().start();
128: }
129: }
130: });
131: add(startButton);
132: }
133:
134: /**
135: * @see nextapp.echo2.app.Component#dispose()
136: */
137: public void dispose() {
138: if (taskQueue != null) {
139: getApplicationInstance().removeTaskQueue(taskQueue);
140: }
141: super.dispose();
142: }
143:
144: }
|