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.Table;
  29 import org.eclipse.swt.widgets.TableColumn;
  30 import org.eclipse.swt.widgets.TableItem;
  31 import org.jemmy.action.GetAction;
  32 import org.jemmy.control.ControlType;
  33 import org.jemmy.control.Property;
  34 import org.jemmy.env.Environment;
  35 import org.jemmy.interfaces.Parent;
  36 import org.jemmy.interfaces.Selectable;
  37 import org.jemmy.interfaces.Selector;
  38 import org.jemmy.interfaces.TypeControlInterface;
  39 
  40 /**
  41  *
  42  * @author shura
  43  */
  44 @ControlType(Table.class)
  45 public class TableWrap<T extends Table> extends ScrollableWrap<T> implements Selectable<Integer> {
  46 
  47     private ItemParent<TableItem> items = null;
  48     private List<Integer> states = null;
  49     private ItemParent<TableColumn> columns = null;
  50     private IndexItemSelector selector = null;
  51 
  52     public TableWrap(Environment env, T node) {
  53         super(env, node);
  54     }
  55 
  56     @Override
  57     public <TYPE, INTERFACE extends TypeControlInterface<TYPE>> INTERFACE as(Class<INTERFACE> interfaceClass, Class<TYPE> type) {
  58         if (interfaceClass.equals(Parent.class) && TableItem.class.equals(type)) {
  59             List<TableItem> itemList = Arrays.asList(new GetAction<TableItem[]>() {
  60 
  61                 @Override
  62                 public void run(Object... parameters) {
  63                     setResult(getControl().getItems());
  64                 }
  65             }.dispatch(getEnvironment()));
  66             if (items == null) {
  67                 items = new ItemParent<TableItem>(this, TableItem.class) {
  68 
  69                     @Override
  70                     protected List<TableItem> getItems() {
  71                         return Arrays.asList(new GetAction<TableItem[]>() {
  72 
  73                             @Override
  74                             public void run(Object... parameters) {
  75                                 setResult(getControl().getItems());
  76                             }
  77                         }.dispatch(getEnvironment()));
  78                     }
  79                 };
  80             }
  81             return (INTERFACE) items;
  82         }
  83         if (interfaceClass.equals(Parent.class) && TableColumn.class.equals(type)) {
  84             if (columns == null) {
  85                 columns = new ItemParent<TableColumn>(this, TableColumn.class) {
  86 
  87                     @Override
  88                     protected List<TableColumn> getItems() {
  89                         return Arrays.asList(new GetAction<TableColumn[]>() {
  90 
  91                             @Override
  92                             public void run(Object... parameters) {
  93                                 setResult(getControl().getColumns());
  94                             }
  95                         }.dispatch(getEnvironment()));
  96                     }
  97                 };
  98             }
  99             return (INTERFACE) columns;
 100         }
 101         return super.as(interfaceClass, type);
 102     }
 103 
 104     @Override
 105     public <TYPE, INTERFACE extends TypeControlInterface<TYPE>> boolean is(Class<INTERFACE> interfaceClass, Class<TYPE> type) {
 106         if (interfaceClass.equals(Parent.class) && TableItem.class.equals(type)) {
 107             return true;
 108         }
 109         return super.is(interfaceClass, type);
 110     }
 111 
 112     public int getItemCount() {
 113         return new GetAction<Integer>() {
 114 
 115             @Override
 116             public void run(Object... parameters) {
 117                 setResult(getControl().getItemCount());
 118             }
 119         }.dispatch(getEnvironment());
 120     }
 121 
 122     public List<Integer> getStates() {
 123         if (states == null) {
 124             states = new ArrayList<Integer>();
 125         }
 126         int size = getItemCount();
 127         if (states.size() != size) {
 128             states.clear();
 129             for (int i = 0; i < size; i++) {
 130                 states.add(i);
 131             }
 132         }
 133         return states;
 134     }
 135 
 136     @Property(Selectable.STATE_PROP_NAME)
 137     public Integer getState() {
 138         return new GetAction<Integer>() {
 139 
 140             @Override
 141             public void run(Object... parameters) {
 142                 setResult(getControl().getSelectionIndex());
 143             }
 144         }.dispatch(getEnvironment());
 145     }
 146 
 147     public Selector<Integer> selector() {
 148         if (selector == null) {
 149             if (items == null) {
 150                 as(Parent.class, TableItem.class);
 151             }
 152             selector = new IndexItemSelector(items);
 153         }
 154         return selector;
 155     }
 156 
 157     public Class<Integer> getType() {
 158         return Integer.class;
 159     }
 160 }