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