001: package abbot.editor.editors;
002:
003: import java.awt.*;
004: import java.awt.event.ActionEvent;
005: import java.util.*;
006:
007: import javax.swing.*;
008:
009: import abbot.i18n.Strings;
010: import abbot.script.*;
011: import abbot.tester.ComponentTester;
012:
013: /** Provide convenient editing of an Assert step. */
014: public class AssertEditor extends PropertyCallEditor {
015:
016: private Assert step;
017: private JTextField value;
018: private JCheckBox invert;
019: private JCheckBox wait;
020: private JTextField timeout = null;
021: private JTextField interval = null;
022: private int optionsIndex;
023:
024: public AssertEditor(Assert step) {
025: super (step);
026: this .step = step;
027:
028: value = addTextField(Strings.get("ExpectedResult"), step
029: .getExpectedResult());
030: value.setName(TAG_VALUE);
031:
032: invert = addCheckBox(Strings.get("Invert"), step.isInverted());
033: invert.setName(TAG_INVERT);
034:
035: wait = addCheckBox(Strings.get("WaitToggle"), step.isWait());
036: wait.setName(TAG_WAIT);
037:
038: // Put wait/invert side by side
039: Component c;
040: while ((c = getComponent(getComponentCount() - 1)) != value) {
041: remove(c);
042: }
043: JPanel p = new JPanel();
044: p.setLayout(new GridLayout(0, 2));
045: p.add(invert);
046: p.add(wait);
047: add(p);
048: optionsIndex = getComponentCount();
049:
050: configureWaitOptionFields();
051: }
052:
053: /** Add in assertXXX methods to the list already generated. */
054: protected Collection getComponentTesterMethods(
055: ComponentTester tester) {
056:
057: ArrayList list = new ArrayList(Arrays.asList(tester
058: .getPropertyMethods()));
059: list.addAll(Arrays.asList(tester.getAssertMethods()));
060: list.addAll(Arrays.asList(tester.getComponentAssertMethods()));
061: return list;
062: }
063:
064: private void configureWaitOptionFields() {
065:
066: if (step.isWait()) {
067: if (timeout == null) {
068: timeout = addTextField(Strings.get("Timeout"), String
069: .valueOf(step.getTimeout()), String
070: .valueOf(Assert.DEFAULT_TIMEOUT));
071: timeout.setName(TAG_TIMEOUT);
072: interval = addTextField(Strings.get("PollInterval"),
073: String.valueOf(step.getPollInterval()), String
074: .valueOf(Assert.DEFAULT_INTERVAL));
075: interval.setName(TAG_POLL_INTERVAL);
076: }
077: } else if (timeout != null) {
078: // remove them
079: while (getComponentCount() > optionsIndex) {
080: remove(getComponentCount() - 1);
081: }
082: timeout = interval = null;
083: }
084: revalidate();
085: repaint();
086: }
087:
088: protected boolean validateTimeout(String value) {
089: try {
090: step.setTimeout(Long.parseLong(value));
091: timeout.setForeground(DEFAULT_FOREGROUND);
092: return true;
093: } catch (NumberFormatException nfe) {
094: timeout.setForeground(ERROR_FOREGROUND);
095: }
096: return false;
097: }
098:
099: protected boolean validateInterval(String value) {
100: try {
101: step.setPollInterval(Long.parseLong(value));
102: interval.setForeground(DEFAULT_FOREGROUND);
103: return true;
104: } catch (NumberFormatException nfe) {
105: interval.setForeground(ERROR_FOREGROUND);
106: }
107: return false;
108: }
109:
110: public void actionPerformed(ActionEvent ev) {
111: Object src = ev.getSource();
112: if (src == value) {
113: step.setExpectedResult(value.getText());
114: fireStepChanged();
115: } else if (src == invert) {
116: step.setInverted(!step.isInverted());
117: fireStepChanged();
118: } else if (src == wait) {
119: step.setWait(!step.isWait());
120: configureWaitOptionFields();
121: fireStepChanged();
122: } else if (src == timeout) {
123: String value = timeout.getText();
124: if (validateTimeout(value))
125: fireStepChanged();
126: } else if (src == interval) {
127: String value = interval.getText();
128: if (validateInterval(value))
129: fireStepChanged();
130: } else {
131: super.actionPerformed(ev);
132: }
133: }
134: }
|