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;
048:
049: import java.awt.Component;
050: import java.awt.Container;
051:
052: /**
053: *
054: * Contains methods to search for components below a
055: * a given <code>java.awt.Container</code> in the display containment hierarchy.
056: * Uses a <code>ComponentChooser</code> interface implementation to find a
057: * component.
058: *
059: * @see ComponentChooser
060: *
061: * @author Alexandre Iline (alexandre.iline@sun.com)
062: */
063:
064: public class ComponentSearcher implements Outputable {
065:
066: private int ordinalIndex;
067: private Container container;
068: private TestOut out;
069: private QueueTool queueTool;
070: private String containerToString;
071:
072: /**
073: * Contructor.
074: * The search is constrained so that only components that lie below
075: * the given container in the containment hierarchy are considered.
076: * @param c Container to find components in.
077: */
078: public ComponentSearcher(Container c) {
079: super ();
080: container = c;
081: setOutput(JemmyProperties.getProperties().getOutput());
082: queueTool = new QueueTool();
083: }
084:
085: /**
086: * Creates <code>ComponentChooser</code> implementation
087: * whose <code>checkComponent(Component)</code>
088: * method returns <code>true</code> for any component.
089: * @param description Component description.
090: * @return ComponentChooser instance.
091: */
092: public static ComponentChooser getTrueChooser(String description) {
093: class TrueChooser implements ComponentChooser {
094: private String description;
095:
096: public TrueChooser(String desc) {
097: description = desc;
098: }
099:
100: public boolean checkComponent(Component comp) {
101: return (true);
102: }
103:
104: public String getDescription() {
105: return (description);
106: }
107: }
108: return (new TrueChooser(description));
109: }
110:
111: /**
112: * Defines print output streams or writers.
113: *
114: * @param output ?out? Identify the streams or writers used for print output.
115: * @see org.netbeans.jemmy.TestOut
116: * @see org.netbeans.jemmy.Outputable
117: * @see #getOutput
118: */
119: public void setOutput(TestOut output) {
120: out = output;
121: }
122:
123: /**
124: * Returns print output streams or writers.
125: * @return an object that contains references to objects for
126: * printing to output and err streams.
127: * @see org.netbeans.jemmy.TestOut
128: * @see org.netbeans.jemmy.Outputable
129: * @see #setOutput
130: */
131: public TestOut getOutput() {
132: return (out);
133: }
134:
135: /** Returns container.toString(). It is called in dispatch thread.
136: * @return container.toString()
137: */
138: private String containerToString() {
139: if (containerToString == null) {
140: containerToString = container == null ? "null"
141: : (String) queueTool
142: .invokeSmoothly(new QueueTool.QueueAction(
143: "container.toString()") {
144: public Object launch() {
145: return container.toString();
146: }
147: });
148: }
149: return containerToString;
150: }
151:
152: /**
153: * Searches for a component.
154: * The search for the component proceeds recursively in the component hierarchy
155: * rooted in this <code>ComponentChooser</code>'s container.
156: * @param chooser ComponentChooser instance, defining and applying the
157: * search criteria.
158: * @param index Ordinal component index. Indices start at 0.
159: * @return the <code>index</code>'th component from among those components
160: * for which the chooser's <code>checkComponent(Component)</code> method
161: * returns <code>true</code>.
162: * A <code>null</code> reference is returned if there are fewer than
163: * <code>index-1</code> components meeting the search
164: * criteria exist in the component hierarchy rooted in this
165: * <code>ComponentChooser</code>'s container.
166: */
167: public Component findComponent(ComponentChooser chooser, int index) {
168: ordinalIndex = 0;
169: final Component result = findComponentInContainer(container,
170: chooser, index);
171: if (result != null) {
172: // get result.toString() - run in dispatch thread
173: String resultToString = (String) queueTool
174: .invokeSmoothly(new QueueTool.QueueAction(
175: "result.toString()") {
176: public Object launch() {
177: return result.toString();
178: }
179: });
180: out.printTrace("Component " + chooser.getDescription()
181: + "\n was found in container "
182: + containerToString() + "\n " + resultToString);
183: out.printGolden("Component \"" + chooser.getDescription()
184: + "\" was found");
185: } else {
186: out.printTrace("Component " + chooser.getDescription()
187: + "\n was not found in container "
188: + containerToString());
189: out.printGolden("Component \"" + chooser.getDescription()
190: + "\" was not found");
191: }
192: return (result);
193: }
194:
195: /**
196: * Searches for a component.
197: * The search for the component proceeds recursively in the component hierarchy
198: * rooted in this <code>ComponentChooser</code>'s container.
199: * @param chooser ComponentChooser instance, defining and applying the
200: * search criteria.
201: * @return the first component for which the chooser's
202: * <code>checkComponent(Component)</code> method returns <code>true</code>.
203: * A <code>null</code> reference is returned if no component meeting the search
204: * criteria exist in the component hierarchy rooted in this
205: * <code>ComponentChooser</code>'s container.
206: */
207: public Component findComponent(ComponentChooser chooser) {
208: return (findComponent(chooser, 0));
209: }
210:
211: private Component findComponentInContainer(Container cont,
212: ComponentChooser chooser, int index) {
213: Component[] components = cont.getComponents();
214: Component target;
215: for (int i = 0; i < components.length; i++) {
216: if (components[i] != null) {
217: if (chooser.checkComponent(components[i])) {
218: if (ordinalIndex == index) {
219: return (components[i]);
220: } else {
221: ordinalIndex++;
222: }
223: }
224: if (components[i] instanceof Container) {
225: if ((target = findComponentInContainer(
226: (Container) components[i], chooser, index)) != null) {
227: return (target);
228: }
229: }
230: }
231: }
232: return (null);
233: }
234: }
|