001 /*
002 * Created on Apr 12, 2008
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 *
014 * Copyright @2008-2010 the original author or authors.
015 */
016 package org.fest.swing.driver;
017
018 import static org.fest.swing.driver.ModelValueToString.asText;
019
020 import java.awt.Component;
021
022 import javax.swing.JList;
023
024 import org.fest.swing.annotation.RunsInCurrentThread;
025 import org.fest.swing.cell.JListCellReader;
026
027 /**
028 * Understands the default implementation of <code>{@link JListCellReader}</code>.
029 *
030 * @author Alex Ruiz
031 * @author Yvonne Wang
032 */
033 public class BasicJListCellReader implements JListCellReader {
034
035 private final CellRendererReader rendererReader;
036
037 /**
038 * Creates a new </code>{@link BasicJListCellReader}</code> that uses a
039 * <code>{@link BasicCellRendererReader}</code> to read the value from the cell renderer component in a
040 * <code>JList</code>.
041 */
042 public BasicJListCellReader() {
043 this(new BasicCellRendererReader());
044 }
045
046 /**
047 * Creates a new </code>{@link BasicJListCellReader}</code>.
048 * @param rendererReader knows how to read values from the cell renderer component in a
049 * <code>JList</code>.
050 * @throws NullPointerException if <code>rendererReader</code> is <code>null</code>.
051 */
052 public BasicJListCellReader(CellRendererReader rendererReader) {
053 if (rendererReader == null)
054 throw new NullPointerException("CellRendererReader should not be null");
055 this.rendererReader = rendererReader;
056 }
057
058 /**
059 * Returns the internal value of a cell in a <code>{@link JList}</code> as expected in a test.
060 * <p>
061 * <b>Note:</b> This method is <b>not</b> guaranteed to be executed in the event dispatch thread (EDT.) Clients are
062 * responsible for calling this method from the EDT.
063 * </p>
064 * @param list the given <code>JList</code>.
065 * @param index the index of the cell.
066 * @return the internal value of a cell in a <code>JList</code> as expected in a test.
067 * @see CellRendererReader#valueFrom(Component)
068 */
069 @RunsInCurrentThread
070 public String valueAt(JList list, int index) {
071 Object element = list.getModel().getElementAt(index);
072 Component c = list.getCellRenderer().getListCellRendererComponent(list, element, index, true, true);
073 String value = (c != null) ? rendererReader.valueFrom(c) : null;
074 if (value != null) return value;
075 return asText(element);
076 }
077 }