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.SWTException;
  28 import org.eclipse.swt.widgets.Display;
  29 import org.eclipse.swt.widgets.Shell;
  30 import org.jemmy.action.GetAction;
  31 import org.jemmy.control.DefaultWrapper;
  32 import org.jemmy.env.Environment;
  33 import org.jemmy.input.AWTRobotInputFactory;
  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, erikgreijus
  43  */
  44 public class Shells extends AbstractParent<Shell> {
  45 
  46     public static final Shells SHELLS = new Shells();
  47     private final Environment env;
  48     private final ShellList shells;
  49     final DefaultWrapper swtWrapper;
  50 
  51     private Shells() {
  52         this(new Environment(Environment.getEnvironment()));
  53     }
  54 
  55     private Shells(Environment env) {
  56         this.env = env;
  57         env.setExecutor(new SWTExecutor());
  58         shells = new ShellList(env);
  59         swtWrapper = new DefaultWrapper(env);
  60         swtWrapper.addAnnotated(ShellWrap.class, ControlWrap.class,
  61                 CompositeWrap.class, TextWrap.class, TabFolderWrap.class,
  62                 ToolBarWrap.class, TableWrap.class, ListWrap.class, ComboWrap.class,
  63                 TreeWrap.class, CTabFolderWrap.class, CComboWrap.class, ScrollableWrap.class);
  64         env.setInputFactory(new AWTRobotInputFactory());
  65     }
  66 
  67     public <ST extends Shell> Lookup<ST> lookup(Class<ST> controlClass, LookupCriteria<ST> criteria) {
  68         return new HierarchyLookup(env, shells, swtWrapper, controlClass, criteria);
  69     }
  70 
  71     public Lookup<Shell> lookup(LookupCriteria<Shell> criteria) {
  72         return lookup(getType(), criteria);
  73     }
  74 
  75     public Environment getEnvironment() {
  76         return env;
  77     }
  78 
  79     public Class<Shell> getType() {
  80         return Shell.class;
  81     }
  82 
  83     private static class ShellList implements ControlHierarchy {
  84 
  85         Environment env;
  86 
  87         public ShellList(Environment env) {
  88             this.env = env;
  89         }
  90 
  91         public List<?> getControls() {
  92             GetAction<Shell[]> action = new GetAction<Shell[]>() {
  93 
  94                 public void run(Object... parameters) {
  95                     Display.getDefault().syncExec(new Runnable() {
  96 
  97                         public void run() {
  98                             try {
  99                                 setResult(Display.getDefault().getShells());
 100                             } catch (SWTException e) {
 101                                 setResult(new Shell[0]);
 102                             }
 103                         }
 104                     });
 105                 }
 106             };
 107             env.getExecutor().execute(env, true, action);
 108             return Arrays.asList(action.getResult());
 109         }
 110 
 111         public List<?> getChildren(final Object subParent) {
 112             if (!(subParent instanceof Shell)) {
 113                 return null;
 114             }
 115             GetAction<Shell[]> action = new GetAction<Shell[]>() {
 116 
 117                 public void run(Object... parameters) {
 118                     Display.getDefault().syncExec(new Runnable() {
 119 
 120                         public void run() {
 121                             try {
 122                                 setResult(Shell.class.cast(subParent).getShells());
 123                             } catch (SWTException e) {
 124                                 setResult(new Shell[0]);
 125                             }
 126                         }
 127                     });
 128                 }
 129             };
 130             env.getExecutor().execute(env, true, action);
 131             return Arrays.asList(action.getResult());
 132         }
 133 
 134         public Object getParent(Object child) {
 135             //dunno how to do this
 136             return null;
 137         }
 138     }
 139 }