package osa.attribute.atgrammar.gui.panels.atgrammar_panels;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

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

import osa.attribute.atgrammar.gui.tables.assignments_table.*;

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

public class AssignmentsTablePane
  extends JPanel
  {

  protected class TableSelectionListener
    implements ListSelectionListener
    {
    public void valueChanged(ListSelectionEvent e)
      {
      DefaultListSelectionModel source=(DefaultListSelectionModel)e.getSource();

      int selected_index=source.getMaxSelectionIndex();

      if(selected_index>-1)
        {
        remove_btn.setEnabled(true);
        }
      else
        {
        remove_btn.setEnabled(false);
        }
      }
    }

  protected class AddBtnActionListener
    implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
      {
      AssignmentsTM model=(AssignmentsTM)assignments_table.getModel();
      model.addRow();
      assignments_table.repaint();
      }
    }

  protected class RemoveBtnActionListener
    implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
      {
      AssignmentsTM model=(AssignmentsTM)assignments_table.getModel();
      int selected_row_index=assignments_table.getSelectedRow();
      model.removeRow(selected_row_index);
      assignments_table.repaint();
      }
     }


  protected AssignmentsTable assignments_table;

  protected GrammarProduction production;

  BorderLayout borderLayout1 = new BorderLayout(5,5);
  JScrollPane table_scroll_pane = new JScrollPane();
  JPanel buttons_panel = new JPanel();
  GridLayout gridLayout1 = new GridLayout();
  Component component1;
  Component component2;
  JButton remove_btn = new JButton();
  JButton add_btn = new JButton();

  public void setGrammarProductionToView(GrammarProduction production)
    {
    this.production=production;
    assignments_table.setModel(new AssignmentsTM(production));
    }


  public AssignmentsTablePane()
    {
    assignments_table=new AssignmentsTable();
    assignments_table.setModel(new AssignmentsTM(null));
    table_scroll_pane.setViewportView(assignments_table);

    assignments_table.getSelectionModel().addListSelectionListener(new TableSelectionListener());

    add_btn.addActionListener(new AddBtnActionListener());

    remove_btn.addActionListener(new RemoveBtnActionListener());
    remove_btn.setEnabled(false);

    try{
       jbInit();
       }
    catch(Exception e)
      {
      e.printStackTrace();
      }
    }
  private void jbInit() throws Exception
    {
    component1 = Box.createHorizontalStrut(8);
    component2 = Box.createHorizontalStrut(8);
    this.setLayout(borderLayout1);
    buttons_panel.setLayout(gridLayout1);
    gridLayout1.setColumns(4);
    gridLayout1.setHgap(5);
    gridLayout1.setRows(1);
    gridLayout1.setVgap(5);
    remove_btn.setText("Удалить");
    add_btn.setText("Добавить");
    this.add(table_scroll_pane, BorderLayout.CENTER);
    this.add(buttons_panel, BorderLayout.SOUTH);
    buttons_panel.add(component1, null);
    buttons_panel.add(component2, null);
    buttons_panel.add(add_btn, null);
    buttons_panel.add(remove_btn, null);
    }
  }