1 /*
   2  * Copyright (c) 2007, 2018, 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.jemmy.swt;
  24 
  25 import java.util.Arrays;
  26 import java.util.List;
  27 import org.eclipse.swt.SWT;
  28 import org.eclipse.swt.widgets.Menu;
  29 import org.eclipse.swt.widgets.MenuItem;
  30 import org.eclipse.swt.widgets.Shell;
  31 import org.jemmy.action.GetAction;
  32 import org.jemmy.control.Wrap;
  33 import org.jemmy.control.Wrapper;
  34 import org.jemmy.lookup.AbstractParent;
  35 import org.jemmy.lookup.ControlHierarchy;
  36 import org.jemmy.lookup.HierarchyLookup;
  37 import org.jemmy.lookup.Lookup;
  38 import org.jemmy.lookup.LookupCriteria;
  39 
  40 /**
  41  *
  42  * @author shura
  43  */
  44 public class MenuItemParent extends AbstractParent<MenuItem> {
  45 
  46     private Wrap<? extends Shell> shell;
  47     private MenuHierarchy hierarchy;
  48     private Wrapper wrapper;
  49 
  50     public <ST extends MenuItem> Lookup<ST> lookup(Class<ST> controlClass, LookupCriteria<ST> criteria) {
  51         return new HierarchyLookup<ST>(shell.getEnvironment(), hierarchy, wrapper, controlClass, criteria);
  52     }
  53 
  54     public Lookup<MenuItem> lookup(LookupCriteria<MenuItem> criteria) {
  55         return lookup(MenuItem.class, criteria);
  56     }
  57 
  58     public Class<MenuItem> getType() {
  59         return MenuItem.class;
  60     }
  61 
  62     private class MenuHierarchy implements ControlHierarchy {
  63 
  64         public List<?> getChildren(final Object subParent) {
  65             return new GetAction<List<?>>() {
  66 
  67                 @Override
  68                 public void run(Object... parameters) throws Exception {
  69                     if(((MenuItem)subParent).getStyle() == SWT.CASCADE) {
  70                         setResult(Arrays.asList(((MenuItem)subParent).getMenu().getItems()));
  71                     } else {
  72                         setResult(null);
  73                     }
  74                 }
  75             }.dispatch(shell.getEnvironment());
  76         }
  77 
  78         public Object getParent(final Object child) {
  79             return new GetAction<MenuItem>() {
  80 
  81                 @Override
  82                 public void run(Object... parameters) throws Exception {
  83                     setResult(((MenuItem)child).getMenu().getParentItem());
  84                 }
  85             }.dispatch(shell.getEnvironment());
  86         }
  87 
  88         public List<?> getControls() {
  89             return new GetAction<List<?>>() {
  90 
  91                 @Override
  92                 public void run(Object... parameters) throws Exception {
  93                     setResult(Arrays.asList(shell.getControl().getMenuBar().getItems()));
  94                 }
  95             }.dispatch(shell.getEnvironment());
  96         }
  97     }
  98 
  99 
 100 }