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