org.fest.swing.core
Class BasicComponentFinder

java.lang.Object
  extended by org.fest.swing.core.BasicComponentFinder
All Implemented Interfaces:
ComponentFinder

public final class BasicComponentFinder
extends Object
implements ComponentFinder

Understands GUI Component lookup.

Author:
Alex Ruiz

Constructor Summary
protected BasicComponentFinder(ComponentHierarchy hierarchy)
          Creates a new BasicComponentFinder.
protected BasicComponentFinder(ComponentHierarchy hierarchy, Settings settings)
          Creates a new BasicComponentFinder.
 
Method Summary
 Component find(ComponentMatcher m)
          Finds a Component using the given ComponentMatcher.
 Component find(Container root, ComponentMatcher m)
          Finds a Component using the given ComponentMatcher in the hierarchy under the given root.
<T extends Component>
T
find(Container root, GenericTypeMatcher<T> m)
          Finds a Component using the given GenericTypeMatcher in the hierarchy under the given root.
<T extends Component>
T
find(GenericTypeMatcher<T> m)
          Finds a Component using the given GenericTypeMatcher.
 Collection<Component> findAll(ComponentMatcher m)
          Returns all the Components that match the search criteria specified in the given ComponentMatcher.
 Collection<Component> findAll(Container root, ComponentMatcher m)
          Returns all the Components under the given root that match the search criteria specified in the given ComponentMatcher.
<T extends Component>
Collection<T>
findAll(Container root, GenericTypeMatcher<T> m)
          Returns all the Components under the given root that match the search criteria specified in the given GenericTypeMatcher.
<T extends Component>
Collection<T>
findAll(GenericTypeMatcher<T> m)
          Returns all the Components that match the search criteria specified in the given GenericTypeMatcher.
 Component findByLabel(Container root, String label)
          Finds a Component by the text of its associated JLabel, in the hierarchy under the given root.
 Component findByLabel(Container root, String label, boolean showing)
          Finds a Component by the text of its associated JLabel, in the hierarchy under the given root.
<T extends Component>
T
findByLabel(Container root, String label, Class<T> type)
          Finds a Component by the text of its associated JLabel and type, in the hierarchy under the given root.
<T extends Component>
T
findByLabel(Container root, String label, Class<T> type, boolean showing)
          Finds a Component by the text of its associated JLabel and type, in the hierarchy under the given root.
 Component findByLabel(String label)
           Finds a Component by by the text of its associated JLabel.
 Component findByLabel(String label, boolean showing)
          Finds a Component by by the text of its associated JLabel.
<T extends Component>
T
findByLabel(String label, Class<T> type)
          Finds a Component by the text of its associated JLabel and type.
<T extends Component>
T
findByLabel(String label, Class<T> type, boolean showing)
          Finds a Component by the text of its associated JLabel and type.
 Component findByName(Container root, String name)
          Finds a Component by name, in the hierarchy under the given root.
 Component findByName(Container root, String name, boolean showing)
          Finds a Component by name, in the hierarchy under the given root.
<T extends Component>
T
findByName(Container root, String name, Class<T> type)
          Finds a Component by name and type, in the hierarchy under the given root.
<T extends Component>
T
findByName(Container root, String name, Class<T> type, boolean showing)
          Finds a Component by name and type, in the hierarchy under the given root.
 Component findByName(String name)
           Finds a Component by name.
 Component findByName(String name, boolean showing)
          Finds a Component by name.
<T extends Component>
T
findByName(String name, Class<T> type)
          Finds a Component by name and type.
<T extends Component>
T
findByName(String name, Class<T> type, boolean showing)
          Finds a Component by name and type.
<T extends Component>
T
findByType(Class<T> type)
          Finds a Component by type.
<T extends Component>
T
findByType(Class<T> type, boolean showing)
          Finds a Component by type.
<T extends Component>
T
findByType(Container root, Class<T> type)
           Finds a Component by type in the hierarchy under the given root.
<T extends Component>
T
findByType(Container root, Class<T> type, boolean showing)
          Finds a Component by type in the hierarchy under the given root.
static ComponentFinder finderWithCurrentAwtHierarchy()
          Creates a new BasicComponentFinder that has access to all the GUI components in the AWT hierarchy.
static ComponentFinder finderWithNewAwtHierarchy()
          Creates a new BasicComponentFinder with a new AWT hierarchy.
 boolean includeHierarchyIfComponentNotFound()
          Returns whether the message in a ComponentLookupException should include the current component hierarchy.
 void includeHierarchyIfComponentNotFound(boolean newValue)
          Updates whether the message in a ComponentLookupException should include the current component hierarchy.
 ComponentPrinter printer()
          Returns the ComponentPrinter in this finder.
protected  boolean requireShowingFromSettingsOr(boolean defaultValue)
          Returns the value of the flag "requireShowing" in the ComponentLookupScope this finder's Settings.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BasicComponentFinder

protected BasicComponentFinder(ComponentHierarchy hierarchy)
Creates a new BasicComponentFinder. The created finder does not use any Settings.

Parameters:
hierarchy - the component hierarchy to use.

BasicComponentFinder

protected BasicComponentFinder(ComponentHierarchy hierarchy,
                               Settings settings)
Creates a new BasicComponentFinder.

Parameters:
hierarchy - the component hierarchy to use.
settings - the configuration settings to use. It can be null.
Method Detail

finderWithNewAwtHierarchy

public static ComponentFinder finderWithNewAwtHierarchy()
Creates a new BasicComponentFinder with a new AWT hierarchy. Components created before the created BasicComponentFinder cannot be accessed by the created BasicComponentFinder.

Returns:
the created finder.

finderWithCurrentAwtHierarchy

public static ComponentFinder finderWithCurrentAwtHierarchy()
Creates a new BasicComponentFinder that has access to all the GUI components in the AWT hierarchy.

Returns:
the created finder.

printer

public ComponentPrinter printer()
Returns the ComponentPrinter in this finder.

Specified by:
printer in interface ComponentFinder
Returns:
the ComponentPrinter in this finder.

findByType

public <T extends Component> T findByType(Class<T> type)
Finds a Component by type. If this finder is attached to a Robot, it will use the component lookup scope in the Robot's Settings to determine whether the component to find should be showing or not. If this finder is not attached to any Robot, the component to find does not have to be showing.

Example:

 JTextField textbox = finder.findByType(JTextField.class);
 

Specified by:
findByType in interface ComponentFinder
Type Parameters:
T - the parameterized type of the component to find.
Parameters:
type - the type of the component to find.
Returns:
the found component.
See Also:
Robot.settings(), Settings.componentLookupScope(), ComponentLookupScope

findByType

@RunsInEDT
public <T extends Component> T findByType(Class<T> type,
                                                    boolean showing)
Finds a Component by type. For example:

Specified by:
findByType in interface ComponentFinder
Type Parameters:
T - the parameterized type of the component to find.
Parameters:
type - the type of the component to find.
showing - indicates whether the component to find should be visible (or showing) or not.
Returns:
the found component.
See Also:
ComponentFinder.findByType(Class)

findByType

@RunsInEDT
public <T extends Component> T findByType(Container root,
                                                    Class<T> type)

Finds a Component by type in the hierarchy under the given root. If this finder is attached to a Robot, it will use the component lookup scope in the Robot's Settings to determine whether the component to find should be showing or not. If this finder is not attached to any Robot, the component to find does not have to be showing.

Let's assume we have the following JFrame containing a JTextField:

 JFrame myFrame = new JFrame();
 myFrame.add(new JTextField());
 

If we want to get a reference to the JTextField in that particular JFrame without going through the whole AWT component hierarchy, we could simply specify:

 JTextField textbox = finder.findByType(myFrame, JTextField.class);
 

Specified by:
findByType in interface ComponentFinder
Type Parameters:
T - the parameterized type of the component to find.
Parameters:
root - the root used as the starting point of the search.
type - the type of the component to find.
Returns:
the found component.
See Also:
Robot.settings(), Settings.componentLookupScope(), ComponentLookupScope

findByType

