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.ToolBar; 29 import org.eclipse.swt.widgets.ToolItem; 30 import org.jemmy.action.GetAction; 31 import org.jemmy.control.ControlType; 32 import org.jemmy.control.Property; 33 import org.jemmy.env.Environment; 34 import org.jemmy.interfaces.Parent; 35 import org.jemmy.interfaces.Selectable; 36 import org.jemmy.interfaces.Selector; 37 import org.jemmy.interfaces.TypeControlInterface; 38 import org.jemmy.lookup.Lookup; 39 40 /** 41 * 42 * @author shura, erikgreijus 43 * @param <T> 44 */ 45 @ControlType(ToolBar.class) 46 public class ToolBarWrap<T extends ToolBar> extends ControlWrap<T> implements Selectable<String> { 47 48 private ItemParent<ToolItem> items; 49 private ToolTipItemSelector selector; 50 51 public ToolBarWrap(Environment env, T node) { 52 super(env, node); 53 this.items = null; 54 this.selector = null; 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) && ToolItem.class.equals(type)) { 60 if (items == null) { 61 items = new ItemParent<ToolItem>(this, ToolItem.class) { 62 63 @Override 64 protected List<ToolItem> getItems() { 65 return Arrays.asList(new GetAction<ToolItem[]>() { 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) && ToolItem.class.equals(type)) { 83 return true; 84 } 85 return super.is(interfaceClass, type); 86 } 87 88 @Property(Selectable.STATES_PROP_NAME) 89 public List<String> getStates() { 90 return new GetAction<List<String>>() { 91 92 @Override 93 public void run(Object... parameters) { 94 Lookup<ToolItem> lookup = as(Parent.class, ToolItem.class).lookup(); 95 ArrayList<String> res = new ArrayList<String>(); 96 for (int i = 0; i < lookup.size(); i++) { 97 res.add(lookup.get(i).getToolTipText()); 98 } 99 setResult(res); 100 } 101 }.dispatch(getEnvironment()); 102 } 103 104 @Property(Selectable.STATE_PROP_NAME) 105 public String getState() { 106 return new GetAction<String>() { 107 108 @Override 109 public void run(Object... parameters) { 110 Lookup<ToolItem> lookup = as(Parent.class, ToolItem.class).lookup(); 111 String selected = "No selection"; 112 for (int i = 0; i < lookup.size(); i++) { 113 if (lookup.get(i).getSelection()) { 114 selected = lookup.get(i).getToolTipText(); 115 break; 116 } 117 } 118 setResult(selected); 119 } 120 }.dispatch(getEnvironment()); 121 } 122 123 public Selector<String> selector() { 124 if (selector == null) { 125 if (items == null) { 126 as(Parent.class, ToolItem.class); 127 } 128 selector = new ToolTipItemSelector(items); 129 } 130 return selector; 131 } 132 133 public Class<String> getType() { 134 return String.class; 135 } 136 }