001: package org.uispec4j;
002:
003: import junit.framework.Assert;
004: import junit.framework.AssertionFailedError;
005: import org.uispec4j.assertion.Assertion;
006: import org.uispec4j.finder.FinderUtils;
007: import org.uispec4j.finder.StringMatcher;
008: import org.uispec4j.utils.ArrayUtils;
009: import org.uispec4j.utils.KeyUtils;
010:
011: import javax.swing.*;
012: import java.awt.*;
013: import java.util.ArrayList;
014: import java.util.List;
015: import java.util.Arrays;
016:
017: /**
018: * Wrapper for JList components.</p>
019: * This class provides means for checking the contents and selection of the list,
020: * changing the selection, etc. For all these methods, when using String values,
021: * the means of retrieving a String representation of the displayed values can be customized
022: * using the {@link #setCellValueConverter(ListBoxCellValueConverter)} method and providing
023: * a new {@link ListBoxCellValueConverter} implementation.
024: * A {@link DefaultListBoxCellValueConverter} is set up by default.
025: */
026: public class ListBox extends AbstractUIComponent {
027: public static final String TYPE_NAME = "listBox";
028: public static final Class[] SWING_CLASSES = { JList.class };
029:
030: private JList jList;
031: private ListBoxCellValueConverter cellValueConverter = new DefaultListBoxCellValueConverter();
032:
033: public ListBox(JList list) {
034: this .jList = list;
035: }
036:
037: public String getDescriptionTypeName() {
038: return TYPE_NAME;
039: }
040:
041: public Component getAwtComponent() {
042: return jList;
043: }
044:
045: public void setCellValueConverter(
046: ListBoxCellValueConverter cellValueConverter) {
047: this .cellValueConverter = cellValueConverter;
048: }
049:
050: public Assertion isEmpty() {
051: return new Assertion() {
052: public void check() {
053: if (getSize() != 0) {
054: Assert.fail("List should be empty but contains: "
055: + ArrayUtils.toString(getContent()));
056: }
057: }
058: };
059: }
060:
061: public Assertion contentEquals(final String[] displayedValues) {
062: return new Assertion() {
063: public void check() {
064: ArrayUtils.assertEquals(displayedValues, getContent());
065: }
066: };
067: }
068:
069: public Assertion contains(String item) {
070: return contains(new String[] { item });
071: }
072:
073: public Assertion contains(final String[] items) {
074: return new Assertion() {
075: public void check() throws Exception {
076: List content = Arrays.asList(getContent());
077: for (int i = 0; i < items.length; i++) {
078: String item = items[i];
079: if (!content.contains(item)) {
080: throw new AssertionFailedError("Item '" + item
081: + "' not found - actual content:"
082: + content);
083: }
084: }
085: }
086: };
087: }
088:
089: public void selectIndex(int index) {
090: jList.setSelectedIndex(index);
091: }
092:
093: public void selectIndices(int[] indices) {
094: jList.getSelectionModel().setValueIsAdjusting(true);
095: jList.setSelectedIndices(indices);
096: jList.getSelectionModel().setValueIsAdjusting(false);
097: }
098:
099: public void select(String[] values) {
100: int[] indices = new int[values.length];
101: for (int i = 0; i < values.length; i++) {
102: indices[i] = getIndexForString(values[i]);
103: }
104: selectIndices(indices);
105: }
106:
107: public void select(String value) {
108: int index = getIndexForString(value);
109: if (index == -1) {
110: throw new AssertionFailedError("Item '" + value
111: + "' not found in "
112: + ArrayUtils.toString(getContent()));
113: }
114: jList.setSelectedIndex(index);
115: }
116:
117: public void clearSelection() {
118: jList.clearSelection();
119: }
120:
121: public int getSize() {
122: return jList.getModel().getSize();
123: }
124:
125: public void doubleClick() {
126: Mouse.doubleClick(this );
127: }
128:
129: public Assertion selectionIsEmpty() {
130: return new Assertion() {
131: public void check() {
132: if (jList.getSelectedIndices().length != 0) {
133: String[] names = getSelectedItemNames();
134: Assert.fail("Selection should be empty but is: "
135: + ArrayUtils.toString(names));
136: }
137: }
138: };
139: }
140:
141: public Assertion selectionEquals(final String item) {
142: return selectionEquals(new String[] { item });
143: }
144:
145: public Assertion selectionEquals(final String[] items) {
146: return new Assertion() {
147: public void check() {
148: ArrayUtils.assertEquals(items, getSelectedItemNames());
149: }
150: };
151: }
152:
153: public void pressKey(Key key) {
154: KeyUtils.pressKey(jList, key);
155: }
156:
157: private String[] getContent() {
158: String[] names = new String[jList.getModel().getSize()];
159: for (int i = 0, max = jList.getModel().getSize(); i < max; i++) {
160: names[i] = getRenderedValue(i);
161: }
162: return names;
163: }
164:
165: private String[] getSelectedItemNames() {
166: int[] selectedIndices = jList.getSelectedIndices();
167: String[] names = new String[selectedIndices.length];
168: for (int i = 0; i < selectedIndices.length; i++) {
169: names[i] = getRenderedValue(selectedIndices[i]);
170: }
171: return names;
172: }
173:
174: private String getRenderedValue(int index) {
175: return cellValueConverter.getValue(index, getComponent(index),
176: jList.getModel().getElementAt(index));
177: }
178:
179: private Component getComponent(int index) {
180: ListCellRenderer renderer = jList.getCellRenderer();
181: return renderer.getListCellRendererComponent(jList, jList
182: .getModel().getElementAt(index), index, false, false);
183: }
184:
185: private int getIndexForString(String searchedValue) {
186: StringMatcher[] matchers = FinderUtils
187: .getMatchers(searchedValue);
188: for (int i = 0; i < matchers.length; i++) {
189: StringMatcher matcher = matchers[i];
190: List indexes = new ArrayList();
191: for (int listIndex = 0, max = jList.getModel().getSize(); listIndex < max; listIndex++) {
192: String renderedValue = getRenderedValue(listIndex);
193: if (matcher.matches(renderedValue)) {
194: indexes.add(new Integer(listIndex));
195: }
196: }
197: if (indexes.size() == 1) {
198: return ((Integer) indexes.get(0)).intValue();
199: }
200: if (indexes.size() > 1) {
201: String[] items = new String[indexes.size()];
202: for (int j = 0; j < items.length; j++) {
203: items[j] = getRenderedValue(j);
204: }
205: throw new ItemAmbiguityException(searchedValue, items);
206: }
207: }
208: return -1;
209: }
210: }
|