Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2010 Daniel Murphy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
Created at Sep 9, 2010, 4:35:08 PM
 
 package com.dmurph.mvc.gui.combo;
 
 
Copied from DefaultComboBoxModel, with methods added for more control over the model data.

Author(s):
Daniel Murphy
 
 @SuppressWarnings({"rawtypes""unchecked"})
 public class MVCJComboBoxModel<E> extends AbstractListModel implements MutableComboBoxModelSerializable{
 	private static final long serialVersionUID = 1L;
 	
 	private final Vector<E> objects;
     private E selectedObject;

    
Constructs an empty DefaultComboBoxModel.
 
     public MVCJComboBoxModel() {
          = new Vector();
     }

    
Constructs a DefaultComboBoxModel object initialized with an array of objects.

Parameters:
items an array of Object objects
 
 	public MVCJComboBoxModel(final E items[]) {
          = new Vector();
         .ensureCapacityitems.length );
 
         int i,c;
         for ( i=0,c=items.length;i<c;i++ )
             .addElement(items[i]);
 
         if ( getSize() > 0 ) {
              = getElementAt( 0 );
         }
     }

    
Constructs a DefaultComboBoxModel object initialized with a vector.

Parameters:
v a Vector object ...
 
     public MVCJComboBoxModel(Vector<E> v) {
          = v;
 
         if ( getSize() > 0 ) {
              = getElementAt( 0 );
         }
     }
 
     // implements javax.swing.ComboBoxModel
     
Set the value of the selected item. The selected item may be null.

Parameters:
anObject The combo box value or null for no selection.
 
     public void setSelectedItem(Object anObject) {
         if (( != null && !.equalsanObject )) ||
 	     == null && anObject != null) {
	     = (E) anObject;
	    fireContentsChanged(this, -1, -1);
        }
    }
    // implements javax.swing.ComboBoxModel
    public E getSelectedItem() {
        return ;
    }
    // implements javax.swing.ListModel
    public int getSize() {
        return .size();
    }
    // implements javax.swing.ListModel
    public E getElementAt(int index) {
        if ( index >= 0 && index < .size() )
            return .elementAt(index);
        else
            return null;
    }

    
Returns the index-position of the specified object in the list.

Parameters:
anObject
Returns:
an int representing the index position, where 0 is the first position
    public int getIndexOf(Object anObject) {
        return .indexOf(anObject);
    }
    // implements javax.swing.MutableComboBoxModel
    public void addElement(Object anObject) {
        .addElement((E)anObject);
        fireIntervalAdded(this,.size()-1, .size()-1);
        if ( .size() == 1 &&  == null && anObject != null ) {
            setSelectedItemanObject );
        }
    }
    
    public void addElements(int argIndexCollection<E> argElements){
    	if(argElements == null){
    		return;
    	}
    	
    	.addAll(argIndexargElements);
        fireIntervalAdded(thisargIndexargIndex + argElements.size() - 1);  
        
        if ( .size() == 1 &&  == null) {
        	setSelectedItem(.get(0));
        }
    }
    
    public void addElements(Collection<E> argElements){
    	if(argElements == null){
    		return;
    	}
    	
    	int oldSize = argElements.size();
    	.addAll(argElements);
        fireIntervalAdded(this,oldSize.size()-1);  
        
        if ( .size() == 1 &&  == null) {
        	setSelectedItem(.get(0));
        }
    }
    // implements javax.swing.MutableComboBoxModel
    public void insertElementAt(Object anObject,int index) {
        .insertElementAt((E)anObject,index);
        fireIntervalAdded(thisindexindex);
    }
    
    public void setElementAt(E anObjectint index){
    	.setElementAt(anObjectindex);
    	fireContentsChanged(thisindexindex);
    }
    
    // implements javax.swing.MutableComboBoxModel
    public void removeElementAt(int index) {
        if ( getElementAtindex ) ==  ) {
            if ( index == 0 ) {
                setSelectedItemgetSize() == 1 ? null : getElementAtindex + 1 ) );
            }
            else {
                setSelectedItemgetElementAtindex - 1 ) );
            }
        }
        .removeElementAt(index);
        fireIntervalRemoved(thisindexindex);
    }
    
    public void sort(Comparator<E> argComparator){
    	Collections.sort(argComparator);
    }
    // implements javax.swing.MutableComboBoxModel
    public void removeElement(Object anObject) {
        int index = .indexOf(anObject);
        if ( index != -1 ) {
            removeElementAt(index);
        }
    }

    
Empties the list.
    public void removeAllElements() {
	    if ( .size() > 0 ) {
	            int firstIndex = 0;
	            int lastIndex = .size() - 1;
	            .removeAllElements();
		     = null;
	            fireIntervalRemoved(thisfirstIndexlastIndex);
	        } else {
		     = null;
		}
    }
New to GrepCode? Check out our FAQ X