1 /*
   2  * Copyright (c) 2009, 2012, 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. Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package org.jemmy.fx.control;
  26 
  27 import javafx.scene.control.Menu;
  28 import javafx.scene.control.MenuItem;
  29 import org.jemmy.action.FutureAction;
  30 import org.jemmy.control.As;
  31 import org.jemmy.control.ControlInterfaces;
  32 import org.jemmy.control.ControlType;
  33 import org.jemmy.control.Property;
  34 import org.jemmy.env.Environment;
  35 import org.jemmy.input.StringMenuOwner;
  36 import org.jemmy.interfaces.Parent;
  37 
  38 import java.util.List;
  39 
  40 @ControlType({Menu.class})
  41 @ControlInterfaces(value = {Parent.class, StringMenuOwner.class},
  42         encapsulates = {MenuItem.class, MenuItem.class}, name = {"asMenuParent"})
  43 public class MenuWrap<ITEM extends Menu> extends MenuItemWrap<ITEM> {
  44 
  45     public static final String SHOWING_PROPERTY_NAME = "isShowing";
  46     public static final String PARENT_SHOWN_PROPERTY_NAME = "parent.shown";
  47 
  48     private StringMenuOwnerImpl menuOwner = null;
  49     private Parent<MenuItem> parent = null;
  50 
  51     /**
  52      * @param env
  53      */
  54     @SuppressWarnings("unchecked")
  55     public MenuWrap(Environment env, ITEM item) {
  56         super(env, item);
  57     }
  58 
  59     @As(MenuItem.class)
  60     public Parent<MenuItem> asMenuParent() {
  61         if (parent == null) {
  62             parent = new MenuItemParent(this) {
  63 
  64                 @Override
  65                 protected List getControls() {
  66                     return new FutureAction<>(getEnvironment(), () -> getControl().getItems()).get();
  67                 }
  68             };
  69         }
  70         return parent;
  71     }
  72 
  73     @As(MenuItem.class)
  74     public StringMenuOwner<MenuItem> asMenuOwner() {
  75         if (menuOwner == null) {
  76             menuOwner = new StringMenuOwnerImpl(this, this.as(Parent.class, Menu.class));
  77         }
  78         return menuOwner;
  79     }
  80 
  81     @Property(SHOWING_PROPERTY_NAME)
  82     public boolean isShowing() {
  83         return isShowing(getControl(), getEnvironment());
  84     }
  85 
  86     @Property(PARENT_SHOWN_PROPERTY_NAME)
  87     public boolean isParentShown() {
  88         return isParentShown(getControl(), getEnvironment());
  89     }
  90 
  91     static boolean isShowing(final Menu menu, Environment env) {
  92         return new FutureAction<>(env, menu::isShowing).get();
  93     }
  94 
  95     static boolean isParentShown(final Menu menu, Environment env) {
  96         return new FutureAction<>(env, () -> {
  97             if (menu.getParentPopup() != null) {
  98                 return menu.getParentPopup().isShowing();
  99             } else {
 100                 return true;
 101             }
 102         }).get();
 103     }
 104 }