
package turgen.config;

import javax.swing.JCheckBox;
import javax.swing.JComponent;


public class BooleanConfigEntry extends AbstractConfigEntry {
    
    private final boolean defaultBooleanValue;
    
    
    
    public BooleanConfigEntry(String key,String name,boolean dflt) {
        this.setEntryKey(key);
        this.name=name;
        defaultBooleanValue=dflt;
    }
    
    
    public int getAsInt() {
        return (getAsBoolean()==true)? 1 : 0;
    }

    public boolean getAsBoolean() {
        boolean b=defaultBooleanValue;
        String v = this.getEntryValue();
        if (v!=null) {
            if (v.equals("1") || v.equalsIgnoreCase("YES") || v.equalsIgnoreCase("TRUE")) return true;
            if (v.equals("0") || v.equalsIgnoreCase("NO") || v.equalsIgnoreCase("FALSE")) return false;
        }
        return b;
    }

    public String getAsString() {
        return this.getEntryValue();
    }

    public double getAsDouble() {
        return (double)getAsInt();
    }

    public JComponent createEntryEditor() {
        
        JCheckBox jcb = new JCheckBox();
        fillEntryEditor(jcb);
        return jcb;
    }

    public void flushValueFromEditor(JComponent c) {
        JCheckBox jc = (JCheckBox)c;
        if (jc.isSelected()==true) setEntryValue("true"); else setEntryValue("false");
    }

    public String verifyEntry(JComponent c) {
        
        return null;
    }

    public void fillEntryEditor(JComponent c) {
        JCheckBox jcb = (JCheckBox)c;
        jcb.setSelected(getAsBoolean());
    }

}
