package usda.weru.mcrew;

import com.klg.jclass.cell.JCCellInfo;
import com.klg.jclass.table.JCTable;
import com.klg.jclass.table.JCTableCellInfo;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
import javax.swing.JComboBox;

/**
 *
 * @author mark
 * 
 * These functions are shared by both 
 * KillComboBoxEditor and KillComboBoxRenderer.
 * Put here so that they are coded only once.
 * 
 * SuppressWarnings were added here to stop compiler warnings.
 * Since this methods is calling addItem() from JComboBox and is using an old API 
 * (JCCComboBoxCellEditor and JCComboBoxCellRenderer) there isn't a clean way to 
 * functionally get rid of the warnings you either put the methods in each of
 * the above classes where you would still get unchecked warnings or you keep
 * everything like this and get both "rawtypes" and "unchecked" warnings- EL
 */
public class KillComboBoxSupport {
    
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static void parseChoices (JComboBox combo, Hashtable<String,String> choices, 
                                               ArrayList<String> names) {
        if (choices != null) {
            Set<String> choiceKeys = choices.keySet();
            Iterator<String> iterator = choiceKeys.iterator();
            while (iterator.hasNext()) {
                String key = iterator.next();
                names.add(choices.get(key));
                combo.addItem(choices.get(key));
            }
        }
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    public static boolean setComboItems (JComboBox combo, JCCellInfo cellInfo,
                                  Hashtable<String,String> choices, ArrayList<String> names) {
        boolean haskilflag;
        Object currentVal = "";
        
        haskilflag = false;
        if(cellInfo instanceof JCTableCellInfo){
            JCTableCellInfo info = (JCTableCellInfo) cellInfo;
            int row = info.getRow();
            JCTable tbl = info.getTable();
            Object d = tbl.getUserData(row,1);
            if (d instanceof OperationObject) {
                OperationObject oo = (OperationObject) d;
                Identity i = new Identity(31, "P");
                boolean isP31 = oo.getAllIds().contains(i);
                Object val = oo.getValue("kilflag");
                if (val != null) {
                    currentVal = val;
                    haskilflag = true;
                }
             }
            combo.setFocusable(false);
        }
        if (haskilflag) {
           
            combo.removeAllItems();
           
            for (int i=0; i<names.size(); i++) {
                combo.addItem(names.get(i));
            }
            // currentVal is the key value, not the
            // Combo text, so get the text value
            // and select.
            String item = choices.get(currentVal);
            combo.setSelectedItem(item);
            combo.setEnabled(true);
           
        } else {
            combo.removeAllItems();
            Object obj = "";
            combo.addItem(obj);
            combo.setSelectedItem(obj);
            combo.setEnabled(false);
        }
        return haskilflag;
    }
    
}
