001: package org.uispec4j.finder;
002:
003: import org.uispec4j.utils.ComponentUtils;
004:
005: import java.awt.*;
006:
007: /**
008: * Standard searching policies, implemented as {@link ComponentMatcher} objects.
009: */
010: public class ComponentMatchers {
011:
012: /**
013: * Matches components whose displayed name is exactly the same as the reference.
014: */
015: public static ComponentMatcher displayedNameIdentity(
016: String reference) {
017: return new DisplayedNameComponentMatcher(StringMatcher
018: .identity(reference));
019: }
020:
021: /**
022: * Matches components whose displayed name is a substring of the reference.
023: */
024: public static ComponentMatcher displayedNameSubstring(
025: String reference) {
026: return new DisplayedNameComponentMatcher(StringMatcher
027: .substring(reference));
028: }
029:
030: /**
031: * Matches components whose displayed name matches with the regexp reference.
032: */
033: public static ComponentMatcher displayedNameRegexp(String reference) {
034: return new DisplayedNameComponentMatcher(StringMatcher
035: .regexp(reference));
036: }
037:
038: /**
039: * Matches components whose inner name is exactly the same as the reference.
040: */
041: public static ComponentMatcher innerNameIdentity(String reference) {
042: return new InnerNameComponentMatcher(StringMatcher
043: .identity(reference));
044: }
045:
046: /**
047: * Matches components whose inner name is a substring of the reference.
048: */
049: public static ComponentMatcher innerNameSubstring(String reference) {
050: return new InnerNameComponentMatcher(StringMatcher
051: .substring(reference));
052: }
053:
054: /**
055: * Matches components whose inner name matches with the regexp reference.
056: */
057: public static ComponentMatcher innerNameRegexp(String reference) {
058: return new InnerNameComponentMatcher(StringMatcher
059: .regexp(reference));
060: }
061:
062: /**
063: * Matches components that are instances of the class.
064: */
065: public static ComponentMatcher fromClass(final Class swingClass) {
066: return new ComponentMatcher() {
067: public boolean matches(Component component) {
068: return swingClass.isInstance(component);
069: }
070: };
071: }
072:
073: /**
074: * Matches components that match all its sub-matchers.
075: */
076: public static ComponentMatcher intersection(
077: final ComponentMatcher[] matchers) {
078: return new ComponentMatcher() {
079: public boolean matches(Component component) {
080: for (int i = 0; i < matchers.length; i++) {
081: ComponentMatcher matcher = matchers[i];
082: if (!matcher.matches(component)) {
083: return false;
084: }
085: }
086: return true;
087: }
088: };
089: }
090:
091: /**
092: * Matches components that match at least one of its sub-matchers.
093: */
094: public static ComponentMatcher union(
095: final ComponentMatcher[] matchers) {
096: return new ComponentMatcher() {
097: public boolean matches(Component component) {
098: for (int i = 0; i < matchers.length; i++) {
099: ComponentMatcher matcher = matchers[i];
100: if (matcher.matches(component)) {
101: return true;
102: }
103: }
104: return false;
105: }
106: };
107: }
108:
109: /**
110: * Matches components rejected by the inner matcher.
111: */
112: public static ComponentMatcher not(final ComponentMatcher matcher) {
113: return new ComponentMatcher() {
114: public boolean matches(Component component) {
115: return !matcher.matches(component);
116: }
117: };
118: }
119:
120: private static class DisplayedNameComponentMatcher implements
121: ComponentMatcher {
122: private final StringMatcher stringMatcher;
123:
124: public DisplayedNameComponentMatcher(StringMatcher stringMatcher) {
125: this .stringMatcher = stringMatcher;
126: }
127:
128: public boolean matches(Component component) {
129: if (!ComponentUtils.hasDisplayedName(component.getClass())) {
130: return false;
131: }
132: String displayedName = ComponentUtils
133: .getDisplayedName(component);
134: return stringMatcher.matches(displayedName);
135: }
136: }
137:
138: private static class InnerNameComponentMatcher implements
139: ComponentMatcher {
140: private final StringMatcher stringMatcher;
141:
142: public InnerNameComponentMatcher(StringMatcher stringMatcher) {
143: this .stringMatcher = stringMatcher;
144: }
145:
146: public boolean matches(Component component) {
147: return stringMatcher.matches(component.getName());
148: }
149: }
150: }
|