net.sf.javagimmicks.swing.model.ComboBoxModelAdapter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gimmicks Show documentation
Show all versions of gimmicks Show documentation
Utility classes, APIs and tools for Java
package net.sf.javagimmicks.swing.model;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ComboBoxModel;
import net.sf.javagimmicks.lang.LangUtils;
/**
* A {@link ComboBoxModel} implementation that is also a {@link List}.
*/
public class ComboBoxModelAdapter extends ListModelAdapter implements ComboBoxModel
{
private static final long serialVersionUID = -5100057877489621837L;
protected Object _selectedItem;
/**
* Creates a new instance using a new {@link ArrayList} internally.
*/
public ComboBoxModelAdapter()
{
super();
}
/**
* Creates a new instance wrapping a given {@link List}.
*
* @param internalList
* the {@link List} to use internally for element management
*/
public ComboBoxModelAdapter(final List internalList)
{
super(internalList);
}
@Override
public Object getSelectedItem()
{
return _selectedItem;
}
@Override
public void setSelectedItem(final Object item)
{
if (_selectedItem != null && !_selectedItem.equals(item) || _selectedItem == null && item != null)
{
_selectedItem = item;
fireContentsChanged(this, -1, -1);
}
}
@Override
public ComboBoxModelAdapter subList(final int from, final int to)
{
return new SubListDecorator(this, from, to);
}
@Override
public boolean equals(final Object o)
{
if (!(o instanceof ComboBoxModelAdapter>))
{
return false;
}
final ComboBoxModelAdapter> other = (ComboBoxModelAdapter>) o;
return LangUtils.equalsNullSafe(this._selectedItem, other._selectedItem) && super.equals(o);
}
@Override
public int hashCode()
{
return super.hashCode() + (1 << 15) + (_selectedItem == null ? 0 : _selectedItem.hashCode());
}
// Unfortunately I had to duplicate the code from ListModelListDecorator to
// here
// in order to keep the type ComboBoxModelListDecorator also for the sub
// list.
// Maybe there is a much better way. If you know one, please send me your
// proposal.
protected static class SubListDecorator extends ComboBoxModelAdapter
{
private static final long serialVersionUID = -5301211360041031237L;
protected final int m_iOffset;
protected final ComboBoxModelAdapter m_oParent;
protected SubListDecorator(final ComboBoxModelAdapter oParent, final int iFromIndex, final int iToIndex)
{
super(oParent._internalList.subList(iFromIndex, iToIndex));
m_oParent = oParent;
m_iOffset = iFromIndex;
}
@Override
protected void fireContentsChanged(final Object source, final int index0, final int index1)
{
super.fireContentsChanged(source, index0, index1);
if (source == this && index0 >= 0 && index1 >= 0)
{
m_oParent.fireContentsChanged(m_oParent, m_iOffset + index0, m_iOffset + index1);
}
}
@Override
protected void fireIntervalAdded(final Object source, final int index0, final int index1)
{
super.fireIntervalAdded(source, index0, index1);
if (source == this)
{
m_oParent.fireIntervalAdded(m_oParent, m_iOffset + index0, m_iOffset + index1);
}
}
@Override
protected void fireIntervalRemoved(final Object source, final int index0, final int index1)
{
super.fireIntervalRemoved(source, index0, index1);
if (source == this)
{
m_oParent.fireIntervalRemoved(m_oParent, m_iOffset + index0, m_iOffset + index1);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy