package cobalt.deployment.ui;

import java.util.Set;
import java.util.Iterator;

import javax.swing.table.DefaultTableModel;

import cobalt.deployment.descriptor.MethodDescriptor;

/**
 * Title:        Package & Deployment Step
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      ETRI
 * @author YeonJun Choi
 * @version 1.0
 */

public class Jeus30MethodTableModel extends DefaultTableModel {

    boolean DEBUG = true;
    void info(String msg) {
        if (DEBUG)
            System.out.println(getClass().getName() + '>' + msg);
    }

    Set methods = null;
    Set selectedMethods = null;

    public Jeus30MethodTableModel(Object[] columnNames, int row) {
        super(columnNames, row);
    }
    public void removeAll() {
        int num = getRowCount() - 1;
        for ( int i=num ; i>=0 ; i--) removeRow(i);
    }
    public void removeAllChecked() {
        if ( selectedMethods == null ) return;
        Iterator iterator = selectedMethods.iterator();
        MethodDescriptor method = null;
        while ( iterator.hasNext() ) {
            method = (MethodDescriptor)iterator.next();
            removeSelectedMethod(method);
        }
        for ( int i=0 ; i<getRowCount() ; i++ ) // uncheck all
            super.setValueAt(new Boolean(false), i, 2);
    }
    public Class getColumnClass(int c) {
      return getValueAt(0, c).getClass();
    }
    public boolean isCellEditable(int row, int column) {
      if (column < 2)
        return false;
      return true;
    }
    public void setValueAt(Object aValue, int row, int column) {
        if ( aValue instanceof Boolean ) {
            if ( ((Boolean)aValue).booleanValue() )
                ;
            else
                removeMethod();
        }
        super.setValueAt(aValue, row, column);
    }
    public void setAllMethods(Set methods, Set selectedMethods) {
        setMethods(methods);
        setSelectedMethods(selectedMethods);
    }
    public void setMethods(Set methods) {
        this.methods = methods;
        organizeMethods();

        //this.selectedMethods = null;

        //if ( this.selectedMethods != null )
        //    checkMethods();
    }
    public void setSelectedMethods(Set methods) {
        this.selectedMethods = selectedMethods;
        if ( this.methods != null )
            checkMethods();
    }
    void organizeMethods() {
      removeAll();
      MethodDescriptor method;
      Iterator iterator = methods.iterator();
      while ( iterator.hasNext() ) {
        method = (MethodDescriptor)iterator.next();
        Object[] entries = method.toArray();
  info("Method : " + method.toString());
          entries[2] = new Boolean(false);
        addRow(entries);
      }
    }
    void checkMethods() {
        removeAllChecked();
        Iterator iterator = methods.iterator();
        MethodDescriptor method = null;
        int i=0;
        while ( iterator.hasNext() ) {
            if ( selectedMethods.contains(iterator.next()) )
                super.setValueAt(new Boolean(true), i, 2);
            i++;
        }
    }
    void removeSelectedMethod(MethodDescriptor method) {
        selectedMethods.remove(method);
    }
}
