org.eclipse.swtbot.swt.finder.widgets
Class AbstractSWTBot<T extends Widget>

java.lang.Object
  extended by org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot<T>
Direct Known Subclasses:
AbstractSWTBotControl, SWTBotCTabItem, SWTBotExpandBar, SWTBotExpandItem, SWTBotGefContextMenu, SWTBotMenu, SWTBotScale, SWTBotSlider, SWTBotSpinner, SWTBotStyledText, SWTBotTabItem, SWTBotTable, SWTBotTableColumn, SWTBotTableItem, SWTBotText, SWTBotToggleButton, SWTBotToolbarButton, SWTBotTrayItem, SWTBotTree, SWTBotTreeItem

public abstract class AbstractSWTBot<T extends Widget>
extends Object

Helper to find SWT Widgets and perform operations on them.

Version:
$Id$
Author:
Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>, Joshua Gosse <jlgosse [at] ca [dot] ibm [dot] com>

Field Summary
protected  SelfDescribing description
          The description of the widget.
 Display display
          With great power comes great responsibility, use carefully.
protected  org.apache.log4j.Logger log
          The logger.
 T widget
          With great power comes great responsibility, use carefully.
 
Constructor Summary
AbstractSWTBot(T w)
          Constructs a new instance with the given widget.
AbstractSWTBot(T w, SelfDescribing description)
          Constructs a new instance with the given widget.
 
Method Summary
protected  Rectangle absoluteLocation()
           
protected  void assertEnabled()
          Check if the widget is enabled, throws if the widget is disabled.
protected  void asyncExec(VoidResult toExecute)
          Invokes Result.run() asynchronously on the UI thread.
 Color backgroundColor()
          Gets the background color of the widget.
protected  AbstractSWTBot<T> click()
          Clicks on this widget.
protected  AbstractSWTBot<T> click(int x, int y, boolean post)
          Perform a click action at the given coordinates
protected  void clickXY(int x, int y)
          Click on the table at given coordinates
protected  SWTBotMenu contextMenu(Control control, String text)
          Gets the context menu on the given control, matching the text.
 SWTBotMenu contextMenu(String text)
          Gets the context menu matching the text.
protected  Event createEvent()
          Creates an event.
protected  Event createMouseEvent(int x, int y, int button, int stateMask, int count)
          Create a mouse event
protected  Event createSelectionEvent(int stateMask)
          Create a selection event with a particular state mask
protected  void doubleClickXY(int x, int y)
          Double-click on the table at given coordinates
 Color foregroundColor()
          Gets the foreground color of the widget.
 String getId()
          Gets the value of Widget#getData(String)) for the key SWTBotPreferences.DEFAULT_KEY of this object's widget.
 String getText()
          Gets the text of this object's widget.
 String getToolTipText()
          Gets the tooltip of this object's widget.
protected  boolean hasStyle(Widget w, int style)
          Check if this widget has a style attribute.
 boolean isActive()
           
 boolean isEnabled()
          Gets if the object's widget is enabled.
protected  boolean isEnabledInternal()
          Gets if the widget is enabled.
 boolean isVisible()
          Checks if the widget is visible.
protected  Keyboard keyboard()
           
protected  void notify(int eventType)
          Sends a non-blocking notification of the specified type to the widget.
protected  void notify(int eventType, Event createEvent)
          Sends a non-blocking notification of the specified type to the widget.
protected  void notify(int eventType, Event createEvent, Widget widget)
          Sends a non-blocking notification of the specified type to the widget.
 AbstractSWTBot<T> pressShortcut(int modificationKeys, char c)
          Presses the shortcut specified by the given keys.
 AbstractSWTBot<T> pressShortcut(int modificationKeys, int keyCode, char c)
          Presses the shortcut specified by the given keys.
 AbstractSWTBot<T> pressShortcut(KeyStroke... keys)
          Presses the shortcut specified by the given keys.
protected  AbstractSWTBot<T> rightClick()
          Empty method stub, since it should be overridden by subclass#rightClick
protected  AbstractSWTBot<T> rightClick(int x, int y, boolean post)
          Perform a right-click action at the given coordinates
 void setFocus()
          Sets the focus on this control.
protected static void sleep(long millis)
          Sleeps for millis milliseconds.
protected
<T> T[]
syncExec(ArrayResult<T> toExecute)
          Invokes ArrayResult.run() on the UI thread.
protected  boolean syncExec(BoolResult toExecute)
          Invokes Result.run() synchronously on the UI thread.
protected  int syncExec(IntResult toExecute)
          Invokes Result.run() synchronously on the UI thread.
protected
<E> List<E>
syncExec(ListResult<E> toExecute)
          Invokes Result.run() on the UI thread.
protected
<T> T
syncExec(Result<T> toExecute)
          Invokes Result.run() synchronously on the UI thread.
protected  String syncExec(StringResult toExecute)
          Invokes Result.run() synchronously on the UI thread.
protected  void syncExec(VoidResult toExecute)
          Invokes VoidResult.run() on the UI thread.
