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