
package turgen.config;


import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JSpinner;

import javax.swing.SpinnerNumberModel;


public class IntegerConfigEntry extends AbstractConfigEntry {
    
    private int defaultIntValue;
    private int lowest;
    private int highest;
    private int[] possibleValues;
    
    
    
    public IntegerConfigEntry(String key,String name,int dflt,int lo,int hi,int[] possible) {
        this.setEntryKey(key);
        defaultIntValue=dflt;
        lowest=lo;
        highest=hi;
        if (possible!=null) {
            possibleValues=(int[])possible.clone();
        }
        else possibleValues=null;
        this.name=name;
        
    }
    
    public IntegerConfigEntry(String key,String name,int dflt) {
        this(key,name,dflt,Integer.MIN_VALUE,Integer.MAX_VALUE,null);
    }
    
    public IntegerConfigEntry(String key,String name, int[] possible, int defaultIndex) {
        this(key,name,possible[defaultIndex],Integer.MIN_VALUE,Integer.MAX_VALUE,possible);
    } 
    
    public IntegerConfigEntry(String key,String name, int dflt,int lo,int hi) {
        this(key,name,dflt,lo,hi,null);
    }
    
    
    public int getAsInt() {
        
        int v;
        
        /*Parse int*/
        try {
            v=Integer.parseInt(this.getEntryValue());
        }
        /*If not integer, default value is returned*/
        catch (Exception e) {
            v=defaultIntValue;
            return v;
        }
        
        /*Range check*/
        if (v<lowest || v>highest ) return defaultIntValue;
        
        /*Enumeration ?*/
        if (possibleValues==null) return v;
        
        for(int k=0;k<possibleValues.length;k++) {
            if (possibleValues[k]==v) return v;
        }
        
        return defaultIntValue;
        
    }

    public boolean getAsBoolean() {
        if (getAsInt()==0) return false;else return true;
    }

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

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

    
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("ICE: ");
        sb.append(this.getEntryKey());
        sb.append('%');
        sb.append(defaultIntValue);sb.append(',');
        sb.append(lowest);sb.append(',');
        sb.append(highest);sb.append(',');
        sb.append(possibleValues);sb.append(',');
        sb.append("CurrentValue: ");
        sb.append(getAsInt());
        return sb.toString();
    }
    
    public JComponent createEntryEditor() {
        
        JComponent retVal=null;
        
        /*Enumeration*/
        if (possibleValues!=null && possibleValues.length>0) {
            JComboBox jc = new JComboBox();
            int v;
            for(int i=0;i<possibleValues.length;i++) {
                v=possibleValues[i];
                /*Must be in interval*/
                if (v>=lowest && v<=highest) jc.addItem(Integer.toString(possibleValues[i]));
            }
            fillEntryEditor(jc);
            retVal=jc;
        }
        
        /*Spinner*/
        if (retVal==null) {
            JSpinner spin = new JSpinner();
            
            SpinnerNumberModel model = new SpinnerNumberModel();
            model.setMaximum(new Integer(highest));
            model.setMinimum(new Integer(lowest));
            model.setStepSize(new Integer(1));
            spin.setModel(model);
            spin.setEditor(new JSpinner.NumberEditor(spin,"#"));
            fillEntryEditor(spin);
            
            retVal=spin;
        }
        
        return retVal;
    }
    
    public String verifyEntry(JComponent c) {

        /*Enumeration = always OK*/
        if (c instanceof JComboBox) return null;

        /*Is a number in the spinner*/
        int val=0;
        try {
            val=((Integer)(((JSpinner)c).getValue())).intValue();
        }
        catch (Exception e) {
            return "Not integer decimal";
        }
        /*Range check*/
        if (val>=lowest && val<=highest) return null;
        return "Number out of interval range: |"+Integer.toString(lowest)+","+Integer.toString(highest)+"|";
    }
    
    
    public void flushValueFromEditor(JComponent c) {
      
        
        if (c instanceof JComboBox) {
            JComboBox cb = (JComboBox) c;
            if (cb.getSelectedItem()!=null ) {
                this.setEntryValue((String) cb.getSelectedItem());
            }
        } else {
            JSpinner spin = (JSpinner)c;
            Integer ci = (Integer)spin.getValue();
            this.setEntryValue(ci.toString());
        }
        
    }

    public void fillEntryEditor(JComponent c) {
        if (c instanceof JComboBox) {
            JComboBox jcb = (JComboBox)c;
            jcb.setSelectedItem(Integer.toString(getAsInt()));
        }
        else {
            JSpinner s = (JSpinner)c;
            s.setValue(new Integer(getAsInt()));
        }
    }

}
