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 
  28 import org.eclipse.swt.widgets.Composite;
  29 import org.eclipse.swt.widgets.Widget;
  30 import org.eclipse.swt.widgets.Control;
  31 import org.eclipse.swt.widgets.Display;
  32 import org.jemmy.action.GetAction;
  33 import org.jemmy.control.ControlType;
  34 import org.jemmy.env.Environment;
  35 import org.jemmy.interfaces.Parent;
  36 import org.jemmy.lookup.Any;
  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  * @param <T>
  46  */
  47 @ControlType(Composite.class)
  48 public class CompositeWrap<T extends Composite> extends ControlWrap<T> implements Parent<Control> {
  49 
  50     private final SWTHierarchy swth;
  51     public CompositeWrap(Environment env, T node) {
  52         super(env, node);
  53         swth = new SWTHierarchy();
  54     }
  55 
  56     public <ST extends org.eclipse.swt.widgets.Control> Lookup<ST> lookup(Class<ST> controlClass, LookupCriteria<ST> criteria) {
  57         return new HierarchyLookup(getEnvironment(), swth, Shells.SHELLS.swtWrapper, controlClass, criteria);
  58     }
  59 
  60     public <ST extends Control> Lookup<ST> lookup(Class<ST> controlClass) {
  61         return lookup(controlClass, new Any<ST>());
  62     }
  63 
  64     public Lookup<org.eclipse.swt.widgets.Control> lookup(LookupCriteria<org.eclipse.swt.widgets.Control> criteria) {
  65         return lookup(getType(), criteria);
  66     }
  67 
  68     public Lookup<Control> lookup() {
  69         return lookup(new Any<Control>());
  70     }
  71 
  72     public Class<org.eclipse.swt.widgets.Control> getType() {
  73         return org.eclipse.swt.widgets.Control.class;
  74     }
  75 
  76     private class SWTHierarchy implements ControlHierarchy {
  77 
  78         public List<?> getControls() {
  79             GetAction<org.eclipse.swt.widgets.Control[]> action = new GetAction<org.eclipse.swt.widgets.Control[]>() {
  80                 public void run(Object... parameters) {
  81                     Display.getDefault().syncExec(new Runnable() {
  82                         public void run() {
  83                             if (!getControl().isDisposed()) {
  84                                 setResult(getControl().getChildren());
  85                             } else {
  86                                 setResult(new Control[0]);
  87                             }
  88                         }
  89                     });
  90                 }
  91             };
  92             getEnvironment().getExecutor().execute(getEnvironment(), true, action);
  93             return Arrays.asList(action.getResult());
  94         }
  95 
  96         public List<?> getChildren(final Object subParent) {
  97             if(!(subParent instanceof Composite)) return null;
  98             GetAction<org.eclipse.swt.widgets.Control[]> action = new GetAction<org.eclipse.swt.widgets.Control[]>() {
  99                 public void run(Object... parameters) {
 100                     Display.getDefault().syncExec(new Runnable() {
 101                         public void run() {
 102                             Composite subParentComposite = Composite.class.cast(subParent);                      
 103                             if (!subParentComposite.isDisposed()) {
 104                                 setResult(subParentComposite.getChildren());
 105                             } else {
 106                                 setResult(new Control[0]);
 107                             }
 108                         }
 109                     });
 110                 }
 111             };
 112             getEnvironment().getExecutor().execute(getEnvironment(), true, action);
 113             return Arrays.asList(action.getResult());
 114         }
 115 
 116         public Object getParent(final Object child) {
 117             GetAction<Object> action = new GetAction<Object>() {
 118                 public void run(Object... parameters) {
 119                     Display.getDefault().syncExec(new Runnable() {
 120                         public void run() {
 121                             Widget childComposite = Widget.class.cast(child);
 122                             if (!childComposite.isDisposed()) {
 123                                 setResult(((org.eclipse.swt.widgets.Control)child).getParent());
 124                             } else {
 125                                 setResult(null);
 126                             }
 127                         }
 128                     });
 129                 }
 130             };
 131             getEnvironment().getExecutor().execute(getEnvironment(), true, action);
 132             return action.getResult();
 133         }
 134     }
 135 
 136 }