|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
Recorder.java | 50% | 65% | 72.7% | 65.7% |
|
1 |
package abbot.editor.recorder;
|
|
2 |
|
|
3 |
import java.awt.*;
|
|
4 |
import java.awt.event.*;
|
|
5 |
|
|
6 |
import abbot.*;
|
|
7 |
import abbot.i18n.Strings;
|
|
8 |
import abbot.script.*;
|
|
9 |
import abbot.script.Resolver;
|
|
10 |
import abbot.tester.Robot;
|
|
11 |
|
|
12 |
/**
|
|
13 |
* The <code>Recorder</code> provides a mechanism for recording an event stream and
|
|
14 |
* generating a sequence of script steps from that stream.
|
|
15 |
* <p>
|
|
16 |
* NOTE: when writing a recorder, be very careful not to test for
|
|
17 |
* platform-specific behavior, and avoid being susceptible to
|
|
18 |
* platform-specific bugs. Please make sure the recorder works on both
|
|
19 |
* pointer-focus and click-to-focus window managers, as well as on at least
|
|
20 |
* two platforms.<p>
|
|
21 |
*/
|
|
22 |
public abstract class Recorder { |
|
23 |
private ActionListener al;
|
|
24 |
private Resolver resolver;
|
|
25 |
private long lastEventTime = 0; |
|
26 |
|
|
27 |
/** Create a Recorder for use in converting events into script steps. */
|
|
28 | 92 |
public Recorder(Resolver resolver) {
|
29 | 92 |
this.resolver = resolver;
|
30 |
} |
|
31 |
|
|
32 |
/** The recorder supports zero or one listeners. */
|
|
33 | 21 |
public void addActionListener(ActionListener al) { |
34 | 21 |
this.al = al;
|
35 |
} |
|
36 |
|
|
37 | 305 |
protected ActionListener getListener() { return al; } |
38 |
|
|
39 |
/** Start recording a new event stream. */
|
|
40 | 0 |
public void start() { |
41 | 0 |
lastEventTime = System.currentTimeMillis(); |
42 |
} |
|
43 |
|
|
44 |
/** Indicate the end of the current event input stream. */
|
|
45 |
public abstract void terminate() throws RecordingFailedException; |
|
46 |
|
|
47 | 141 |
public long getLastEventTime() { |
48 | 141 |
return lastEventTime;
|
49 |
} |
|
50 |
|
|
51 |
/** Create a step or sequence of steps based on the event stream so far. */
|
|
52 |
protected abstract Step createStep();
|
|
53 |
|
|
54 |
/** Return a step or sequence of steps representing the steps created thus
|
|
55 |
far, or null if none.
|
|
56 |
*/
|
|
57 | 17 |
public Step getStep() {
|
58 | 17 |
return createStep();
|
59 |
} |
|
60 |
|
|
61 |
/** Insert an arbitrary step into the recording stream. */
|
|
62 | 0 |
public void insertStep(Step step) { |
63 |
// Default does nothing
|
|
64 |
} |
|
65 |
|
|
66 |
/** Process the given event.
|
|
67 |
@throws RecordingFailedException if an error was encountered and
|
|
68 |
recording should discontinue.
|
|
69 |
*/
|
|
70 | 1913 |
public void record(java.awt.AWTEvent event) |
71 |
throws RecordingFailedException {
|
|
72 | 1913 |
if (Log.isClassDebugEnabled(getClass()))
|
73 | 0 |
Log.debug("REC: " + Robot.toString(event));
|
74 | 1913 |
lastEventTime = System.currentTimeMillis(); |
75 | 1913 |
try {
|
76 | 1913 |
recordEvent(event); |
77 |
} |
|
78 |
catch(RecordingFailedException e) {
|
|
79 | 0 |
throw e;
|
80 |
} |
|
81 |
catch(Throwable thrown) {
|
|
82 | 0 |
Log.log("REC: Unexpected failure: " + thrown);
|
83 | 0 |
String msg = Strings.get("editor.recording.exception");
|
84 | 0 |
throw new RecordingFailedException(new BugReport(msg, thrown)); |
85 |
} |
|
86 |
} |
|
87 |
|
|
88 |
/** Implement this to actually handle the event.
|
|
89 |
@throws RecordingFailedException if an error was encountered and
|
|
90 |
recording should be discontinued.
|
|
91 |
*/
|
|
92 |
protected abstract void recordEvent(AWTEvent event) |
|
93 |
throws RecordingFailedException;
|
|
94 |
|
|
95 |
/** Return the events of interest to this Recorder. */
|
|
96 | 0 |
public long getEventMask() { |
97 | 0 |
return -1;
|
98 |
} |
|
99 |
|
|
100 |
/** @return the {@link Resolver} to be used by this <code>Recorder</code>. */
|
|
101 | 431 |
protected Resolver getResolver() { return resolver; } |
102 |
|
|
103 |
/** Indicate the current recording state, so that the status may be
|
|
104 |
* displayed elsewhere.
|
|
105 |
*/
|
|
106 | 292 |
protected void setStatus(String msg) { |
107 | 292 |
if (al != null) { |
108 | 292 |
ActionEvent event = |
109 |
new ActionEvent(this, ActionEvent.ACTION_PERFORMED, msg); |
|
110 | 292 |
al.actionPerformed(event); |
111 |
} |
|
112 |
} |
|
113 |
} |
|
114 |
|
|