All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.sfac.gui.utils.VariableComboBoxModel Maven / Gradle / Ivy

Go to download

This project is the core part of the Swing Framework and Components (SFaC). It contains the Swing framework classes and the graphical component classes.

The newest version!
/*-------------------------------------------------------------------------
 Copyright 2009 Olivier Berlanger

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 -------------------------------------------------------------------------*/
package net.sf.sfac.gui.utils;


import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;

import net.sf.sfac.gui.editor.cmp.ComboBoxEditorModel;


/**
 * ComboBox model allowing to change the set of available values (defined as Object[]). The selected value must be one of the
 * available ones. If the selected value is set to a non-existing one, the first available value (index 0) is set.
 * 
 * @author Olivier Berlanger
 */
@SuppressWarnings("serial")
public class VariableComboBoxModel extends AbstractListModel implements ComboBoxModel, ComboBoxEditorModel {

    /** Array holding all the available values for the comboBox. */
    private Object[] availableValues;
    /** The index of the currently selected value. */
    private int selectedIndex;


    /**
     * Build a comboBox model with the given available values.
     * 
     * @param values
     *            the available values.
     * @exception IllegalArgumentException
     *                if the given value array is null or empty (zero size).
     */
    public VariableComboBoxModel(Object[] values) {
        if (values == null) throw new IllegalArgumentException("Values cannot be null");
        if (values.length == 0) throw new IllegalArgumentException("Values cannot be empty");
        availableValues = values;
    }


    /**
     * Set the available values of the comboBox model. This method will ensure that the currently selected value stays the same (if
     * possible) and that the current value is well one of the available ones.
     * 
     * @param values
     *            the available values.
     * @exception IllegalArgumentException
     *                if the given value array is null or empty (zero size).
     */
    public void setAvailableValues(Object[] values) {
        if (values != availableValues) {
            if (values == null) {
                values = new Object[0];
            }
            Object selectedItem = getSelectedItem();
            availableValues = values;
            int oldIndex = selectedIndex;
            selectedIndex = getItemIndex(selectedItem);
            fireContentsChanged(this, 0, Integer.MAX_VALUE);
            if (oldIndex != selectedIndex) fireContentsChanged(this, -1, -1);
        }
    }


    /**
     * Get the index of a value in the available values array. returns 0 if the given value is not found.
     * 
     * @param anItem
     *            a value
     * @return the index of a value in the available values array.
     */
    private int getItemIndex(Object anItem) {
        int len = availableValues.length;
        int index = (len == 0) ? -1 : 0;
        if (anItem != null) {
            for (int i = 0; i < len; i++) {
                if (anItem.equals(availableValues[i])) {
                    index = i;
                    break;
                }
            }
        }
        return index;
    }


    // ------------------- ListModel interface implementation --------------------------------------

    /**
     * Returns the length of the list.
     * 
     * @return the length of the list
     */
    public int getSize() {
        return availableValues.length;
    }


    /**
     * Returns the value at the specified index.
     * 
     * @param index
     *            the requested index
     * @return the value at index
     */
    public Object getElementAt(int index) {
        return availableValues[index];
    }


    // ------------------- ComboBoxModel interface implementation --------------------------------------

    /**
     * Set the selected item. The implementation of this method should notify all registered ListDataListeners that the
     * contents have changed.
     * 
     * @param anItem
     *            the list object to select or null to clear the selection
     */
    public void setSelectedItem(Object anItem) {
        if (getSelectedItem() != anItem) {
            int oldIndex = selectedIndex;
            selectedIndex = getItemIndex(anItem);
            if (oldIndex != selectedIndex) fireContentsChanged(this, -1, -1);
        }
    }


    /**
     * Returns the selected item.
     * 
     * @return The selected item or null if there is no selection
     */
    public Object getSelectedItem() {
        return (selectedIndex < 0) ? null : availableValues[selectedIndex];
    }


    public void fireAllDataChanged() {
        fireContentsChanged(this, 0, getSize());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy