View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 eviware.com
    3    *
    4    *  soapUI is free software; you can redistribute it and/or modify it under the
    5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
    6    *  the Free Software Foundation.
    7    *
    8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
    9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    10   *  See the GNU Lesser General Public License for more details at gnu.org.
    11   */
    12
    13  package com.eviware.soapui.support.swing;
    14
    15  import java.io.Serializable;
    16  import java.util.Vector;
    17
    18  import javax.swing.AbstractListModel;
    19  import javax.swing.MutableComboBoxModel;
    20
    21  public class ExtendedComboBoxModel extends AbstractListModel implements MutableComboBoxModel, Serializable {
    22     Vector objects;
    23     Object selectedObject;
    24
    25     /***
    26      * Constructs an empty DefaultComboBoxModel object.
    27      */
    28     public ExtendedComboBoxModel() {
    29         objects = new Vector();
    30     }
    31
    32     /***
    33      * Constructs a DefaultComboBoxModel object initialized with
    34      * an array of objects.
    35      *
    36      * @param items  an array of Object objects
    37      */
    38     public ExtendedComboBoxModel(final Object items[]) {
    39         objects = new Vector();
    40         objects.ensureCapacity( items.length );
    41
    42         int i,c;
    43         for ( i=0,c=items.length;i<c;i++ )
    44             objects.addElement(items[i]);
    45
    46         if ( getSize() > 0 ) {
    47             selectedObject = getElementAt( 0 );
    48         }
    49     }
    50
    51     /***
    52      * Constructs a DefaultComboBoxModel object initialized with
    53      * a vector.
    54      *
    55      * @param v  a Vector object ...
    56      */
    57     public ExtendedComboBoxModel(Vector<?> v) {
    58         objects = v;
    59
    60         if ( getSize() > 0 ) {
    61             selectedObject = getElementAt( 0 );
    62         }
    63     }
    64
    65     // implements javax.swing.ComboBoxModel
    66     /***
    67      * Set the value of the selected item. The selected item may be null.
    68      * <p>
    69      * @param anObject The combo box value or null for no selection.
    70      */
    71     public void setSelectedItem(Object anObject) {
    72         if ((selectedObject != null && !selectedObject.equals( anObject )) ||
    73  	    selectedObject == null && anObject != null) {
    74  	    selectedObject = anObject;
    75  	    fireContentsChanged(this, -1, -1);
    76         }
    77     }
    78
    79     // implements javax.swing.ComboBoxModel
    80     public Object getSelectedItem() {
    81         return selectedObject;
    82     }
    83
    84     // implements javax.swing.ListModel
    85     public int getSize() {
    86         return objects.size();
    87     }
    88
    89     // implements javax.swing.ListModel
    90     public Object getElementAt(int index) {
    91         if ( index >= 0 && index < objects.size() )
    92             return objects.elementAt(index);
    93         else
    94             return null;
    95     }
    96
    97     /***
    98      * Returns the index-position of the specified object in the list.
    99      *
    100     * @param anObject
    101     * @return an int representing the index position, where 0 is
    102     *         the first position
    103     */
    104    public int getIndexOf(Object anObject) {
    105        return objects.indexOf(anObject);
    106    }
    107
    108    // implements javax.swing.MutableComboBoxModel
    109    public void addElement(Object anObject) {
    110        objects.addElement(anObject);
    111        fireIntervalAdded(this,objects.size()-1, objects.size()-1);
    112        if ( objects.size() == 1 && selectedObject == null && anObject != null ) {
    113            setSelectedItem( anObject );
    114        }
    115    }
    116
    117    // implements javax.swing.MutableComboBoxModel
    118    public void insertElementAt(Object anObject,int index) {
    119        objects.insertElementAt(anObject,index);
    120        fireIntervalAdded(this, index, index);
    121    }
    122
    123    // implements javax.swing.MutableComboBoxModel
    124    public void removeElementAt(int index) {
    125        if ( getElementAt( index ) == selectedObject ) {
    126            if ( index == 0 ) {
    127                setSelectedItem( getSize() == 1 ? null : getElementAt( index + 1 ) );
    128            }
    129            else {
    130                setSelectedItem( getElementAt( index - 1 ) );
    131            }
    132        }
    133
    134        objects.removeElementAt(index);
    135
    136        fireIntervalRemoved(this, index, index);
    137    }
    138
    139    // implements javax.swing.MutableComboBoxModel
    140    public void removeElement(Object anObject) {
    141        int index = objects.indexOf(anObject);
    142        if ( index != -1 ) {
    143            removeElementAt(index);
    144        }
    145    }
    146
    147    /***
    148     * Empties the list.
    149     */
    150    public void removeAllElements() {
    151        if ( objects.size() > 0 ) {
    152            int firstIndex = 0;
    153            int lastIndex = objects.size() - 1;
    154            objects.removeAllElements();
    155 	    selectedObject = null;
    156            fireIntervalRemoved(this, firstIndex, lastIndex);
    157        } else {
    158 	    selectedObject = null;
    159 	}
    160    }
    161
    162    public void setElementAt( Object obj, int index )
    163    {
    164    	objects.setElementAt( obj, index );
    165    	fireContentsChanged( this, index, index );
    166    }
    167 }