5a1d5319ccb6d5fca52b06778cb18275667a8845
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.swtbot.tests / shared / org / eclipse / tracecompass / tmf / ui / swtbot / tests / shared / SWTBotTimeGraphEntry.java
1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.eclipse.jface.viewers.ITableLabelProvider;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Rectangle;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Event;
23 import org.eclipse.swt.widgets.Menu;
24 import org.eclipse.swtbot.swt.finder.SWTBot;
25 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
26 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
27 import org.eclipse.swtbot.swt.finder.results.ArrayResult;
28 import org.eclipse.swtbot.swt.finder.results.Result;
29 import org.eclipse.swtbot.swt.finder.results.VoidResult;
30 import org.eclipse.swtbot.swt.finder.waits.Conditions;
31 import org.eclipse.swtbot.swt.finder.waits.WaitForObjectCondition;
32 import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBotControl;
33 import org.eclipse.swtbot.swt.finder.widgets.SWTBotRootMenu;
34 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
35 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.TimeGraphControl;
36
37 /**
38 * SWTBot class representing a time graph entry
39 */
40 public class SWTBotTimeGraphEntry extends AbstractSWTBotControl<TimeGraphControl> {
41
42 private final ITimeGraphEntry fEntry;
43
44 /**
45 * Constructor
46 *
47 * @param w the widget
48 * @param entry the time graph entry
49 *
50 * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
51 */
52 public SWTBotTimeGraphEntry(TimeGraphControl w, ITimeGraphEntry entry) throws WidgetNotFoundException {
53 super(w);
54 fEntry = entry;
55 }
56
57
58 @Override
59 protected SWTBotRootMenu contextMenu(final Control control) throws WidgetNotFoundException {
60 UIThreadRunnable.syncExec(new VoidResult() {
61 @Override
62 public void run() {
63 Rectangle bounds = widget.getItemBounds(fEntry);
64 if (bounds == null) {
65 return;
66 }
67 final Event event = new Event();
68 event.time = (int) System.currentTimeMillis();
69 event.display = control.getDisplay();
70 event.widget = control;
71 event.x = bounds.x + widget.getTimeDataProvider().getNameSpace() / 2;
72 event.y = bounds.y + bounds.height / 2;
73 control.notifyListeners(SWT.MenuDetect, event);
74 }
75 });
76
77 WaitForObjectCondition<Menu> waitForMenu = Conditions.waitForPopupMenu(control);
78 new SWTBot().waitUntilWidgetAppears(waitForMenu);
79 return new SWTBotRootMenu(waitForMenu.get(0));
80 }
81
82 /**
83 * Get the child entries of this entry
84 *
85 * @return the array of child entries
86 */
87 public SWTBotTimeGraphEntry[] getEntries() {
88 return syncExec(new ArrayResult<SWTBotTimeGraphEntry>() {
89 @Override
90 public SWTBotTimeGraphEntry[] run() {
91 List<SWTBotTimeGraphEntry> entries = new ArrayList<>();
92 for (ITimeGraphEntry entry : widget.getExpandedElements()) {
93 if (fEntry.equals(entry.getParent())) {
94 entries.add(new SWTBotTimeGraphEntry(widget, entry));
95 }
96 }
97 return entries.toArray(new SWTBotTimeGraphEntry[0]);
98 }
99 });
100 }
101
102 /**
103 * Get the child entry of this entry with the given name
104 *
105 * @param name
106 * the name of the entry
107 *
108 * @return the child entry
109 */
110 public SWTBotTimeGraphEntry getEntry(String name) {
111 return syncExec(new Result<SWTBotTimeGraphEntry>() {
112 @Override
113 public SWTBotTimeGraphEntry run() {
114 ITableLabelProvider labelProvider = widget.getLabelProvider();
115 for (ITimeGraphEntry entry : widget.getExpandedElements()) {
116 if (fEntry.equals(entry.getParent())) {
117 String label = labelProvider == null ? entry.getName() : labelProvider.getColumnText(entry, 0);
118 if (name.equals(label)) {
119 return new SWTBotTimeGraphEntry(widget, entry);
120 }
121 }
122 }
123 throw new WidgetNotFoundException("Timed out waiting for time graph entry " + name); //$NON-NLS-1$
124 }
125 });
126 }
127
128 /**
129 * Get the text of this entry
130 *
131 * @return the text
132 */
133 @Override
134 public String getText() {
135 return getText(0);
136 }
137
138 /**
139 * Get the text of this entry for the given column index
140 *
141 * @param column
142 * the column index
143 * @return the column text
144 */
145 public String getText(int column) {
146 ITableLabelProvider labelProvider = widget.getLabelProvider();
147 return labelProvider != null ? labelProvider.getColumnText(fEntry, column) : column == 0 ? fEntry.getName() : "";
148 }
149
150 /**
151 * Select this time graph entry
152 *
153 * @return itself
154 */
155 public SWTBotTimeGraphEntry select() {
156 syncExec(new VoidResult() {
157 @Override
158 public void run() {
159 widget.setFocus();
160 widget.selectItem(fEntry, true);
161 }
162 });
163 return this;
164 }
165 }
This page took 0.033555 seconds and 4 git commands to generate.