001: package abbot.tester;
002:
003: import java.awt.*;
004: import java.io.File;
005: import javax.swing.*;
006:
007: import abbot.finder.*;
008: import abbot.finder.matchers.*;
009: import abbot.i18n.Strings;
010:
011: /**
012: * Tester for the {@link JFileChooser}.
013: * Note: only a small subset of JFileChooser functionality is exposed here.
014: * @author twall@users.sf.net
015: */
016: public class JFileChooserTester extends JComponentTester {
017:
018: private JTextComponentTester tester = new JTextComponentTester();
019: private ComponentFinder finder = BasicFinder.getDefault();
020:
021: private Component find(Container chooser, Matcher m) {
022: try {
023: return finder.find(chooser, m);
024: } catch (ComponentSearchException e) {
025: return null;
026: }
027: }
028:
029: private JButton findButton(Container chooser, final String text) {
030: JButton button = (JButton) find(chooser, new ClassMatcher(
031: JButton.class) {
032: public boolean matches(Component c) {
033: return super .matches(c)
034: && text.equals(((JButton) c).getText());
035: }
036: });
037: return button;
038: }
039:
040: /** Sets the selected file. */
041: public void actionSetSelectedFile(Component c, final File file) {
042: final JFileChooser chooser = (JFileChooser) c;
043: int mode = chooser.getFileSelectionMode();
044: if (mode == JFileChooser.FILES_ONLY && file.isDirectory()) {
045: String msg = Strings.get("tester.JFileChooser.files_only");
046: throw new ActionFailedException(msg);
047: }
048: if (mode == JFileChooser.DIRECTORIES_ONLY
049: && !file.isDirectory()) {
050: String msg = Strings.get("tester.JFileChooser.dirs_only");
051: throw new ActionFailedException(msg);
052: }
053: invokeAndWait(new Runnable() {
054: public void run() {
055: chooser.setSelectedFile(file);
056: }
057: });
058: }
059:
060: /** Sets the text in the file name/path text field. Note that the
061: * {@link JFileChooser} may behave differently depending on what
062: * you actually feed into that field.
063: */
064: public void actionSetFilename(Component c, String filename) {
065: JTextField tf = (JTextField) find((JFileChooser) c,
066: new ClassMatcher(JTextField.class));
067: if (tf == null) {
068: String msg = Strings
069: .get("tester.JFileChooser.filename_not_found");
070: throw new ActionFailedException(msg);
071: }
072: tester.actionEnterText(tf, filename);
073: }
074:
075: /** Sets the current directory from which a file or directory may
076: * be selected. This is not the same thing as setting the
077: * <em>selected</em> directory when in directory selection mode.
078: */
079: public void actionSetDirectory(Component c, final String path) {
080: final JFileChooser chooser = (JFileChooser) c;
081: invokeAndWait(new Runnable() {
082: public void run() {
083: chooser.setCurrentDirectory(new File(path));
084: }
085: });
086: waitForIdle();
087: }
088:
089: /** Press the approve button. Fails if the button is disabled.
090: */
091: public void actionApprove(Component c) {
092: // Could invoke chooser.approveSelection, but that doesn't actually
093: // fire the approve button.
094: JFileChooser chooser = (JFileChooser) c;
095: String text = chooser.getApproveButtonText();
096: if (text == null) {
097: text = chooser.getUI().getApproveButtonText(chooser);
098: }
099: JButton approve = findButton(chooser, text);
100: if (approve == null) {
101: String msg = Strings
102: .get("tester.JFileChooser.approve_not_found");
103: throw new ActionFailedException(msg);
104: }
105: if (!approve.isEnabled()) {
106: String msg = Strings
107: .get("tester.JFileChooser.approve_not_enabled");
108: throw new ActionFailedException(msg);
109: }
110: actionClick(approve);
111: }
112:
113: /** Press the cancel button. Fails if the button is disabled. */
114: public void actionCancel(Component c) {
115: // We could invoke chooser.cancelSelection, but that wouldn't actually
116: // fire the cancel button...
117: JFileChooser chooser = (JFileChooser) c;
118: JButton cancel = findButton(chooser, UIManager
119: .getString("FileChooser.cancelButtonText"));
120: if (cancel == null) {
121: String msg = Strings
122: .get("tester.JFileChooser.cancel_not_found");
123: throw new ActionFailedException(msg);
124: }
125: if (!cancel.isEnabled()) {
126: String msg = Strings
127: .get("tester.JFileChooser.cancel_not_enabled");
128: throw new ActionFailedException(msg);
129: }
130: actionClick(cancel);
131: }
132: }
|