01: package abbot.editor.recorder;
02:
03: import java.awt.*;
04: import java.awt.event.MouseEvent;
05:
06: import javax.swing.JTabbedPane;
07: import javax.swing.JMenuItem;
08: import javax.swing.plaf.TabbedPaneUI;
09:
10: import abbot.Platform;
11: import abbot.tester.ComponentLocation;
12: import abbot.tester.JTabbedPaneLocation;
13: import abbot.script.*;
14:
15: /**
16: * Record basic semantic events you might find on an JTabbedPane. <p>
17: * <ul>
18: * <li>Select a tab.
19: * </ul>
20: */
21: public class JTabbedPaneRecorder extends JComponentRecorder {
22:
23: private JTabbedPane tabbedPane;
24:
25: public JTabbedPaneRecorder(Resolver resolver) {
26: super (resolver);
27: }
28:
29: public boolean accept(AWTEvent event) {
30: if (isClick(event)) {
31: MouseEvent me = (MouseEvent) event;
32: JTabbedPane tp = tabbedPane = (JTabbedPane) me
33: .getComponent();
34: TabbedPaneUI ui = tp.getUI();
35: int index = ui.tabForCoordinate(tp, me.getX(), me.getY());
36: if (index != -1) {
37: setStatus("Selecting tab '" + tp.getTitleAt(index));
38: init(SE_CLICK);
39: return true;
40: }
41: if (Platform.isOSX()) {
42: int xmin = tp.getWidth();
43: int xmax = 0;
44: int ymin = tp.getHeight();
45: int ymax = 0;
46: for (int i = 0; i < tp.getTabCount(); i++) {
47: Rectangle rect = ui.getTabBounds(tp, i);
48: if (rect.x >= 0) {
49: xmin = Math.min(rect.x, xmin);
50: xmax = Math.max(rect.x + rect.width, xmax);
51: ymin = Math.min(rect.y, ymin);
52: ymax = Math.max(rect.y + rect.height, ymax);
53: }
54: }
55: if (me.getX() > xmax
56: && me.getX() < tp.getWidth() - xmin
57: && me.getY() >= ymin && me.getY() <= ymax) {
58: // Expect a popup menu, then let popup menu recording take
59: // over.
60: init(SE_MENU);
61: return true;
62: }
63: }
64: }
65: return super .accept(event);
66: }
67:
68: /** Special case for OSX tab selection from popup menu. */
69: protected Step createMenuSelection(Component menuItem) {
70: ComponentReference ref = getResolver().addComponent(tabbedPane);
71: ComponentLocation loc = new JTabbedPaneLocation(
72: ((JMenuItem) menuItem).getText());
73: Step step = new Action(getResolver(), null, "actionSelectTab",
74: new String[] { ref.getID(), loc.toString(), });
75: return step;
76: }
77:
78: /** Parse clicks, notably those that select a tab. */
79: protected Step createClick(Component target, int x, int y,
80: int mods, int count) {
81: ComponentReference cr = getResolver().addComponent(target);
82: JTabbedPane tp = (JTabbedPane) target;
83: TabbedPaneUI ui = tp.getUI();
84: int index = ui.tabForCoordinate(tp, x, y);
85: if (index != -1) {
86: // NOTE only tab selections are allowed for when clicking on tabs;
87: // no multi-clicks or other buttons are saved, although nothing
88: // prevents manual generation of such actions.
89: return new Action(getResolver(), null, "actionSelectTab",
90: new String[] { cr.getID(),
91: getLocationArgument(tp, x, y), },
92: javax.swing.JTabbedPane.class);
93: }
94: return super.createClick(target, x, y, mods, count);
95: }
96: }
|