📄 slider.java
字号:
package org.uispec4j;
import junit.framework.Assert;
import org.uispec4j.assertion.Assertion;
import org.uispec4j.utils.ComponentUtils;
import org.uispec4j.utils.Utils;
import javax.swing.JComponent;
import javax.swing.JSlider;
import java.awt.Component;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.TreeMap;
/**
* Wrapper for JSlider components.<p/>
* This class provides means for checking the contents and the current position of the knob,
* changing the position, etc.
*/
public class Slider extends AbstractUIComponent {
public static final String TYPE_NAME = "slider";
public static final Class[] SWING_CLASSES = {JSlider.class};
private JSlider jSlider;
private int precision = 2;
public Slider(JSlider jSlider) {
this.jSlider = jSlider;
}
public Component getAwtComponent() {
return jSlider;
}
public String getDescriptionTypeName() {
return TYPE_NAME;
}
/**
* Checks the slider labels in order.
*/
public Assertion labelsEqual(final String[] expected) {
return new Assertion() {
public void check() throws Exception {
TreeMap sortedTree = getSortedTree();
Utils.assertEquals(expected, sortedTree.values().toArray(new Object[sortedTree.values().size()]));
}
};
}
/**
* Moves the knob at the specified label position
*/
public void setPosition(String label) throws ItemNotFoundException {
int index = getIndexForLabel(label);
if (index == -1) {
throw new ItemNotFoundException("No label '" + label + "' has been found");
}
jSlider.setValue(index);
}
/**
* Checks that the current position corresponds to the specified label
*/
public Assertion positionEquals(final String expectedLabel) {
return new Assertion() {
public void check() throws Exception {
Assert.assertEquals(expectedLabel, getCurrentLabel());
}
};
}
/**
* Checks the knob position as a percentage (0-100) of the available range.
* The actual completion must be equal to the given value plus or minus the slider precision.
*
* @param expectedValue an int between 0 and 100, or -1 if the status is undeterminate
* @see #setPrecision(int)
*/
public Assertion relativePositionEquals(final int expectedValue) {
return new Assertion() {
public void check() throws Exception {
int relativePosition = getRelativePosition();
Assert.assertTrue("Expected " + expectedValue + " but was " + relativePosition,
isRoughlyEqual(expectedValue, relativePosition));
}
};
}
/**
* Sets the precision for the relative position check. This precision is the greatest difference
* allowed between the actual and expected position values (both are integers between 0
* and 100).<p/>
* The default precision is 2.
*
* @see #relativePositionEquals(int)
*/
public void setPrecision(int value) {
this.precision = value;
}
private int getRelativePosition() {
int minimum = jSlider.getMinimum();
int value = jSlider.getValue() - minimum;
int length = jSlider.getMaximum() - minimum;
return value * 100 / length;
}
private boolean isRoughlyEqual(int actualValue, int expectedValue) {
return Math.abs(actualValue - expectedValue) <= precision;
}
private int getIndexForLabel(String label) {
Dictionary dictionary = jSlider.getLabelTable();
for (Enumeration indices = dictionary.keys(); indices.hasMoreElements();) {
Integer indice = (Integer) indices.nextElement();
JComponent component = (JComponent) dictionary.get(indice);
if (label.equals(ComponentUtils.getDisplayedName(component))) {
return indice.intValue();
}
}
return -1;
}
private String getCurrentLabel() {
int value = jSlider.getValue();
Dictionary dictionary = jSlider.getLabelTable();
for (Enumeration indices = dictionary.keys(); indices.hasMoreElements();) {
Integer indice = (Integer) indices.nextElement();
JComponent component = (JComponent) dictionary.get(indice);
if (indice.intValue() == value) {
return ComponentUtils.getDisplayedName(component);
}
}
return null;
}
private TreeMap getSortedTree() {
Dictionary dictionary = jSlider.getLabelTable();
TreeMap treeMap = new TreeMap();
for (Enumeration indices = dictionary.keys(); indices.hasMoreElements();) {
Integer indice = (Integer) indices.nextElement();
JComponent component = (JComponent) dictionary.get(indice);
treeMap.put(indice, ComponentUtils.getDisplayedName(component));
}
return treeMap;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -