public abstract class GUIFilter extends Object
Component
when searching. Note that the methods and fields
in this class are designed specifically to support a natural, readable,
boolean expression "mini-language" for use in describing a single
Component
(or group of Component
s) by its (or their)
properties. As a result, it does violate some conventions regarding
the use of public fields (although note that all here are immutable)
and occasionally even the naming conventions for constants (e.g.,
where
). However, breaking these conventions is necessary
in this class in order to support the more natural syntax for filter
expressions, and so was deemed a better design choice.
Client classes that wish to use these filters should add the following static import directive:
import static student.testingsupport.GUIFilter.ClientImports.*;
Note that the GUITestCase
class already re-exports the
items defined in the GUIFilter.ClientImports
nested class, so GUI test
cases should not include the static import.
The expressions that you can create with this class are designed to represent "filters" or boolean predicates that can be applied to a Component, returning true if the component "matches" the filter or false if the component does not match.
Often, a filter object is created solely for the purpose of passing
the filter into some other operation, such as a search operation. For
example, the student.GUITestCase class provides a
getComponent()
method that takes a filter as a parameter. For the examples below, we
will use getComponent()
as the context, specifying each
filter as an argument value in a call to that method.
The basic principles for using this class are as follows:
Never try to create a GUIFilter object directly. Instead, always
write something that looks like a boolean expression, and that
starts with the operator where
:
JButton button = getComponent(JButton.class, where.nameIs("okButton"));
The basic properties you can check with filters include:
nameIs()
, textIs()
,
enabledIs()
, hasFocusIs()
,
hasFocusIs()
, and typeIs()
. They are
all used the same way:
JButton done = getComponent(JButton.class, where.textIs("Done")); JLabel name = getComponent(JLabel.class, where.textIs("Name:"));
You can combine filters using logical "and" as necessary:
JButton done = getComponent(JButton.class, where.nameIs("done").and.enabledIs(true).and.hasFocusIs(true));
You can also use "or":
JButton done = getComponent(JButton.class, where.nameIs("done").or.enabledIs(true).or.hasFocusIs(true));
Operators like "and" and "or" are interpreted strictly left to right. There is no precedence, because of the way Java interprets dot notation.
JButton done = getComponent(JButton.class, where.nameIs("done").or.enabledIs(true).and.hasFocusIs(true)); // means ((name = "done" or enabled = true) and focus = true) // note that the left operator is always evaluated first!
If you want to force a different order of evaluation
than strictly left-to-right, then use parentheses by writing the
appropriate operator as and()
or or()
. Just
be sure to start the new expression inside the parentheses with
where
:
JButton done = getComponent(JButton.class, where.nameIs("done").or(where.enabledIs(true).and.hasFocusIs(true))); // now means (name = "done" or (enabled = true and focus = true)) // because of the extra parentheses used
Finally, you can even use "not" (logical negation), but it is
called like a method, so parentheses (and thus a leading where)
are always required to make the intended extent of the negation
clear:
JButton done = getComponent(JButton.class, where.nameIs("done").and.not(where.enabledIs(true).or.hasFocusIs(true)));
Modifier and Type | Class and Description |
---|---|
class |
GUIFilter.BinaryOperator
A non-static subclass for binary operators that implicitly
captures the outer filter to which it belongs, using it as
the first/left argument to the operator.
|
static class |
GUIFilter.ClientImports
This class represents the "where" operator that is used to begin
a filter expression.
|
static class |
GUIFilter.Operator
This base class represents an operator used to create a query.
|
Modifier and Type | Field and Description |
---|---|
GUIFilter.BinaryOperator |
and
The "and" operator for combining filters, designed to be used in
expressions like
where.nameIs("...").and.enabledIs(true) . |
GUIFilter.BinaryOperator |
or
The "or" operator for combining filters, designed to be used in
expressions like
where.nameIs("abc").or.nameIs("def") . |
Modifier | Constructor and Description |
---|---|
protected |
GUIFilter(String description)
Creates a new filter object.
|
Modifier and Type | Method and Description |
---|---|
GUIFilter |
and(GUIFilter otherFilter)
The "and" operator for combining filters, when you want to use
parentheses to group its righthand argument.
|
GUIFilter |
or(GUIFilter otherFilter)
The "or" operator for combining filters, when you want to use
parentheses to group its righthand argument.
|
abstract boolean |
test(Component component)
Evaluate whether a component matches this filter.
|
String |
toString()
Get a string representation of this filter.
|
public final GUIFilter.BinaryOperator and
where.nameIs("...").and.enabledIs(true)
.
This operator is implemented as a public field so that the simple
.and.
notation can be used as a connective between
filters. If you want to use parentheses for grouping to define
the right argument, see and(GUIFilter)
instead.public final GUIFilter.BinaryOperator or
where.nameIs("abc").or.nameIs("def")
.
This operator is implemented as a public field so that the simple
.or.
notation can be used as a connective between
filters. If you want to use parentheses for grouping to define
the right argument, see or(GUIFilter)
instead.protected GUIFilter(String description)
description
- A string description of this filter, used in
toString()
.public String toString()
public final GUIFilter and(GUIFilter otherFilter)
where.nameIs("abc").and(enabledIs(true).or.hasFocusIs(true))
.
If you wish to use the .and.
notation instead, leaving
off the parentheses, see and(GUIFilter)
.otherFilter
- The second argument to "and".public final GUIFilter or(GUIFilter otherFilter)
where.nameIs("abc").or(enabledIs(true).and.hasFocusIs(true))
.
If you wish to use the .or.
notation instead, leaving
off the parentheses, see or(GUIFilter)
.otherFilter
- The second argument to "or".public abstract boolean test(Component component)
component
- The component to check