@RunsInEDT
public <T extends Component> T findByType(Container root,
                                                    Class<T> type,
                                                    boolean showing)
Finds a Component by type in the hierarchy under the given root.

Specified by:
findByType in interface ComponentFinder
Type Parameters:
T - the parameterized type of the component to find.
Parameters:
root - the root used as the starting point of the search.
type - the type of the component to find.
showing - indicates whether the component to find should be visible (or showing) or not.
Returns:
the found component.
See Also:
ComponentFinder.findByType(Container, Class)

findByName

@RunsInEDT
public <T extends Component> T findByName(String name,
                                                    Class<T> type)
Finds a Component by name and type. If this finder is attached to a Robot, it will use the component lookup scope in the Robot's Settings to determine whether the component to find should be showing or not. If this finder is not attached to any Robot, the component to find does not have to be showing.

Specified by:
findByName in interface ComponentFinder
Type Parameters:
T - the parameterized type of the component to find.
Parameters:
name - the name of the component to find.
type - the type of the component to find.
Returns:
the found component.
See Also:
Robot.settings(), Settings.componentLookupScope(), ComponentLookupScope, ComponentFinder.findByName(String)

findByName

@RunsInEDT
public <T extends Component> T findByName(String name,
                                                    Class<T> type,
                                                    boolean showing)
Finds a Component by name and type.

Specified by:
findByName in interface ComponentFinder
Type Parameters:
T - the parameterized type of the component to find.
Parameters:
name - the name of the component to find.
type - the type of the component to find.
showing - indicates whether the component to find should be visible (or showing) or not.
Returns:
the found component.
See Also:
ComponentFinder.findByName(String)

findByName

@RunsInEDT
public Component findByName(String name)

Finds a Component by name. If this finder is attached to a Robot, it will use the component lookup scope in the Robot's Settings to determine whether the component to find should be showing or not. If this finder is not attached to any Robot, the component to find does not have to be showing.

Let's assume we have the JTextField with name "myTextBox";

 JTextField textbox = new JTextField();
 textBox.setName("myTextBox");
 

To get a reference to this JTextField by its name, we can specify:

 JTextField textBox = (JTextField) finder.findByName("myTextBox");
 

Please note that you need to cast the result of the lookup to the right type. To avoid casting, please use one of following:

  1. ComponentFinder.findByName(String, Class)
  2. ComponentFinder.findByName(String, Class, boolean)
  3. ComponentFinder.findByName(Container, String, Class)
  4. ComponentFinder.findByName(Container, String, Class, boolean)

Specified by:
findByName in interface ComponentFinder
Parameters:
name - the name of the component to find.
Returns:
the found component.
See Also:
Robot.settings(), Settings.componentLookupScope(), ComponentLookupScope

findByName

@RunsInEDT
public Component findByName(String name,
                                      boolean showing)
Finds a Component by name.

Specified by:
findByName in interface ComponentFinder
Parameters:
name - the name of the component to find.
showing - indicates whether the component to find should be visible (or showing) or not.
Returns:
the found component.
See Also:
ComponentFinder.findByName(String)

findByLabel

@RunsInEDT
public <T extends Component> T findByLabel(String label,
                                                     Class<T> type)
Finds a Component by the text of its associated JLabel and type. If this finder is attached to a Robot, it will use the component lookup scope in the Robot's Settings to determine whether the component to find should be showing or not. If this finder is not attached to any Robot, the component to find does not have to be showing.

Specified by:
findByLabel in interface ComponentFinder
Type Parameters:
T - the parameterized type of the component to find.
Parameters:
label - the text of the JLabel associated to the component to find.
type - the type of the component to find.
Returns:
the found component.
See Also:
ComponentFinder.findByLabel(String), JLabel.getLabelFor(), JLabel.setLabelFor(Component), Robot.settings(), Settings.componentLookupScope(), ComponentLookupScope

findByLabel

@RunsInEDT
public <T extends Component> T findByLabel(String label,
                                                     Class<T> type,
                                                     boolean showing)
Finds a Component by the text of its associated JLabel and type.

