SpinnerListModel.java revision 2362
1008N/A/*
1008N/A * Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved.
1008N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1008N/A *
1008N/A * This code is free software; you can redistribute it and/or modify it
1008N/A * under the terms of the GNU General Public License version 2 only, as
1008N/A * published by the Free Software Foundation. Oracle designates this
1008N/A * particular file as subject to the "Classpath" exception as provided
1008N/A * by Oracle in the LICENSE file that accompanied this code.
1008N/A *
1008N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1008N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1008N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1008N/A * version 2 for more details (a copy is included in the LICENSE file that
1008N/A * accompanied this code).
1008N/A *
1008N/A * You should have received a copy of the GNU General Public License version
1008N/A * 2 along with this work; if not, write to the Free Software Foundation,
1008N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1008N/A *
1008N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1008N/A * or visit www.oracle.com if you need additional information or have any
1008N/A * questions.
1008N/A */
3233N/A
1008N/Apackage javax.swing;
1008N/A
1008N/Aimport java.util.*;
1280N/Aimport java.io.Serializable;
1008N/A
1008N/A
1008N/A/**
1008N/A * A simple implementation of <code>SpinnerModel</code> whose
1008N/A * values are defined by an array or a <code>List</code>.
3646N/A * For example to create a model defined by
2617N/A * an array of the names of the days of the week:
1008N/A * <pre>
1008N/A * String[] days = new DateFormatSymbols().getWeekdays();
2086N/A * SpinnerModel model = new SpinnerListModel(Arrays.asList(days).subList(1, 8));
1008N/A * </pre>
2617N/A * This class only stores a reference to the array or <code>List</code>
2617N/A * so if an element of the underlying sequence changes, it's up
2617N/A * to the application to notify the <code>ChangeListeners</code> by calling
2617N/A * <code>fireStateChanged</code>.
2617N/A * <p>
1008N/A * This model inherits a <code>ChangeListener</code>.
1008N/A * The <code>ChangeListener</code>s are notified whenever the
1008N/A * model's <code>value</code> or <code>list</code> properties changes.
3646N/A *
3646N/A * @see JSpinner
1008N/A * @see SpinnerModel
1008N/A * @see AbstractSpinnerModel
1008N/A * @see SpinnerNumberModel
1008N/A * @see SpinnerDateModel
1008N/A *
2617N/A * @author Hans Muller
2617N/A * @since 1.4
1008N/A */
1008N/Apublic class SpinnerListModel extends AbstractSpinnerModel implements Serializable
1008N/A{
1008N/A private List list;
1008N/A private int index;
1008N/A
1008N/A
1008N/A /**
1008N/A * Constructs a <code>SpinnerModel</code> whose sequence of
1008N/A * values is defined by the specified <code>List</code>.
1008N/A * The initial value (<i>current element</i>)
1008N/A * of the model will be <code>values.get(0)</code>.
1008N/A * If <code>values</code> is <code>null</code> or has zero
1008N/A * size, an <code>IllegalArugmentException</code> is thrown.
1008N/A *
2617N/A * @param values the sequence this model represents
2617N/A * @throws IllegalArugmentException if <code>values</code> is
2617N/A * <code>null</code> or zero size
2617N/A */
2617N/A public SpinnerListModel(List<?> values) {
2617N/A if (values == null || values.size() == 0) {
2617N/A throw new IllegalArgumentException("SpinnerListModel(List) expects non-null non-empty List");
2617N/A }
2617N/A this.list = values;
2617N/A this.index = 0;
2617N/A }
2617N/A
2617N/A
2617N/A /**
2617N/A * Constructs a <code>SpinnerModel</code> whose sequence of values
1008N/A * is defined by the specified array. The initial value of the model
1008N/A * will be <code>values[0]</code>. If <code>values</code> is
1008N/A * <code>null</code> or has zero length, an
1008N/A * <code>IllegalArugmentException</code> is thrown.
2617N/A *
1008N/A * @param values the sequence this model represents
1008N/A * @throws IllegalArugmentException if <code>values</code> is
1008N/A * <code>null</code> or zero length
1008N/A */
1008N/A public SpinnerListModel(Object[] values) {
1008N/A if (values == null || values.length == 0) {
1008N/A throw new IllegalArgumentException("SpinnerListModel(Object[]) expects non-null non-empty Object[]");
3646N/A }
3646N/A this.list = Arrays.asList(values);
1008N/A this.index = 0;
1008N/A }
1008N/A
1008N/A
1008N/A /**
1008N/A * Constructs an effectively empty <code>SpinnerListModel</code>.
1008N/A * The model's list will contain a single
1008N/A * <code>"empty"</code> string element.
1008N/A */
1008N/A public SpinnerListModel() {
1008N/A this(new Object[]{"empty"});
1008N/A }
1008N/A
1008N/A
2617N/A /**
2617N/A * Returns the <code>List</code> that defines the sequence for this model.
2617N/A *
2617N/A * @return the value of the <code>list</code> property
2617N/A * @see #setList
2617N/A */
1008N/A public List<?> getList() {
1008N/A return list;
3646N/A }
1008N/A
3646N/A
3646N/A /**
3646N/A * Changes the list that defines this sequence and resets the index
1008N/A * of the models <code>value</code> to zero. Note that <code>list</code>
1008N/A * is not copied, the model just stores a reference to it.
1008N/A * <p>
1008N/A * This method fires a <code>ChangeEvent</code> if <code>list</code> is
1008N/A * not equal to the current list.
2617N/A *
2617N/A * @param list the sequence that this model represents
2617N/A * @throws IllegalArgumentException if <code>list</code> is
2617N/A * <code>null</code> or zero length
2617N/A * @see #getList
2617N/A */
1008N/A public void setList(List<?> list) {
1008N/A if ((list == null) || (list.size() == 0)) {
1008N/A throw new IllegalArgumentException("invalid list");
1008N/A }
1008N/A if (!list.equals(this.list)) {
1008N/A this.list = list;
1008N/A index = 0;
1008N/A fireStateChanged();
3646N/A }
3646N/A }
3646N/A
3646N/A
1008N/A /**
1008N/A * Returns the current element of the sequence.
1008N/A *
1008N/A * @return the <code>value</code> property
1008N/A * @see SpinnerModel#getValue
1008N/A * @see #setValue
1008N/A */
1008N/A public Object getValue() {
1008N/A return list.get(index);
1008N/A }
1008N/A
2617N/A
2617N/A /**
2617N/A * Changes the current element of the sequence and notifies
2617N/A * <code>ChangeListeners</code>. If the specified
2617N/A * value is not equal to an element of the underlying sequence
2617N/A * then an <code>IllegalArgumentException</code> is thrown.
2617N/A * In the following example the <code>setValue</code> call
2617N/A * would cause an exception to be thrown:
2617N/A * <pre>
2617N/A * String[] values = {"one", "two", "free", "four"};
1008N/A * SpinnerModel model = new SpinnerListModel(values);
1008N/A * model.setValue("TWO");
2086N/A * </pre>
1008N/A *
1008N/A * @param elt the sequence element that will be model's current value
1008N/A * @throws IllegalArgumentException if the specified value isn't allowed
1008N/A * @see SpinnerModel#setValue
1008N/A * @see #getValue
1008N/A */
public void setValue(Object elt) {
int index = list.indexOf(elt);
if (index == -1) {
throw new IllegalArgumentException("invalid sequence element");
}
else if (index != this.index) {
this.index = index;
fireStateChanged();
}
}
/**
* Returns the next legal value of the underlying sequence or
* <code>null</code> if value is already the last element.
*
* @return the next legal value of the underlying sequence or
* <code>null</code> if value is already the last element
* @see SpinnerModel#getNextValue
* @see #getPreviousValue
*/
public Object getNextValue() {
return (index >= (list.size() - 1)) ? null : list.get(index + 1);
}
/**
* Returns the previous element of the underlying sequence or
* <code>null</code> if value is already the first element.
*
* @return the previous element of the underlying sequence or
* <code>null</code> if value is already the first element
* @see SpinnerModel#getPreviousValue
* @see #getNextValue
*/
public Object getPreviousValue() {
return (index <= 0) ? null : list.get(index - 1);
}
/**
* Returns the next object that starts with <code>substring</code>.
*
* @param substring the string to be matched
* @return the match
*/
Object findNextMatch(String substring) {
int max = list.size();
if (max == 0) {
return null;
}
int counter = index;
do {
Object value = list.get(counter);
String string = value.toString();
if (string != null && string.startsWith(substring)) {
return value;
}
counter = (counter + 1) % max;
} while (counter != index);
return null;
}
}