001: package abbot.finder;
002:
003: import java.awt.Container;
004: import java.awt.Component;
005: import java.awt.Window;
006: import java.util.*;
007: import javax.swing.SwingUtilities;
008:
009: import abbot.i18n.Strings;
010:
011: /** Provides basic component lookup, examining each component in turn.
012: Searches all components of interest in a given hierarchy.
013: */
014:
015: public class BasicFinder implements ComponentFinder {
016: private Hierarchy hierarchy;
017:
018: private static final ComponentFinder DEFAULT = new BasicFinder(
019: new AWTHierarchy());
020:
021: public static ComponentFinder getDefault() {
022: return DEFAULT;
023: }
024:
025: private class SingleComponentHierarchy implements Hierarchy {
026: private Component root;
027: private ArrayList list = new ArrayList();
028:
029: public SingleComponentHierarchy(Container root) {
030: this .root = root;
031: list.add(root);
032: }
033:
034: public Collection getRoots() {
035: return list;
036: }
037:
038: public Collection getComponents(Component c) {
039: return getHierarchy().getComponents(c);
040: }
041:
042: public Container getParent(Component c) {
043: return getHierarchy().getParent(c);
044: }
045:
046: public boolean contains(Component c) {
047: return getHierarchy().contains(c)
048: && SwingUtilities.isDescendingFrom(c, root);
049: }
050:
051: public void dispose(Window w) {
052: getHierarchy().dispose(w);
053: }
054: }
055:
056: public BasicFinder() {
057: this (AWTHierarchy.getDefault());
058: }
059:
060: public BasicFinder(Hierarchy h) {
061: hierarchy = h;
062: }
063:
064: protected Hierarchy getHierarchy() {
065: return hierarchy;
066: }
067:
068: /** Find a Component, using the given Matcher to determine whether a given
069: component in the hierarchy under the given root is the desired
070: one.
071: */
072: public Component find(Container root, Matcher m)
073: throws ComponentNotFoundException,
074: MultipleComponentsFoundException {
075: Hierarchy h = root != null ? new SingleComponentHierarchy(root)
076: : getHierarchy();
077: return find(h, m);
078: }
079:
080: /** Find a Component, using the given Matcher to determine whether a given
081: component in the hierarchy used by this ComponentFinder is the desired
082: one.
083: */
084: public Component find(Matcher m) throws ComponentNotFoundException,
085: MultipleComponentsFoundException {
086: return find(getHierarchy(), m);
087: }
088:
089: protected Component find(Hierarchy h, Matcher m)
090: throws ComponentNotFoundException,
091: MultipleComponentsFoundException {
092: Set found = new HashSet();
093: Iterator iter = h.getRoots().iterator();
094: while (iter.hasNext()) {
095: findMatches(h, m, (Component) iter.next(), found);
096: }
097: if (found.size() == 0) {
098: String msg = Strings.get("finder.not_found",
099: new Object[] { m.toString() });
100: throw new ComponentNotFoundException(msg);
101: } else if (found.size() > 1) {
102: Component[] list = (Component[]) found
103: .toArray(new Component[found.size()]);
104: if (!(m instanceof MultiMatcher)) {
105: String msg = Strings.get("finder.multiple_found",
106: new Object[] { m.toString() });
107: throw new MultipleComponentsFoundException(msg, list);
108: }
109: return ((MultiMatcher) m).bestMatch(list);
110: }
111: return (Component) found.iterator().next();
112: }
113:
114: protected void findMatches(Hierarchy h, Matcher m, Component c,
115: Set found) {
116: if (found.size() == 1 && !(m instanceof MultiMatcher))
117: return;
118:
119: Iterator iter = h.getComponents(c).iterator();
120: while (iter.hasNext()) {
121: findMatches(h, m, (Component) iter.next(), found);
122: }
123: if (m.matches(c)) {
124: found.add(c);
125: }
126: }
127: }
|