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: JMenuItemOperator.java,v 1.9 2007/10/05 11:35:44 jskrivanek Exp $ $Revision: 1.9 $ $Date: 2007/10/05 11:35:44 $
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.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:
058: import java.awt.Component;
059: import java.awt.Container;
060:
061: import java.awt.event.KeyEvent;
062: import java.awt.event.MouseEvent;
063:
064: import java.util.Hashtable;
065:
066: import javax.swing.Action;
067: import javax.swing.JMenu;
068: import javax.swing.JMenuItem;
069: import javax.swing.KeyStroke;
070: import javax.swing.MenuElement;
071: import javax.swing.MenuSelectionManager;
072:
073: import javax.swing.event.MenuDragMouseEvent;
074: import javax.swing.event.MenuDragMouseListener;
075: import javax.swing.event.MenuKeyEvent;
076: import javax.swing.event.MenuKeyListener;
077:
078: import javax.swing.plaf.MenuItemUI;
079: import org.netbeans.jemmy.util.EmptyVisualizer;
080:
081: /**
082: *
083: * <BR><BR>Timeouts used: <BR>
084: * JMenuItemOperator.PushMenuTimeout - time between button pressing and releasing<BR>
085: * ComponentOperator.WaitComponentTimeout - time to wait button displayed <BR>
086: * ComponentOperator.WaitComponentEnabledTimeout - time to wait button enabled <BR>.
087: *
088: * @see org.netbeans.jemmy.Timeouts
089: *
090: * @author Alexandre Iline (alexandre.iline@sun.com)
091: *
092: */
093:
094: public class JMenuItemOperator extends AbstractButtonOperator implements
095: Timeoutable, Outputable {
096:
097: private final static long PUSH_MENU_TIMEOUT = 0;
098:
099: private Timeouts timeouts;
100: private TestOut output;
101:
102: /**
103: * Constructor.
104: * @param item a component
105: */
106: public JMenuItemOperator(JMenuItem item) {
107: super (item);
108: setTimeouts(JemmyProperties.getProperties().getTimeouts());
109: setOutput(JemmyProperties.getProperties().getOutput());
110: }
111:
112: /**
113: * Constructs a JMenuItemOperator object.
114: * @param cont a container
115: * @param chooser a component chooser specifying searching criteria.
116: * @param index an index between appropriate ones.
117: */
118: public JMenuItemOperator(ContainerOperator cont,
119: ComponentChooser chooser, int index) {
120: this ((JMenuItem) cont.waitSubComponent(new JMenuItemFinder(
121: chooser), index));
122: copyEnvironment(cont);
123: }
124:
125: /**
126: * Constructs a JMenuItemOperator object.
127: * @param cont a container
128: * @param chooser a component chooser specifying searching criteria.
129: */
130: public JMenuItemOperator(ContainerOperator cont,
131: ComponentChooser chooser) {
132: this (cont, chooser, 0);
133: }
134:
135: /**
136: * Constructor.
137: * Waits component in container first.
138: * Uses cont's timeout and output for waiting and to init operator.
139: * @param cont a container
140: * @param text Button text.
141: * @param index Ordinal component index.
142: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
143: * @throws TimeoutExpiredException
144: */
145: public JMenuItemOperator(ContainerOperator cont, String text,
146: int index) {
147: this ((JMenuItem) waitComponent(cont,
148: new JMenuItemByLabelFinder(text, cont.getComparator()),
149: index));
150: setTimeouts(cont.getTimeouts());
151: setOutput(cont.getOutput());
152: }
153:
154: /**
155: * Constructor.
156: * Waits component in container first.
157: * Uses cont's timeout and output for waiting and to init operator.
158: * @param cont a container
159: * @param text Button text.
160: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
161: * @throws TimeoutExpiredException
162: */
163: public JMenuItemOperator(ContainerOperator cont, String text) {
164: this (cont, text, 0);
165: }
166:
167: /**
168: * Constructor.
169: * Waits component in container first.
170: * Uses cont's timeout and output for waiting and to init operator.
171: * @param cont a container
172: * @param index Ordinal component index.
173: * @throws TimeoutExpiredException
174: */
175: public JMenuItemOperator(ContainerOperator cont, int index) {
176: this ((JMenuItem) waitComponent(cont, new JMenuItemFinder(),
177: index));
178: copyEnvironment(cont);
179: }
180:
181: /**
182: * Constructor.
183: * Waits component in container first.
184: * Uses cont's timeout and output for waiting and to init operator.
185: * @param cont a container
186: * @throws TimeoutExpiredException
187: */
188: public JMenuItemOperator(ContainerOperator cont) {
189: this (cont, 0);
190: }
191:
192: /**
193: * Searches JMenuItem in container.
194: * @param menu Container to search component in.
195: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
196: * @param index Ordinal component index.
197: * @return JMenuItem instance or null if component was not found.
198: */
199: public static JMenuItem findJMenuItem(Container menu,
200: ComponentChooser chooser, int index) {
201: return ((JMenuItem) findComponent(menu, new JMenuItemFinder(
202: chooser), index));
203: }
204:
205: /**
206: * Searches 0'th JMenuItem in container.
207: * @param menu Container to search component in.
208: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
209: * @return JMenuItem instance or null if component was not found.
210: */
211: public static JMenuItem findJMenuItem(Container menu,
212: ComponentChooser chooser) {
213: return (findJMenuItem(menu, chooser, 0));
214: }
215:
216: /**
217: * Searches JMenuItem by text.
218: * @param menu Container to search component in.
219: * @param text Button text. If null, contents is not checked.
220: * @param ce Compare text exactly.
221: * @param ccs Compare text case sensitively.
222: * @param index Ordinal component index.
223: * @return JMenuItem instance or null if component was not found.
224: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
225: */
226: public static JMenuItem findJMenuItem(Container menu, String text,
227: boolean ce, boolean ccs, int index) {
228: return (findJMenuItem(menu, new JMenuItemByLabelFinder(text,
229: new DefaultStringComparator(ce, ccs)), index));
230: }
231:
232: /**
233: * Searches JMenuItem by text.
234: * @param menu Container to search component in.
235: * @param text Button text. If null, contents is not checked.
236: * @param ce Compare text exactly.
237: * @param ccs Compare text case sensitively.
238: * @return JMenuItem instance or null if component was not found.
239: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
240: */
241: public static JMenuItem findJMenuItem(Container menu, String text,
242: boolean ce, boolean ccs) {
243: return (findJMenuItem(menu, text, ce, ccs, 0));
244: }
245:
246: /**
247: * Waits JMenuItem in container.
248: * @param menu Container to search component in.
249: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
250: * @param index Ordinal component index.
251: * @return JMenuItem instance.
252: * @throws TimeoutExpiredException
253: */
254: public static JMenuItem waitJMenuItem(Container menu,
255: ComponentChooser chooser, int index) {
256: return ((JMenuItem) waitComponent(menu, new JMenuItemFinder(
257: chooser), index));
258: }
259:
260: /**
261: * Waits 0'th JMenuItem in container.
262: * @param menu Container to search component in.
263: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
264: * @return JMenuItem instance.
265: * @throws TimeoutExpiredException
266: */
267: public static JMenuItem waitJMenuItem(Container menu,
268: ComponentChooser chooser) {
269: return (waitJMenuItem(menu, chooser, 0));
270: }
271:
272: /**
273: * Waits JMenuItem by text.
274: * @param menu Container to search component in.
275: * @param text Button text. If null, contents is not checked.
276: * @param ce Compare text exactly.
277: * @param ccs Compare text case sensitively.
278: * @param index Ordinal component index.
279: * @return JMenuItem instance.
280: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
281: * @throws TimeoutExpiredException
282: */
283: public static JMenuItem waitJMenuItem(Container menu, String text,
284: boolean ce, boolean ccs, int index) {
285: return (waitJMenuItem(menu, new JMenuItemByLabelFinder(text,
286: new DefaultStringComparator(ce, ccs)), index));
287: }
288:
289: /**
290: * Waits JMenuItem by text.
291: * @param menu Container to search component in.
292: * @param text Button text. If null, contents is not checked.
293: * @param ce Compare text exactly.
294: * @param ccs Compare text case sensitively.
295: * @return JMenuItem instance.
296: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
297: * @throws TimeoutExpiredException
298: */
299: public static JMenuItem waitJMenuItem(Container menu, String text,
300: boolean ce, boolean ccs) {
301: return (waitJMenuItem(menu, text, ce, ccs, 0));
302: }
303:
304: static {
305: Timeouts.initDefault("JMenuItemOperator.PushMenuTimeout",
306: PUSH_MENU_TIMEOUT);
307: }
308:
309: public void setTimeouts(Timeouts timeouts) {
310: super .setTimeouts(timeouts);
311: this .timeouts = timeouts;
312: }
313:
314: public Timeouts getTimeouts() {
315: return (timeouts);
316: }
317:
318: public void setOutput(TestOut out) {
319: super .setOutput(out);
320: output = out;
321: }
322:
323: public TestOut getOutput() {
324: return (output);
325: }
326:
327: public Hashtable getDump() {
328: Hashtable result = super .getDump();
329: result.remove(AbstractButtonOperator.IS_SELECTED_DPROP);
330: return (result);
331: }
332:
333: /** Push this menu item. */
334: public void push() {
335: setVisualizer(new EmptyVisualizer());
336: super .push();
337: }
338:
339: /** Push this menu item and no block further execution. */
340: public void pushNoBlock() {
341: setVisualizer(new EmptyVisualizer());
342: super .pushNoBlock();
343: }
344:
345: ////////////////////////////////////////////////////////
346: //Mapping //
347:
348: /**Maps <code>JMenuItem.addMenuDragMouseListener(MenuDragMouseListener)</code> through queue*/
349: public void addMenuDragMouseListener(
350: final MenuDragMouseListener menuDragMouseListener) {
351: runMapping(new MapVoidAction("addMenuDragMouseListener") {
352: public void map() {
353: ((JMenuItem) getSource())
354: .addMenuDragMouseListener(menuDragMouseListener);
355: }
356: });
357: }
358:
359: /**Maps <code>JMenuItem.addMenuKeyListener(MenuKeyListener)</code> through queue*/
360: public void addMenuKeyListener(final MenuKeyListener menuKeyListener) {
361: runMapping(new MapVoidAction("addMenuKeyListener") {
362: public void map() {
363: ((JMenuItem) getSource())
364: .addMenuKeyListener(menuKeyListener);
365: }
366: });
367: }
368:
369: /**Maps <code>JMenuItem.getAccelerator()</code> through queue*/
370: public KeyStroke getAccelerator() {
371: return ((KeyStroke) runMapping(new MapAction("getAccelerator") {
372: public Object map() {
373: return (((JMenuItem) getSource()).getAccelerator());
374: }
375: }));
376: }
377:
378: /**Maps <code>JMenuItem.getComponent()</code> through queue*/
379: public Component getComponent() {
380: return ((Component) runMapping(new MapAction("getComponent") {
381: public Object map() {
382: return (((JMenuItem) getSource()).getComponent());
383: }
384: }));
385: }
386:
387: /**Maps <code>JMenuItem.getSubElements()</code> through queue*/
388: public MenuElement[] getSubElements() {
389: return ((MenuElement[]) runMapping(new MapAction(
390: "getSubElements") {
391: public Object map() {
392: return (((JMenuItem) getSource()).getSubElements());
393: }
394: }));
395: }
396:
397: /**Maps <code>JMenuItem.isArmed()</code> through queue*/
398: public boolean isArmed() {
399: return (runMapping(new MapBooleanAction("isArmed") {
400: public boolean map() {
401: return (((JMenuItem) getSource()).isArmed());
402: }
403: }));
404: }
405:
406: /**Maps <code>JMenuItem.menuSelectionChanged(boolean)</code> through queue*/
407: public void menuSelectionChanged(final boolean b) {
408: runMapping(new MapVoidAction("menuSelectionChanged") {
409: public void map() {
410: ((JMenuItem) getSource()).menuSelectionChanged(b);
411: }
412: });
413: }
414:
415: /**Maps <code>JMenuItem.processKeyEvent(KeyEvent, MenuElement[], MenuSelectionManager)</code> through queue*/
416: public void processKeyEvent(final KeyEvent keyEvent,
417: final MenuElement[] menuElement,
418: final MenuSelectionManager menuSelectionManager) {
419: runMapping(new MapVoidAction("processKeyEvent") {
420: public void map() {
421: ((JMenuItem) getSource()).processKeyEvent(keyEvent,
422: menuElement, menuSelectionManager);
423: }
424: });
425: }
426:
427: /**Maps <code>JMenuItem.processMenuDragMouseEvent(MenuDragMouseEvent)</code> through queue*/
428: public void processMenuDragMouseEvent(
429: final MenuDragMouseEvent menuDragMouseEvent) {
430: runMapping(new MapVoidAction("processMenuDragMouseEvent") {
431: public void map() {
432: ((JMenuItem) getSource())
433: .processMenuDragMouseEvent(menuDragMouseEvent);
434: }
435: });
436: }
437:
438: /**Maps <code>JMenuItem.processMenuKeyEvent(MenuKeyEvent)</code> through queue*/
439: public void processMenuKeyEvent(final MenuKeyEvent menuKeyEvent) {
440: runMapping(new MapVoidAction("processMenuKeyEvent") {
441: public void map() {
442: ((JMenuItem) getSource())
443: .processMenuKeyEvent(menuKeyEvent);
444: }
445: });
446: }
447:
448: /**Maps <code>JMenuItem.processMouseEvent(MouseEvent, MenuElement[], MenuSelectionManager)</code> through queue*/
449: public void processMouseEvent(final MouseEvent mouseEvent,
450: final MenuElement[] menuElement,
451: final MenuSelectionManager menuSelectionManager) {
452: runMapping(new MapVoidAction("processMouseEvent") {
453: public void map() {
454: ((JMenuItem) getSource()).processMouseEvent(mouseEvent,
455: menuElement, menuSelectionManager);
456: }
457: });
458: }
459:
460: /**Maps <code>JMenuItem.removeMenuDragMouseListener(MenuDragMouseListener)</code> through queue*/
461: public void removeMenuDragMouseListener(
462: final MenuDragMouseListener menuDragMouseListener) {
463: runMapping(new MapVoidAction("removeMenuDragMouseListener") {
464: public void map() {
465: ((JMenuItem) getSource())
466: .removeMenuDragMouseListener(menuDragMouseListener);
467: }
468: });
469: }
470:
471: /**Maps <code>JMenuItem.removeMenuKeyListener(MenuKeyListener)</code> through queue*/
472: public void removeMenuKeyListener(
473: final MenuKeyListener menuKeyListener) {
474: runMapping(new MapVoidAction("removeMenuKeyListener") {
475: public void map() {
476: ((JMenuItem) getSource())
477: .removeMenuKeyListener(menuKeyListener);
478: }
479: });
480: }
481:
482: /**Maps <code>JMenuItem.setAccelerator(KeyStroke)</code> through queue*/
483: public void setAccelerator(final KeyStroke keyStroke) {
484: runMapping(new MapVoidAction("setAccelerator") {
485: public void map() {
486: ((JMenuItem) getSource()).setAccelerator(keyStroke);
487: }
488: });
489: }
490:
491: /**Maps <code>JMenuItem.setArmed(boolean)</code> through queue*/
492: public void setArmed(final boolean b) {
493: runMapping(new MapVoidAction("setArmed") {
494: public void map() {
495: ((JMenuItem) getSource()).setArmed(b);
496: }
497: });
498: }
499:
500: /**Maps <code>JMenuItem.setUI(MenuItemUI)</code> through queue*/
501: public void setUI(final MenuItemUI menuItemUI) {
502: runMapping(new MapVoidAction("setUI") {
503: public void map() {
504: ((JMenuItem) getSource()).setUI(menuItemUI);
505: }
506: });
507: }
508:
509: //End of mapping //
510: ////////////////////////////////////////////////////////
511:
512: /**
513: * Prepares the button to click.
514: */
515: protected void prepareToClick() {
516: output.printLine("Push menu item\n :" + toStringSource());
517: output.printGolden("Push menu item");
518: Timeouts times = timeouts.cloneThis();
519: times
520: .setTimeout(
521: "AbstractButtonOperator.PushButtonTimeout",
522: timeouts
523: .getTimeout("JMenuItemOperator.PushMenuTimeout"));
524: super .setTimeouts(times);
525: super .setOutput(output.createErrorOutput());
526: }
527:
528: static JMenuItemOperator[] getMenuItems(Object[] elements,
529: Operator env) {
530: int size = 0;
531: for (int i = 0; i < elements.length; i++) {
532: if (elements[i] instanceof JMenuItem) {
533: size++;
534: }
535: }
536: JMenuItemOperator[] result = new JMenuItemOperator[size];
537: int index = 0;
538: for (int i = 0; i < elements.length; i++) {
539: if (elements[i] instanceof JMenuItem) {
540: result[index] = new JMenuItemOperator(
541: (JMenuItem) elements[i]);
542: result[index].copyEnvironment(env);
543: index++;
544: }
545: }
546: return (result);
547: }
548:
549: static JMenuItemOperator[] getMenuItems(MenuElement parent,
550: Operator env) {
551: return (getMenuItems(parent.getSubElements(), env));
552: }
553:
554: static JMenuItemOperator[] getMenuItems(JMenu parent, Operator env) {
555: return (getMenuItems(parent.getMenuComponents(), env));
556: }
557:
558: static ComponentChooser[] createChoosers(String[] names,
559: StringComparator comparator) {
560: ComponentChooser[] choosers = new ComponentChooser[names.length];
561: for (int i = 0; i < choosers.length; i++) {
562: choosers[i] = new JMenuItemOperator.JMenuItemByLabelFinder(
563: names[i], comparator);
564: }
565: return (choosers);
566: }
567:
568: /**
569: * Allows to find component by text.
570: */
571: public static class JMenuItemByLabelFinder implements
572: ComponentChooser {
573: String label;
574: StringComparator comparator;
575:
576: /**
577: * Constructs JMenuItemByLabelFinder.
578: * @param lb a text pattern
579: * @param comparator specifies string comparision algorithm.
580: */
581: public JMenuItemByLabelFinder(String lb,
582: StringComparator comparator) {
583: label = lb;
584: this .comparator = comparator;
585: }
586:
587: /**
588: * Constructs JMenuItemByLabelFinder.
589: * @param lb a text pattern
590: */
591: public JMenuItemByLabelFinder(String lb) {
592: this (lb, Operator.getDefaultStringComparator());
593: }
594:
595: public boolean checkComponent(Component comp) {
596: if (comp instanceof JMenuItem) {
597: if (((JMenuItem) comp).getText() != null) {
598: return (comparator.equals(((JMenuItem) comp)
599: .getText(), label));
600: }
601: }
602: return (false);
603: }
604:
605: public String getDescription() {
606: return ("JMenuItem with text \"" + label + "\"");
607: }
608: }
609:
610: /**
611: * Checks component type.
612: */
613: public static class JMenuItemFinder extends Finder {
614: /**
615: * Constructs JMenuItemFinder.
616: * @param sf other searching criteria.
617: */
618: public JMenuItemFinder(ComponentChooser sf) {
619: super (JMenuItem.class, sf);
620: }
621:
622: /**
623: * Constructs JMenuItemFinder.
624: */
625: public JMenuItemFinder() {
626: super (JMenuItem.class);
627: }
628: }
629: }
|