| /******************************************************************************* |
| * Copyright (c) 2008, 2017 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 |
| * Patrick Tasse - Support contextMenu() on table column header |
| * - Improve SWTBot menu API and implementation (Bug 479091) |
| *******************************************************************************/ |
| package org.eclipse.swtbot.swt.finder.widgets; |
| |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.graphics.Point; |
| import org.eclipse.swt.graphics.Rectangle; |
| import org.eclipse.swt.widgets.Table; |
| import org.eclipse.swt.widgets.TableColumn; |
| import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; |
| import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; |
| import org.eclipse.swtbot.swt.finder.results.Result; |
| import org.eclipse.swtbot.swt.finder.results.WidgetResult; |
| import org.hamcrest.SelfDescribing; |
| |
| /** |
| * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com> |
| * @version $Id$ |
| */ |
| public class SWTBotTableColumn extends AbstractSWTBot<TableColumn> { |
| |
| private final Table parent; |
| |
| /** |
| * Constructs a new instance of this object. |
| * |
| * @param w the widget. |
| * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed. |
| * @since 2.0 |
| */ |
| public SWTBotTableColumn(final TableColumn w) throws WidgetNotFoundException { |
| this(w, UIThreadRunnable.syncExec(new WidgetResult<Table>() { |
| @Override |
| public Table run() { |
| return w.getParent(); |
| } |
| })); |
| } |
| |
| /** |
| * Constructs a new instance of this object. |
| * |
| * @param w the widget. |
| * @param parent the parent table. |
| * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed. |
| * @since 2.0 |
| */ |
| public SWTBotTableColumn(TableColumn w, Table parent) throws WidgetNotFoundException { |
| this(w, parent, null); |
| } |
| |
| /** |
| * Constructs a new instance of this object. |
| * |
| * @param w the widget. |
| * @param parent the parent table. |
| * @param description the description of the widget, this will be reported by {@link #toString()} |
| * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed. |
| * @since 2.0 |
| */ |
| public SWTBotTableColumn(TableColumn w, Table parent, SelfDescribing description) throws WidgetNotFoundException { |
| super(w, description); |
| this.parent = parent; |
| } |
| |
| @Override |
| protected Rectangle getBounds() { |
| return syncExec(new Result<Rectangle>() { |
| @Override |
| public Rectangle run() { |
| Table table = widget.getParent(); |
| Point location = widget.getDisplay().map(table.getParent(), table, table.getLocation()); |
| Rectangle bounds = new Rectangle(location.x, location.y, widget.getWidth(), table.getHeaderHeight()); |
| for (int i : table.getColumnOrder()) { |
| TableColumn column = table.getColumn(i); |
| if (column.equals(widget)) { |
| break; |
| } else { |
| bounds.x += column.getWidth(); |
| } |
| } |
| return bounds; |
| } |
| }); |
| } |
| |
| /** |
| * Clicks the item. |
| */ |
| @Override |
| public SWTBotTableColumn click() { |
| waitForEnabled(); |
| notify(SWT.Selection); |
| // Mouse event doesn't seem to be sent by the real SWT? |
| notify(SWT.MouseUp, createMouseEvent(1, SWT.BUTTON1, 1), parent); |
| return this; |
| } |
| |
| @Override |
| public boolean isEnabled() { |
| return true; |
| } |
| |
| @Override |
| public SWTBotRootMenu contextMenu() throws WidgetNotFoundException { |
| new SWTBotTable(parent).waitForEnabled(); |
| return contextMenu(parent); |
| } |
| } |