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