001 /** 002 * EnumEditor.java 003 * jCOLIBRI2 framework. 004 * @author Juan A. Recio-García. 005 * GAIA - Group for Artificial Intelligence Applications 006 * http://gaia.fdi.ucm.es 007 * 02/11/2007 008 */ 009 package jcolibri.method.gui.editors; 010 011 012 import java.util.ArrayList; 013 import java.util.Collection; 014 import java.util.List; 015 016 import javax.swing.JComboBox; 017 import javax.swing.JComponent; 018 019 020 /** 021 * Parameter Editor for Enum values. 022 * 023 * @author Juan A. Recio-Garcia 024 * @version 1.0 025 * @see jcolibri.method.gui.editors.ParameterEditor 026 */ 027 public class EnumEditor extends JComboBox implements 028 ParameterEditor { 029 private static final long serialVersionUID = 1L; 030 031 032 private final String EMPTY = "<empty>"; 033 034 /** 035 * Creates a new instance 036 */ 037 public EnumEditor(Class enumeration) 038 { 039 super(); 040 Object[] constants = enumeration.getEnumConstants(); 041 this.addItem(EMPTY); 042 for(int i=0; i<constants.length; i++) 043 this.addItem(constants[i]); 044 } 045 046 047 /** 048 * Returns an Enum value object 049 */ 050 public Object getEditorValue() { 051 Object value = this.getSelectedItem(); 052 if (value.equals(EMPTY)) 053 return null; 054 return value; 055 } 056 057 /** 058 * Returns the JComponent 059 */ 060 public JComponent getJComponent() { 061 return this; 062 } 063 064 /** 065 * Receives a Boolean value 066 */ 067 public void setEditorValue(Object value) { 068 if(value==null) 069 this.setSelectedItem(EMPTY); 070 else 071 this.setSelectedItem(value); 072 } 073 074 075 /** 076 * Receives a list of Enum values 077 */ 078 @SuppressWarnings("unchecked") 079 public void setAllowedValues(Collection<Object> allowedValues) 080 { 081 this.removeAllItems(); 082 List<Enum> list = new ArrayList<Enum>(); 083 for(Object o: allowedValues) 084 list.add((Enum)o); 085 java.util.Collections.sort(list); 086 for(Enum e: list) 087 this.addItem(e); 088 } 089 090 }