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