Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c45a74247bb6eeffd52a35d29edf51c4f5ad6e11 (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
/*******************************************************************************
 * Copyright (c) 2016 Cadence Design Systems, Inc. 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:
 *     Aparna Argade(Cadence Design Systems, Inc.) - initial API and implementation
 *******************************************************************************/
package org.eclipse.swtbot.nebula.nattable.finder.widgets;
import org.eclipse.nebula.widgets.nattable.NatTable;
import org.eclipse.nebula.widgets.nattable.edit.editor.ICellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swtbot.nebula.nattable.finder.finders.NatTableContextMenuFinder;
import org.eclipse.swtbot.swt.finder.ReferenceBy;
import org.eclipse.swtbot.swt.finder.SWTBotWidget;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
import org.eclipse.swtbot.swt.finder.results.IntResult;
import org.eclipse.swtbot.swt.finder.results.Result;
import org.eclipse.swtbot.swt.finder.results.VoidResult;
import org.eclipse.swtbot.swt.finder.utils.internal.Assert;
import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotRootMenu;
import org.hamcrest.SelfDescribing;

/**
 * A class which allows an SWTBot to detect and interact with a NatTable.
 * <p>
 * All row and column numbers are relative to the visible grid layer. The row
 * and column numbers include headers also and are affected by filtering,
 * sorting and scrolling, etc.
 */
@SWTBotWidget(clasz = NatTable.class, preferredName = "NatTable", referenceBy = { ReferenceBy.LABEL })
public class SWTBotNatTable extends AbstractSWTBot<NatTable> {
	/**
	 * The default constructor
	 *
	 * @param natTable
	 *            The NatTable to be wrapped.
	 * @throws WidgetNotFoundException
	 *             An exception to be thrown when the SWTBot is unable to find a
	 *             valid NatTable.
	 */
	public SWTBotNatTable(NatTable natTable) throws WidgetNotFoundException {
		this(natTable, null);
	}

	/**
	 * The default constructor
	 *
	 * @param natTable
	 *            The NatTable to be wrapped.
	 * @param description
	 *            Self description
	 * @throws WidgetNotFoundException
	 *             An exception to be thrown when the SWTBot is unable to find a
	 *             valid NatTable.
	 */
	public SWTBotNatTable(NatTable natTable, SelfDescribing description) throws WidgetNotFoundException {
		super(natTable, description);
	}

	/**
	 * Gets visible row count for the NatTable
	 *
	 * @return The number of rows in the NatTable that are visible
	 */
	public int rowCount() {
		return syncExec(new IntResult() {
			@Override
			public Integer run() {
				return widget.getRowCount();
			}
		});
	}

	/**
	 * Gets total row count for the NatTable
	 *
	 * @return Total number of rows in the NatTable
	 */
	public int preferredRowCount() {
		return syncExec(new IntResult() {
			@Override
			public Integer run() {
				return widget.getPreferredRowCount();
			}
		});
	}

	/**
	 * Gets the visible column count.
	 *
	 * @return the number of columns in the NatTable that are visible
	 */
	public int columnCount() {
		return syncExec(new IntResult() {
			@Override
			public Integer run() {
				return widget.getColumnCount();
			}
		});
	}

	/**
	 * Gets the total column count.
	 *
	 * @return total number of columns in the NatTable
	 */
	public int preferredColumnCount() {
		return syncExec(new IntResult() {
			@Override
			public Integer run() {
				return widget.getPreferredColumnCount();
			}
		});
	}

	/**
	 * Click on the NatTable on given cell.
	 *
	 * @param row
	 *            the visible row number in the NatTable
	 * @param column
	 *            the visible column number in the NatTable
	 * @return itself
	 */
	public SWTBotNatTable click(final int row, final int column) {
		assertIsLegalCell(row, column);
		waitForEnabled();
		setFocus();
		syncExec(new VoidResult() {
			@Override
			public void run() {
				Rectangle cellBounds = widget.getBoundsByPosition(column, row);
				clickXY(cellBounds.x + (cellBounds.width / 2), cellBounds.y + (cellBounds.height / 2));
			}
		});
		return this;
	}

	/**
	 * DoubleClick on the NatTable on given cell.
	 *
	 * @param row
	 *            the visible row number in the NatTable
	 * @param column
	 *            the visible column number in the NatTable
	 * @return itself
	 */
	public SWTBotNatTable doubleclick(final int row, final int column) {
		assertIsLegalCell(row, column);
		setFocus();
		syncExec(new VoidResult() {
			@Override
			public void run() {
				Rectangle cellBounds = widget.getBoundsByPosition(column, row);
				doubleClickXY(cellBounds.x + (cellBounds.width / 2), cellBounds.y + (cellBounds.height / 2));
			}
		});
		return this;
	}

	/**
	 * RightClick on the NatTable on given cell.
	 *
	 * @param row
	 * 	the visible row number in the NatTable
	 * @param column
	 * 	the visible column number in the NatTable
	 * @return itself
	 */
	public SWTBotNatTable rightClick(final int row, final int column) {
		assertIsLegalCell(row, column);
		setFocus();
		UIThreadRunnable.syncExec(new VoidResult() {
			@Override
			public void run() {
				Rectangle cellBounds = widget.getBoundsByPosition(column, row);
				rightClick(cellBounds.x + (cellBounds.width / 2), cellBounds.y + (cellBounds.height / 2), false);
			}
		});
		return this;
	}

	/**
	 * RightClick on the NatTable at the center of widget.
	 *
	 * @return itself
	 */
	@Override
	public SWTBotNatTable rightClick() {
		setFocus();
		UIThreadRunnable.syncExec(new VoidResult() {
			@Override
			public void run() {
				Rectangle widgetBounds = widget.getBounds();
				rightClick(widgetBounds.width / 2, widgetBounds.height / 2, false);
			}
		});
		return this;
	}

	/**
	 * Get the bounds of the Widget
	 *
	 * @return the bounds of the Widget relative to its parent
	 */
	protected Rectangle widgetBounds() {
		return syncExec(new Result<Rectangle>() {
			@Override
			public Rectangle run() {
				return widget.getBounds();
			}
		});
	}

	/**
	 * Gets the context menu of the given control.
	 * <p>
	 * The context menu is invoked at the center of this widget.
	 *
	 * @param control
	 *            the control.
	 * @return the context menu.
	 * @throws WidgetNotFoundException
	 *             if the widget is not found.
	 */
	@Override
	protected SWTBotRootMenu contextMenu(final Control control) throws WidgetNotFoundException {
		waitForEnabled();
		Rectangle location = widgetBounds();
		return NatTableContextMenuFinder.contextMenu(widget, location.width / 2, location.height / 2);
	}

	/**
	 * Gets the context menu on the given cell.
	 *
	 * @param row
	 *            the visible row number in the NatTable
	 * @param column
	 *            the visible column number in the NatTable
	 * @return the context menu.
	 * @throws WidgetNotFoundException
	 *             if the widget is not found.
	 */
	public SWTBotRootMenu contextMenu(int row, int column) throws WidgetNotFoundException {
		assertIsLegalCell(row, column);
		waitForEnabled();
		Rectangle location = widget.getBoundsByPosition(column, row);
		return NatTableContextMenuFinder.contextMenu(widget, location.x + (location.width / 2),
				location.y + (location.height / 2));
	}

	/**
	 * Asserts that the row and column are legal for this instance of the
	 * NatTable.
	 *
	 * @param row
	 *            the row number
	 * @param column
	 *            the column number
	 */
	protected void assertIsLegalCell(final int row, final int column) {
		int rowCount = rowCount();
		int columnCount = columnCount(); // 0 if no TableColumn has been created
		Assert.isLegal(row >= 0);
		Assert.isLegal(column >= 0);
		Assert.isLegal(row < rowCount, "The row number (" + row + ") is more than the number of visible rows (" //$NON-NLS-1$ //$NON-NLS-2$
				+ rowCount + ") in the table."); //$NON-NLS-1$
		Assert.isLegal((column < columnCount) || ((columnCount == 0) && (column == 0)), "The column number (" + column //$NON-NLS-1$
				+ ") is more than the number of visible columns (" + columnCount + ") in the table."); //$NON-NLS-1$ //$NON-NLS-2$
	}

	/**
	 * Reads the NatTable Cell with the given row and column indices.
	 *
	 * @param row
	 *            the visible row number in the NatTable
	 * @param column
	 *            the visible column number in the NatTable
	 * @return String value of the cell
	 *
	 */
	public String getCellDataValueByPosition(int row, int column) {
		assertIsLegalCell(row, column);
		Object obj = widget.getDataValueByPosition(column, row);
		if (obj != null) {
			return obj.toString();
		} else {
			return "";
		}
	}

	/**
	 * Sets the data in the NatTable Cell with the given row and column indices.
	 *
	 * @param row
	 *            the visible row number in the NatTable
	 * @param column
	 *            the visible column number in the NatTable
	 * @param text
	 *            the text to set.
	 */
	public void setCellDataValueByPosition(final int row, final int column, final String text) {
		assertIsLegalCell(row, column);
		Assert.isNotNull(text);
		waitForEnabled();
		Assert.isTrue(!hasStyle(widget, SWT.READ_ONLY));
		asyncExec(new VoidResult() {
			@Override
			public void run() {
				click(row, column);
				ICellEditor editor = widget.getActiveCellEditor();
				editor.setEditorValue(text);
			}
		});
		notify(SWT.Modify);
	}

}

Back to the top