001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s): Alexandre Iline.
025: *
026: * The Original Software is the Jemmy library.
027: * The Initial Developer of the Original Software is Alexandre Iline.
028: * All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: *
041: *
042: *
043: * $Id$ $Revision$ $Date$
044: *
045: */
046:
047: package org.netbeans.jemmy.util;
048:
049: import java.awt.Component;
050: import java.awt.Dialog;
051:
052: import org.netbeans.jemmy.JemmyProperties;
053: import org.netbeans.jemmy.Outputable;
054: import org.netbeans.jemmy.TestOut;
055: import org.netbeans.jemmy.Timeoutable;
056: import org.netbeans.jemmy.Timeouts;
057: import org.netbeans.jemmy.WindowWaiter;
058:
059: import org.netbeans.jemmy.operators.WindowOperator;
060:
061: import java.awt.Window;
062:
063: import java.util.Vector;
064:
065: /**
066: * Class allows to make periodical window jobs like error window closing.
067: * @see WindowJob
068: * @author Alexandre Iline (alexandre.iline@sun.com)
069: */
070:
071: public class WindowManager implements Timeoutable, Outputable {
072:
073: /**
074: * Default value for WindowManager.TimeDelta timeout.
075: */
076: private static long TIME_DELTA = 1000;
077:
078: private static WindowManager manager;
079:
080: private Vector jobs;
081: private Timeouts timeouts;
082: private TestOut output;
083:
084: private WindowManager() {
085: super ();
086: setTimeouts(JemmyProperties.getCurrentTimeouts());
087: setOutput(JemmyProperties.getCurrentOutput());
088: jobs = new Vector();
089: }
090:
091: /**
092: * Adds job to list.
093: * @param job a job to perform.
094: */
095: public static void addJob(WindowJob job) {
096: manager.add(job);
097: }
098:
099: /**
100: * Removes job from list.
101: * @param job a job to remove.
102: */
103: public static void removeJob(WindowJob job) {
104: manager.remove(job);
105: }
106:
107: public static void performJob(WindowJob job) {
108: while (manager.performJobOnce(job)) {
109: }
110: }
111:
112: static {
113: Timeouts.initDefault("WindowManager.TimeDelta", TIME_DELTA);
114: manager = new WindowManager();
115: }
116:
117: public void setTimeouts(Timeouts timeouts) {
118: this .timeouts = timeouts;
119: }
120:
121: public Timeouts getTimeouts() {
122: return (timeouts);
123: }
124:
125: public void setOutput(TestOut output) {
126: this .output = output;
127: }
128:
129: public TestOut getOutput() {
130: return (output);
131: }
132:
133: /**
134: * Adds job to list.
135: * @param job a job to perform.
136: */
137: public void add(WindowJob job) {
138: output.printLine("Starting job \"" + job.getDescription()
139: + "\"");
140: synchronized (jobs) {
141: JobThread thread = new JobThread(job);
142: jobs.add(thread);
143: thread.start();
144: }
145: }
146:
147: /**
148: * Removes job from list.
149: * @param job a job to remove.
150: */
151: public void remove(WindowJob job) {
152: output
153: .printLine("Killing job \"" + job.getDescription()
154: + "\"");
155: synchronized (jobs) {
156: for (int i = 0; i < jobs.size(); i++) {
157: if (((JobThread) jobs.get(i)).job == job) {
158: ((JobThread) jobs.get(i)).needStop = true;
159: jobs.remove(i);
160: }
161: }
162: }
163: }
164:
165: private boolean performJobOnce(WindowJob job) {
166: Window win = WindowWaiter.getWindow(job);
167: if (win != null) {
168: job.launch(win);
169: return (true);
170: } else {
171: return (false);
172: }
173: }
174:
175: public static class ModalDialogChoosingJob implements WindowJob {
176: public boolean checkComponent(Component comp) {
177: return (comp instanceof Dialog && ((Dialog) comp).isModal());
178: }
179:
180: public Object launch(Object obj) {
181: new WindowOperator((Window) obj).close();
182: return (null);
183: }
184:
185: public String getDescription() {
186: return ("A job of closing modal dialogs");
187: }
188: }
189:
190: private static class JobThread extends Thread {
191: WindowJob job;
192: boolean needStop = false;
193:
194: public JobThread(WindowJob job) {
195: this .job = job;
196: }
197:
198: private boolean getNS() {
199: synchronized (this ) {
200: return (needStop);
201: }
202: }
203:
204: public void run() {
205: while (!getNS()) {
206: manager.performJobOnce(job);
207: manager.timeouts.sleep("WindowManager.TimeDelta");
208: }
209: }
210: }
211:
212: }
|