protected  T syncExec(WidgetResult<T> toExecute)
          Invokes Result.run() synchronously on the UI thread.
 String toString()
           
 boolean traverse(Traverse traverse)
           
protected  void waitForEnabled()
          Wait until the widget is enabled.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

log

protected final org.apache.log4j.Logger log
The logger.


widget

public final T extends Widget widget
With great power comes great responsibility, use carefully.


display

public final Display display
With great power comes great responsibility, use carefully.


description

protected final SelfDescribing description
The description of the widget.

Constructor Detail

AbstractSWTBot

public AbstractSWTBot(T w)
               throws WidgetNotFoundException
Constructs a new instance with the given widget.

Parameters:
w - the widget.
Throws:
WidgetNotFoundException - if the widget is null or widget has been disposed.

AbstractSWTBot

public AbstractSWTBot(T w,
                      SelfDescribing description)
               throws WidgetNotFoundException
Constructs a new instance with the given widget.

Parameters:
w - the widget.
description - the description of the widget, this will be reported by toString()
Throws:
WidgetNotFoundException - if the widget is null or widget has been disposed.
Method Detail

notify

protected void notify(int eventType)
Sends a non-blocking notification of the specified type to the widget.

Parameters:
eventType - the event type.
See Also:
Widget.notifyListeners(int, Event)

notify

protected void notify(int eventType,
                      Event createEvent)
Sends a non-blocking notification of the specified type to the widget.

Parameters:
eventType - the type of event.
createEvent - the event to be sent to the widget.

notify

protected void notify(int eventType,
                      Event createEvent,
                      Widget widget)
Sends a non-blocking notification of the specified type to the widget.

Parameters:
eventType - the type of event.
createEvent - the event to be sent to the widget.
widget - the widget to send the event to.

sleep

protected static void sleep(long millis)
Sleeps for millis milliseconds. Delegate to SWTUtils.sleep(long)

Parameters:
millis - the time in milli seconds

createEvent

protected Event createEvent()
Creates an event.

Returns:
an event that encapsulates widget and display. Subclasses may override to set other event properties.

createMouseEvent

protected Event createMouseEvent(int x,
                                 int y,
                                 int button,
                                 int stateMask,
                                 int count)
Create a mouse event

Parameters:
x - the x co-ordinate of the mouse event.
y - the y co-ordinate of the mouse event.
button - the mouse button that was clicked.
stateMask - the state of the keyboard modifier keys.
count - the number of times the mouse was clicked.
Returns:
an event that encapsulates widget and display
Since:
1.2

createSelectionEvent

protected Event createSelectionEvent(int stateMask)
Create a selection event with a particular state mask

Parameters:
stateMask - the state of the keyboard modifier keys.

clickXY

protected void clickXY(int x,
                       int y)
Click on the table at given coordinates

Parameters:
x - the x co-ordinate of the click
y - the y co-ordinate of the click
Since:
2.0

doubleClickXY

protected void doubleClickXY(int x,
                             int y)
Double-click on the table at given coordinates

Parameters:
x - the x co-ordinate of the click
y - the y co-ordinate of the click
Since:
2.0

toString

public String toString()
Overrides:
toString in class Object

getText

public String getText()
Gets the text of this object's widget.

Returns:
the text on the widget.

getId

public String getId()
Gets the value of Widget#getData(String)) for the key SWTBotPreferences.DEFAULT_KEY of this object's widget.

Returns:
the id that SWTBot may use to search this widget.
See Also:
WithId

getToolTipText

public String getToolTipText()
Gets the tooltip of this object's widget.

Returns:
the tooltip on the widget.
Since:
1.0

hasStyle

protected boolean hasStyle(Widget w,
                           int style)
Check if this widget has a style attribute.

Parameters:
w - the widget.
style - the style bits, one of the constants in SWT.
Returns:
true if style is set on the widget.

contextMenu

public SWTBotMenu contextMenu(String text)
                       throws WidgetNotFoundException
Gets the context menu matching the text.

Parameters:
text - the text on the context menu.
Returns:
the menu that has the given text.
Throws:
WidgetNotFoundException - if the widget is not found.

contextMenu

protected SWTBotMenu contextMenu(Control control,
                                 String text)
Gets the context menu on the given control, matching the text.

Parameters:
control - the control
text - the text on the context menu.
Returns:
the menu that has the given text.
Throws:
WidgetNotFoundException - if the widget is not found.
Since:
2.0

isEnabled

public boolean isEnabled()
Gets if the object's widget is enabled.

Returns:
true if the widget is enabled.
See Also:
Control.isEnabled()

isEnabledInternal

protected boolean isEnabledInternal()
Gets if the widget is enabled.

This method is not thread safe, and must be called from the UI thread.

Returns:
true if the widget is enabled.
Since:
1.0

syncExec

protected <T> T[] syncExec(ArrayResult<T> toExecute)
Invokes ArrayResult.run() on the UI thread.

Parameters:
toExecute - the object to be invoked in the UI thread.
Returns:
the array returned by toExecute.

syncExec

