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