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