001/**
002 * BooleanEditor.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 */
009package es.ucm.fdi.gaia.jcolibri.method.gui.editors;
010
011import java.util.Collection;
012
013import javax.swing.JComboBox;
014import javax.swing.JComponent;
015
016/**
017 * Parameter Editor for Boolean values.
018 * 
019 * @author Juan A. Recio-Garcia
020 * @version 1.0
021 * @see ParameterEditor
022 */
023public class BooleanEditor extends JComboBox<Object> implements ParameterEditor {
024        private static final long serialVersionUID = 1L;
025        
026
027        private final String ANY   = "<any>";
028        private final String TRUE  = "true";
029        private final String FALSE = "false";
030        
031        /**
032         *  Creates a new instance
033         */
034        public BooleanEditor() {
035                super();
036                addItem(ANY);
037                addItem(TRUE);
038                addItem(FALSE);
039        }
040
041        /**
042         * Returns a Boolean object
043         */
044        public Object getEditorValue() {
045                if(getSelectedItem().equals(ANY))
046                    return null;
047                if(getSelectedItem().equals(TRUE))
048                    return true;
049                else
050                    return false;
051        }
052
053        /**
054         * Returns the JComponent
055         */
056        public JComponent getJComponent() {
057                return (JComponent) this;
058        }
059
060        /**
061         * Receives a Boolean value
062         */
063        public void setEditorValue(Object value) {
064                if(value == null)
065                    setSelectedItem(ANY);
066                if (!(value instanceof Boolean))
067                        return;
068                Boolean bvalue = (Boolean) value;
069                if(bvalue.booleanValue())
070                    setSelectedItem(TRUE);
071                else
072                    setSelectedItem(FALSE);
073
074        }
075
076        /**
077         * Receives a Collection of Boolean objects.
078         */
079        public void setAllowedValues(Collection<Object> allowedValues)
080        {
081            boolean containTrue = false;
082            boolean containFalse = false;
083            for(Object o: allowedValues)
084            {
085                Boolean bool = (Boolean)o;
086                if(!containTrue)
087                    containTrue = bool.booleanValue();
088                if(!containFalse)
089                    containFalse = !bool.booleanValue();
090            }
091            if(!containTrue)
092                removeItem(TRUE);
093            if(!containFalse)
094                removeItem(FALSE);
095        }
096}