Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7c112fed282cb7ff669e16a75271c68c4dfc3463 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*******************************************************************************
 * Copyright (c) 2008 Ketan Padegaonkar and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Ketan Padegaonkar - initial API and implementation
 *******************************************************************************/
package org.eclipse.swtbot.eclipse.spy;

import java.util.Iterator;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.CoolItem;
import org.eclipse.swt.widgets.Decorations;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.Tracker;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.swtbot.swt.finder.finders.ControlFinder;
import org.eclipse.swtbot.swt.finder.finders.PathGenerator;
import org.eclipse.swtbot.swt.finder.resolvers.IChildrenResolver;
import org.eclipse.swtbot.swt.finder.resolvers.IParentResolver;
import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
import org.eclipse.swtbot.swt.finder.utils.TreePath;

class EclipseWidgetTracker implements Runnable {
	protected IChildrenResolver	childrenResolver;

	protected ControlFinder		controlFinder;

	protected Display			display;

	protected IParentResolver	parentResolver;

	protected EclipseSpy		view;

	EclipseWidgetTracker(EclipseSpy view, IChildrenResolver childrenResolver, IParentResolver parentResolver) {
		this.view = view;
		this.childrenResolver = childrenResolver;
		this.parentResolver = parentResolver;
	}

	// public void clickOnToolbar(Control control) {
	// if (control instanceof ToolBar) {
	// ToolBar toolbar = (ToolBar) control;
	// Display display = toolbar.getDisplay();
	//
	// Point cursorLocation = Display.getCurrent().getCursorLocation();
	//
	// Event event = new Event();
	// event.display = display;
	//
	// event.button = 1;
	// event.type = SWT.MouseDown;
	//
	// event.doit = true;
	// event.x = cursorLocation.x;
	// event.y = cursorLocation.y;
	//
	// // display.post(event);
	//
	// event.type = SWT.MouseUp;
	// // display.post(event);
	// }

	// }

	public String getClassName(Object o) {
		if (o == null)
			return null;
		int i = o.getClass().getName().lastIndexOf('.');
		return o.getClass().getName().substring(i + 1);
	}

