📄 listbox.java
字号:
package org.uispec4j;
import junit.framework.Assert;
import junit.framework.AssertionFailedError;
import org.uispec4j.assertion.Assertion;
import org.uispec4j.finder.FinderUtils;
import org.uispec4j.finder.StringMatcher;
import org.uispec4j.utils.ArrayUtils;
import org.uispec4j.utils.KeyUtils;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
/**
* Wrapper for JList components.</p>
* This class provides means for checking the contents and selection of the list,
* changing the selection, etc. For all these methods, when using String values,
* the means of retrieving a String representation of the displayed values can be customized
* using the {@link #setCellValueConverter(ListBoxCellValueConverter)} method and providing
* a new {@link ListBoxCellValueConverter} implementation.
* A {@link DefaultListBoxCellValueConverter} is set up by default.
*/
public class ListBox extends AbstractUIComponent {
public static final String TYPE_NAME = "listBox";
public static final Class[] SWING_CLASSES = {JList.class};
private JList jList;
private ListBoxCellValueConverter cellValueConverter = new DefaultListBoxCellValueConverter();
public ListBox(JList list) {
this.jList = list;
}
public String getDescriptionTypeName() {
return TYPE_NAME;
}
public Component getAwtComponent() {
return jList;
}
public void setCellValueConverter(ListBoxCellValueConverter cellValueConverter) {
this.cellValueConverter = cellValueConverter;
}
public Assertion isEmpty() {
return new Assertion() {
public void check() {
if (getSize() != 0) {
Assert.fail("List should be empty but contains: " + ArrayUtils.toString(getContent()));
}
}
};
}
public Assertion contentEquals(final String[] displayedValues) {
return new Assertion() {
public void check() {
ArrayUtils.assertEquals(displayedValues, getContent());
}
};
}
public Assertion contains(String item) {
return contains(new String[]{item});
}
public Assertion contains(final String[] items) {
return new Assertion() {
public void check() throws Exception {
List content = Arrays.asList(getContent());
for (int i = 0; i < items.length; i++) {
String item = items[i];
if (!content.contains(item)) {
throw new AssertionFailedError("Item '" + item + "' not found - actual content:" + content);
}
}
}
};
}
public void selectIndex(int index) {
jList.setSelectedIndex(index);
}
public void selectIndices(int[] indices) {
jList.getSelectionModel().setValueIsAdjusting(true);
jList.setSelectedIndices(indices);
jList.getSelectionModel().setValueIsAdjusting(false);
}
public void select(String[] values) {
int[] indices = new int[values.length];
for (int i = 0; i < values.length; i++) {
indices[i] = getIndexForString(values[i]);
}
selectIndices(indices);
}
public void select(String value) {
int index = getIndexForString(value);
if (index == -1) {
throw new AssertionFailedError("Item '" + value + "' not found in " +
ArrayUtils.toString(getContent()));
}
jList.setSelectedIndex(index);
}
public void clearSelection() {
jList.clearSelection();
}
public int getSize() {
return jList.getModel().getSize();
}
public void doubleClick() {
Mouse.doubleClick(this);
}
public Assertion selectionIsEmpty() {
return new Assertion() {
public void check() {
if (jList.getSelectedIndices().length != 0) {
String[] names = getSelectedItemNames();
Assert.fail("Selection should be empty but is: " + ArrayUtils.toString(names));
}
}
};
}
public Assertion selectionEquals(final String item) {
return selectionEquals(new String[]{item});
}
public Assertion selectionEquals(final String[] items) {
return new Assertion() {
public void check() {
ArrayUtils.assertEquals(items, getSelectedItemNames());
}
};
}
public void pressKey(Key key) {
KeyUtils.pressKey(jList, key);
}
private String[] getContent() {
String[] names = new String[jList.getModel().getSize()];
for (int i = 0, max = jList.getModel().getSize(); i < max; i++) {
names[i] = getRenderedValue(i);
}
return names;
}
private String[] getSelectedItemNames() {
int[] selectedIndices = jList.getSelectedIndices();
String[] names = new String[selectedIndices.length];
for (int i = 0; i < selectedIndices.length; i++) {
names[i] = getRenderedValue(selectedIndices[i]);
}
return names;
}
private String getRenderedValue(int index) {
return cellValueConverter.getValue(index, getComponent(index), jList.getModel().getElementAt(index));
}
private Component getComponent(int index) {
ListCellRenderer renderer = jList.getCellRenderer();
return renderer.getListCellRendererComponent(jList,
jList.getModel().getElementAt(index),
index, false, false);
}
private int getIndexForString(String searchedValue) {
StringMatcher[] matchers = FinderUtils.getMatchers(searchedValue);
for (int i = 0; i < matchers.length; i++) {
StringMatcher matcher = matchers[i];
List indexes = new ArrayList();
for (int listIndex = 0, max = jList.getModel().getSize(); listIndex < max; listIndex++) {
String renderedValue = getRenderedValue(listIndex);
if (matcher.matches(renderedValue)) {
indexes.add(new Integer(listIndex));
}
}
if (indexes.size() == 1) {
return ((Integer)indexes.get(0)).intValue();
}
if (indexes.size() > 1) {
String[] items = new String[indexes.size()];
for (int j = 0; j < items.length; j++) {
items[j] = getRenderedValue(j);
}
throw new ItemAmbiguityException(searchedValue, items);
}
}
return -1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -