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: DefaultCommentWindow.java,v 1.4 2007/10/05 11:37:30 jskrivanek Exp $ $Revision: 1.4 $ $Date: 2007/10/05 11:37:30 $
044: *
045: */
046:
047: package org.netbeans.jemmy.demo;
048:
049: import org.netbeans.jemmy.EventDispatcher;
050: import org.netbeans.jemmy.JemmyProperties;
051: import org.netbeans.jemmy.TestOut;
052: import org.netbeans.jemmy.TimeoutExpiredException;
053: import org.netbeans.jemmy.Timeouts;
054:
055: import org.netbeans.jemmy.operators.JButtonOperator;
056:
057: import java.awt.BorderLayout;
058: import java.awt.Dimension;
059: import java.awt.Toolkit;
060:
061: import java.awt.event.ActionEvent;
062: import java.awt.event.ActionListener;
063:
064: import javax.swing.JButton;
065: import javax.swing.JDialog;
066: import javax.swing.JFrame;
067: import javax.swing.JPanel;
068: import javax.swing.JScrollPane;
069: import javax.swing.JTextArea;
070:
071: /**
072: *
073: * Default org.netbeans.jemmy.demo.CommentWindow implementation.
074: *
075: * @author Alexandre Iline (alexandre.iline@sun.com)
076: *
077: */
078:
079: public class DefaultCommentWindow extends JDialog implements
080: CommentWindow {
081:
082: JTextArea comments;
083: JButton finishButton;
084: JButton nextStepButton;
085: JButton contButton;
086:
087: boolean stopped = true;
088: boolean continual = false;
089: boolean finished = false;
090: boolean interrupted = false;
091:
092: long readCommentTimeout = 1000;
093:
094: /**
095: * Constructs a DefaultCommentWindow object.
096: * @param modal Display as modal dialog.
097: */
098: public DefaultCommentWindow(boolean modal) {
099:
100: super ();
101:
102: nextStepButton = new JButton("One step");
103: nextStepButton.addActionListener(new ActionListener() {
104: public void actionPerformed(ActionEvent e) {
105: hideWindow();
106: nextStepButton.setEnabled(false);
107: contButton.setEnabled(false);
108: finishButton.setEnabled(false);
109: setStopped(false);
110: }
111: });
112:
113: contButton = new JButton("All steps");
114: contButton.addActionListener(new ActionListener() {
115: public void actionPerformed(ActionEvent e) {
116: hideWindow();
117: nextStepButton.setEnabled(false);
118: contButton.setEnabled(false);
119: finishButton.setEnabled(false);
120: continual = true;
121: setStopped(false);
122: }
123: });
124:
125: finishButton = new JButton("Interrupt");
126: finishButton.addActionListener(new ActionListener() {
127: public void actionPerformed(ActionEvent e) {
128: hideWindow();
129: if (!finished) {
130: setInterrupted(true);
131: }
132: continual = false;
133: setStopped(false);
134: }
135: });
136:
137: JPanel prepreNavPane = new JPanel();
138: prepreNavPane.setLayout(new BorderLayout());
139: prepreNavPane.add(finishButton, BorderLayout.NORTH);
140:
141: JPanel preNavPane = new JPanel();
142: preNavPane.setLayout(new BorderLayout());
143: preNavPane.add(contButton, BorderLayout.NORTH);
144: preNavPane.add(prepreNavPane, BorderLayout.CENTER);
145:
146: JPanel navPane = new JPanel();
147: navPane.setLayout(new BorderLayout());
148: navPane.add(nextStepButton, BorderLayout.NORTH);
149: navPane.add(preNavPane, BorderLayout.CENTER);
150:
151: comments = new JTextArea("");
152: comments.setEditable(false);
153: comments.setLineWrap(true);
154:
155: getContentPane().setLayout(new BorderLayout());
156: getContentPane().add(navPane, BorderLayout.WEST);
157: getContentPane().add(new JScrollPane(comments),
158: BorderLayout.CENTER);
159:
160: Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
161: setSize((int) screen.getWidth(), 200);
162: setLocation(0, (int) screen.getHeight() - 200);
163:
164: setModal(modal);
165: }
166:
167: /**
168: * Constructs a DefaultCommentWindow object.
169: */
170: public DefaultCommentWindow() {
171: this (false);
172: }
173:
174: /**
175: * Specifies the time to display comment.
176: * @param timeout lond value.
177: */
178: public void setCommentTimeout(long timeout) {
179: readCommentTimeout = timeout;
180: }
181:
182: public boolean isStopped() {
183: return (stopped);
184: }
185:
186: public void nextStep(String stepComment) {
187: comments.setText(stepComment);
188: nextStepButton.setEnabled(true);
189: contButton.setEnabled(true);
190: finishButton.setEnabled(true);
191: setStopped(true);
192: if (!continual) {
193: new Mover(nextStepButton).enter();
194: } else {
195: new Mover(nextStepButton).push();
196: }
197: showWindow();
198: }
199:
200: public void showFinalComment(String stepComment) {
201: setStopped(true);
202: finished = true;
203: continual = false;
204: finishButton.setEnabled(true);
205: finishButton.setText("Finish");
206: comments.setText(stepComment);
207: new Mover(finishButton).enter();
208: showWindow();
209: }
210:
211: public boolean isInterrupted() {
212: return (interrupted);
213: }
214:
215: public String getInterruptMessage() {
216: return ("Step comments: \"" + comments.getText() + "\"");
217: }
218:
219: public void close() {
220: setVisible(false);
221: }
222:
223: /**
224: * Perform a mouse action at the end of test step.
225: * If demo is executed in continual mode
226: * (i.e. "All Steps" button has been pushed),
227: * performs "Next" button pushing.
228: * Otherwise simply moves mouse to the "Next" button.
229: */
230: public class Mover extends Thread {
231: JButtonOperator bo;
232: boolean toPush = false;
233:
234: /**
235: * Creates a Mover object.
236: * @param button a Button
237: */
238: public Mover(JButton button) {
239: super ();
240: bo = new JButtonOperator(button);
241: Timeouts times = JemmyProperties.getCurrentTimeouts();
242: times.setTimeout("AbstractButton.PushButtonTimeout",
243: readCommentTimeout);
244: bo.setTimeouts(times);
245: }
246:
247: /**
248: * Pushes the button.
249: */
250: public void push() {
251: toPush = true;
252: start();
253: }
254:
255: /**
256: * Moves mouse pointer to the button.
257: */
258: public void enter() {
259: toPush = false;
260: start();
261: }
262:
263: public void run() {
264: try {
265: while (!bo.getSource().isShowing()) {
266: Thread.currentThread().sleep(100);
267: }
268: EventDispatcher.waitQueueEmpty(TestOut.getNullOutput(),
269: JemmyProperties.getCurrentTimeouts());
270: bo.enterMouse();
271: if (toPush) {
272: Thread.currentThread().sleep(readCommentTimeout);
273: bo.push();
274: }
275: } catch (InterruptedException e) {
276: e.printStackTrace();
277: } catch (TimeoutExpiredException e) {
278: e.printStackTrace();
279: }
280: }
281: }
282:
283: private void hideWindow() {
284: if (isModal()) {
285: hide();
286: } else {
287: toBack();
288: }
289: }
290:
291: private void showWindow() {
292: show();
293: if (!isModal()) {
294: toFront();
295: }
296: }
297:
298: private void setStopped(boolean value) {
299: stopped = value;
300: }
301:
302: private void setInterrupted(boolean value) {
303: interrupted = value;
304: }
305: }
|