org.jdesktop.swingx.combobox.EnumComboBoxModel Maven / Gradle / Ivy
/*
* $Id$
*
* Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
* Santa Clara, California 95054, U.S.A. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.jdesktop.swingx.combobox;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
*
* A ComboBoxModel implementation that safely wraps an Enum. It allows the
* developer to directly use an enum as their model for a combobox without any
* extra work, though the display can can be further customized.
*
*
* Simple Usage
*
*
* The simplest usage is to wrap an enum
inside the
* EnumComboBoxModel
and then set it as the model on the combo
* box. The combo box will then appear on screen with each value in the
* enum
as a value in the combobox.
*
*
* ex:
*
*
*
* enum MyEnum { GoodStuff, BadStuff };
* ...
* JComboBox combo = new JComboBox();
* combo.setModel(new EnumComboBoxModel(MyEnum.class));
*
*
* Type safe access
*
* By using generics and co-variant types you can make accessing elements from
* the model be completely typesafe. ex:
*
*
*
* EnumComboBoxModel<MyEnum> enumModel = new EnumComboBoxModel<MyEnum1>(
* MyEnum1.class);
*
* MyEnum first = enumModel.getElement(0);
*
* MyEnum selected = enumModel.getSelectedItem();
*
*
* Advanced Usage
*
* Since the exact toString()
value of each enum constant may not
* be exactly what you want on screen (the values won't have spaces, for
* example) you can override to toString() method on the values when you declare
* your enum. Thus the display value is localized to the enum and not in your
* GUI code. ex:
*
*
* private enum MyEnum {GoodStuff, BadStuff;
* public String toString() {
* switch(this) {
* case GoodStuff: return "Some Good Stuff";
* case BadStuff: return "Some Bad Stuff";
* }
* return "ERROR";
* }
* };
*
*
* Note: if more than one enum constant returns the same {@code String} via
* {@code toString()}, this model will throw an exception on creation.
*
* @author joshy
* @author Karl Schaefer
*/
public class EnumComboBoxModel> extends ListComboBoxModel {
private static final long serialVersionUID = 2176566393195371004L;
private final Map valueMap;
private final Class enumClass;
/**
* Creates an {@code EnumComboBoxModel} for the enum represent by the
* {@code Class} {@code en}.
*
* @param en
* the enum class type
* @throws IllegalArgumentException
* if the {@code Enum.toString} returns the same value for more
* than one constant
*/
public EnumComboBoxModel(Class en) {
super(new ArrayList(EnumSet.allOf(en)));
//we could size these, probably not worth it; enums are usually small
valueMap = new HashMap();
enumClass = en;
Iterator iter = data.iterator();
while (iter.hasNext()) {
E element = iter.next();
String s = element.toString();
if (valueMap.containsKey(s)) {
throw new IllegalArgumentException(
"multiple constants map to one string value");
}
valueMap.put(s, element);
}
}
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
public void setSelectedItem(Object anItem) {
E input = null;
if (enumClass.isInstance(anItem)) {
input = (E) anItem;
} else {
input = valueMap.get(anItem);
}
if (input != null || anItem == null) {
selected = input;
}
this.fireContentsChanged(this, 0, getSize());
}
/*
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BoxLayout(frame.getContentPane(),BoxLayout.Y_AXIS));
JComboBox combo1 = new JComboBox();
combo1.setModel(new EnumComboBoxModel(MyEnum1.class));
frame.add(combo1);
JComboBox combo2 = new JComboBox();
combo2.setModel(new EnumComboBoxModel(MyEnum2.class));
frame.add(combo2);
EnumComboBoxModel enumModel = new EnumComboBoxModel(MyEnum1.class);
JComboBox combo3 = new JComboBox();
combo3.setModel(enumModel);
frame.add(combo3);
MyEnum1 selected = enumModel.getSelectedItem();
//uncomment to see the ClassCastException
// enumModel.setSelectedItem("Die clown");
frame.pack();
frame.setVisible(true);
}
private enum MyEnum1 {GoodStuff, BadStuff};
private enum MyEnum2 {GoodStuff, BadStuff;
public String toString() {
switch(this) {
case GoodStuff: return "Some Good Stuff";
case BadStuff: return "Some Bad Stuff";
}
return "ERROR";
}
};
*/
}