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: ContainerOperator.java,v 1.9 2007/10/05 11:35:53 jskrivanek Exp $ $Revision: 1.9 $ $Date: 2007/10/05 11:35:53 $
044: *
045: */
046:
047: package org.netbeans.jemmy.operators;
048:
049: import java.awt.event.ContainerListener;
050:
051: import org.netbeans.jemmy.ComponentSearcher;
052: import org.netbeans.jemmy.ComponentChooser;
053: import org.netbeans.jemmy.JemmyException;
054: import org.netbeans.jemmy.Outputable;
055: import org.netbeans.jemmy.TestOut;
056: import org.netbeans.jemmy.Timeoutable;
057: import org.netbeans.jemmy.TimeoutExpiredException;
058: import org.netbeans.jemmy.Timeouts;
059: import org.netbeans.jemmy.Waitable;
060: import org.netbeans.jemmy.Waiter;
061:
062: import java.awt.Component;
063: import java.awt.Container;
064: import java.awt.Graphics;
065: import java.awt.Insets;
066: import java.awt.LayoutManager;
067: import java.awt.Point;
068:
069: /**
070: * <BR><BR>Timeouts used: <BR>
071: * ComponentOperator.WaitComponentTimeout - time to wait container displayed <BR>.
072: *
073: * @see org.netbeans.jemmy.Timeouts
074: *
075: * @author Alexandre Iline (alexandre.iline@sun.com)
076: *
077: */
078:
079: public class ContainerOperator extends ComponentOperator implements
080: Timeoutable, Outputable {
081:
082: private static int POINT_RECT_SIZE = 10;
083: private final static long WAIT_SUBCOMPONENT_TIMEOUT = 60000;
084:
085: private ComponentSearcher searcher;
086: private Timeouts timeouts;
087: private TestOut output;
088:
089: /**
090: * Constructor.
091: * @param b Container component.
092: */
093: public ContainerOperator(Container b) {
094: super (b);
095: searcher = new ComponentSearcher(b);
096: searcher.setOutput(TestOut.getNullOutput());
097: }
098:
099: /**
100: * Constructs a ContainerOperator object.
101: * @param cont container
102: * @param chooser a component chooser specifying searching criteria.
103: * @param index an index between appropriate ones.
104: */
105: public ContainerOperator(ContainerOperator cont,
106: ComponentChooser chooser, int index) {
107: this ((Container) cont.waitSubComponent(new ContainerFinder(
108: chooser), index));
109: copyEnvironment(cont);
110: }
111:
112: /**
113: * Constructs a ContainerOperator object.
114: * @param cont container
115: * @param chooser a component chooser specifying searching criteria.
116: */
117: public ContainerOperator(ContainerOperator cont,
118: ComponentChooser chooser) {
119: this (cont, chooser, 0);
120: }
121:
122: /**
123: * Constructor.
124: * Waits component in container first.
125: * Uses cont's timeout and output for waiting and to init operator.
126: * @param cont Operator pointing a container to search component in.
127: * @param index Ordinal component index.
128: * @throws TimeoutExpiredException
129: */
130: public ContainerOperator(ContainerOperator cont, int index) {
131: this ((Container) waitComponent(cont, new ContainerFinder(),
132: index));
133: copyEnvironment(cont);
134: }
135:
136: /**
137: * Constructor.
138: * Waits component in container first.
139: * Uses cont's timeout and output for waiting and to init operator.
140: * @param cont Operator pointing a container to search component in.
141: * @throws TimeoutExpiredException
142: */
143: public ContainerOperator(ContainerOperator cont) {
144: this (cont, 0);
145: }
146:
147: /**
148: * Searches Container in container.
149: * @param cont Container to search component in.
150: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
151: * @param index Ordinal component index.
152: * @return Container instance or null if component was not found.
153: */
154: public static Container findContainer(Container cont,
155: ComponentChooser chooser, int index) {
156: return ((Container) findComponent(cont, new ContainerFinder(
157: chooser), index));
158: }
159:
160: /**
161: * Searches 0'th Container in container.
162: * @param cont Container to search component in.
163: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
164: * @return Container instance or null if component was not found.
165: */
166: public static Container findContainer(Container cont,
167: ComponentChooser chooser) {
168: return (findContainer(cont, chooser, 0));
169: }
170:
171: /**
172: * Searches Container in container.
173: * @param cont Container to search component in.
174: * @param index Ordinal component index.
175: * @return Container instance or null if component was not found.
176: */
177: public static Container findContainer(Container cont, int index) {
178: return (findContainer(cont, ComponentSearcher
179: .getTrueChooser(Integer.toString(index)
180: + "'th Container instance"), index));
181: }
182:
183: /**
184: * Searches 0'th Container in container.
185: * @param cont Container to search component in.
186: * @return Container instance or null if component was not found.
187: */
188: public static Container findContainer(Container cont) {
189: return (findContainer(cont, 0));
190: }
191:
192: /**
193: * Searches Container object which component lies on.
194: * @param comp Component to find Container under.
195: * @param chooser a chooser specifying searching criteria.
196: * @return Container instance or null if component was not found.
197: */
198: public static Container findContainerUnder(Component comp,
199: ComponentChooser chooser) {
200: return ((Container) new ComponentOperator(comp)
201: .getContainer(new ContainerFinder(chooser)));
202: }
203:
204: /**
205: * Searches Container object which component lies on.
206: * @param comp Component to find Container under.
207: * @return Container instance or null if component was not found.
208: */
209: public static Container findContainerUnder(Component comp) {
210: return (findContainerUnder(comp, ComponentSearcher
211: .getTrueChooser("Container")));
212: }
213:
214: /**
215: * Waits Container in container.
216: * @param cont Container to search component in.
217: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
218: * @param index Ordinal component index.
219: * @return Container instance.
220: * @throws TimeoutExpiredException
221: */
222: public static Container waitContainer(Container cont,
223: ComponentChooser chooser, int index) {
224: return ((Container) waitComponent(cont, new ContainerFinder(
225: chooser), index));
226: }
227:
228: /**
229: * Waits 0'th Container in container.
230: * @param cont Container to search component in.
231: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
232: * @return Container instance.
233: * @throws TimeoutExpiredException
234: */
235: public static Container waitContainer(Container cont,
236: ComponentChooser chooser) {
237: return (waitContainer(cont, chooser, 0));
238: }
239:
240: /**
241: * Waits Container in container.
242: * @param cont Container to search component in.
243: * @param index Ordinal component index.
244: * @return Container instance.
245: * @throws TimeoutExpiredException
246: */
247: public static Container waitContainer(Container cont, int index) {
248: return (waitContainer(cont, ComponentSearcher
249: .getTrueChooser(Integer.toString(index)
250: + "'th Container instance"), index));
251: }
252:
253: /**
254: * Waits 0'th Container in container.
255: * @param cont Container to search component in.
256: * @return Container instance.
257: * @throws TimeoutExpiredException
258: */
259: public static Container waitContainer(Container cont) {
260: return (waitContainer(cont, 0));
261: }
262:
263: static {
264: Timeouts.initDefault("ComponentOperator.WaitComponentTimeout",
265: WAIT_SUBCOMPONENT_TIMEOUT);
266: }
267:
268: public void setTimeouts(Timeouts timeouts) {
269: super .setTimeouts(timeouts);
270: this .timeouts = timeouts;
271: }
272:
273: public Timeouts getTimeouts() {
274: return (timeouts);
275: }
276:
277: public void setOutput(TestOut out) {
278: output = out;
279: super .setOutput(output.createErrorOutput());
280: }
281:
282: public TestOut getOutput() {
283: return (output);
284: }
285:
286: /**
287: * Searches for a subcomponent.
288: * @param chooser a chooser specifying searching criteria.
289: * @param index Ordinal component index.
290: * @return Component instance.
291: */
292: public Component findSubComponent(ComponentChooser chooser,
293: int index) {
294: getOutput().printLine(
295: "Looking for \"" + chooser.getDescription()
296: + "\" subcomponent");
297: return (searcher.findComponent(chooser, index));
298: }
299:
300: /**
301: * Searches for a subcomponent.
302: * @param chooser a chooser specifying searching criteria.
303: * @return Component instance.
304: */
305: public Component findSubComponent(ComponentChooser chooser) {
306: return (findSubComponent(chooser, 0));
307: }
308:
309: /**
310: * Waits for a subcomponent.
311: * @param chooser a chooser specifying searching criteria.
312: * @param index Ordinal component index.
313: * @return Component instance.
314: */
315: public Component waitSubComponent(final ComponentChooser chooser,
316: final int index) {
317: getOutput().printLine(
318: "Waiting for \"" + chooser.getDescription()
319: + "\" subcomponent");
320: final ComponentSearcher searcher = new ComponentSearcher(
321: (Container) getSource());
322: searcher.setOutput(getOutput().createErrorOutput());
323: Waiter waiter = new Waiter(new Waitable() {
324: public Object actionProduced(Object obj) {
325: return (searcher.findComponent(chooser, index));
326: }
327:
328: public String getDescription() {
329: return ("Wait for \"" + chooser.getDescription() + "\" subcomponent to be displayed");
330: }
331: });
332: waiter.setTimeouts(getTimeouts().cloneThis());
333: waiter.getTimeouts().setTimeout(
334: "Waiter.WaitingTime",
335: getTimeouts().getTimeout(
336: "ComponentOperator.WaitComponentTimeout"));
337: waiter.setOutput(getOutput());
338: try {
339: return ((Component) waiter.waitAction(null));
340: } catch (InterruptedException e) {
341: throw (new JemmyException("Waiting for \""
342: + chooser.getDescription()
343: + "\" component has been interrupted", e));
344: }
345: }
346:
347: /**
348: * Waits for a subcomponent.
349: * @param chooser a chooser specifying searching criteria.
350: * @return Component instance.
351: */
352: public Component waitSubComponent(ComponentChooser chooser) {
353: return (waitSubComponent(chooser, 0));
354: }
355:
356: /**
357: * Waits for a subcomponent and creates an operator.
358: * @param chooser a chooser specifying searching criteria.
359: * @param index Ordinal component index.
360: * @return Component instance.
361: */
362: public ComponentOperator createSubOperator(
363: ComponentChooser chooser, int index) {
364: return (createOperator(waitSubComponent(chooser, index)));
365: }
366:
367: /**
368: * Waits for a subcomponent and creates an operator.
369: * @param chooser a chooser specifying searching criteria.
370: * @return Component instance.
371: */
372: public ComponentOperator createSubOperator(ComponentChooser chooser) {
373: return (createSubOperator(chooser, 0));
374: }
375:
376: ////////////////////////////////////////////////////////
377: //Mapping //
378:
379: /**Maps <code>Container.add(Component)</code> through queue*/
380: public Component add(final Component component) {
381: return ((Component) runMapping(new MapAction("add") {
382: public Object map() {
383: return (((Container) getSource()).add(component));
384: }
385: }));
386: }
387:
388: /**Maps <code>Container.add(Component, int)</code> through queue*/
389: public Component add(final Component component, final int i) {
390: return ((Component) runMapping(new MapAction("add") {
391: public Object map() {
392: return (((Container) getSource()).add(component, i));
393: }
394: }));
395: }
396:
397: /**Maps <code>Container.add(Component, Object)</code> through queue*/
398: public void add(final Component component, final Object object) {
399: runMapping(new MapVoidAction("add") {
400: public void map() {
401: ((Container) getSource()).add(component, object);
402: }
403: });
404: }
405:
406: /**Maps <code>Container.add(Component, Object, int)</code> through queue*/
407: public void add(final Component component, final Object object,
408: final int i) {
409: runMapping(new MapVoidAction("add") {
410: public void map() {
411: ((Container) getSource()).add(component, object, i);
412: }
413: });
414: }
415:
416: /**Maps <code>Container.add(String, Component)</code> through queue*/
417: public Component add(final String string, final Component component) {
418: return ((Component) runMapping(new MapAction("add") {
419: public Object map() {
420: return (((Container) getSource())
421: .add(string, component));
422: }
423: }));
424: }
425:
426: /**Maps <code>Container.addContainerListener(ContainerListener)</code> through queue*/
427: public void addContainerListener(
428: final ContainerListener containerListener) {
429: runMapping(new MapVoidAction("addContainerListener") {
430: public void map() {
431: ((Container) getSource())
432: .addContainerListener(containerListener);
433: }
434: });
435: }
436:
437: /**Maps <code>Container.findComponentAt(int, int)</code> through queue*/
438: public Component findComponentAt(final int i, final int i1) {
439: return ((Component) runMapping(new MapAction("findComponentAt") {
440: public Object map() {
441: return (((Container) getSource())
442: .findComponentAt(i, i1));
443: }
444: }));
445: }
446:
447: /**Maps <code>Container.findComponentAt(Point)</code> through queue*/
448: public Component findComponentAt(final Point point) {
449: return ((Component) runMapping(new MapAction("findComponentAt") {
450: public Object map() {
451: return (((Container) getSource())
452: .findComponentAt(point));
453: }
454: }));
455: }
456:
457: /**Maps <code>Container.getComponent(int)</code> through queue*/
458: public Component getComponent(final int i) {
459: return ((Component) runMapping(new MapAction("getComponent") {
460: public Object map() {
461: return (((Container) getSource()).getComponent(i));
462: }
463: }));
464: }
465:
466: /**Maps <code>Container.getComponentCount()</code> through queue*/
467: public int getComponentCount() {
468: return (runMapping(new MapIntegerAction("getComponentCount") {
469: public int map() {
470: return (((Container) getSource()).getComponentCount());
471: }
472: }));
473: }
474:
475: /**Maps <code>Container.getComponents()</code> through queue*/
476: public Component[] getComponents() {
477: return ((Component[]) runMapping(new MapAction("getComponents") {
478: public Object map() {
479: return (((Container) getSource()).getComponents());
480: }
481: }));
482: }
483:
484: /**Maps <code>Container.getInsets()</code> through queue*/
485: public Insets getInsets() {
486: return ((Insets) runMapping(new MapAction("getInsets") {
487: public Object map() {
488: return (((Container) getSource()).getInsets());
489: }
490: }));
491: }
492:
493: /**Maps <code>Container.getLayout()</code> through queue*/
494: public LayoutManager getLayout() {
495: return ((LayoutManager) runMapping(new MapAction("getLayout") {
496: public Object map() {
497: return (((Container) getSource()).getLayout());
498: }
499: }));
500: }
501:
502: /**Maps <code>Container.isAncestorOf(Component)</code> through queue*/
503: public boolean isAncestorOf(final Component component) {
504: return (runMapping(new MapBooleanAction("isAncestorOf") {
505: public boolean map() {
506: return (((Container) getSource())
507: .isAncestorOf(component));
508: }
509: }));
510: }
511:
512: /**Maps <code>Container.paintComponents(Graphics)</code> through queue*/
513: public void paintComponents(final Graphics graphics) {
514: runMapping(new MapVoidAction("paintComponents") {
515: public void map() {
516: ((Container) getSource()).paintComponents(graphics);
517: }
518: });
519: }
520:
521: /**Maps <code>Container.printComponents(Graphics)</code> through queue*/
522: public void printComponents(final Graphics graphics) {
523: runMapping(new MapVoidAction("printComponents") {
524: public void map() {
525: ((Container) getSource()).printComponents(graphics);
526: }
527: });
528: }
529:
530: /**Maps <code>Container.remove(int)</code> through queue*/
531: public void remove(final int i) {
532: runMapping(new MapVoidAction("remove") {
533: public void map() {
534: ((Container) getSource()).remove(i);
535: }
536: });
537: }
538:
539: /**Maps <code>Container.remove(Component)</code> through queue*/
540: public void remove(final Component component) {
541: runMapping(new MapVoidAction("remove") {
542: public void map() {
543: ((Container) getSource()).remove(component);
544: }
545: });
546: }
547:
548: /**Maps <code>Container.removeAll()</code> through queue*/
549: public void removeAll() {
550: runMapping(new MapVoidAction("removeAll") {
551: public void map() {
552: ((Container) getSource()).removeAll();
553: }
554: });
555: }
556:
557: /**Maps <code>Container.removeContainerListener(ContainerListener)</code> through queue*/
558: public void removeContainerListener(
559: final ContainerListener containerListener) {
560: runMapping(new MapVoidAction("removeContainerListener") {
561: public void map() {
562: ((Container) getSource())
563: .removeContainerListener(containerListener);
564: }
565: });
566: }
567:
568: /**Maps <code>Container.setLayout(LayoutManager)</code> through queue*/
569: public void setLayout(final LayoutManager layoutManager) {
570: runMapping(new MapVoidAction("setLayout") {
571: public void map() {
572: ((Container) getSource()).setLayout(layoutManager);
573: }
574: });
575: }
576:
577: //End of mapping //
578: ////////////////////////////////////////////////////////
579:
580: /**
581: * Checks component type.
582: */
583: public static class ContainerFinder extends Finder {
584: /**
585: * Constructs ContainerFinder.
586: * @param sf other searching criteria.
587: */
588: public ContainerFinder(ComponentChooser sf) {
589: super (Container.class, sf);
590: }
591:
592: /**
593: * Constructs ContainerFinder.
594: */
595: public ContainerFinder() {
596: super (Container.class);
597: }
598: }
599: }
|