Specified by:
findByLabel in interface ComponentFinder
Type Parameters:
T - the parameterized type of the component to find.
Parameters:
label - the text of the JLabel associated to the component to find.
type - the type of the component to find.
showing - indicates whether the component to find should be visible (or showing) or not.
Returns:
the found component.
See Also:
ComponentFinder.findByLabel(String), JLabel.getLabelFor(), JLabel.setLabelFor(Component)

findByLabel

@RunsInEDT
public Component findByLabel(String label)

Finds a Component by by the text of its associated JLabel. If this finder is attached to a Robot, it will use the component lookup scope in the Robot's Settings to determine whether the component to find should be showing or not. If this finder is not attached to any Robot, the component to find does not have to be showing.

Let's assume we have the JTextField with a JLabel with text "Name";

 JLabel label = new JLabel("Name");
 JTextField textbox = new JTextField();
 label.setLabelFor(textBox);
 

To get a reference to this JTextField by the text of its associated JLabel, we can specify:

 JTextField textBox = (JTextField) finder.findByLabel("Name");
 

Please note that you need to cast the result of the lookup to the right type. To avoid casting, please use one of following:

  1. ComponentFinder.findByLabel(String, Class)
  2. ComponentFinder.findByLabel(String, Class, boolean)
  3. ComponentFinder.findByLabel(Container, String, Class)
  4. ComponentFinder.findByLabel(Container, String, Class, boolean)

Specified by:
findByLabel in interface ComponentFinder
Parameters:
label - the text of the JLabel associated to the component to find.
Returns:
the found component.
See Also:
JLabel.getLabelFor(), JLabel.setLabelFor(Component), Robot.settings(), Settings.componentLookupScope(), ComponentLookupScope

findByLabel

@RunsInEDT
public Component findByLabel(String label,
                                       boolean showing)
Finds a Component by by the text of its associated JLabel.

Specified by:
findByLabel in interface ComponentFinder
Parameters:
label - the text of the JLabel associated to the component to find.
showing - indicates whether the component to find should be visible (or showing) or not.
Returns:
the found component.
See Also:
ComponentFinder.findByLabel(String), JLabel.getLabelFor(), JLabel.setLabelFor(Component)

find

@RunsInEDT
public <T extends Component> T find(GenericTypeMatcher<T> m)
Finds a Component using the given GenericTypeMatcher. The given matcher will be evaluated in the event dispatch thread. Implementations of GenericTypeMatcher do not need to be concerned about the event dispatch thread.

Specified by:
find in interface ComponentFinder
Type Parameters:
T - the type of component the given matcher can handle.
Parameters:
m - the matcher to use to find the component of interest.
Returns:
the found component.

find

@RunsInEDT
public Component find(ComponentMatcher m)
Finds a Component using the given ComponentMatcher. The given matcher will be evaluated in the event dispatch thread. Implementations of ComponentMatcher do not need to be concerned about the event dispatch thread.

Specified by:
find in interface ComponentFinder
Parameters:
m - the matcher to use to find the component of interest.
Returns:
the found component.

findByName

@RunsInEDT
public <T extends Component> T findByName(Container root,
                                                    String name,
                                                    Class<T> type)
Finds a Component by name and type, in the hierarchy under the given root. If this finder is attached to a Robot, it will use the component lookup scope in the Robot's Settings to determine whether the component to find should be showing or not. If this finder is not attached to any Robot, the component to find does not have to be showing.

Specified by:
findByName in interface ComponentFinder
Type Parameters:
T - the parameterized type of the component to find.
Parameters:
root - the root used as the starting point of the search.
name - the name of the component to find.
type - the type of the component to find.
Returns:
the found component.
See Also:
Robot.settings(), Settings.componentLookupScope(), ComponentLookupScope, ComponentFinder.findByName(String)

findByName

@RunsInEDT
public <T extends Component> T findByName(Container root,
                                                    String name,
                                                    Class<T> type,
                                                    boolean showing)
Finds a Component by name and type, in the hierarchy under the given root.

