uk.org.retep.swing.model.AbstractModel Maven / Gradle / Ivy
/*
* Copyright (c) 1998-2010, Peter T Mount
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of the retep.org.uk nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package uk.org.retep.swing.model;
import java.util.HashSet;
import java.util.Set;
import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.event.ListDataListener;
/**
* A ComboBoxModel that displays values of an enum
*
* @author peter
*/
public abstract class AbstractModel
implements ComboBoxModel
{
private final Set listeners = new HashSet();
private Object selected;
private T defaultValue;
/** Creates a new instance of ColorModel
* @param defaultValue
*/
public AbstractModel( T defaultValue )
{
this.defaultValue = defaultValue;
}
public final void setModel( final JComboBox component )
{
component.setModel( this );
component.setRenderer( this.getRenderer() );
component.setSelectedItem( getDefaultValue() );
}
public final void setModel( final JList component )
{
component.setModel( this );
component.setCellRenderer( this.getRenderer() );
component.setSelectedValue( getDefaultValue(), true );
}
public final T getDefaultValue()
{
return defaultValue;
}
public abstract ListCellRenderer getRenderer();
@SuppressWarnings( "unchecked" )
public final T getSelected()
{
return (T) getSelectedItem();
}
/**
* Returns the length of the list.
* @return the length of the list
*/
@Override
public abstract int getSize();
/**
* Returns the value at the specified index.
* @param index the requested index
* @return the value at index
*/
@Override
public abstract Object getElementAt( int index );
/**
* Adds a listener to the list that's notified each time a change
* to the data model occurs.
* @param l the ListDataListener
to be added
*/
@Override
public final void addListDataListener( final ListDataListener l )
{
listeners.add( l );
}
/**
* Removes a listener from the list that's notified each time a
* change to the data model occurs.
* @param l the ListDataListener
to be removed
*/
@Override
public final void removeListDataListener( final ListDataListener l )
{
listeners.remove( l );
}
/**
* Set the selected item. The implementation of this method should notify
* all registered ListDataListener
s that the contents
* have changed.
*
* @param anItem the list object to select or null
* to clear the selection
*/
@Override
public void setSelectedItem( final Object anItem )
{
selected = anItem == null ? defaultValue : anItem;
}
/**
* Returns the selected item
* @return The selected item or null
if there is no selection
*/
@Override
public Object getSelectedItem()
{
return selected == null ? defaultValue : selected;
}
}