diff options
author | Patrick Tasse | 2015-06-18 14:16:33 +0000 |
---|---|---|
committer | Patrick Tasse | 2015-06-18 22:10:45 +0000 |
commit | 76186b70364e992c0185d4894322e8410d75d192 (patch) | |
tree | 0efb96a8eb9a9c7eb4419b25c59c9b3c685f10f4 | |
parent | 127fc7e80f7fd0db9a2a740c1663c60fdc6b476f (diff) | |
download | org.eclipse.swtbot-76186b70364e992c0185d4894322e8410d75d192.tar.gz org.eclipse.swtbot-76186b70364e992c0185d4894322e8410d75d192.tar.xz org.eclipse.swtbot-76186b70364e992c0185d4894322e8410d75d192.zip |
Support contextMenu() on table and tree column headers
This ensures that a SWT.MenuDetect event is sent at the coordinates of
the column header before the context menu is searched.
Change-Id: Iba2599366dc2c85c61b7ca633f84a9da6286eee0
Signed-off-by: Patrick Tasse <patrick.tasse@gmail.com>
6 files changed, 200 insertions, 26 deletions
diff --git a/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/TableTab.java b/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/TableTab.java index 565c69a6..16c7a887 100644 --- a/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/TableTab.java +++ b/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/TableTab.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2007, 2013 IBM Corporation and others. + * Copyright (c) 2000, 2015 IBM Corporation 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 @@ -8,6 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation * Kristine Jetzke - Bug 379185 + * Patrick Tasse - Add column header popup menu item *******************************************************************************/ package org.eclipse.swt.examples.controlexample; @@ -87,16 +88,39 @@ class TableTab extends ScrollableTab { } protected void specialPopupMenuItems(Menu menu, Event event) { - MenuItem item = new MenuItem(menu, SWT.PUSH); - item.setText("getItem(Point) on mouse coordinates"); - menuMouseCoords = table1.toControl(new Point(event.x, event.y)); - item.addSelectionListener(new SelectionAdapter() { - public void widgetSelected(SelectionEvent e) { - eventConsole.append("getItem(Point(" + menuMouseCoords + ")) returned: " - + table1.getItem(menuMouseCoords)); - eventConsole.append("\n"); - }; - }); + final Table table = (Table) event.widget; + Point p = table.getParent().toDisplay(table.getLocation()); + if (headerVisibleButton.getSelection() && event.y >= p.y && + event.y < p.y + table.getHeaderHeight()) { + int x = p.x; + for (int i : table.getColumnOrder()) { + final TableColumn column = table.getColumn(i); + if (event.x >= x && event.x < x + column.getWidth()) { + MenuItem item = new MenuItem(menu, SWT.PUSH); + item.setText("Get Column Header Text"); + item.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + eventConsole.append("Get Column Header Text returned: " + + column.getText()); + eventConsole.append("\n"); + }; + }); + break; + } + x += column.getWidth(); + } + } else { + MenuItem item = new MenuItem(menu, SWT.PUSH); + item.setText("getItem(Point) on mouse coordinates"); + menuMouseCoords = table.toControl(new Point(event.x, event.y)); + item.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + eventConsole.append("getItem(Point(" + menuMouseCoords + ")) returned: " + + table.getItem(menuMouseCoords)); + eventConsole.append("\n"); + }; + }); + } } void changeFontOrColor(int index) { diff --git a/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/TreeTab.java b/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/TreeTab.java index 4c40fd3c..53be3d45 100644 --- a/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/TreeTab.java +++ b/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/TreeTab.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2007, 2013 IBM Corporation and others. + * Copyright (c) 2000, 2015 IBM Corporation 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 @@ -8,6 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation * Kristine Jetzke - Bug 379185 + * Patrick Tasse - Add column header popup menu item *******************************************************************************/ package org.eclipse.swt.examples.controlexample; @@ -87,16 +88,38 @@ class TreeTab extends ScrollableTab { } protected void specialPopupMenuItems(Menu menu, Event event) { - MenuItem item = new MenuItem(menu, SWT.PUSH); - item.setText("getItem(Point) on mouse coordinates"); - final Tree t = (Tree) event.widget; - menuMouseCoords = t.toControl(new Point(event.x, event.y)); - item.addSelectionListener(new SelectionAdapter() { - public void widgetSelected(SelectionEvent e) { - eventConsole.append("getItem(Point(" + menuMouseCoords + ")) returned: " + t.getItem(menuMouseCoords)); - eventConsole.append("\n"); - }; - }); + final Tree tree = (Tree) event.widget; + Point p = tree.getParent().toDisplay(tree.getLocation()); + if (headerVisibleButton.getSelection() && event.y >= p.y && + event.y < p.y + tree.getHeaderHeight()) { + int x = p.x; + for (int i : tree.getColumnOrder()) { + final TreeColumn column = tree.getColumn(i); + if (event.x >= x && event.x < x + column.getWidth()) { + MenuItem item = new MenuItem(menu, SWT.PUSH); + item.setText("Get Column Header Text"); + item.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + eventConsole.append("Get Column Header Text returned: " + + column.getText()); + eventConsole.append("\n"); + }; + }); + break; + } + x += column.getWidth(); + } + } else { + MenuItem item = new MenuItem(menu, SWT.PUSH); + item.setText("getItem(Point) on mouse coordinates"); + menuMouseCoords = tree.toControl(new Point(event.x, event.y)); + item.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + eventConsole.append("getItem(Point(" + menuMouseCoords + ")) returned: " + tree.getItem(menuMouseCoords)); + eventConsole.append("\n"); + }; + }); + } } void changeFontOrColor(int index) { diff --git a/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTableColumnTest.java b/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTableColumnTest.java index d197a963..6aae62b9 100644 --- a/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTableColumnTest.java +++ b/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTableColumnTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008 Ketan Padegaonkar and others. + * Copyright (c) 2008, 2015 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 @@ -7,6 +7,7 @@ * * Contributors: * Ketan Padegaonkar - initial API and implementation + * Patrick Tasse - Add column header context menu test *******************************************************************************/ package org.eclipse.swtbot.swt.finder.widgets; @@ -47,11 +48,34 @@ public class SWTBotTableColumnTest extends AbstractControlExampleTest { assertTextContains("data=null button=1 stateMask=" + toStateMask(524288, table.widget) + " x=0 y=0 count=1}", text); } + @Test + public void clicksHeaderContextMenuItem() throws Exception { + Text text = bot.textInGroup("Listeners").widget; + + SWTBotTableColumn header = table.header("Name"); + header.contextMenu("Get Column Header Text").click(); + assertTextContains("Get Column Header Text returned: Name", text); + + header = table.header("Type"); + header.contextMenu("Get Column Header Text").click(); + assertTextContains("Get Column Header Text returned: Type", text); + + header = table.header("Size"); + header.contextMenu("Get Column Header Text").click(); + assertTextContains("Get Column Header Text returned: Size", text); + + header = table.header("Modified"); + header.contextMenu("Get Column Header Text").click(); + assertTextContains("Get Column Header Text returned: Modified", text); + } + @Before public void prepareExample() throws Exception { bot.tabItem("Table").activate(); bot.radio("SWT.MULTI").click(); bot.checkBox("Header Visible").select(); + bot.checkBox("Multiple Columns").select(); + bot.checkBox("Popup Menu").select(); bot.checkBox("Listen").select(); bot.button("Clear").click(); table = bot.tableInGroup("Table"); diff --git a/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeColumnTest.java b/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeColumnTest.java index fd34a93e..e9d07e60 100644 --- a/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeColumnTest.java +++ b/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeColumnTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Robin Stocker and others. + * Copyright (c) 2013, 2015 Robin Stocker 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 @@ -7,6 +7,7 @@ * * Contributors: * Robin Stocker - initial implementation + * Patrick Tasse - Add column header context menu test *******************************************************************************/ package org.eclipse.swtbot.swt.finder.widgets; @@ -43,12 +44,34 @@ public class SWTBotTreeColumnTest extends AbstractControlExampleTest { assertTextContains("data=null button=1 stateMask=" + toStateMask(524288, tree.widget) + " x=0 y=0 count=1}", text); } + @Test + public void clicksHeaderContextMenuItem() throws Exception { + Text text = bot.textInGroup("Listeners").widget; + + SWTBotTreeColumn header = tree.header("Name"); + header.contextMenu("Get Column Header Text").click(); + assertTextContains("Get Column Header Text returned: Name", text); + + header = tree.header("Type"); + header.contextMenu("Get Column Header Text").click(); + assertTextContains("Get Column Header Text returned: Type", text); + + header = tree.header("Size"); + header.contextMenu("Get Column Header Text").click(); + assertTextContains("Get Column Header Text returned: Size", text); + + header = tree.header("Modified"); + header.contextMenu("Get Column Header Text").click(); + assertTextContains("Get Column Header Text returned: Modified", text); + } + @Before public void prepareExample() throws Exception { bot.tabItem("Tree").activate(); bot.radio("SWT.MULTI").click(); bot.checkBox("Header Visible").select(); bot.checkBox("Multiple Columns").select(); + bot.checkBox("Popup Menu").select(); bot.checkBox("Listen").select(); bot.button("Clear").click(); tree = bot.treeInGroup("Tree"); diff --git a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTableColumn.java b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTableColumn.java index c809a2ff..3374934b 100644 --- a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTableColumn.java +++ b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTableColumn.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008 Ketan Padegaonkar and others. + * Copyright (c) 2008, 2015 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 @@ -7,14 +7,20 @@ * * Contributors: * Ketan Padegaonkar - initial API and implementation + * Patrick Tasse - Support contextMenu() on table column header *******************************************************************************/ 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.Event; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; +import org.eclipse.swt.widgets.Widget; 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; @@ -82,4 +88,38 @@ public class SWTBotTableColumn extends AbstractSWTBot<TableColumn> { return true; } + @Override + public SWTBotMenu contextMenu(String text) throws WidgetNotFoundException { + new SWTBotTable(parent).waitForEnabled(); + Rectangle bounds = getHeaderBounds(); + Event event = createEvent(); + event.widget = parent; + event.x = bounds.x + bounds.width / 2; + event.y = bounds.y + bounds.height / 2; + notify(SWT.MenuDetect, event, (Widget) parent); + return super.contextMenu(parent, text); + } + + /** + * Get the bounds of this column's header in display-relative coordinates. + * + * @return the column header bounds + */ + private Rectangle getHeaderBounds() { + return syncExec(new Result<Rectangle>() { + public Rectangle run() { + Point location = parent.getParent().toDisplay(parent.getLocation()); + Rectangle bounds = new Rectangle(location.x, location.y, widget.getWidth(), parent.getHeaderHeight()); + for (int i : parent.getColumnOrder()) { + TableColumn column = parent.getColumn(i); + if (column.equals(widget)) { + break; + } else { + bounds.x += column.getWidth(); + } + } + return bounds; + } + }); + } } diff --git a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeColumn.java b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeColumn.java index 79823b8e..b311728d 100644 --- a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeColumn.java +++ b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotTreeColumn.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Robin Stocker and others. + * Copyright (c) 2013, 2015 Robin Stocker 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 @@ -7,14 +7,20 @@ * * Contributors: * Robin Stocker - initial API and implementation + * Patrick Tasse - Support contextMenu() on tree column header *******************************************************************************/ 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.Event; import org.eclipse.swt.widgets.Tree; import org.eclipse.swt.widgets.TreeColumn; +import org.eclipse.swt.widgets.Widget; 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; @@ -84,4 +90,38 @@ public class SWTBotTreeColumn extends AbstractSWTBot<TreeColumn> { return true; } + @Override + public SWTBotMenu contextMenu(String text) throws WidgetNotFoundException { + new SWTBotTree(parent).waitForEnabled(); + Rectangle bounds = getHeaderBounds(); + Event event = createEvent(); + event.widget = parent; + event.x = bounds.x + bounds.width / 2; + event.y = bounds.y + bounds.height / 2; + notify(SWT.MenuDetect, event, (Widget) parent); + return super.contextMenu(parent, text); + } + + /** + * Get the bounds of this column's header in display-relative coordinates. + * + * @return the column header bounds + */ + private Rectangle getHeaderBounds() { + return syncExec(new Result<Rectangle>() { + public Rectangle run() { + Point location = parent.getParent().toDisplay(parent.getLocation()); + Rectangle bounds = new Rectangle(location.x, location.y, widget.getWidth(), parent.getHeaderHeight()); + for (int i : parent.getColumnOrder()) { + TreeColumn column = parent.getColumn(i); + if (column.equals(widget)) { + break; + } else { + bounds.x += column.getWidth(); + } + } + return bounds; + } + }); + } } |