Specified by:
findByName in interface ComponentFinder
Type Parameters:
T - the parameterized type of the component to find.
Parameters:
root - the root used as the starting point of the search.
name - the name of the component to find.
type - the type of the component to find.
showing - indicates whether the component to find should be visible (or showing) or not.
Returns:
the found component.
See Also:
ComponentFinder.findByName(String)

findByName

@RunsInEDT
public Component findByName(Container root,
                                      String name)
Finds a Component by name, in the hierarchy under the given root. If this finder is attached to a Robot, it will use the component lookup scope in the Robot's Settings to determine whether the component to find should be showing or not. If this finder is not attached to any Robot, the component to find does not have to be showing.

Specified by:
findByName in interface ComponentFinder
Parameters:
root - the root used as the starting point of the search.
name - the name of the component to find.
Returns:
the found component.
See Also:
Robot.settings(), Settings.componentLookupScope(), ComponentLookupScope, ComponentFinder.findByName(String)

findByName

@RunsInEDT
public Component findByName(Container root,
                                      String name,
                                      boolean showing)
Finds a Component by name, in the hierarchy under the given root.

Specified by:
findByName in interface ComponentFinder
Parameters:
root - the root used as the starting point of the search.
name - the name of the component to find.
showing - indicates whether the component to find should be visible (or showing) or not.
Returns:
the found component.
See Also:
ComponentFinder.findByName(String)

findByLabel

@RunsInEDT
public <T extends Component> T findByLabel(Container root,
                                                     String label,
                                                     Class<T> type)
Finds a Component by the text of its associated JLabel and type, in the hierarchy under the given root. If this finder is attached to a Robot, it will use the component lookup scope in the Robot's Settings to determine whether the component to find should be showing or not. If this finder is not attached to any Robot, the component to find does not have to be showing.

Specified by:
findByLabel in interface ComponentFinder
Type Parameters:
T - the parameterized type of the component to find.
Parameters:
root - the root used as the starting point of the search.
label - the text of the JLabel associated to the component to find.
type - the type of the component to find.
Returns:
the found component.
See Also:
ComponentFinder.findByLabel(String), JLabel.getLabelFor(), JLabel.setLabelFor(Component), Robot.settings(), Settings.componentLookupScope(), ComponentLookupScope

findByLabel

@RunsInEDT
public <T extends Component> T findByLabel(Container root,
                                                     String label,
                                                     Class<T> type,
                                                     boolean showing)
Finds a Component by the text of its associated JLabel and type, in the hierarchy under the given root.

Specified by:
findByLabel in interface ComponentFinder
Type Parameters:
T - the parameterized type of the component to find.
Parameters:
root - the root used as the starting point of the search.
label - the text of the JLabel associated to the component to find.
type - the type of the component to find.
showing - indicates whether the component to find should be visible (or showing) or not.
Returns:
the found component.
See Also:
ComponentFinder.findByLabel(String), JLabel.getLabelFor(), JLabel.setLabelFor(Component)

findByLabel

@RunsInEDT
public Component findByLabel(Container root,
                                       String label)
Finds a Component by the text of its associated JLabel, in the hierarchy under the given root. If this finder is attached to a Robot, it will use the component lookup scope in the Robot's Settings to determine whether the component to find should be showing or not. If this finder is not attached to any Robot, the component to find does not have to be showing.

Specified by:
findByLabel in interface ComponentFinder
Parameters:
root - the root used as the starting point of the search.
label - the text of the JLabel associated to the component to find.
Returns:
the found component.
See Also:
ComponentFinder.findByLabel(String), JLabel.getLabelFor(), JLabel.setLabelFor(Component), Robot.settings(), Settings.componentLookupScope(), ComponentLookupScope

findByLabel

@RunsInEDT
public Component findByLabel(Container root,
                                       String label,
                                       boolean showing)
Finds a Component by the text of its associated JLabel, in the hierarchy under the given root.

Specified by:
findByLabel in interface ComponentFinder
Parameters:
root - the root used as the starting point of the search.
label - the text of the JLabel associated to the component to find.
showing - indicates whether the component to find should be visible (or showing) or not.
Returns:
the found component.
See Also:
ComponentFinder.findByLabel(String), JLabel.getLabelFor(), JLabel.setLabelFor(Component)

