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.ArrayList;
  28 import java.util.Arrays;
  29 import java.util.List;
  30 
  31 import org.eclipse.swt.custom.CTabFolder;
  32 import org.eclipse.swt.custom.CTabItem;
  33 import org.jemmy.action.GetAction;
  34 import org.jemmy.control.ControlType;
  35 import org.jemmy.control.Property;
  36 import org.jemmy.env.Environment;
  37 import org.jemmy.interfaces.Parent;
  38 import org.jemmy.interfaces.Selectable;
  39 import org.jemmy.interfaces.Selector;
  40 import org.jemmy.interfaces.TypeControlInterface;
  41 import org.jemmy.lookup.Lookup;
  42 
  43 /**
  44  *
  45  * @author shura, erikgreijus
  46  * @param <T>
  47  */
  48 @ControlType(CTabFolder.class)
  49 public class CTabFolderWrap<T extends CTabFolder> extends ControlWrap<T> implements Selectable<String> {
  50 
  51     private ItemParent<CTabItem> items = null;
  52     private TextItemSelector selector = null;
  53 
  54     public CTabFolderWrap(Environment env, T node) {
  55         super(env, node);
  56     }
  57 
  58     @Override
  59     public <TYPE, INTERFACE extends TypeControlInterface<TYPE>> INTERFACE as(Class<INTERFACE> interfaceClass, Class<TYPE> type) {
  60         if (interfaceClass.equals(Parent.class) && CTabItem.class.equals(type)) {
  61             if (items == null) {
  62                 items = new ItemParent<CTabItem>(this, CTabItem.class) {
  63 
  64                     @Override
  65                     protected List<CTabItem> getItems() {
  66                         return Arrays.asList(new GetAction<CTabItem[]>() {
  67 
  68                             @Override
  69                             public void run(Object... parameters) {
  70                                 setResult(getControl().getItems());
  71                             }
  72                         }.dispatch(getEnvironment()));
  73                     }
  74                 };
  75             }
  76             return (INTERFACE) items;
  77         }
  78         return super.as(interfaceClass, type);
  79     }
  80 
  81     @Override
  82     public <TYPE, INTERFACE extends TypeControlInterface<TYPE>> boolean is(Class<INTERFACE> interfaceClass, Class<TYPE> type) {
  83         if (interfaceClass.equals(Parent.class) && CTabItem.class.equals(type)) {
  84             return true;
  85         }
  86         return super.is(interfaceClass, type);
  87     }
  88 
  89     public int getItemCount() {
  90         return new GetAction<Integer>() {
  91 
  92             @Override
  93             public void run(Object... parameters) {
  94                 setResult(getControl().getItemCount());
  95             }
  96         }.dispatch(getEnvironment());
  97     }
  98 
  99     @Property(Selectable.STATES_PROP_NAME)
 100     public List<String> getStates() {
 101         return new GetAction<List<String>>() {
 102             @Override
 103             public void run(Object... parameters) {
 104                 Lookup<CTabItem> lookup = as(Parent.class, CTabItem.class).lookup();
 105                 ArrayList<String> res = new ArrayList<String>();
 106                 for (int i = 0; i < lookup.size(); i++) {
 107                     res.add(lookup.get(i).getText());
 108                 }
 109                 setResult(res);
 110             }
 111         }.dispatch(getEnvironment());
 112     }
 113 
 114     @Property(Selectable.STATE_PROP_NAME)
 115     public String getState() {
 116         return new GetAction<String>() {
 117 
 118             @Override
 119             public void run(Object... parameters) {
 120                 CTabItem selected = getControl().getSelection();
 121                 setResult(selected != null ? selected.getText() : "No selection");
 122             }
 123         }.dispatch(getEnvironment());
 124     }
 125 
 126     public Selector<String> selector() {
 127         if (selector == null) {
 128             if (items == null) {
 129                 as(Parent.class, CTabItem.class);
 130             }
 131             selector = new TextItemSelector(items);
 132         }
 133         return selector;
 134     }
 135 
 136     public Class<String> getType() {
 137         return String.class;
 138     }
 139 }