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.awt.AWTException; 26 import java.util.Arrays; 27 import java.util.List; 28 29 import org.eclipse.swt.custom.CTabFolder; 30 import org.eclipse.swt.widgets.Composite; 31 import org.eclipse.swt.widgets.Control; 32 import org.eclipse.swt.widgets.Display; 33 import org.eclipse.swt.widgets.Item; 34 import org.eclipse.swt.widgets.Shell; 35 import org.eclipse.swt.widgets.TabFolder; 36 import org.eclipse.swt.widgets.Table; 37 import org.eclipse.swt.widgets.TableItem; 38 import org.eclipse.swt.widgets.ToolBar; 39 import org.eclipse.swt.widgets.Tree; 40 import org.jemmy.action.GetAction; 41 import org.jemmy.browser.BrowserDescriptor; 42 import org.jemmy.browser.HierarchyDescriptor; 43 import org.jemmy.browser.HierarchyView; 44 import org.jemmy.control.Wrap; 45 import org.jemmy.control.Wrapper; 46 import org.jemmy.env.Environment; 47 import org.jemmy.lookup.ControlHierarchy; 48 import org.jemmy.lookup.ControlList; 49 50 /** 51 * 52 * @author shura, erikgreijus 53 */ 54 public class Browser implements BrowserDescriptor { 55 56 public static void main(final String[] args) throws AWTException { 57 HierarchyView.startApp(args); 58 try { 59 new HierarchyView(new Browser()).setVisible(true); 60 } catch (AWTException e) { 61 e.printStackTrace(); 62 } 63 } 64 65 public String getTitle() { 66 return "SWT hierarchy"; 67 } 68 69 public ControlList getHierarchy() { 70 return new ControlHierarchy() { 71 72 public List<?> getChildren(final Object subParent) { 73 return Arrays.asList(new GetAction<Object[]>() { 74 75 public void run(Object... parameters) { 76 Display.getDefault().syncExec(new Runnable() { 77 78 public void run() { 79 /* 80 if (subParent instanceof Table) { 81 setResult(((Table) subParent).getItems()); 82 } else if (subParent instanceof ToolBar) { 83 setResult(((ToolBar) subParent).getItems()); 84 } else if (subParent instanceof TabFolder) { 85 setResult(((TabFolder) subParent).getItems()); 86 } else { 87 setResult(new Object[0]); 88 } 89 if (subParent instanceof Composite) { 90 Object[] oldResult = getResult(); 91 Object[] children = ((Composite)subParent).getChildren(); 92 Object[] newResult = new Object[oldResult.length + children.length]; 93 System.arraycopy(oldResult, 0, newResult, 0, oldResult.length); 94 System.arraycopy(children, 0, newResult, oldResult.length, children.length); 95 setResult(newResult); 96 } 97 * 98 */ 99 if (subParent instanceof Composite) { 100 setResult(((Composite) subParent).getChildren()); 101 } else { 102 setResult(new Object[0]); 103 } 104 } 105 }); 106 } 107 }.dispatch(Environment.getEnvironment())); 108 } 109 110 public Object getParent(final Object child) { 111 return new GetAction<Object>() { 112 113 public void run(Object... parameters) { 114 Display.getDefault().syncExec(new Runnable() { 115 116 public void run() { 117 if (child instanceof Control) { 118 setResult(((Control) child).getParent()); 119 } else if (child instanceof Item) { 120 setResult(""); 121 } else { 122 setResult(null); 123 } 124 } 125 }); 126 } 127 }.dispatch(Environment.getEnvironment()); 128 } 129 130 public List<?> getControls() { 131 return Arrays.asList(new GetAction<Shell[]>() { 132 133 public void run(Object... parameters) { 134 Display.getDefault().syncExec(new Runnable() { 135 136 public void run() { 137 setResult(Display.getDefault().getShells()); 138 } 139 }); 140 } 141 }.dispatch(Environment.getEnvironment())); 142 } 143 }; 144 } 145 146 public Wrapper getWrapper() { 147 return Shells.SHELLS.swtWrapper; 148 } 149 150 public HierarchyDescriptor getSubHierarchyDescriptor(Wrap wrap) { 151 if (wrap.getControl() instanceof Control) { 152 ItemsGet ig = new ItemsGet((Control) wrap.getControl()); 153 Display.getDefault().syncExec(ig); 154 if (ig.items != null) { 155 return new ItemHierarchyDescriptor(wrap, TableItem.class, 156 ig.items); 157 } 158 } 159 return null; 160 } 161 162 class ItemsGet implements Runnable { 163 164 Item[] items; 165 Control control; 166 167 public ItemsGet(Control control) { 168 this.control = control; 169 } 170 171 public void run() { 172 if (control instanceof Table) { 173 items = ((Table) control).getItems(); 174 } else if (control instanceof Tree) { 175 items = ((Tree) control).getItems(); 176 } else if (control instanceof ToolBar) { 177 items = ((ToolBar) control).getItems(); 178 } else if (control instanceof TabFolder) { 179 items = ((TabFolder) control).getItems(); 180 } else if (control instanceof CTabFolder) { 181 items = ((CTabFolder) control).getItems(); 182 } 183 } 184 } 185 186 private class ItemHierarchyDescriptor implements HierarchyDescriptor { 187 188 Wrap<?> parent; 189 ItemParent<?> items; 190 191 public ItemHierarchyDescriptor(Wrap<?> parent, Class type, final Item[] items) { 192 this.parent = parent; 193 this.items = new ItemParent(parent, type) { 194 195 @Override 196 protected List getItems() { 197 return Arrays.asList(items); 198 } 199 }; 200 } 201 202 public ControlList getHierarchy() { 203 return items.controList; 204 } 205 206 public Wrapper getWrapper() { 207 return items.wrapper; 208 } 209 210 public HierarchyDescriptor getSubHierarchyDescriptor(Wrap wrap) { 211 return null; 212 } 213 } 214 }