find

@RunsInEDT
public <T extends Component> T find(Container root,
                                              GenericTypeMatcher<T> m)
Finds a Component using the given GenericTypeMatcher in the hierarchy under the given root. The given matcher will be evaluated in the event dispatch thread. Implementations of GenericTypeMatcher do not need to be concerned about the event dispatch thread.

Specified by:
find in interface ComponentFinder
Type Parameters:
T - the type of component the given matcher can handle.
Parameters:
root - the root used as the starting point of the search.
m - the matcher to use to find the component.
Returns:
the found component.

find

@RunsInEDT
public Component find(Container root,
                                ComponentMatcher m)
Finds a Component using the given ComponentMatcher in the hierarchy under the given root. The given matcher will be evaluated in the event dispatch thread. Implementations of ComponentMatcher do not need to be concerned about the event dispatch thread.

Specified by:
find in interface ComponentFinder
Parameters:
root - the root used as the starting point of the search.
m - the matcher to use to find the component.
Returns:
the found component.

includeHierarchyIfComponentNotFound

public boolean includeHierarchyIfComponentNotFound()
Returns whether the message in a ComponentLookupException should include the current component hierarchy. The default value is true.

Specified by:
includeHierarchyIfComponentNotFound in interface ComponentFinder
Returns:
true if the component hierarchy is included as part of the ComponentLookupException message, false otherwise.

includeHierarchyIfComponentNotFound

public void includeHierarchyIfComponentNotFound(boolean newValue)
Updates whether the message in a ComponentLookupException should include the current component hierarchy. The default value is true.

Specified by:
includeHierarchyIfComponentNotFound in interface ComponentFinder
Parameters:
newValue - the new value to set.

findAll

public Collection<Component> findAll(ComponentMatcher m)
Returns all the Components that match the search criteria specified in the given ComponentMatcher.

Specified by:
findAll in interface ComponentFinder
Parameters:
m - the matcher to use to find the component.
Returns:
all the Components that match the search criteria specified in the given ComponentMatcher; or an empty collection, if there are no matching components.

findAll

public Collection<Component> findAll(Container root,
                                     ComponentMatcher m)
Returns all the Components under the given root that match the search criteria specified in the given ComponentMatcher.

Specified by:
findAll in interface ComponentFinder
Parameters:
root - the root used as the starting point of the search.
m - the matcher to use to find the component.
Returns:
all the Components under the given root that match the search criteria specified in the given ComponentMatcher; or an empty collection, if there are no matching components.

findAll

public <T extends Component> Collection<T> findAll(GenericTypeMatcher<T> m)
Returns all the Components that match the search criteria specified in the given GenericTypeMatcher.

Specified by:
findAll in interface ComponentFinder
Type Parameters:
T - the generic type of component that this search supports.
Parameters:
m - the matcher to use to find the component.
Returns:
all the Components that match the search criteria specified in the given GenericTypeMatcher; or an empty collection, if there are no matching components.

findAll

public <T extends Component> Collection<T> findAll(Container root,
                                                   GenericTypeMatcher<T> m)
Returns all the Components under the given root that match the search criteria specified in the given GenericTypeMatcher.

Specified by:
findAll in interface ComponentFinder
Type Parameters:
T - the generic type of component that this search supports.
Parameters:
root - the root used as the starting point of the search.
m - the matcher to use to find the component.
Returns:
all the Components under the given root that match the search criteria specified in the given GenericTypeMatcher; or an empty collection, if there are no matching components.

requireShowingFromSettingsOr

protected final boolean requireShowingFromSettingsOr(boolean defaultValue)
Returns the value of the flag "requireShowing" in the ComponentLookupScope this finder's Settings. If the settings object is null, this method will return the provided default value.

Parameters:
defaultValue - the value to return if this matcher does not have any configuration settings.
Returns:
the value of the flag "requireShowing" in this finder's settings, or the provided default value if this finder does not have configuration settings.


Copyright © 2007-2011 FEST (Fixtures for Easy Software Testing). All Rights Reserved.