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