1 /*
   2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.netbeans.jemmy.operators;
  24 
  25 import java.awt.Component;
  26 import java.awt.Container;
  27 
  28 import javax.accessibility.AccessibleContext;
  29 import javax.swing.JFrame;
  30 import javax.swing.JLayeredPane;
  31 import javax.swing.JMenuBar;
  32 import javax.swing.JRootPane;
  33 
  34 import org.netbeans.jemmy.ComponentChooser;
  35 import org.netbeans.jemmy.FrameWaiter;
  36 import org.netbeans.jemmy.JemmyProperties;
  37 
  38 /**
  39  * <BR><BR>Timeouts used: <BR>
  40  * FrameWaiter.WaitFrameTimeout - time to wait frame displayed <BR>
  41  * FrameWaiter.AfterFrameTimeout - time to sleep after frame has been dispayed
  42  * <BR>.
  43  *
  44  * @see org.netbeans.jemmy.Timeouts
  45  *
  46  * @author Alexandre Iline (alexandre.iline@oracle.com)
  47  *
  48  */
  49 public class JFrameOperator extends FrameOperator {
  50 
  51     /**
  52      * Constructor.
  53      *
  54      * @param w window
  55      */
  56     public JFrameOperator(JFrame w) {
  57         super(w);
  58     }
  59 
  60     /**
  61      * Constructs a JFrameOperator object.
  62      *
  63      * @param chooser a component chooser specifying searching criteria.
  64      * @param index an index between appropriate ones.
  65      * @param env an operator to copy environment from.
  66      */
  67     public JFrameOperator(ComponentChooser chooser, int index, Operator env) {
  68         this((JFrame) waitFrame(new JFrameFinder(chooser),
  69                 index,
  70                 env.getTimeouts(),
  71                 env.getOutput()));
  72         copyEnvironment(env);
  73     }
  74 
  75     /**
  76      * Constructs a JFrameOperator object.
  77      *
  78      * @param chooser a component chooser specifying searching criteria.
  79      * @param index an index between appropriate ones.
  80      */
  81     public JFrameOperator(ComponentChooser chooser, int index) {
  82         this(chooser, index, Operator.getEnvironmentOperator());
  83     }
  84 
  85     /**
  86      * Constructs a JFrameOperator object.
  87      *
  88      * @param chooser a component chooser specifying searching criteria.
  89      */
  90     public JFrameOperator(ComponentChooser chooser) {
  91         this(chooser, 0);
  92     }
  93 
  94     /**
  95      * Constructor. Waits for the frame with "title" subtitle. Constructor can
  96      * be used in complicated cases when output or timeouts should differ from
  97      * default.
  98      *
  99      * @param title a window title
 100      * @param index Ordinal component index.
 101      * @param env an operator to copy environment from.
 102      * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
 103      *
 104      */
 105     public JFrameOperator(String title, int index, Operator env) {
 106         this(new FrameByTitleFinder(title, env.getComparator()), index, env);
 107     }
 108 
 109     /**
 110      * Constructor. Waits for the frame with "title" subtitle. Uses current
 111      * timeouts and output values.
 112      *
 113      * @param title a window title
 114      * @param index Ordinal component index.
 115      * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
 116      * @see JemmyProperties#getCurrentTimeouts()
 117      * @see JemmyProperties#getCurrentOutput()
 118      *
 119      */
 120     public JFrameOperator(String title, int index) {
 121         this(title, index,
 122                 ComponentOperator.getEnvironmentOperator());
 123     }
 124 
 125     /**
 126      * Constructor. Waits for the frame with "title" subtitle. Uses current
 127      * timeouts and output values.
 128      *
 129      * @param title a window title
 130      * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
 131      * @see JemmyProperties#getCurrentTimeouts()
 132      * @see JemmyProperties#getCurrentOutput()
 133      *
 134      */
 135     public JFrameOperator(String title) {
 136         this(title, 0);
 137     }
 138 
 139     /**
 140      * Constructor. Waits for the index'th frame. Uses current timeout and
 141      * output for waiting and to init operator.
 142      *
 143      * @param index Ordinal component index.
 144      *
 145      */
 146     public JFrameOperator(int index) {
 147         this((JFrame) waitFrame(new JFrameFinder(),
 148                 index,
 149                 ComponentOperator.getEnvironmentOperator().getTimeouts(),
 150                 ComponentOperator.getEnvironmentOperator().getOutput()));
 151         copyEnvironment(ComponentOperator.getEnvironmentOperator());
 152     }
 153 
 154     /**
 155      * Constructor. Waits for the first frame. Uses current timeout and output
 156      * for waiting and to init operator.
 157      *
 158      */
 159     public JFrameOperator() {
 160         this(0);
 161     }
 162 
 163     /**
 164      * Searches an index'th frame.
 165      *
 166      * @param chooser a component chooser specifying searching criteria.
 167      * @param index an index between appropriate ones.
 168      * @return JFrame instance or null if component was not found.
 169      */
 170     public static JFrame findJFrame(ComponentChooser chooser, int index) {
 171         return (JFrame) FrameWaiter.getFrame(new JFrameFinder(chooser), index);
 172     }
 173 
 174     /**
 175      * Searches a frame.
 176      *
 177      * @param chooser a component chooser specifying searching criteria.
 178      * @return JFrame instance or null if component was not found.
 179      */
 180     public static JFrame findJFrame(ComponentChooser chooser) {
 181         return findJFrame(chooser, 0);
 182     }
 183 
 184     /**
 185      * Searches an index'th frame by title.
 186      *
 187      * @param title Frame title
 188      * @param ce Compare exactly. If true, text can be a substring of caption.
 189      * @param cc Compare case sensitively. If true, both text and caption are
 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(String title, boolean ce, boolean cc, int index) {
 194         return ((JFrame) FrameWaiter.
 195                 getFrame(new JFrameFinder(new FrameByTitleFinder(title,
 196                         new DefaultStringComparator(ce, cc))),
 197                         index));
 198     }
 199 
 200     /**
 201      * Searches a frame by title.
 202      *
 203      * @param title Frame title
 204      * @param ce Compare exactly. If true, text can be a substring of caption.
 205      * @param cc Compare case sensitively. If true, both text and caption are
 206      * @return JFrame instance or null if component was not found.
 207      */
 208     public static JFrame findJFrame(String title, boolean ce, boolean cc) {
 209         return findJFrame(title, ce, cc, 0);
 210     }
 211 
 212     /**
 213      * Waits an index'th frame.
 214      *
 215      * @param chooser a component chooser specifying searching criteria.
 216      * @param index an index between appropriate ones.
 217      * @return JFrame instance or null if component was not found.
 218      *
 219      */
 220     public static JFrame waitJFrame(ComponentChooser chooser, int index) {
 221         return ((JFrame) waitFrame(new JFrameFinder(chooser), index,
 222                 JemmyProperties.getCurrentTimeouts(),
 223                 JemmyProperties.getCurrentOutput()));
 224     }
 225 
 226     /**
 227      * Waits a frame.
 228      *
 229      * @param chooser a component chooser specifying searching criteria.
 230      * @return JFrame instance or null if component was not found.
 231      *
 232      */
 233     public static JFrame waitJFrame(ComponentChooser chooser) {
 234         return waitJFrame(chooser, 0);
 235     }
 236 
 237     /**
 238      * Waits an index'th frame by title.
 239      *
 240      * @param title Frame title
 241      * @param ce Compare exactly. If true, text can be a substring of caption.
 242      * @param cc Compare case sensitively. If true, both text and caption are
 243      * @param index an index between appropriate ones.
 244      * @return JFrame instance or null if component was not found.
 245      *
 246      */
 247     public static JFrame waitJFrame(String title, boolean ce, boolean cc, int index) {
 248         try {
 249             return ((JFrame) (new FrameWaiter()).
 250                     waitFrame(new JFrameFinder(new FrameByTitleFinder(title,
 251                             new DefaultStringComparator(ce, cc))),
 252                             index));
 253         } catch (InterruptedException e) {
 254             JemmyProperties.getCurrentOutput().printStackTrace(e);
 255             return null;
 256         }
 257     }
 258 
 259     /**
 260      * Waits a frame by title.
 261      *
 262      * @param title Frame title
 263      * @param ce Compare exactly. If true, text can be a substring of caption.
 264      * @param cc Compare case sensitively. If true, both text and caption are
 265      * @return JFrame instance or null if component was not found.
 266      *
 267      */
 268     public static JFrame waitJFrame(String title, boolean ce, boolean cc) {
 269         return waitJFrame(title, ce, cc, 0);
 270     }
 271 
 272     ////////////////////////////////////////////////////////
 273     //Mapping                                             //
 274     /**
 275      * Maps {@code JFrame.getAccessibleContext()} through queue
 276      */
 277     public AccessibleContext getAccessibleContext() {
 278         return (runMapping(new MapAction<AccessibleContext>("getAccessibleContext") {
 279             @Override
 280             public AccessibleContext map() {
 281                 return getSource().getAccessibleContext();
 282             }
 283         }));
 284     }
 285 
 286     /**
 287      * Maps {@code JFrame.getContentPane()} through queue
 288      */
 289     public Container getContentPane() {
 290         return (runMapping(new MapAction<Container>("getContentPane") {
 291             @Override
 292             public Container map() {
 293                 return ((JFrame) getSource()).getContentPane();
 294             }
 295         }));
 296     }
 297 
 298     /**
 299      * Maps {@code JFrame.getDefaultCloseOperation()} through queue
 300      */
 301     public int getDefaultCloseOperation() {
 302         return (runMapping(new MapIntegerAction("getDefaultCloseOperation") {
 303             @Override
 304             public int map() {
 305                 return ((JFrame) getSource()).getDefaultCloseOperation();
 306             }
 307         }));
 308     }
 309 
 310     /**
 311      * Maps {@code JFrame.getGlassPane()} through queue
 312      */
 313     public Component getGlassPane() {
 314         return (runMapping(new MapAction<Component>("getGlassPane") {
 315             @Override
 316             public Component map() {
 317                 return ((JFrame) getSource()).getGlassPane();
 318             }
 319         }));
 320     }
 321 
 322     /**
 323      * Maps {@code JFrame.getJMenuBar()} through queue
 324      */
 325     public JMenuBar getJMenuBar() {
 326         return (runMapping(new MapAction<JMenuBar>("getJMenuBar") {
 327             @Override
 328             public JMenuBar map() {
 329                 return ((JFrame) getSource()).getJMenuBar();
 330             }
 331         }));
 332     }
 333 
 334     /**
 335      * Maps {@code JFrame.getLayeredPane()} through queue
 336      */
 337     public JLayeredPane getLayeredPane() {
 338         return (runMapping(new MapAction<JLayeredPane>("getLayeredPane") {
 339             @Override
 340             public JLayeredPane map() {
 341                 return ((JFrame) getSource()).getLayeredPane();
 342             }
 343         }));
 344     }
 345 
 346     /**
 347      * Maps {@code JFrame.getRootPane()} through queue
 348      */
 349     public JRootPane getRootPane() {
 350         return (runMapping(new MapAction<JRootPane>("getRootPane") {
 351             @Override
 352             public JRootPane map() {
 353                 return ((JFrame) getSource()).getRootPane();
 354             }
 355         }));
 356     }
 357 
 358     /**
 359      * Maps {@code JFrame.setContentPane(Container)} through queue
 360      */
 361     public void setContentPane(final Container container) {
 362         runMapping(new MapVoidAction("setContentPane") {
 363             @Override
 364             public void map() {
 365                 ((JFrame) getSource()).setContentPane(container);
 366             }
 367         });
 368     }
 369 
 370     /**
 371      * Maps {@code JFrame.setDefaultCloseOperation(int)} through queue
 372      */
 373     public void setDefaultCloseOperation(final int i) {
 374         runMapping(new MapVoidAction("setDefaultCloseOperation") {
 375             @Override
 376             public void map() {
 377                 ((JFrame) getSource()).setDefaultCloseOperation(i);
 378             }
 379         });
 380     }
 381 
 382     /**
 383      * Maps {@code JFrame.setGlassPane(Component)} through queue
 384      */
 385     public void setGlassPane(final Component component) {
 386         runMapping(new MapVoidAction("setGlassPane") {
 387             @Override
 388             public void map() {
 389                 ((JFrame) getSource()).setGlassPane(component);
 390             }
 391         });
 392     }
 393 
 394     /**
 395      * Maps {@code JFrame.setJMenuBar(JMenuBar)} through queue
 396      */
 397     public void setJMenuBar(final JMenuBar jMenuBar) {
 398         runMapping(new MapVoidAction("setJMenuBar") {
 399             @Override
 400             public void map() {
 401                 ((JFrame) getSource()).setJMenuBar(jMenuBar);
 402             }
 403         });
 404     }
 405 
 406     /**
 407      * Maps {@code JFrame.setLayeredPane(JLayeredPane)} through queue
 408      */
 409     public void setLayeredPane(final JLayeredPane jLayeredPane) {
 410         runMapping(new MapVoidAction("setLayeredPane") {
 411             @Override
 412             public void map() {
 413                 ((JFrame) getSource()).setLayeredPane(jLayeredPane);
 414             }
 415         });
 416     }
 417 
 418     //End of mapping                                      //
 419     ////////////////////////////////////////////////////////
 420     /**
 421      * Checks component type.
 422      */
 423     public static class JFrameFinder extends Finder {
 424 
 425         /**
 426          * Constructs JFrameFinder.
 427          *
 428          * @param sf other searching criteria.
 429          */
 430         public JFrameFinder(ComponentChooser sf) {
 431             super(JFrame.class, sf);
 432         }
 433 
 434         /**
 435          * Constructs JFrameFinder.
 436          */
 437         public JFrameFinder() {
 438             super(JFrame.class);
 439         }
 440     }
 441 }