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: DialogOperator.java,v 1.10 2007/10/05 11:35:49 jskrivanek Exp $ $Revision: 1.10 $ $Date: 2007/10/05 11:35:49 $
044: *
045: */
046:
047: package org.netbeans.jemmy.operators;
048:
049: import org.netbeans.jemmy.ComponentChooser;
050: import org.netbeans.jemmy.ComponentSearcher;
051: import org.netbeans.jemmy.DialogWaiter;
052: import org.netbeans.jemmy.JemmyProperties;
053: import org.netbeans.jemmy.TestOut;
054: import org.netbeans.jemmy.TimeoutExpiredException;
055: import org.netbeans.jemmy.Timeouts;
056:
057: import java.awt.Component;
058: import java.awt.Dialog;
059: import java.awt.Window;
060:
061: import java.util.Hashtable;
062:
063: /**
064: * <BR><BR>Timeouts used: <BR>
065: * DialogWaiter.WaitDialogTimeout - time to wait dialog displayed <BR>
066: * DialogWaiter.AfterDialogTimeout - time to sleep after dialog has been dispayed <BR>
067: * ComponentOperator.WaitStateTimeout - time to wait for title <BR>.
068: *
069: * @see org.netbeans.jemmy.Timeouts
070: *
071: * @author Alexandre Iline (alexandre.iline@sun.com)
072: *
073: */
074:
075: public class DialogOperator extends WindowOperator {
076:
077: /**
078: * Identifier for a title property.
079: * @see #getDump
080: */
081: public static final String TITLE_DPROP = "Title";
082:
083: /**
084: * Identifier for a modal property.
085: * @see #getDump
086: */
087: public static final String IS_MODAL_DPROP = "Modal";
088:
089: /**
090: * Identifier for a resizable property.
091: * @see #getDump
092: */
093: public static final String IS_RESIZABLE_DPROP = "Resizable";
094:
095: /**
096: * Constructs a DialogOperator object.
097: * @param w window
098: */
099: public DialogOperator(Dialog w) {
100: super (w);
101: }
102:
103: /**
104: * Constructs a DialogOperator object.
105: * @param chooser a component chooser specifying searching criteria.
106: * @param index an index between appropriate ones.
107: * @param env an operator to copy environment from.
108: */
109: public DialogOperator(ComponentChooser chooser, int index,
110: Operator env) {
111: this (waitDialog(new DialogFinder(chooser), index, env
112: .getTimeouts(), env.getOutput()));
113: copyEnvironment(env);
114: }
115:
116: /**
117: * Constructs a DialogOperator object.
118: * @param chooser a component chooser specifying searching criteria.
119: * @param index an index between appropriate ones.
120: */
121: public DialogOperator(ComponentChooser chooser, int index) {
122: this (chooser, index, Operator.getEnvironmentOperator());
123: }
124:
125: /**
126: * Constructs a DialogOperator object.
127: * @param chooser a component chooser specifying searching criteria.
128: */
129: public DialogOperator(ComponentChooser chooser) {
130: this (chooser, 0);
131: }
132:
133: /**
134: * Constructs a DialogOperator object.
135: * @param owner window - owner
136: * @param chooser a component chooser specifying searching criteria.
137: * @param index an index between appropriate ones.
138: */
139: public DialogOperator(WindowOperator owner,
140: ComponentChooser chooser, int index) {
141: this ((Dialog) owner.waitSubWindow(new DialogFinder(chooser),
142: index));
143: copyEnvironment(owner);
144: }
145:
146: /**
147: * Constructs a DialogOperator object.
148: * @param owner window - owner
149: * @param chooser a component chooser specifying searching criteria.
150: */
151: public DialogOperator(WindowOperator owner, ComponentChooser chooser) {
152: this (owner, chooser, 0);
153: }
154:
155: /**
156: * Constructor.
157: * Waits for a dialog to show. The dialog is identified as the
158: * <code>index+1</code>'th <code>java.awt.Dialog</code> that shows, is
159: * owned by the window managed by the <code>WindowOperator</code>
160: * <code>owner</code>, and that has the desired title. Uses owner's
161: * timeout and output for waiting and to init this operator.
162: * @param owner Operator pointing to a window owner.
163: * @param title The desired title.
164: * @param index Ordinal index. The first dialog has <code>index</code> 0.
165: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
166: * @throws TimeoutExpiredException
167: */
168: public DialogOperator(WindowOperator owner, String title, int index) {
169: this (waitDialog(owner, new DialogByTitleFinder(title, owner
170: .getComparator()), index));
171: copyEnvironment(owner);
172: }
173:
174: /**
175: * Uses owner's timeout and output for waiting and to init operator.
176: * Waits for a dialog to show. The dialog is identified as the
177: * first <code>java.awt.Dialog</code> that shows, is
178: * owned by the window managed by the <code>WindowOperator</code>
179: * <code>owner</code>, and that has the desired title. Uses owner's
180: * timeout and output for waiting and to init this operator.
181: * @param owner Operator pointing to a window owner.
182: * @param title The desired title.
183: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
184: * @throws TimeoutExpiredException
185: */
186: public DialogOperator(WindowOperator owner, String title) {
187: this (owner, title, 0);
188: }
189:
190: /**
191: * Constructor.
192: * Waits for the index'th dialog between owner's children.
193: * Uses owner'th timeout and output for waiting and to init operator.
194: * @param owner Operator pointing to a window owner.
195: * @param index Ordinal component index.
196: * @throws TimeoutExpiredException
197: */
198: public DialogOperator(WindowOperator owner, int index) {
199: this ((Dialog) waitDialog(owner, new DialogFinder(), index));
200: copyEnvironment(owner);
201: }
202:
203: /**
204: * Constructor.
205: * Waits for the first dialog between owner's children.
206: * Uses owner'th timeout and output for waiting and to init operator.
207: * @param owner Operator pointing to a window owner.
208: * @throws TimeoutExpiredException
209: */
210: public DialogOperator(WindowOperator owner) {
211: this (owner, 0);
212: }
213:
214: /**
215: * Constructor.
216: * Waits for the dialog with "title" subtitle.
217: * Constructor can be used in complicated cases when
218: * output or timeouts should differ from default.
219: * @param title a window title
220: * @param index Ordinal component index.
221: * @param env an operator to copy environment from.
222: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
223: * @throws TimeoutExpiredException
224: */
225: public DialogOperator(String title, int index, Operator env) {
226: this (new DialogByTitleFinder(title, env.getComparator()),
227: index, env);
228: }
229:
230: /**
231: * Constructor.
232: * Waits for the dialog with "title" subtitle.
233: * Uses current timeouts and output values.
234: * @param title a window title
235: * @param index Ordinal component index.
236: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
237: * @see JemmyProperties#getCurrentTimeouts()
238: * @see JemmyProperties#getCurrentOutput()
239: * @throws TimeoutExpiredException
240: */
241: public DialogOperator(String title, int index) {
242: this (title, index, ComponentOperator.getEnvironmentOperator());
243: }
244:
245: /**
246: * Constructor.
247: * Waits for the dialog with "title" subtitle.
248: * Uses current timeouts and output values.
249: * @param title a window title
250: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
251: * @see JemmyProperties#getCurrentTimeouts()
252: * @see JemmyProperties#getCurrentOutput()
253: * @throws TimeoutExpiredException
254: */
255: public DialogOperator(String title) {
256: this (title, 0);
257: }
258:
259: /**
260: * Constructor.
261: * Waits for the index'th dialog.
262: * Uses current timeout and output for waiting and to init operator.
263: * @param index Ordinal component index.
264: * @throws TimeoutExpiredException
265: */
266: public DialogOperator(int index) {
267: this ((Dialog) waitDialog(new DialogFinder(), index,
268: ComponentOperator.getEnvironmentOperator()
269: .getTimeouts(), ComponentOperator
270: .getEnvironmentOperator().getOutput()));
271: copyEnvironment(ComponentOperator.getEnvironmentOperator());
272: }
273:
274: /**
275: * Constructor.
276: * Waits for the first dialog.
277: * Uses current timeout and output for waiting and to init operator.
278: * @throws TimeoutExpiredException
279: */
280: public DialogOperator() {
281: this (0);
282: }
283:
284: /**
285: * Waits for title. Uses getComparator() comparator.
286: * @param title Title to wait for.
287: */
288: public void waitTitle(final String title) {
289: getOutput().printLine(
290: "Wait \"" + title + "\" title of dialog \n : "
291: + toStringSource());
292: getOutput().printGolden("Wait \"" + title + "\" title");
293: waitState(new DialogByTitleFinder(title, getComparator()));
294: }
295:
296: /**
297: * Returns information about component.
298: */
299: public Hashtable getDump() {
300: Hashtable result = super .getDump();
301: if (((Dialog) getSource()).getTitle() != null) {
302: result.put(TITLE_DPROP, ((Dialog) getSource()).getTitle());
303: }
304: result.put(IS_MODAL_DPROP,
305: ((Dialog) getSource()).isModal() ? "true" : "false");
306: result.put(IS_RESIZABLE_DPROP, ((Dialog) getSource())
307: .isResizable() ? "true" : "false");
308: return (result);
309: }
310:
311: ////////////////////////////////////////////////////////
312: //Mapping //
313:
314: /**Maps <code>Dialog.getTitle()</code> through queue*/
315: public String getTitle() {
316: return ((String) runMapping(new MapAction("getTitle") {
317: public Object map() {
318: return (((Dialog) getSource()).getTitle());
319: }
320: }));
321: }
322:
323: /**Maps <code>Dialog.isModal()</code> through queue*/
324: public boolean isModal() {
325: return (runMapping(new MapBooleanAction("isModal") {
326: public boolean map() {
327: return (((Dialog) getSource()).isModal());
328: }
329: }));
330: }
331:
332: /**Maps <code>Dialog.isResizable()</code> through queue*/
333: public boolean isResizable() {
334: return (runMapping(new MapBooleanAction("isResizable") {
335: public boolean map() {
336: return (((Dialog) getSource()).isResizable());
337: }
338: }));
339: }
340:
341: /**Maps <code>Dialog.setModal(boolean)</code> through queue*/
342: public void setModal(final boolean b) {
343: runMapping(new MapVoidAction("setModal") {
344: public void map() {
345: ((Dialog) getSource()).setModal(b);
346: }
347: });
348: }
349:
350: /**Maps <code>Dialog.setResizable(boolean)</code> through queue*/
351: public void setResizable(final boolean b) {
352: runMapping(new MapVoidAction("setResizable") {
353: public void map() {
354: ((Dialog) getSource()).setResizable(b);
355: }
356: });
357: }
358:
359: /**Maps <code>Dialog.setTitle(String)</code> through queue*/
360: public void setTitle(final String string) {
361: runMapping(new MapVoidAction("setTitle") {
362: public void map() {
363: ((Dialog) getSource()).setTitle(string);
364: }
365: });
366: }
367:
368: //End of mapping //
369: ////////////////////////////////////////////////////////
370:
371: /**
372: * A method to be used from subclasses.
373: * Uses timeouts and output passed as parameters during the waiting.
374: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
375: * @param index Ordinal component index.
376: * @param timeouts timeouts to be used during the waiting.
377: * @param output an output to be used during the waiting.
378: * @return Component instance or null if component was not found.
379: */
380: protected static Dialog waitDialog(ComponentChooser chooser,
381: int index, Timeouts timeouts, TestOut output) {
382: try {
383: DialogWaiter waiter = new DialogWaiter();
384: waiter.setTimeouts(timeouts);
385: waiter.setOutput(output);
386: return ((Dialog) waiter.waitDialog(
387: new DialogFinder(chooser), index));
388: } catch (InterruptedException e) {
389: output.printStackTrace(e);
390: return (null);
391: }
392: }
393:
394: /**
395: * A method to be used from subclasses.
396: * Uses <code>owner</code>'s timeouts and output during the waiting.
397: * @param owner a window - dialog owner.
398: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
399: * @param index Ordinal component index.
400: * @return Component instance or null if component was not found.
401: */
402: protected static Dialog waitDialog(WindowOperator owner,
403: ComponentChooser chooser, int index) {
404: return (waitDialog((Window) owner.getSource(), chooser, index,
405: owner.getTimeouts(), owner.getOutput()));
406: }
407:
408: /**
409: * A method to be used from subclasses.
410: * Uses timeouts and output passed as parameters during the waiting.
411: * @param owner a window - dialog owner.
412: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
413: * @param index Ordinal component index.
414: * @param timeouts timeouts to be used during the waiting.
415: * @param output an output to be used during the waiting.
416: * @return Component instance or null if component was not found.
417: */
418: protected static Dialog waitDialog(Window owner,
419: ComponentChooser chooser, int index, Timeouts timeouts,
420: TestOut output) {
421: try {
422: DialogWaiter waiter = new DialogWaiter();
423: waiter.setTimeouts(timeouts);
424: waiter.setOutput(output);
425: return ((Dialog) waiter.waitDialog(owner, new DialogFinder(
426: chooser), index));
427: } catch (InterruptedException e) {
428: JemmyProperties.getCurrentOutput().printStackTrace(e);
429: return (null);
430: }
431: }
432:
433: /**
434: * Checks component type.
435: */
436: public static class DialogFinder extends Finder {
437: /**
438: * Constructs DialogFinder.
439: * @param sf other searching criteria.
440: */
441: public DialogFinder(ComponentChooser sf) {
442: super (Dialog.class, sf);
443: }
444:
445: /**
446: * Constructs DialogFinder.
447: */
448: public DialogFinder() {
449: super (Dialog.class);
450: }
451: }
452:
453: /**
454: * Allows to find component by title.
455: */
456: public static class DialogByTitleFinder implements ComponentChooser {
457: String title;
458: StringComparator comparator;
459:
460: /**
461: * Constructs DialogByTitleFinder.
462: * @param t a text pattern
463: * @param comparator specifies string comparision algorithm.
464: */
465: public DialogByTitleFinder(String t, StringComparator comparator) {
466: title = t;
467: this .comparator = comparator;
468: }
469:
470: /**
471: * Constructs DialogByTitleFinder.
472: * @param t a text pattern
473: */
474: public DialogByTitleFinder(String t) {
475: this (t, Operator.getDefaultStringComparator());
476: }
477:
478: public boolean checkComponent(Component comp) {
479: if (comp instanceof Dialog) {
480: if (((Dialog) comp).isShowing()
481: && ((Dialog) comp).getTitle() != null) {
482: return (comparator.equals(((Dialog) comp)
483: .getTitle(), title));
484: }
485: }
486: return (false);
487: }
488:
489: public String getDescription() {
490: return ("Dialog with title \"" + title + "\"");
491: }
492: }
493: }
|