protected void syncExec(VoidResult toExecute)
Invokes VoidResult.run() on the UI thread.

Parameters:
toExecute - the object to be invoked in the UI thread.

syncExec

protected <E> List<E> syncExec(ListResult<E> toExecute)
Invokes Result.run() on the UI thread.

Parameters:
toExecute - the object to be invoked in the UI thread.
Returns:
the list returned by toExecute

syncExec

protected boolean syncExec(BoolResult toExecute)
Invokes Result.run() synchronously on the UI thread.

Parameters:
toExecute - the object to be invoked in the UI thread.
Returns:
the boolean returned by toExecute

syncExec

protected String syncExec(StringResult toExecute)
Invokes Result.run() synchronously on the UI thread.

Parameters:
toExecute - the object to be invoked in the UI thread.
Returns:
the boolean returned by toExecute

syncExec

protected <T> T syncExec(Result<T> toExecute)
Invokes Result.run() synchronously on the UI thread.

Parameters:
toExecute - the object to be invoked in the UI thread.
Returns:
the boolean returned by toExecute

syncExec

protected T syncExec(WidgetResult<T> toExecute)
Invokes Result.run() synchronously on the UI thread.

Parameters:
toExecute - the object to be invoked in the UI thread.
Returns:
the Widget returned by toExecute

syncExec

protected int syncExec(IntResult toExecute)
Invokes Result.run() synchronously on the UI thread.

Parameters:
toExecute - the object to be invoked in the UI thread.
Returns:
the integer returned by toExecute

asyncExec

protected void asyncExec(VoidResult toExecute)
Invokes Result.run() asynchronously on the UI thread.

Parameters:
toExecute - the object to be invoked in the UI thread.

foregroundColor

public Color foregroundColor()
Gets the foreground color of the widget.

Returns:
the foreground color on the widget, or null if the widget is not an instance of Control.
Since:
1.0

backgroundColor

public Color backgroundColor()
Gets the background color of the widget.

Returns:
the background color on the widget, or null if the widget is not an instance of Control.
Since:
1.0

assertEnabled

protected void assertEnabled()
Check if the widget is enabled, throws if the widget is disabled.

Since:
1.3

waitForEnabled

protected void waitForEnabled()
Wait until the widget is enabled.

Since:
2.0

isVisible

public boolean isVisible()
Checks if the widget is visible.

Returns:
true if the widget is visible, false otherwise.
Since:
1.0

setFocus

public void setFocus()
Sets the focus on this control.

Since:
1.2

traverse

public boolean traverse(Traverse traverse)
Parameters:
traverse - the kind of traversal to perform.
Returns:
true if the traversal succeeded.
See Also:
Control.traverse(int)

isActive

public boolean isActive()
Returns:
true if this widget has focus.
See Also:
Display.getFocusControl()

click

protected AbstractSWTBot<T> click()
Clicks on this widget.

Returns:
itself.

rightClick

protected AbstractSWTBot<T> rightClick()
Empty method stub, since it should be overridden by subclass#rightClick

Returns:
itself.

click

protected AbstractSWTBot<T> click(int x,
                                  int y,
                                  boolean post)
Perform a click action at the given coordinates

Parameters:
x - the x coordinate
y - the y coordinate
post - Whether or not Display.post(org.eclipse.swt.widgets.Event) should be used
Returns:
itself.

rightClick

protected AbstractSWTBot<T> rightClick(int x,
                                       int y,
                                       boolean post)
Perform a right-click action at the given coordinates

Parameters:
x - the x coordinate
y - the y coordinate
post - Whether or not Display.post(org.eclipse.swt.widgets.Event) should be used
Returns:
itself.

absoluteLocation

protected Rectangle absoluteLocation()
Returns:
the absolute location of the widget relative to the display.

keyboard

protected Keyboard keyboard()
Returns:
the keyboard to use to type on this widget.

pressShortcut

public AbstractSWTBot<T> pressShortcut(int modificationKeys,
                                       char c)
Presses the shortcut specified by the given keys.

Parameters:
modificationKeys - the combination of SWT.ALT | SWT.CTRL | SWT.SHIFT | SWT.COMMAND.
c - the character
Returns:
the same instance
See Also:
Keystrokes.toKeys(int, char)

pressShortcut

public AbstractSWTBot<T> pressShortcut(int modificationKeys,
                                       int keyCode,
                                       char c)
Presses the shortcut specified by the given keys.

Parameters:
modificationKeys - the combination of SWT.ALT | SWT.CTRL | SWT.SHIFT | SWT.COMMAND.
keyCode - the keyCode, these may be special keys like F1-F12, or navigation keys like HOME, PAGE_UP
c - the character
Returns:
the same instance
See Also:
Keystrokes.toKeys(int, char)

pressShortcut

public AbstractSWTBot<T> pressShortcut(KeyStroke... keys)
Presses the shortcut specified by the given keys.

Parameters:
keys - the keys to press
Returns:
the same instance
See Also:
Keyboard.pressShortcut(KeyStroke...), Keystrokes