package osa.attribute.atgrammar.gui.tables.assignments_table;

import javax.swing.*;
import javax.swing.table.*;


import osa.attribute.atgrammar.grammar.*;
import osa.attribute.atgrammar.grammar.symbols.*;


/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class AssignmentsTM
  extends AbstractTableModel
  {

  GrammarProduction production_reference;

  public AssignmentsTM(GrammarProduction production)
    {
    this.production_reference=production;
    }

  public Object getValueAt(int row, int column)
    {
    SimpleAssignment assignment=production_reference.getAssignmentAt(row);

    return assignment;
    }

  public void setValueAt(Object aValue, int rowIndex, int columnIndex)
    {
    SimpleAssignment assignment=(SimpleAssignment)aValue;
    production_reference.setAssignmentAt(rowIndex,assignment);
    }


  public int getColumnCount()
    {
    return 5;
    }

  public int getRowCount()
    {
    if(production_reference!=null)
      {
      return production_reference.getAsslignmentsCount();
      }
    return 0;
    }

  public boolean isCellEditable(int rowIndex, int columnIndex)
    {
    boolean is_editable=false;

    switch(columnIndex)
          {
          case 1:
            is_editable=true;
            break;
          case 3:
            is_editable=true;
            break;
          case 5:
            is_editable=true;
            break;
          case 7:
            is_editable=true;
            break;
          default:
          }
    return is_editable;
    }

  public String getColumnName(int columnIndex)
    {
    String return_value;

    switch(columnIndex)
          {
          case 0:
            return_value="№";
            break;
          case 1:
            return_value="Приёмник";
            break;
          case 2:
            return_value="Атрибут";
            break;
          case 3:
            return_value="Источник";
            break;
          case 4:
            return_value="Атрибут";
            break;
          default:
          return_value="???";
          }
    return return_value;
    }


  public void addRow()
    {
    if(production_reference==null)
      {
      return;
      }
    int rows_count=this.production_reference.getAsslignmentsCount();
    SimpleAssignment simple_assignment=new SimpleAssignment(production_reference);
    production_reference.addAssignment(simple_assignment);
    rows_count++;
    fireTableRowsInserted(rows_count,rows_count);

    System.err.println("Adding row!!!");
    }

  public void removeRow(int row_index)
    {
    if(production_reference==null)
      {
      return;
      }
    if(production_reference.removeAssignmentAt(row_index))
      {
      this.fireTableRowsDeleted(row_index,row_index);
      }
    }
  }