01: package abbot.finder.matchers;
02:
03: import java.awt.Component;
04:
05: import javax.swing.JMenuItem;
06: import javax.swing.JPopupMenu;
07:
08: import abbot.finder.Matcher;
09: import abbot.util.ExtendedComparator;
10:
11: import java.util.ArrayList;
12: import java.util.List;
13:
14: /**
15: * Matches a {@link JMenuItem} given a simple label or a menu path of the
16: * format "menu|submenu|menuitem", for example "File|Open|Can of worms".
17: *
18: * @author twall
19: * @version $Id: JMenuItemMatcher.java 2235 2007-03-08 09:37:51Z gdavison $
20: */
21: public class JMenuItemMatcher implements Matcher {
22: private String label;
23:
24: public JMenuItemMatcher(String label) {
25: this .label = label;
26: }
27:
28: public static String getPath(JMenuItem item) {
29: Component parent = item.getParent();
30: if (parent instanceof JPopupMenu) {
31: parent = ((JPopupMenu) parent).getInvoker();
32: }
33: if (parent instanceof JMenuItem) {
34: return getPath((JMenuItem) parent) + "|" + item.getText();
35: }
36: return item.getText();
37: }
38:
39: /**
40: * @param path A path of the form File|Open|Can of worms
41: * @return A list of strings, File, File|Open, File|Open|Can of worms
42: */
43:
44: public static List splitMenuPath(String path) {
45: // Split the path
46: //
47:
48: int lastFoundIndex = -1;
49: java.util.List selectionPath = new ArrayList();
50: while ((lastFoundIndex = path.indexOf('|', lastFoundIndex)) != -1) {
51: selectionPath.add(path.substring(0, lastFoundIndex));
52: lastFoundIndex = lastFoundIndex + 1;
53: }
54:
55: selectionPath.add(path);
56: return selectionPath;
57: }
58:
59: public boolean matches(Component c) {
60: if (c instanceof JMenuItem) {
61: JMenuItem mi = (JMenuItem) c;
62: String text = mi.getText();
63: return ExtendedComparator.stringsMatch(label, text)
64: || ExtendedComparator.stringsMatch(label,
65: getPath(mi));
66: }
67: return false;
68: }
69:
70: }
|