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: JProgressBarOperator.java,v 1.10 2007/10/05 11:35:43 jskrivanek Exp $ $Revision: 1.10 $ $Date: 2007/10/05 11:35:43 $
044: *
045: */
046:
047: package org.netbeans.jemmy.operators;
048:
049: import org.netbeans.jemmy.ComponentSearcher;
050: import org.netbeans.jemmy.ComponentChooser;
051: import org.netbeans.jemmy.JemmyException;
052: import org.netbeans.jemmy.Outputable;
053: import org.netbeans.jemmy.TestOut;
054: import org.netbeans.jemmy.Timeoutable;
055: import org.netbeans.jemmy.TimeoutExpiredException;
056: import org.netbeans.jemmy.Timeouts;
057: import org.netbeans.jemmy.Waitable;
058: import org.netbeans.jemmy.Waiter;
059:
060: import java.util.Dictionary;
061: import java.util.Hashtable;
062:
063: import java.awt.Component;
064: import java.awt.Container;
065:
066: import javax.swing.BoundedRangeModel;
067: import javax.swing.JButton;
068: import javax.swing.JProgressBar;
069:
070: import javax.swing.event.ChangeListener;
071:
072: import javax.swing.plaf.ProgressBarUI;
073:
074: /**
075: *
076: * Operator is supposed to be used to operate with an instance of
077: * javax.swing.JProgressBar class.
078: *
079: * <BR><BR>Timeouts used: <BR>
080: * JProgressBarOperator.WaitValueTimeout - used from waitValue() method <BR>.
081: *
082: * @see org.netbeans.jemmy.Timeouts
083: *
084: * @author Alexandre Iline (alexandre.iline@sun.com)
085: */
086:
087: public class JProgressBarOperator extends JComponentOperator implements
088: Timeoutable, Outputable {
089:
090: /**
091: * Identifier for a "minimum" property.
092: * @see #getDump
093: */
094: public static final String MINIMUM_DPROP = "Minimum";
095:
096: /**
097: * Identifier for a "maximum" property.
098: * @see #getDump
099: */
100: public static final String MAXIMUM_DPROP = "Maximum";
101:
102: /**
103: * Identifier for a "value" property.
104: * @see #getDump
105: */
106: public static final String VALUE_DPROP = "Value";
107:
108: private static long WAIT_VALUE_TIMEOUT = 60000;
109:
110: private Timeouts timeouts;
111: private TestOut output;
112:
113: /**
114: * Constructor.
115: * @param b JProgressBar component.
116: */
117: public JProgressBarOperator(JProgressBar b) {
118: super (b);
119: }
120:
121: /**
122: * Constructs a JProgressBarOperator object.
123: * @param cont a container
124: * @param chooser a component chooser specifying searching criteria.
125: * @param index an index between appropriate ones.
126: */
127: public JProgressBarOperator(ContainerOperator cont,
128: ComponentChooser chooser, int index) {
129: this ((JProgressBar) cont.waitSubComponent(
130: new JProgressBarFinder(chooser), index));
131: copyEnvironment(cont);
132: }
133:
134: /**
135: * Constructs a JProgressBarOperator object.
136: * @param cont a container
137: * @param chooser a component chooser specifying searching criteria.
138: */
139: public JProgressBarOperator(ContainerOperator cont,
140: ComponentChooser chooser) {
141: this (cont, chooser, 0);
142: }
143:
144: /**
145: * Constructor.
146: * Waits component in container first.
147: * Uses cont's timeout and output for waiting and to init operator.
148: * @param cont Operator pointing a container to search component in.
149: * @param index Ordinal component index.
150: * @throws TimeoutExpiredException
151: */
152: public JProgressBarOperator(ContainerOperator cont, int index) {
153: this ((JProgressBar) waitComponent(cont,
154: new JProgressBarFinder(), index));
155: copyEnvironment(cont);
156: }
157:
158: /**
159: * Constructor.
160: * Waits component in container first.
161: * Uses cont's timeout and output for waiting and to init operator.
162: * @param cont Operator pointing a container to search component in.
163: * @throws TimeoutExpiredException
164: */
165: public JProgressBarOperator(ContainerOperator cont) {
166: this (cont, 0);
167: }
168:
169: /**
170: * Searches JProgressBar in container.
171: * @param cont Container to search component in.
172: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
173: * @param index Ordinal component index.
174: * @return JProgressBar instance or null if component was not found.
175: */
176: public static JProgressBar findJProgressBar(Container cont,
177: ComponentChooser chooser, int index) {
178: return ((JProgressBar) findComponent(cont,
179: new JProgressBarFinder(chooser), index));
180: }
181:
182: /**
183: * Searches 0'th JProgressBar in container.
184: * @param cont Container to search component in.
185: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
186: * @return JProgressBar instance or null if component was not found.
187: */
188: public static JProgressBar findJProgressBar(Container cont,
189: ComponentChooser chooser) {
190: return (findJProgressBar(cont, chooser, 0));
191: }
192:
193: /**
194: * Searches JProgressBar in container.
195: * @param cont Container to search component in.
196: * @param index Ordinal component index.
197: * @return JProgressBar instance or null if component was not found.
198: */
199: public static JProgressBar findJProgressBar(Container cont,
200: int index) {
201: return (findJProgressBar(cont, ComponentSearcher
202: .getTrueChooser(Integer.toString(index)
203: + "'th JProgressBar instance"), index));
204: }
205:
206: /**
207: * Searches 0'th JProgressBar in container.
208: * @param cont Container to search component in.
209: * @return JProgressBar instance or null if component was not found.
210: */
211: public static JProgressBar findJProgressBar(Container cont) {
212: return (findJProgressBar(cont, 0));
213: }
214:
215: /**
216: * Waits JProgressBar in container.
217: * @param cont Container to search component in.
218: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
219: * @param index Ordinal component index.
220: * @return JProgressBar instance or null if component was not displayed.
221: * @throws TimeoutExpiredException
222: */
223: public static JProgressBar waitJProgressBar(Container cont,
224: ComponentChooser chooser, int index) {
225: return ((JProgressBar) waitComponent(cont,
226: new JProgressBarFinder(chooser), index));
227: }
228:
229: /**
230: * Waits 0'th JProgressBar in container.
231: * @param cont Container to search component in.
232: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
233: * @return JProgressBar instance or null if component was not displayed.
234: * @throws TimeoutExpiredException
235: */
236: public static JProgressBar waitJProgressBar(Container cont,
237: ComponentChooser chooser) {
238: return (waitJProgressBar(cont, chooser, 0));
239: }
240:
241: /**
242: * Waits JProgressBar in container.
243: * @param cont Container to search component in.
244: * @param index Ordinal component index.
245: * @return JProgressBar instance or null if component was not displayed.
246: * @throws TimeoutExpiredException
247: */
248: public static JProgressBar waitJProgressBar(Container cont,
249: int index) {
250: return (waitJProgressBar(cont, ComponentSearcher
251: .getTrueChooser(Integer.toString(index)
252: + "'th JProgressBar instance"), index));
253: }
254:
255: /**
256: * Waits 0'th JProgressBar in container.
257: * @param cont Container to search component in.
258: * @return JProgressBar instance or null if component was not displayed.
259: * @throws TimeoutExpiredException
260: */
261: public static JProgressBar waitJProgressBar(Container cont) {
262: return (waitJProgressBar(cont, 0));
263: }
264:
265: static {
266: Timeouts.initDefault("JProgressBarOperator.WaitValueTimeout",
267: WAIT_VALUE_TIMEOUT);
268: }
269:
270: public void setTimeouts(Timeouts timeouts) {
271: this .timeouts = timeouts;
272: super .setTimeouts(timeouts);
273: }
274:
275: public Timeouts getTimeouts() {
276: return (timeouts);
277: }
278:
279: public void setOutput(TestOut out) {
280: output = out;
281: super .setOutput(output.createErrorOutput());
282: }
283:
284: public TestOut getOutput() {
285: return (output);
286: }
287:
288: /**
289: * Waits for criteria defined by <code>chooser</code> to be reached.
290: * @param chooser an object specifying waiting criteria.
291: * @see #waitValue(int)
292: * @deprecated Use waitState(ComponentChooser) instead.
293: */
294: public void waitValue(final ValueChooser chooser) {
295: output.printLine("Wait \"" + chooser.getDescription()
296: + "\" value in progressbar\n : " + toStringSource());
297: output.printGolden("Wait \"" + chooser.getDescription()
298: + "\" value in progressbar");
299: Waiter wt = new Waiter(new Waitable() {
300: public Object actionProduced(Object obj) {
301: return (chooser.checkValue(((JProgressBar) getSource())
302: .getValue()) ? "" : null);
303: }
304:
305: public String getDescription() {
306: return ("\"" + chooser.getDescription() + "\" value");
307: }
308: });
309: wt.setTimeouts(timeouts.cloneThis());
310: wt.getTimeouts().setTimeout(
311: "Waiter.WaitingTime",
312: getTimeouts().getTimeout(
313: "JProgressBarOperator.WaitValueTimeout"));
314: wt.setOutput(output.createErrorOutput());
315: try {
316: wt.waitAction(null);
317: } catch (InterruptedException e) {
318: throw (new JemmyException(
319: "Exception during progressbar value waiting", e));
320: }
321: }
322:
323: /**
324: * Waits progress bar value to be less or equal to <code>value</code> parameter.
325: * Can be used for typical progress bar (when value is increasing).
326: * @param value a value to reach.
327: * @see Operator.waitState(ComponentChooser)
328: */
329: public void waitValue(final int value) {
330: output.printLine("Wait \"" + value
331: + "\" value in progressbar\n : " + toStringSource());
332: output.printGolden("Wait \"" + value
333: + "\" value in progressbar");
334: waitState(new ComponentChooser() {
335: public boolean checkComponent(Component comp) {
336: return (((JProgressBar) comp).getValue() >= value);
337: }
338:
339: public String getDescription() {
340: return ("greater then " + Integer.toString(value));
341: }
342: });
343: }
344:
345: /**
346: * Waits progress bar string to match <code>value</code> parameter.
347: * @param value a string value.
348: * @see Operator.waitState(ComponentChooser)
349: */
350: public void waitValue(final String value) {
351: output
352: .printLine("Wait \"" + value
353: + "\" string in progressbar\n : "
354: + toStringSource());
355: output.printGolden("Wait \"" + value
356: + "\" string in progressbar");
357: waitState(new ComponentChooser() {
358: public boolean checkComponent(Component comp) {
359: return (getComparator().equals(((JProgressBar) comp)
360: .getString(), value));
361: }
362:
363: public String getDescription() {
364: return ("'" + value + "' string");
365: }
366: });
367: }
368:
369: public Hashtable getDump() {
370: Hashtable result = super .getDump();
371: result.put(MINIMUM_DPROP, Integer
372: .toString(((JProgressBar) getSource()).getMinimum()));
373: result.put(MAXIMUM_DPROP, Integer
374: .toString(((JProgressBar) getSource()).getMaximum()));
375: result.put(VALUE_DPROP, Integer
376: .toString(((JProgressBar) getSource()).getValue()));
377: return (result);
378: }
379:
380: ////////////////////////////////////////////////////////
381: //Mapping //
382:
383: /**Maps <code>JProgressBar.addChangeListener(ChangeListener)</code> through queue*/
384: public void addChangeListener(final ChangeListener changeListener) {
385: runMapping(new MapVoidAction("addChangeListener") {
386: public void map() {
387: ((JProgressBar) getSource())
388: .addChangeListener(changeListener);
389: }
390: });
391: }
392:
393: /**Maps <code>JProgressBar.getMaximum()</code> through queue*/
394: public int getMaximum() {
395: return (runMapping(new MapIntegerAction("getMaximum") {
396: public int map() {
397: return (((JProgressBar) getSource()).getMaximum());
398: }
399: }));
400: }
401:
402: /**Maps <code>JProgressBar.getMinimum()</code> through queue*/
403: public int getMinimum() {
404: return (runMapping(new MapIntegerAction("getMinimum") {
405: public int map() {
406: return (((JProgressBar) getSource()).getMinimum());
407: }
408: }));
409: }
410:
411: /**Maps <code>JProgressBar.getModel()</code> through queue*/
412: public BoundedRangeModel getModel() {
413: return ((BoundedRangeModel) runMapping(new MapAction("getModel") {
414: public Object map() {
415: return (((JProgressBar) getSource()).getModel());
416: }
417: }));
418: }
419:
420: /**Maps <code>JProgressBar.getOrientation()</code> through queue*/
421: public int getOrientation() {
422: return (runMapping(new MapIntegerAction("getOrientation") {
423: public int map() {
424: return (((JProgressBar) getSource()).getOrientation());
425: }
426: }));
427: }
428:
429: /**Maps <code>JProgressBar.getPercentComplete()</code> through queue*/
430: public double getPercentComplete() {
431: return (runMapping(new MapDoubleAction("getPercentComplete") {
432: public double map() {
433: return (((JProgressBar) getSource())
434: .getPercentComplete());
435: }
436: }));
437: }
438:
439: /**Maps <code>JProgressBar.getString()</code> through queue*/
440: public String getString() {
441: return ((String) runMapping(new MapAction("getString") {
442: public Object map() {
443: return (((JProgressBar) getSource()).getString());
444: }
445: }));
446: }
447:
448: /**Maps <code>JProgressBar.getUI()</code> through queue*/
449: public ProgressBarUI getUI() {
450: return ((ProgressBarUI) runMapping(new MapAction("getUI") {
451: public Object map() {
452: return (((JProgressBar) getSource()).getUI());
453: }
454: }));
455: }
456:
457: /**Maps <code>JProgressBar.getValue()</code> through queue*/
458: public int getValue() {
459: return (runMapping(new MapIntegerAction("getValue") {
460: public int map() {
461: return (((JProgressBar) getSource()).getValue());
462: }
463: }));
464: }
465:
466: /**Maps <code>JProgressBar.isBorderPainted()</code> through queue*/
467: public boolean isBorderPainted() {
468: return (runMapping(new MapBooleanAction("isBorderPainted") {
469: public boolean map() {
470: return (((JProgressBar) getSource()).isBorderPainted());
471: }
472: }));
473: }
474:
475: /**Maps <code>JProgressBar.isStringPainted()</code> through queue*/
476: public boolean isStringPainted() {
477: return (runMapping(new MapBooleanAction("isStringPainted") {
478: public boolean map() {
479: return (((JProgressBar) getSource()).isStringPainted());
480: }
481: }));
482: }
483:
484: /**Maps <code>JProgressBar.removeChangeListener(ChangeListener)</code> through queue*/
485: public void removeChangeListener(final ChangeListener changeListener) {
486: runMapping(new MapVoidAction("removeChangeListener") {
487: public void map() {
488: ((JProgressBar) getSource())
489: .removeChangeListener(changeListener);
490: }
491: });
492: }
493:
494: /**Maps <code>JProgressBar.setBorderPainted(boolean)</code> through queue*/
495: public void setBorderPainted(final boolean b) {
496: runMapping(new MapVoidAction("setBorderPainted") {
497: public void map() {
498: ((JProgressBar) getSource()).setBorderPainted(b);
499: }
500: });
501: }
502:
503: /**Maps <code>JProgressBar.setMaximum(int)</code> through queue*/
504: public void setMaximum(final int i) {
505: runMapping(new MapVoidAction("setMaximum") {
506: public void map() {
507: ((JProgressBar) getSource()).setMaximum(i);
508: }
509: });
510: }
511:
512: /**Maps <code>JProgressBar.setMinimum(int)</code> through queue*/
513: public void setMinimum(final int i) {
514: runMapping(new MapVoidAction("setMinimum") {
515: public void map() {
516: ((JProgressBar) getSource()).setMinimum(i);
517: }
518: });
519: }
520:
521: /**Maps <code>JProgressBar.setModel(BoundedRangeModel)</code> through queue*/
522: public void setModel(final BoundedRangeModel boundedRangeModel) {
523: runMapping(new MapVoidAction("setModel") {
524: public void map() {
525: ((JProgressBar) getSource())
526: .setModel(boundedRangeModel);
527: }
528: });
529: }
530:
531: /**Maps <code>JProgressBar.setOrientation(int)</code> through queue*/
532: public void setOrientation(final int i) {
533: runMapping(new MapVoidAction("setOrientation") {
534: public void map() {
535: ((JProgressBar) getSource()).setOrientation(i);
536: }
537: });
538: }
539:
540: /**Maps <code>JProgressBar.setString(String)</code> through queue*/
541: public void setString(final String string) {
542: runMapping(new MapVoidAction("setString") {
543: public void map() {
544: ((JProgressBar) getSource()).setString(string);
545: }
546: });
547: }
548:
549: /**Maps <code>JProgressBar.setStringPainted(boolean)</code> through queue*/
550: public void setStringPainted(final boolean b) {
551: runMapping(new MapVoidAction("setStringPainted") {
552: public void map() {
553: ((JProgressBar) getSource()).setStringPainted(b);
554: }
555: });
556: }
557:
558: /**Maps <code>JProgressBar.setUI(ProgressBarUI)</code> through queue*/
559: public void setUI(final ProgressBarUI progressBarUI) {
560: runMapping(new MapVoidAction("setUI") {
561: public void map() {
562: ((JProgressBar) getSource()).setUI(progressBarUI);
563: }
564: });
565: }
566:
567: /**Maps <code>JProgressBar.setValue(int)</code> through queue*/
568: public void setValue(final int i) {
569: runMapping(new MapVoidAction("setValue") {
570: public void map() {
571: ((JProgressBar) getSource()).setValue(i);
572: }
573: });
574: }
575:
576: //End of mapping //
577: ////////////////////////////////////////////////////////
578:
579: /**
580: * Interface to define criteria for <code>waitValue(ValueChooser)</code>
581: * method.
582: * @see #waitValue(int)
583: * @deprecated Use waitState(ComponentChooser) instead.
584: */
585: public interface ValueChooser {
586: /**
587: * Check if criteria jave been reached.
588: * @param value current value.
589: * @return true if criteria reached.
590: */
591: public boolean checkValue(int value);
592:
593: /**
594: * A description.
595: * @return a description.
596: */
597: public String getDescription();
598: }
599:
600: /**
601: * Checks component type.
602: */
603: public static class JProgressBarFinder extends Finder {
604: /**
605: * Constructs JProgressBarFinder.
606: * @param sf other searching criteria.
607: */
608: public JProgressBarFinder(ComponentChooser sf) {
609: super (JProgressBar.class, sf);
610: }
611:
612: /**
613: * Constructs JProgressBarFinder.
614: */
615: public JProgressBarFinder() {
616: super (JProgressBar.class);
617: }
618: }
619:
620: }
|