001: package abbot.editor.recorder;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005:
006: import abbot.Log;
007: import abbot.script.*;
008:
009: /**
010: * Record basic semantic events you might find on an Choice component. <p>
011: */
012: public class ChoiceRecorder extends ComponentRecorder {
013:
014: private Choice choice = null;
015: /** If selection is null when finished, no step will be generated. */
016: private String selection = null;
017: private ItemListener listener = null;
018:
019: public ChoiceRecorder(Resolver resolver) {
020: super (resolver);
021: }
022:
023: protected void init(int recordingType) {
024: super .init(recordingType);
025: choice = null;
026: selection = null;
027: listener = null;
028: }
029:
030: /** Also accept ItemEvents, since the ChoiceTester will not generate any
031: explicit clicks to control the component. */
032: protected boolean isClick(AWTEvent e) {
033: if (e instanceof ItemEvent) {
034: return true;
035: }
036: return super .isClick(e);
037: }
038:
039: /** Track click -> select ->click, cancelable by ESC or by clicking away
040: from the component.<p>
041: NOTE: press->drag->release produces an identical set of events<br>
042: OSX 1.3.1:<br>
043: MOUSE_PRESSED<br>
044: (ITEM_STATE_CHANGED)|MOUSE_RELEASED|KEY_RELEASED<br>
045: The ItemEvent never makes it to the AWT listener.
046: */
047: protected boolean parseClick(AWTEvent event) {
048: // Have to check here since we handle the ItemEvent artificially
049: if (isFinished()) {
050: Log.debug("already finished");
051: return false;
052: }
053:
054: if (choice == null) {
055: // Parse immediate selections (programmatic/tester driven)
056: if (event instanceof ItemEvent) {
057: choice = (Choice) event.getSource();
058: selection = ((ItemEvent) event).getItem().toString();
059: Log.debug("selection=" + selection);
060: setFinished(true);
061: } else {
062: choice = (Choice) event.getSource();
063: listener = new ItemListener() {
064: public void itemStateChanged(ItemEvent ev) {
065: Log.debug("item event");
066: if (ev.getStateChange() == ItemEvent.SELECTED) {
067: selection = ev.getItem().toString();
068: Log.debug("selection=" + selection);
069: choice.removeItemListener(this );
070: setFinished(true);
071: }
072: }
073: };
074: choice.addItemListener(listener);
075: setStatus("Waiting for selection");
076: }
077: } else if (event.getID() == KeyEvent.KEY_RELEASED
078: && (((KeyEvent) event).getKeyCode() == KeyEvent.VK_SPACE || ((KeyEvent) event)
079: .getKeyCode() == KeyEvent.VK_ENTER)) {
080: Log.debug("enter");
081: setFinished(true);
082: } else if (event.getID() == KeyEvent.KEY_RELEASED
083: && ((KeyEvent) event).getKeyCode() == KeyEvent.VK_ESCAPE) {
084: Log.debug("cancel");
085: selection = null;
086: setFinished(true);
087: } else {
088: Log.debug("Event ignored");
089: }
090:
091: if (isFinished() && choice != null) {
092: choice.removeItemListener(listener);
093: listener = null;
094: }
095:
096: // Events are always consumed by the Choice
097: return true;
098: }
099:
100: protected Step createStep() {
101: Step step = null;
102: if (getRecordingType() == SE_CLICK) {
103: if (selection != null)
104: step = createSelection(choice, selection);
105: } else {
106: step = super .createStep();
107: }
108: return step;
109: }
110:
111: protected Step createSelection(Choice target, String selection) {
112: ComponentReference cr = getResolver().addComponent(choice);
113: return new Action(getResolver(), null, "actionSelectItem",
114: new String[] { cr.getID(), selection },
115: java.awt.Choice.class);
116: }
117: }
|