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

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

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

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

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: </p>
 * <p>Company: </p>
 * @author not attributable
 * @version
 */

public class AttributesPane
  extends JPanel
  {

  protected class AddNewBtnActionListener
    implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
      {
      ((AttributesTM)attributes_table.getModel()).addRow();



      Grammar.getInstance().updateProductions();
      Grammar.getInstance().updateAssignmentsLabels();
      Grammar.getInstance().updateAssignmentsIndexes();




      attributes_table.repaint();
      }
    }

  protected class RemoveBtnActionListener
    implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
      {
      int selected_row=attributes_table.getSelectedRow();
      if(selected_row>-1)
        {
        ((AttributesTM)attributes_table.getModel()).removeRow(selected_row);

        Grammar.getInstance().updateProductions();
//        Grammar.getInstance().updateAssignmentsLabels();
        Grammar.getInstance().updateAssignmentsIndexes();



        attributes_table.repaint();
        }
      }
    }


  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);
          }
        }
      }



  JButton add_new_btn = new JButton();
  JButton remove_btn = new JButton();
  JButton types_btn = new JButton();
  JPanel jPanel1 = new JPanel();
  GridLayout gridLayout1 = new GridLayout();
  JScrollPane attributes_table_scroll_pane = new JScrollPane();
  BorderLayout borderLayout1 = new BorderLayout();

  private Symbol expanded_symbol=null;

  private AttributesTable attributes_table;

  public AttributesPane()
    {
    attributes_table=new AttributesTable();

    add_new_btn.addActionListener(new AddNewBtnActionListener());
    remove_btn.addActionListener(new RemoveBtnActionListener());

    add_new_btn.setEnabled(false);
    remove_btn.setEnabled(false);

    try{
       jbInit();
       }
    catch(Exception e)
       {
       e.printStackTrace();
       }
    }

  public void setSymbolToView(Symbol symbol)
    {
    if(symbol==null)
      {
      add_new_btn.setEnabled(false);
      }
    else
      {
      add_new_btn.setEnabled(true);
      }

    expanded_symbol=symbol;
    attributes_table.setModel(new AttributesTM(expanded_symbol));
    attributes_table.getColumnModel().getColumn(0).setPreferredWidth(5);
    attributes_table.getSelectionModel().addListSelectionListener(new TableSelectionListener());
    }

  private void jbInit() throws Exception
    {
    attributes_table=new AttributesTable();
    attributes_table.setModel(new AttributesTM(expanded_symbol));
    attributes_table.getColumnModel().getColumn(0).setPreferredWidth(5);
    attributes_table_scroll_pane.setViewportView(attributes_table);



    this.setLayout(borderLayout1);
    add_new_btn.setText("Добавить");
    remove_btn.setText("Удалить");
    types_btn.setSelected(false);
    types_btn.setText("Типы...");
    jPanel1.setLayout(gridLayout1);
    gridLayout1.setColumns(4);
    gridLayout1.setHgap(5);
    gridLayout1.setRows(1);
    gridLayout1.setVgap(5);
    this.add(jPanel1,  BorderLayout.SOUTH);
    jPanel1.add(add_new_btn, null);
    jPanel1.add(remove_btn, null);
    jPanel1.add(types_btn, null);
    this.add(attributes_table_scroll_pane, BorderLayout.CENTER);
    }

  public void updateAssignmentsPane()
    {
    getATGrammarPane().getAssignmentsTablePane().repaint();
    }

  ATGrammarPane at_grammar_pane=null;
  protected ATGrammarPane getATGrammarPane()
    {
    if(at_grammar_pane==null)
      {
      at_grammar_pane=ATGrammarTabPane.getInstance().getATGrammarPane();
      }
    return at_grammar_pane;
    }



  }