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.operators;
048:
049: import org.netbeans.jemmy.ComponentChooser;
050: import org.netbeans.jemmy.ComponentSearcher;
051: import org.netbeans.jemmy.JemmyProperties;
052: import org.netbeans.jemmy.Timeouts;
053: import org.netbeans.jemmy.TimeoutExpiredException;
054: import org.netbeans.jemmy.FrameWaiter;
055:
056: import java.awt.Component;
057: import java.awt.Container;
058:
059: import javax.accessibility.AccessibleContext;
060:
061: import javax.swing.JFrame;
062: import javax.swing.JLayeredPane;
063: import javax.swing.JMenuBar;
064: import javax.swing.JRootPane;
065:
066: /**
067: * <BR><BR>Timeouts used: <BR>
068: * FrameWaiter.WaitFrameTimeout - time to wait frame displayed <BR>
069: * FrameWaiter.AfterFrameTimeout - time to sleep after frame has been dispayed <BR>.
070: *
071: * @see org.netbeans.jemmy.Timeouts
072: *
073: * @author Alexandre Iline (alexandre.iline@sun.com)
074: *
075: */
076:
077: public class JFrameOperator extends FrameOperator {
078:
079: /**
080: * Constructor.
081: * @param w window
082: */
083: public JFrameOperator(JFrame w) {
084: super (w);
085: }
086:
087: /**
088: * Constructs a JFrameOperator object.
089: * @param chooser a component chooser specifying searching criteria.
090: * @param index an index between appropriate ones.
091: * @param env an operator to copy environment from.
092: */
093: public JFrameOperator(ComponentChooser chooser, int index,
094: Operator env) {
095: this ((JFrame) waitFrame(new JFrameFinder(chooser), index, env
096: .getTimeouts(), env.getOutput()));
097: copyEnvironment(env);
098: }
099:
100: /**
101: * Constructs a JFrameOperator object.
102: * @param chooser a component chooser specifying searching criteria.
103: * @param index an index between appropriate ones.
104: */
105: public JFrameOperator(ComponentChooser chooser, int index) {
106: this (chooser, index, Operator.getEnvironmentOperator());
107: }
108:
109: /**
110: * Constructs a JFrameOperator object.
111: * @param chooser a component chooser specifying searching criteria.
112: */
113: public JFrameOperator(ComponentChooser chooser) {
114: this (chooser, 0);
115: }
116:
117: /**
118: * Constructor.
119: * Waits for the frame with "title" subtitle.
120: * Constructor can be used in complicated cases when
121: * output or timeouts should differ from default.
122: * @param title a window title
123: * @param index Ordinal component index.
124: * @param env an operator to copy environment from.
125: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
126: *
127: */
128: public JFrameOperator(String title, int index, Operator env) {
129: this (new JFrameFinder(new FrameByTitleFinder(title, env
130: .getComparator())), index, env);
131: }
132:
133: /**
134: * Constructor.
135: * Waits for the frame with "title" subtitle.
136: * Uses current timeouts and output values.
137: * @param title a window title
138: * @param index Ordinal component index.
139: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
140: * @see JemmyProperties#getCurrentTimeouts()
141: * @see JemmyProperties#getCurrentOutput()
142: *
143: */
144: public JFrameOperator(String title, int index) {
145: this (title, index, ComponentOperator.getEnvironmentOperator());
146: }
147:
148: /**
149: * Constructor.
150: * Waits for the frame with "title" subtitle.
151: * Uses current timeouts and output values.
152: * @param title a window title
153: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
154: * @see JemmyProperties#getCurrentTimeouts()
155: * @see JemmyProperties#getCurrentOutput()
156: *
157: */
158: public JFrameOperator(String title) {
159: this (title, 0);
160: }
161:
162: /**
163: * Constructor.
164: * Waits for the index'th frame.
165: * Uses current timeout and output for waiting and to init operator.
166: * @param index Ordinal component index.
167: *
168: */
169: public JFrameOperator(int index) {
170: this ((JFrame) (JFrame) waitFrame(new JFrameFinder(), index,
171: ComponentOperator.getEnvironmentOperator()
172: .getTimeouts(), ComponentOperator
173: .getEnvironmentOperator().getOutput()));
174: copyEnvironment(ComponentOperator.getEnvironmentOperator());
175: }
176:
177: /**
178: * Constructor.
179: * Waits for the first frame.
180: * Uses current timeout and output for waiting and to init operator.
181: *
182: */
183: public JFrameOperator() {
184: this (0);
185: }
186:
187: /**
188: * Searches an index'th frame.
189: * @param chooser a component chooser specifying searching criteria.
190: * @param index an index between appropriate ones.
191: * @return JFrame instance or null if component was not found.
192: */
193: public static JFrame findJFrame(ComponentChooser chooser, int index) {
194: return ((JFrame) FrameWaiter.getFrame(
195: new JFrameFinder(chooser), index));
196: }
197:
198: /**
199: * Searches a frame.
200: * @param chooser a component chooser specifying searching criteria.
201: * @return JFrame instance or null if component was not found.
202: */
203: public static JFrame findJFrame(ComponentChooser chooser) {
204: return (findJFrame(chooser, 0));
205: }
206:
207: /**
208: * Searches an index'th frame by title.
209: * @param title Frame title
210: * @param ce Compare exactly. If true, text can be a substring of caption.
211: * @param cc Compare case sensitively. If true, both text and caption are
212: * @param index an index between appropriate ones.
213: * @return JFrame instance or null if component was not found.
214: */
215: public static JFrame findJFrame(String title, boolean ce,
216: boolean cc, int index) {
217: return ((JFrame) FrameWaiter.getFrame(new JFrameFinder(
218: new FrameByTitleFinder(title,
219: new DefaultStringComparator(ce, cc))), index));
220: }
221:
222: /**
223: * Searches a frame by title.
224: * @param title Frame title
225: * @param ce Compare exactly. If true, text can be a substring of caption.
226: * @param cc Compare case sensitively. If true, both text and caption are
227: * @return JFrame instance or null if component was not found.
228: */
229: public static JFrame findJFrame(String title, boolean ce, boolean cc) {
230: return (findJFrame(title, ce, cc, 0));
231: }
232:
233: /**
234: * Waits an index'th frame.
235: * @param chooser a component chooser specifying searching criteria.
236: * @param index an index between appropriate ones.
237: * @return JFrame instance or null if component was not found.
238: *
239: */
240: public static JFrame waitJFrame(ComponentChooser chooser, int index) {
241: return ((JFrame) waitFrame(new JFrameFinder(chooser), index,
242: JemmyProperties.getCurrentTimeouts(), JemmyProperties
243: .getCurrentOutput()));
244: }
245:
246: /**
247: * Waits a frame.
248: * @param chooser a component chooser specifying searching criteria.
249: * @return JFrame instance or null if component was not found.
250: *
251: */
252: public static JFrame waitJFrame(ComponentChooser chooser) {
253: return (waitJFrame(chooser, 0));
254: }
255:
256: /**
257: * Waits an index'th frame by title.
258: * @param title Frame title
259: * @param ce Compare exactly. If true, text can be a substring of caption.
260: * @param cc Compare case sensitively. If true, both text and caption are
261: * @param index an index between appropriate ones.
262: * @return JFrame instance or null if component was not found.
263: *
264: */
265: public static JFrame waitJFrame(String title, boolean ce,
266: boolean cc, int index) {
267: try {
268: return ((JFrame) (new FrameWaiter()).waitFrame(
269: new JFrameFinder(new FrameByTitleFinder(title,
270: new DefaultStringComparator(ce, cc))),
271: index));
272: } catch (InterruptedException e) {
273: JemmyProperties.getCurrentOutput().printStackTrace(e);
274: return (null);
275: }
276: }
277:
278: /**
279: * Waits a frame by title.
280: * @param title Frame title
281: * @param ce Compare exactly. If true, text can be a substring of caption.
282: * @param cc Compare case sensitively. If true, both text and caption are
283: * @return JFrame instance or null if component was not found.
284: *
285: */
286: public static JFrame waitJFrame(String title, boolean ce, boolean cc) {
287: return (waitJFrame(title, ce, cc, 0));
288: }
289:
290: ////////////////////////////////////////////////////////
291: //Mapping //
292:
293: /**Maps <code>JFrame.getAccessibleContext()</code> through queue*/
294: public AccessibleContext getAccessibleContext() {
295: return ((AccessibleContext) runMapping(new MapAction(
296: "getAccessibleContext") {
297: public Object map() {
298: return (((JFrame) getSource()).getAccessibleContext());
299: }
300: }));
301: }
302:
303: /**Maps <code>JFrame.getContentPane()</code> through queue*/
304: public Container getContentPane() {
305: return ((Container) runMapping(new MapAction("getContentPane") {
306: public Object map() {
307: return (((JFrame) getSource()).getContentPane());
308: }
309: }));
310: }
311:
312: /**Maps <code>JFrame.getDefaultCloseOperation()</code> through queue*/
313: public int getDefaultCloseOperation() {
314: return (runMapping(new MapIntegerAction(
315: "getDefaultCloseOperation") {
316: public int map() {
317: return (((JFrame) getSource())
318: .getDefaultCloseOperation());
319: }
320: }));
321: }
322:
323: /**Maps <code>JFrame.getGlassPane()</code> through queue*/
324: public Component getGlassPane() {
325: return ((Component) runMapping(new MapAction("getGlassPane") {
326: public Object map() {
327: return (((JFrame) getSource()).getGlassPane());
328: }
329: }));
330: }
331:
332: /**Maps <code>JFrame.getJMenuBar()</code> through queue*/
333: public JMenuBar getJMenuBar() {
334: return ((JMenuBar) runMapping(new MapAction("getJMenuBar") {
335: public Object map() {
336: return (((JFrame) getSource()).getJMenuBar());
337: }
338: }));
339: }
340:
341: /**Maps <code>JFrame.getLayeredPane()</code> through queue*/
342: public JLayeredPane getLayeredPane() {
343: return ((JLayeredPane) runMapping(new MapAction(
344: "getLayeredPane") {
345: public Object map() {
346: return (((JFrame) getSource()).getLayeredPane());
347: }
348: }));
349: }
350:
351: /**Maps <code>JFrame.getRootPane()</code> through queue*/
352: public JRootPane getRootPane() {
353: return ((JRootPane) runMapping(new MapAction("getRootPane") {
354: public Object map() {
355: return (((JFrame) getSource()).getRootPane());
356: }
357: }));
358: }
359:
360: /**Maps <code>JFrame.setContentPane(Container)</code> through queue*/
361: public void setContentPane(final Container container) {
362: runMapping(new MapVoidAction("setContentPane") {
363: public void map() {
364: ((JFrame) getSource()).setContentPane(container);
365: }
366: });
367: }
368:
369: /**Maps <code>JFrame.setDefaultCloseOperation(int)</code> through queue*/
370: public void setDefaultCloseOperation(final int i) {
371: runMapping(new MapVoidAction("setDefaultCloseOperation") {
372: public void map() {
373: ((JFrame) getSource()).setDefaultCloseOperation(i);
374: }
375: });
376: }
377:
378: /**Maps <code>JFrame.setGlassPane(Component)</code> through queue*/
379: public void setGlassPane(final Component component) {
380: runMapping(new MapVoidAction("setGlassPane") {
381: public void map() {
382: ((JFrame) getSource()).setGlassPane(component);
383: }
384: });
385: }
386:
387: /**Maps <code>JFrame.setJMenuBar(JMenuBar)</code> through queue*/
388: public void setJMenuBar(final JMenuBar jMenuBar) {
389: runMapping(new MapVoidAction("setJMenuBar") {
390: public void map() {
391: ((JFrame) getSource()).setJMenuBar(jMenuBar);
392: }
393: });
394: }
395:
396: /**Maps <code>JFrame.setLayeredPane(JLayeredPane)</code> through queue*/
397: public void setLayeredPane(final JLayeredPane jLayeredPane) {
398: runMapping(new MapVoidAction("setLayeredPane") {
399: public void map() {
400: ((JFrame) getSource()).setLayeredPane(jLayeredPane);
401: }
402: });
403: }
404:
405: //End of mapping //
406: ////////////////////////////////////////////////////////
407:
408: /**
409: * Checks component type.
410: */
411: public static class JFrameFinder extends Finder {
412: /**
413: * Constructs JFrameFinder.
414: * @param sf other searching criteria.
415: */
416: public JFrameFinder(ComponentChooser sf) {
417: super (JFrame.class, sf);
418: }
419:
420: /**
421: * Constructs JFrameFinder.
422: */
423: public JFrameFinder() {
424: super (JFrame.class);
425: }
426: }
427: }
|