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: LabelOperator.java,v 1.7 2007/10/05 11:35:34 jskrivanek Exp $ $Revision: 1.7 $ $Date: 2007/10/05 11:35:34 $
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.TimeoutExpiredException;
052:
053: import java.awt.Component;
054: import java.awt.Container;
055: import java.awt.Label;
056:
057: import java.util.Hashtable;
058:
059: /**
060: * <BR><BR>Timeouts used: <BR>
061: * ComponentOperator.WaitComponentTimeout - time to wait component displayed <BR>.
062: *
063: * @see org.netbeans.jemmy.Timeouts
064: * @author Alexandre Iline (alexandre.iline@sun.com)
065: *
066: */
067:
068: public class LabelOperator extends ComponentOperator {
069:
070: /**
071: * Identifier for a "text" property.
072: * @see #getDump
073: */
074: public static final String TEXT_DPROP = "Text";
075:
076: /**
077: * Constructor.
078: * @param b a component
079: */
080: public LabelOperator(Label b) {
081: super (b);
082: }
083:
084: /**
085: * Constructs a LabelOperator object.
086: * @param cont a container
087: * @param chooser a component chooser specifying searching criteria.
088: * @param index an index between appropriate ones.
089: */
090: public LabelOperator(ContainerOperator cont,
091: ComponentChooser chooser, int index) {
092: this ((Label) cont.waitSubComponent(new LabelFinder(chooser),
093: index));
094: copyEnvironment(cont);
095: }
096:
097: /**
098: * Constructs a LabelOperator object.
099: * @param cont a container
100: * @param chooser a component chooser specifying searching criteria.
101: */
102: public LabelOperator(ContainerOperator cont,
103: ComponentChooser chooser) {
104: this (cont, chooser, 0);
105: }
106:
107: /**
108: * Constructor.
109: * Waits component in container first.
110: * Uses cont's timeout and output for waiting and to init operator.
111: * @param cont a container
112: * @param text Button text.
113: * @param index Ordinal component index.
114: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
115: * @throws TimeoutExpiredException
116: */
117: public LabelOperator(ContainerOperator cont, String text, int index) {
118: this ((Label) waitComponent(cont, new LabelByLabelFinder(text,
119: cont.getComparator()), index));
120: copyEnvironment(cont);
121: }
122:
123: /**
124: * Constructor.
125: * Waits component in container first.
126: * Uses cont's timeout and output for waiting and to init operator.
127: * @param cont a container
128: * @param text Button text.
129: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
130: * @throws TimeoutExpiredException
131: */
132: public LabelOperator(ContainerOperator cont, String text) {
133: this (cont, text, 0);
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 a container
141: * @param index Ordinal component index.
142: * @throws TimeoutExpiredException
143: */
144: public LabelOperator(ContainerOperator cont, int index) {
145: this ((Label) waitComponent(cont, new LabelFinder(), index));
146: copyEnvironment(cont);
147: }
148:
149: /**
150: * Constructor.
151: * Waits component in container first.
152: * Uses cont's timeout and output for waiting and to init operator.
153: * @param cont a container
154: * @throws TimeoutExpiredException
155: */
156: public LabelOperator(ContainerOperator cont) {
157: this (cont, 0);
158: }
159:
160: /**
161: * Searches Label in container.
162: * @param cont Container to search component in.
163: * @param chooser a component chooser specifying searching criteria.
164: * @param index Ordinal component index.
165: * @return Label instance or null if component was not found.
166: */
167: public static Label findLabel(Container cont,
168: ComponentChooser chooser, int index) {
169: return ((Label) findComponent(cont, new LabelFinder(chooser),
170: index));
171: }
172:
173: /**
174: * Searches Label in container.
175: * @param cont Container to search component in.
176: * @param chooser a component chooser specifying searching criteria.
177: * @return Label instance or null if component was not found.
178: */
179: public static Label findLabel(Container cont,
180: ComponentChooser chooser) {
181: return (findLabel(cont, chooser, 0));
182: }
183:
184: /**
185: * Searches Label by text.
186: * @param cont Container to search component in.
187: * @param text Component text.
188: * @param ce Compare text exactly.
189: * @param ccs Compare text case sensitively.
190: * @param index Ordinal component index.
191: * @return Label instance or null if component was not found.
192: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
193: */
194: public static Label findLabel(Container cont, String text,
195: boolean ce, boolean ccs, int index) {
196: return (findLabel(cont, new LabelByLabelFinder(text,
197: new DefaultStringComparator(ce, ccs)), index));
198: }
199:
200: /**
201: * Searches Label by text.
202: * @param cont Container to search component in.
203: * @param text Component text.
204: * @param ce Compare text exactly.
205: * @param ccs Compare text case sensitively.
206: * @return Label instance or null if component was not found.
207: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
208: */
209: public static Label findLabel(Container cont, String text,
210: boolean ce, boolean ccs) {
211: return (findLabel(cont, text, ce, ccs, 0));
212: }
213:
214: /**
215: * Waits Label in container.
216: * @param cont Container to search component in.
217: * @param chooser a component chooser specifying searching criteria.
218: * @param index Ordinal component index.
219: * @return Label instance.
220: * @throws TimeoutExpiredException
221: */
222: public static Label waitLabel(final Container cont,
223: final ComponentChooser chooser, final int index) {
224: return ((Label) waitComponent(cont, new LabelFinder(chooser),
225: index));
226: }
227:
228: /**
229: * Waits Label in container.
230: * @param cont Container to search component in.
231: * @param chooser a component chooser specifying searching criteria.
232: * @return Label instance.
233: * @throws TimeoutExpiredException
234: */
235: public static Label waitLabel(Container cont,
236: ComponentChooser chooser) {
237: return (waitLabel(cont, chooser, 0));
238: }
239:
240: /**
241: * Waits Label by text.
242: * @param cont Container to search component in.
243: * @param text Component text.
244: * @param ce Compare text exactly.
245: * @param ccs Compare text case sensitively.
246: * @param index Ordinal component index.
247: * @return Label instance.
248: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
249: * @throws TimeoutExpiredException
250: */
251: public static Label waitLabel(Container cont, String text,
252: boolean ce, boolean ccs, int index) {
253: return (waitLabel(cont, new LabelByLabelFinder(text,
254: new DefaultStringComparator(ce, ccs)), index));
255: }
256:
257: /**
258: * Waits Label by text.
259: * @param cont Container to search component in.
260: * @param text Component text.
261: * @param ce Compare text exactly.
262: * @param ccs Compare text case sensitively.
263: * @return Label instance.
264: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
265: * @throws TimeoutExpiredException
266: */
267: public static Label waitLabel(Container cont, String text,
268: boolean ce, boolean ccs) {
269: return (waitLabel(cont, text, ce, ccs, 0));
270: }
271:
272: public Hashtable getDump() {
273: Hashtable result = super .getDump();
274: if (((Label) getSource()).getText() != null) {
275: result.put(TEXT_DPROP, ((Label) getSource()).getText());
276: } else {
277: result.put(TEXT_DPROP, "null");
278: }
279: return (result);
280: }
281:
282: ////////////////////////////////////////////////////////
283: //Mapping //
284:
285: /**Maps <code>Label.getAlignment()</code> through queue*/
286: public int getAlignment() {
287: return (runMapping(new MapIntegerAction("getAlignment") {
288: public int map() {
289: return (((Label) getSource()).getAlignment());
290: }
291: }));
292: }
293:
294: /**Maps <code>Label.getText()</code> through queue*/
295: public String getText() {
296: return ((String) runMapping(new MapAction("getText") {
297: public Object map() {
298: return (((Label) getSource()).getText());
299: }
300: }));
301: }
302:
303: /**Maps <code>Label.setAlignment(int)</code> through queue*/
304: public void setAlignment(final int i) {
305: runMapping(new MapVoidAction("setAlignment") {
306: public void map() {
307: ((Label) getSource()).setAlignment(i);
308: }
309: });
310: }
311:
312: /**Maps <code>Label.setText(String)</code> through queue*/
313: public void setText(final String string) {
314: runMapping(new MapVoidAction("setText") {
315: public void map() {
316: ((Label) getSource()).setText(string);
317: }
318: });
319: }
320:
321: //End of mapping //
322: ////////////////////////////////////////////////////////
323:
324: /**
325: * Allows to find component by LabelByLabelFinder.
326: */
327: public static class LabelByLabelFinder implements ComponentChooser {
328: String label;
329: StringComparator comparator;
330:
331: /**
332: * Constructs LabelByLabelFinder.
333: * @param lb a text pattern
334: * @param comparator specifies string comparision algorithm.
335: */
336: public LabelByLabelFinder(String lb, StringComparator comparator) {
337: label = lb;
338: this .comparator = comparator;
339: }
340:
341: /**
342: * Constructs LabelByLabelFinder.
343: * @param lb a text pattern
344: */
345: public LabelByLabelFinder(String lb) {
346: this (lb, Operator.getDefaultStringComparator());
347: }
348:
349: public boolean checkComponent(Component comp) {
350: if (comp instanceof Label) {
351: if (((Label) comp).getText() != null) {
352: return (comparator.equals(((Label) comp).getText(),
353: label));
354: }
355: }
356: return (false);
357: }
358:
359: public String getDescription() {
360: return ("Label with text \"" + label + "\"");
361: }
362: }
363:
364: /**
365: * Checks component type.
366: */
367: public static class LabelFinder extends Finder {
368: /**
369: * Constructs LabelFinder.
370: * @param sf other searching criteria.
371: */
372: public LabelFinder(ComponentChooser sf) {
373: super (Label.class, sf);
374: }
375:
376: /**
377: * Constructs LabelFinder.
378: */
379: public LabelFinder() {
380: super (Label.class);
381: }
382: }
383: }
|