
package turgen.config;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JTextField;

public class StringConfigEntry extends AbstractConfigEntry {
    
    private String defaultStringValue;
    private String[] possibleValues;
    private boolean editable;

    
    public boolean isBlank() {
        String s = getAsString();
        if (s==null || s.trim().equals("")) return true;
        return false;
    }
    
    public StringConfigEntry(String key,String name,String dflt,String[] possible,boolean editable) {
        this.setEntryKey(key);
        defaultStringValue=dflt;
        possibleValues=possible;
        this.name=name;
        this.editable=editable;
    }

    public StringConfigEntry(String key,String name,String dflt,String[] possible) {
        this(key,name,dflt,possible,false);
    }
    
    public StringConfigEntry(String key,String name,String dflt) {
        this(key,name,dflt,null,false);
    }
    
    
    public int getAsInt() {
        int v;
        
        /*Parse int*/
        try {
            v=Integer.parseInt(this.getEntryValue());
        }
        /*Invalid, return zero*/
        catch (Exception e) {
            return 0;
        }
        
        return v;
    }

    public boolean getAsBoolean() {
        boolean b=false;
        String v = this.getAsString();
        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() {
        String s=this.getEntryValue();
        if (s==null) return defaultStringValue;
        return s;
    }

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

    public JComponent createEntryEditor() {
        JComponent retVal=null;
        
        /*Enumeration = JComboBox*/
        if (possibleValues!=null && possibleValues.length>0) {
            JComboBox jc = new JComboBox();
            jc.setEditable(editable);

            int v;
            for(int i=0;i<possibleValues.length;i++) {
                jc.addItem(possibleValues[i]);
            }
            fillEntryEditor(jc);
            retVal=jc;
        }
        
        /*JTextField */
        if (retVal==null) {
            JTextField tf = new JTextField();
            fillEntryEditor(tf);
            retVal=tf;
        }
        
        return retVal;
    }

    public String verifyEntry(JComponent c) {
        
        if (c==null) System.out.println(getEntryKey());
        
        /*JComboBox, allways OK*/
        if (c instanceof JComboBox) return null;
        
        /*In other case, check for null*/
        String st=((JTextField)c).getText().trim();
        if (st==null) return "String is null";
        
        /*OK*/
        return null;
        
    }
    
    
    public synchronized void flushValueFromEditor(JComponent c) {

        
        if (c instanceof JComboBox) {
            JComboBox cb = (JComboBox) c;
            if (cb.getSelectedItem() != null) {
                this.setEntryValue((String) cb.getSelectedItem());
            }
        } else {
            JTextField tf = (JTextField) c;
            this.setEntryValue(tf.getText().trim());
        }
    
        
        
    }

    public void fillEntryEditor(JComponent c) {
        if (c instanceof JComboBox) {
            JComboBox jcb = (JComboBox)c;
            jcb.setSelectedItem(getAsString());
        }
        else {
            JTextField tf = (JTextField)c;
            tf.setText(getAsString());
        }
    }

}
