|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
FileDialogRecorder.java | 81.8% | 93.3% | 100% | 90.4% |
|
1 |
package abbot.editor.recorder;
|
|
2 |
|
|
3 |
import java.awt.*;
|
|
4 |
import java.awt.event.WindowEvent;
|
|
5 |
|
|
6 |
import abbot.script.*;
|
|
7 |
|
|
8 |
/**
|
|
9 |
* Recorder for the java.awt.FileDialog. Since this is a native component and
|
|
10 |
* no java events are generated other than window open/close, the only things
|
|
11 |
* to take note of are the following:<br>
|
|
12 |
* <ul>
|
|
13 |
* <li>Changes to the directory
|
|
14 |
* <li>Changes to the file
|
|
15 |
* <li>Whether the user hits OK or Cancel
|
|
16 |
* </ul>
|
|
17 |
* @author Vrata Venet, European Space Agency, Madrid-Spain (av@iso.vilspa.esa.es)
|
|
18 |
*/
|
|
19 |
public class FileDialogRecorder extends DialogRecorder{ |
|
20 |
|
|
21 |
private FileDialog dialog = null; |
|
22 |
private String originalFile = null; |
|
23 |
private String originalDir = null; |
|
24 |
|
|
25 |
/** Create a FileDialogRecorder for use in capturing the semantics of a GUI
|
|
26 |
* action.
|
|
27 |
*/
|
|
28 | 6 |
public FileDialogRecorder(Resolver resolver) {
|
29 | 6 |
super(resolver);
|
30 |
} |
|
31 |
|
|
32 | 6 |
protected void init(int type) { |
33 | 6 |
super.init(type);
|
34 | 6 |
dialog = null;
|
35 | 6 |
originalFile = null;
|
36 | 6 |
originalDir = null;
|
37 |
} |
|
38 |
|
|
39 |
/** Override the default window parsing to consume everything between
|
|
40 |
dialog open and close.
|
|
41 |
*/
|
|
42 | 32 |
protected boolean parseWindowEvent(AWTEvent event) { |
43 | 32 |
boolean consumed = true; |
44 | 32 |
if (event.getSource() instanceof FileDialog) { |
45 | 15 |
if (isOpen(event)) {
|
46 | 6 |
dialog = (FileDialog)event.getSource(); |
47 | 6 |
originalFile = dialog.getFile(); |
48 | 6 |
originalDir = dialog.getDirectory(); |
49 |
} |
|
50 |
// The FileDialog robot uses some event listener hacks to set the
|
|
51 |
// correct state on dialog close; make sure we record after that
|
|
52 |
// listener is finished.
|
|
53 | 15 |
if (event instanceof FileDialogTerminator) |
54 | 3 |
setFinished(true);
|
55 | 12 |
else if (event.getSource() == dialog && isClose(event)) { |
56 | 3 |
AWTEvent terminator = |
57 |
new FileDialogTerminator(dialog, event.getID());
|
|
58 | 3 |
dialog.getToolkit().getSystemEventQueue(). |
59 |
postEvent(terminator); |
|
60 |
} |
|
61 |
} |
|
62 | 32 |
return consumed;
|
63 |
} |
|
64 |
|
|
65 |
/** Create one or more steps corresponding to what was done to the file
|
|
66 |
dialog. If the directory is non-null, the directory was changed. If
|
|
67 |
the file is non-null, the file was accepted.
|
|
68 |
*/
|
|
69 | 3 |
protected Step createFileDialogEvents(FileDialog d,
|
70 |
String oldDir, String oldFile) { |
|
71 | 3 |
ComponentReference ref = getResolver().addComponent(d); |
72 | 3 |
String file = d.getFile(); |
73 | 3 |
boolean accepted = file != null; |
74 | 3 |
boolean fileChanged = accepted && !file.equals(oldFile);
|
75 | 3 |
String dir = d.getDirectory(); |
76 | 3 |
boolean dirChanged = dir != oldDir
|
77 |
&& (dir == null || !dir.equals(oldDir));
|
|
78 |
|
|
79 | 3 |
String desc = d.getMode() == FileDialog.SAVE |
80 |
? "Save File" : "Load File"; |
|
81 | 3 |
if (accepted)
|
82 | 2 |
desc += " (" + file + ")"; |
83 |
else
|
|
84 | 1 |
desc += " (canceled)";
|
85 | 3 |
Sequence seq = new Sequence(getResolver(), desc);
|
86 | 3 |
if (dirChanged) {
|
87 | 1 |
seq.addStep(new Action(getResolver(),
|
88 |
null, "actionSetDirectory", |
|
89 |
new String[] { ref.getID(), dir },
|
|
90 |
FileDialog.class));
|
|
91 |
} |
|
92 | 3 |
if (accepted) {
|
93 | 2 |
Step accept = new Action(getResolver(),
|
94 |
null, "actionAccept", |
|
95 |
new String[] { ref.getID() },
|
|
96 |
FileDialog.class);
|
|
97 | 2 |
if (fileChanged) {
|
98 | 2 |
seq.addStep(new Action(getResolver(),
|
99 |
null, "actionSetFile", |
|
100 |
new String[] { ref.getID(), file },
|
|
101 |
FileDialog.class));
|
|
102 | 2 |
seq.addStep(accept); |
103 |
} |
|
104 |
else {
|
|
105 | 0 |
return accept;
|
106 |
} |
|
107 |
} |
|
108 |
else {
|
|
109 | 1 |
Step cancel = new Action(getResolver(),
|
110 |
null, "actionCancel", |
|
111 |
new String[] { ref.getID() },
|
|
112 |
FileDialog.class);
|
|
113 | 1 |
if (dirChanged)
|
114 | 0 |
seq.addStep(cancel); |
115 |
else
|
|
116 | 1 |
return cancel;
|
117 |
} |
|
118 | 2 |
return seq;
|
119 |
} |
|
120 |
|
|
121 | 3 |
protected Step createStep() {
|
122 | 3 |
if (getRecordingType() == SE_WINDOW) {
|
123 | 3 |
return createFileDialogEvents(dialog, originalDir, originalFile);
|
124 |
} |
|
125 |
else {
|
|
126 | 0 |
return super.createStep(); |
127 |
} |
|
128 |
} |
|
129 |
|
|
130 |
private class FileDialogTerminator extends WindowEvent { |
|
131 | 3 |
public FileDialogTerminator(FileDialog fd, int id) { |
132 | 3 |
super(fd, id);
|
133 |
} |
|
134 |
} |
|
135 |
} |
|
136 |
|
|