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.List;
  27 import org.eclipse.swt.widgets.Item;
  28 import org.jemmy.control.Wrap;
  29 import org.jemmy.control.Wrapper;
  30 import org.jemmy.lookup.AbstractParent;
  31 import org.jemmy.lookup.ControlList;
  32 import org.jemmy.lookup.Lookup;
  33 import org.jemmy.lookup.LookupCriteria;
  34 import org.jemmy.lookup.PlainLookup;
  35 
  36 /**
  37  *
  38  * @author shura
  39  */
  40 public abstract class ItemParent<T extends Item> extends AbstractParent<T> {
  41 
  42     private final Wrap<?> itemListWrap;
  43     final ControlList controList;
  44     private final Class<T> type;
  45     final Wrapper wrapper;
  46 
  47     public ItemParent(Wrap<?> itemListWrap, Class<T> type) {
  48         this.itemListWrap = itemListWrap;
  49         this.type = type;
  50         controList = new ControlList() {
  51 
  52             public List<?> getControls() {
  53                 List<T> res = getItems();
  54                 return (res != null) ? res : new ArrayList<T>();
  55             }
  56         };
  57         wrapper = new ItemWrapper(ItemParent.this.itemListWrap);
  58     }
  59 
  60     protected abstract List<T> getItems();
  61 
  62     public <ST extends T> Lookup<ST> lookup(Class<ST> controlClass, LookupCriteria<ST> criteria) {
  63         return new PlainLookup<ST>(itemListWrap.getEnvironment(),
  64                 controList, wrapper, controlClass, criteria);
  65     }
  66 
  67     public Lookup<T> lookup(LookupCriteria<T> criteria) {
  68         return new PlainLookup<T>(itemListWrap.getEnvironment(),
  69                 controList, wrapper, type, criteria);
  70     }
  71 
  72     public Class<T> getType() {
  73         return type;
  74     }
  75 
  76     static class ItemWrapper implements Wrapper {
  77 
  78         Wrap<?> wrap;
  79 
  80         public ItemWrapper(Wrap<?> wrap) {
  81             this.wrap = wrap;
  82         }
  83 
  84         public <T> Wrap<? extends T> wrap(Class<T> controlClass, T control) {
  85             return new ItemWrap(wrap, (Item) control);
  86         }
  87     }
  88 }