001: package org.uispec4j;
002:
003: import junit.framework.Assert;
004: import org.uispec4j.assertion.Assertion;
005: import org.uispec4j.utils.ComponentUtils;
006: import org.uispec4j.utils.Utils;
007:
008: import javax.swing.JComponent;
009: import javax.swing.JSlider;
010: import java.awt.Component;
011: import java.util.Dictionary;
012: import java.util.Enumeration;
013: import java.util.TreeMap;
014:
015: /**
016: * Wrapper for JSlider components.<p/>
017: * This class provides means for checking the contents and the current position of the knob,
018: * changing the position, etc.
019: */
020: public class Slider extends AbstractUIComponent {
021: public static final String TYPE_NAME = "slider";
022: public static final Class[] SWING_CLASSES = { JSlider.class };
023:
024: private JSlider jSlider;
025: private int precision = 2;
026:
027: public Slider(JSlider jSlider) {
028: this .jSlider = jSlider;
029: }
030:
031: public Component getAwtComponent() {
032: return jSlider;
033: }
034:
035: public String getDescriptionTypeName() {
036: return TYPE_NAME;
037: }
038:
039: /**
040: * Checks the slider labels in order.
041: */
042: public Assertion labelsEqual(final String[] expected) {
043: return new Assertion() {
044: public void check() throws Exception {
045: TreeMap sortedTree = getSortedTree();
046: Utils
047: .assertEquals(expected, sortedTree.values()
048: .toArray(
049: new Object[sortedTree.values()
050: .size()]));
051: }
052: };
053: }
054:
055: /**
056: * Moves the knob at the specified label position
057: */
058: public void setPosition(String label) throws ItemNotFoundException {
059: int index = getIndexForLabel(label);
060: if (index == -1) {
061: throw new ItemNotFoundException("No label '" + label
062: + "' has been found");
063: }
064: jSlider.setValue(index);
065: }
066:
067: /**
068: * Checks that the current position corresponds to the specified label
069: */
070: public Assertion positionEquals(final String expectedLabel) {
071: return new Assertion() {
072: public void check() throws Exception {
073: Assert.assertEquals(expectedLabel, getCurrentLabel());
074: }
075: };
076: }
077:
078: /**
079: * Checks the knob position as a percentage (0-100) of the available range.
080: * The actual completion must be equal to the given value plus or minus the slider precision.
081: *
082: * @param expectedValue an int between 0 and 100, or -1 if the status is undeterminate
083: * @see #setPrecision(int)
084: */
085: public Assertion relativePositionEquals(final int expectedValue) {
086: return new Assertion() {
087: public void check() throws Exception {
088: int relativePosition = getRelativePosition();
089: Assert
090: .assertTrue("Expected " + expectedValue
091: + " but was " + relativePosition,
092: isRoughlyEqual(expectedValue,
093: relativePosition));
094: }
095: };
096: }
097:
098: /**
099: * Sets the precision for the relative position check. This precision is the greatest difference
100: * allowed between the actual and expected position values (both are integers between 0
101: * and 100).<p/>
102: * The default precision is 2.
103: *
104: * @see #relativePositionEquals(int)
105: */
106: public void setPrecision(int value) {
107: this .precision = value;
108: }
109:
110: private int getRelativePosition() {
111: int minimum = jSlider.getMinimum();
112: int value = jSlider.getValue() - minimum;
113: int length = jSlider.getMaximum() - minimum;
114: return value * 100 / length;
115: }
116:
117: private boolean isRoughlyEqual(int actualValue, int expectedValue) {
118: return Math.abs(actualValue - expectedValue) <= precision;
119: }
120:
121: private int getIndexForLabel(String label) {
122: Dictionary dictionary = jSlider.getLabelTable();
123: for (Enumeration indices = dictionary.keys(); indices
124: .hasMoreElements();) {
125: Integer indice = (Integer) indices.nextElement();
126: JComponent component = (JComponent) dictionary.get(indice);
127: if (label
128: .equals(ComponentUtils.getDisplayedName(component))) {
129: return indice.intValue();
130: }
131: }
132: return -1;
133: }
134:
135: private String getCurrentLabel() {
136: int value = jSlider.getValue();
137: Dictionary dictionary = jSlider.getLabelTable();
138: for (Enumeration indices = dictionary.keys(); indices
139: .hasMoreElements();) {
140: Integer indice = (Integer) indices.nextElement();
141: JComponent component = (JComponent) dictionary.get(indice);
142: if (indice.intValue() == value) {
143: return ComponentUtils.getDisplayedName(component);
144: }
145: }
146: return null;
147: }
148:
149: private TreeMap getSortedTree() {
150: Dictionary dictionary = jSlider.getLabelTable();
151: TreeMap treeMap = new TreeMap();
152: for (Enumeration indices = dictionary.keys(); indices
153: .hasMoreElements();) {
154: Integer indice = (Integer) indices.nextElement();
155: JComponent component = (JComponent) dictionary.get(indice);
156: treeMap.put(indice, ComponentUtils
157: .getDisplayedName(component));
158: }
159: return treeMap;
160: }
161: }
|