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