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