	public void getCompositeInformation(Control control, StringBuffer buf) {
		List children = childrenResolver.getChildren(control);
		buf.append("\nChildren: " + children.size() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
		for (Iterator iterator = children.iterator(); iterator.hasNext();)
			buf.append("\t" + iterator.next() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
		buf.append("\n"); //$NON-NLS-1$
	}

	public void getLayoutInformation(Control control, StringBuffer buf) {
		buf.append("Layout Information: \n"); //$NON-NLS-1$
		buf.append(SWTUtils.toString(control) + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
		buf.append("\tStyle: " + getStyle(control) + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
		buf.append("\tLayout Data: " + control.getLayoutData() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
		buf.append("\tBounds: " + control.getBounds() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
		buf.append("\n"); //$NON-NLS-1$
	}

	public void getLocationInformation(Control control, StringBuffer buf) {
		TreePath path = controlFinder.getPath(control);
		String stringFromPath = new PathGenerator().getPathAsString(path);
		buf.append("Location: \n").append(stringFromPath).append("\n\n"); //$NON-NLS-1$ //$NON-NLS-2$
	}

	public void getParentInformation(Control control, StringBuffer buf) {
		Composite parent = control.getParent();
		if (parent != null) {
			buf.append("Parent Tree:\n"); //$NON-NLS-1$

			TreePath parents = controlFinder.getPath(control).getParentPath();

			int segmentCount = parents.getSegmentCount();
			for (int i = segmentCount - 1; i >= 0; i--) {
				String prefix = ""; //$NON-NLS-1$
				Widget segment = (Widget) parents.getSegment(i);
				for (int j = 0; j < segmentCount - i - 1; j++)
					prefix += "\t"; //$NON-NLS-1$

				buf.append(prefix + renderWidget(segment) + "[" + SWTUtils.widgetIndex(segment) + "]" + "@" + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
				if (segment instanceof Composite) {
					buf.append(prefix + "\t Layout: " + getClassName(((Composite) segment).getLayout()) + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
					buf.append(prefix + "\t LayoutData: " + getClassName(((Control) segment).getLayoutData()) + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
				}
			}
			buf.append("\n"); //$NON-NLS-1$
		}
	}

	public void getSiblingInformation(Control control, StringBuffer buf) {
		Widget[] siblings = SWTUtils.siblings(control);
		buf.append("Siblings: " + siblings.length + "\n"); //$NON-NLS-1$ //$NON-NLS-2$

		for (int i = 0; i < siblings.length; i++) {
			Widget sibling = siblings[i];
			if (sibling == control)
				buf.append("\t[*]"); //$NON-NLS-1$
			else
				buf.append("\t   "); //$NON-NLS-1$
			if (sibling instanceof Control)
				buf.append(renderWidget(sibling) + ": Layout Data: " //$NON-NLS-1$
						+ ((Control) sibling).getLayoutData() + "\n"); //$NON-NLS-1$
			else
				buf.append(renderWidget(sibling) +  "\n"); //$NON-NLS-1$
		}
		buf.append("\n"); //$NON-NLS-1$
	}

	private String renderWidget(Widget sibling) {
		return SWTUtils.toString(sibling);
	}

	public void run() {
		if ((view.output == null) || view.output.isDisposed() || !view.actionMonitor.isChecked())
			return;

		display = view.output.getDisplay();
		controlFinder = new ControlFinder();

		Control control = display.getCursorControl();
		display.getCursorLocation();

		if (control == null) {
			view.output.setText(""); //$NON-NLS-1$
			view.lastWidget = 0;
		} else if (control != view.lastWidget) {
			view.lastWidget = control;

			StringBuffer buf = new StringBuffer();
			getInformation(control, buf);
			// clickOnToolbar(control);
			view.output.setText(buf.toString());
		}
		display.timerExec(100, view.trackWidgets);
	}

	private void getInformation(Control control, StringBuffer buf) {

		getToggleInformation(control, buf);
		
		getLocationInformation(control, buf);

		getLayoutInformation(control, buf);

		getCompositeInformation(control, buf);

		getSiblingInformation(control, buf);

		getParentInformation(control, buf);

	}

	private void getToggleInformation(Control control, StringBuffer buf) {
		buf.append("To toggle, or freeze info on a particular control, press CTRL+SHIFT: \n").append("\n\n"); //$NON-NLS-1$ //$NON-NLS-2$
	}

	String getStyle(Widget w) {

		int style = w.getStyle();
		String result = ""; //$NON-NLS-1$
		if (style == SWT.DEFAULT)
			return "DEFAULT - bad!"; //$NON-NLS-1$
		if ((style & 1 << 1) != 0)
			if ((w instanceof CTabFolder) || (w instanceof StyledText) || (w instanceof List) || (w instanceof Text)
					|| (w instanceof Table) || (w instanceof Tree))
				result += "MULTI | "; //$NON-NLS-1$
			else if (w instanceof Menu)
				result += "BAR | "; //$NON-NLS-1$
			else if ((w instanceof Label) || (w instanceof MenuItem) || (w instanceof ToolItem))
				result += "SEPARATOR | "; //$NON-NLS-1$
			else if (w instanceof Button)
				result += "TOGGLE | "; //$NON-NLS-1$
			else if (w instanceof ProgressBar)
				result += "INDETERMINATE | "; //$NON-NLS-1$
			else
				result += "BAR or SEPARATOR or TOGGLE or MULTI or INDETERMINATE or DBCS | "; //$NON-NLS-1$
		if ((style & 1 << 2) != 0)
			if ((w instanceof Menu) || (w instanceof ToolItem) || (w instanceof CoolItem) || (w instanceof Combo))
				result += "DROP_DOWN | "; //$NON-NLS-1$
			else if (w instanceof Button)
				result += "ARROW | "; //$NON-NLS-1$
			else if ((w instanceof CTabFolder) || (w instanceof StyledText) || (w instanceof List)
					|| (w instanceof Text) || (w instanceof Table) || (w instanceof Tree))
				result += "SINGLE | "; //$NON-NLS-1$
			else if ((w instanceof Label) || (w instanceof Group))
				result += "SHADOW_IN | "; //$NON-NLS-1$
			else if (w instanceof Decorations)
				result += "TOOL | "; //$NON-NLS-1$
			else
				result += "ALPHA or TOOL or SINGLE or ARROW or DROP_DOWN or SHADOW_IN | "; //$NON-NLS-1$
		if ((style & 1 << 3) != 0)
			if (w instanceof Menu)
				result += "POP_UP | "; //$NON-NLS-1$
			else if ((w instanceof Button) || (w instanceof MenuItem) || (w instanceof ToolItem))
				result += "PUSH | "; //$NON-NLS-1$
			else if ((w instanceof Combo) || (w instanceof Text) || (w instanceof StyledText))
				result += "READ_ONLY | "; //$NON-NLS-1$
			else if ((w instanceof Label) || (w instanceof Group) || (w instanceof ToolBar))
				result += "SHADOW_OUT | "; //$NON-NLS-1$
			else if (w instanceof Decorations)
				result += "NO_TRIM | "; //$NON-NLS-1$
			else
				result += "POP_UP or PUSH or READ_ONLY or SHADOW_OUT or NO_TRIM or NATIVE | "; //$NON-NLS-1$
		if ((style & 1 << 4) != 0)
			if ((w instanceof Button) || (w instanceof MenuItem) || (w instanceof ToolItem))
				result += "RADIO | "; //$NON-NLS-1$
			else if (w instanceof Group)
				result += "SHADOW_ETCHED_IN | "; //$NON-NLS-1$
			else if ((w instanceof Decorations) || (w instanceof Tracker))
				result += "RESIZE | "; //$NON-NLS-1$
			else
				result += "RESIZE or SHADOW_ETCHED_IN or RADIO or PHONETIC | "; //$NON-NLS-1$
		if ((style & 1 << 5) != 0)
			if ((w instanceof Button) || (w instanceof MenuItem) || (w instanceof ToolItem) || (w instanceof Table)
					|| (w instanceof Tree))
				result += "CHECK | "; //$NON-NLS-1$
			else if ((w instanceof Label) || (w instanceof Group))
				result += "SHADOW_NONE | "; //$NON-NLS-1$
			else if (w instanceof Decorations)
				result += "TITLE | "; //$NON-NLS-1$
			else
				result += "ROMAN or CHECK  or SHADOW_NONE or TITLE | "; //$NON-NLS-1$
		if ((style & 1 << 6) != 0)
			if (w instanceof MenuItem)
				result += "CASCADE | "; //$NON-NLS-1$
			else if ((w instanceof StyledText) || (w instanceof Label) || (w instanceof Text) || (w instanceof ToolBar))
				result += "WRAP | "; //$NON-NLS-1$
			else if (w instanceof Combo)
				result += "SIMPLE | "; //$NON-NLS-1$
			else if (w instanceof Group)
				result += "SHADOW_ETCHED_OUT | "; //$NON-NLS-1$
			else if ((w instanceof Decorations) || (w instanceof CTabFolder) || (w instanceof CTabItem))
				result += "CLOSE | "; //$NON-NLS-1$
			else
				result += "CLOSE or MENU or CASCADE or WRAP or SIMPLE or SHADOW_ETCHED_OUT | "; //$NON-NLS-1$
		if ((style & 1 << 7) != 0)
			if (w instanceof Decorations)
				result += "MIN | "; //$NON-NLS-1$
			else if ((w instanceof Button) || (w instanceof Tracker))
				result += "UP | "; //$NON-NLS-1$
			else if (w instanceof CTabFolder)
				result += "TOP | "; //$NON-NLS-1$
			else
				result += "MIN or UP or TOP | "; //$NON-NLS-1$
		if ((style & 1 << 8) != 0)
			result += "HORIZONTAL | "; //$NON-NLS-1$
		if ((style & 1 << 9) != 0)
			result += "VERTICAL | "; //$NON-NLS-1$
		if ((style & 1 << 10) != 0)
			if (w instanceof Decorations)
				result += "MAX | "; //$NON-NLS-1$
			else if ((w instanceof Button) || (w instanceof Tracker))
				result += "DOWN | "; //$NON-NLS-1$
			else if (w instanceof CTabFolder)
				result += "BOTTOM | "; //$NON-NLS-1$
			else
				result += "MAX or DOWN or BOTTOM | "; //$NON-NLS-1$
		if ((style & 1 << 11) != 0)
			result += "BORDER | "; //$NON-NLS-1$
		if ((style & 1 << 12) != 0)
			result += "CLIP_CHILDREN | "; //$NON-NLS-1$
		if ((style & 1 << 13) != 0)
			result += "CLIP_SIBLINGS | "; //$NON-NLS-1$
		if ((style & 1 << 14) != 0)
			result += "ON_TOP or LEAD or LEFT | "; //$NON-NLS-1$
		if ((style & 1 << 15) != 0)
			if (w instanceof Shell)
				result += "PRIMARY_MODAL | "; //$NON-NLS-1$
			else if (w instanceof Table)
				result += "HIDE_SELECTION | "; //$NON-NLS-1$
			else
				result += "PRIMARY_MODAL or HIDE_SELECTION | "; //$NON-NLS-1$
		if ((style & 1 << 16) != 0)
			if ((w instanceof StyledText) || (w instanceof Table))
				result += "FULL_SELECTION | "; //$NON-NLS-1$
			else if (w instanceof Shell)
				result += "APPLICATION_MODAL | "; //$NON-NLS-1$
			else if (w instanceof ProgressBar)
				result += "SMOOTH | "; //$NON-NLS-1$
			else
				result += "FULL_SELECTION or SMOOTH or APPLICATION_MODAL | "; //$NON-NLS-1$
		if ((style & 1 << 17) != 0)
			if (w instanceof Shell)
				result += "SYSTEM_MODAL | "; //$NON-NLS-1$
			else if ((w instanceof Button) || (w instanceof Label) || (w instanceof TableColumn)
					|| (w instanceof Tracker) || (w instanceof ToolBar))
				result += "TRAIL | "; //$NON-NLS-1$
			else
				result += "SYSTEM_MODAL or TRAIL or RIGHT | "; //$NON-NLS-1$
		if ((style & 1 << 18) != 0)
			result += "NO_BACKGROUND | "; //$NON-NLS-1$
		if ((style & 1 << 19) != 0)
			result += "NO_FOCUS | "; //$NON-NLS-1$
		if ((style & 1 << 20) != 0)
			result += "NO_REDRAW_RESIZE | "; //$NON-NLS-1$
		if ((style & 1 << 21) != 0)
			result += "NO_MERGE_PAINTS | "; //$NON-NLS-1$
		if ((style & 1 << 22) != 0)
			if (w instanceof Text)
				result += "PASSWORD | "; //$NON-NLS-1$
			else if (w instanceof Composite)
				result += "NO_RADIO_GROUP | "; //$NON-NLS-1$
			else
				result += "NO_RADIO_GROUP or PASSWORD | "; //$NON-NLS-1$
		if ((style & 1 << 23) != 0)
			result += "FLAT | "; //$NON-NLS-1$
		if ((style & 1 << 24) != 0)
			if ((w instanceof Button) || (w instanceof Label) || (w instanceof TableColumn))
				result += "CENTER | "; //$NON-NLS-1$
			else
				result += "EMBEDDED or CENTER | "; //$NON-NLS-1$
		if ((style & 1 << 25) != 0)
			result += "LEFT_TO_RIGHT | "; //$NON-NLS-1$
		if ((style & 1 << 26) != 0)
			result += "RIGHT_TO_LEFT | "; //$NON-NLS-1$
		if ((style & 1 << 27) != 0)
			result += "MIRRORED | "; //$NON-NLS-1$
		if ((style & 1 << 28) != 0)
			result += "VIRTUAL | "; //$NON-NLS-1$
		int lastOr = result.lastIndexOf("|"); //$NON-NLS-1$
		if (lastOr == result.length() - 2)
			result = result.substring(0, result.length() - 2);
		return result;
	}
}

Back to the top