001: package org.uispec4j;
002:
003: import junit.framework.Assert;
004: import org.uispec4j.assertion.Assertion;
005: import org.uispec4j.finder.ComponentMatcher;
006:
007: import javax.swing.JSpinner;
008: import java.awt.Component;
009:
010: /**
011: * Wrapper for JSpinner components.
012: */
013: public class Spinner extends AbstractUIComponent {
014: public static final String TYPE_NAME = "spinner";
015: public static final Class[] SWING_CLASSES = { JSpinner.class };
016:
017: protected JSpinner jSpinner;
018:
019: public Spinner(JSpinner jSpinner) {
020: this .jSpinner = jSpinner;
021: }
022:
023: public Component getAwtComponent() {
024: return jSpinner;
025: }
026:
027: public String getDescriptionTypeName() {
028: return TYPE_NAME;
029: }
030:
031: /**
032: * Checks that the spinner displays the given value
033: */
034: public Assertion valueEquals(final Object expectedValue) {
035: return new Assertion() {
036: public void check() throws Exception {
037: Assert.assertEquals(expectedValue, jSpinner.getValue());
038: }
039: };
040: }
041:
042: /**
043: * Checks that the previous value is the given value
044: */
045: public Assertion previousValueEquals(
046: final Object expectedPreviousValue) {
047: return new Assertion() {
048: public void check() throws Exception {
049: Object previousValue = jSpinner.getPreviousValue();
050: if (previousValue == null) {
051: Assert.fail("No previous value from the start");
052: }
053: Assert.assertEquals(expectedPreviousValue,
054: previousValue);
055: }
056: };
057: }
058:
059: /**
060: * Checks that the next value is the given value
061: */
062: public Assertion nextValueEquals(final Object expectedNextValue) {
063: return new Assertion() {
064: public void check() throws Exception {
065: Object nextValue = jSpinner.getNextValue();
066: if (nextValue == null) {
067: Assert.fail("No previous value from the end");
068: }
069: Assert.assertEquals(expectedNextValue, nextValue);
070: }
071: };
072: }
073:
074: /**
075: * Changes the displayed text<p/>
076: * This method will throw an exception if the value is not allowed in the spinner.<p/>
077: */
078: public void setValue(Object value) throws ItemNotFoundException {
079: try {
080: jSpinner.setValue(value);
081: } catch (IllegalArgumentException e) {
082: throw new ItemNotFoundException("'" + value + "' not found");
083: }
084: }
085:
086: /**
087: * Clicks on the button for next value
088: */
089: public void clickForNextValue() {
090: try {
091: jSpinner.setValue(jSpinner.getNextValue());
092: } catch (IllegalArgumentException e) {
093: // Ignore it, as in the UI
094: }
095: }
096:
097: /**
098: * Clicks on the button for previous value
099: */
100: public void clickForPreviousValue() {
101: try {
102: jSpinner.setValue(jSpinner.getPreviousValue());
103: } catch (IllegalArgumentException e) {
104: // Ignore it, as in the UI
105: }
106: }
107:
108: static ComponentMatcher getSpinnerMatcherByModel(
109: final Class spinnerModelClass) {
110: return new ComponentMatcher() {
111: public boolean matches(Component component) {
112: if (component instanceof JSpinner) {
113: JSpinner jSpinner = (JSpinner) component;
114: return jSpinner.getModel().getClass()
115: .isAssignableFrom(spinnerModelClass);
116: }
117: return false;
118: }
119: };
120: }
121: }
|