package osa.attribute.atgrammar.gui.panels;

import java.util.*;

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


import osa.attribute.atgrammar.grammar.*;
import osa.attribute.atgrammar.grammar.symbols.*;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: </p>
 * <p>Company: </p>
 * @author not attributable
 * @version
 */

public class DictionaryPanel
  extends JPanel
  {

  protected class SymbolsListModel
    extends AbstractListModel
    {
    ArrayList list_model;

    public SymbolsListModel(Collection dictionary)
      {
      list_model=new ArrayList(dictionary);
      }

    public int getSize()
      {
      return list_model.size();
      }

    public Object getElementAt(int index)
      {
      return list_model.get(index);
      }
    }

  protected class SymbolsListRenderer
    implements ListCellRenderer
    {
    public Component getListCellRendererComponent
      (
      JList list,
      Object value,
      int index,
      boolean isSelected,
      boolean cellHasFocus)
      {
      JLabel result_component=new JLabel("");

      if(isSelected)
        {
        result_component.setForeground(Color.WHITE);
        result_component.setBackground(Color.BLUE);
        }
      else
        {
        result_component.setForeground(Color.BLACK);
        result_component.setBackground(Color.WHITE);
        }

      if( value!=null && value instanceof Symbol)
        {
        result_component.setText(((Symbol)value).getOriginalName());
        }

      return result_component;
      }
    }

  // Lists
  JList non_terminals_lst = new JList();
  JList terminals_lst = new JList();
  JList outputs_lst = new JList();

  // Scroll panes
  JScrollPane non_terminals_scrlpn=new JScrollPane();
  JScrollPane terminals_scrlpn=new JScrollPane();
  JScrollPane outputs_scrlpn=new JScrollPane();

  // Buttons
  JButton add_new_btn = new JButton();
  JButton remove_btn = new JButton();
  JButton rename_btn = new JButton();
  JButton properties_btn = new JButton();
  JButton set_evaluator_btn = new JButton();

  // Labels
  JLabel non_terminals_lbl = new JLabel();
  JLabel terminals_lbl = new JLabel();
  JLabel evaluator_class_lbl = new JLabel();
  JLabel outputs_lbl = new JLabel();

  // panels
  JPanel buttons_panel = new JPanel();
  JPanel jPanel1 = new JPanel();


  // Layouts
  GridLayout gridLayout1 = new GridLayout();
  GridBagLayout gridBagLayout1 = new GridBagLayout();
  GridBagLayout gridBagLayout2 = new GridBagLayout();

  // Text fields
  JTextField evaluator_class_txfld = new JTextField();


  // panel model

  CommonDictionary dictionary_reference;


  public void revalidate()
    {
    Collection terminals = dictionary_reference.getTerminals();
    Collection non_terminals = dictionary_reference.getNonTerminals();
    Collection outputs = dictionary_reference.getOutputs();

    terminals_lst.setModel(new SymbolsListModel(terminals));
    non_terminals_lst.setModel(new SymbolsListModel(non_terminals));
    outputs_lst.setModel(new SymbolsListModel(terminals));


    super.revalidate();
    }



  public DictionaryPanel(CommonDictionary dictionary_reference)
    {
    SymbolsListRenderer list_renderer=new SymbolsListRenderer();

    non_terminals_lst.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    terminals_lst.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    outputs_lst.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    non_terminals_lst.setCellRenderer(list_renderer);
    terminals_lst.setCellRenderer(list_renderer);
    outputs_lst.setCellRenderer(list_renderer);


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

    }

  private void jbInit()
    throws Exception
    {
    this.setLayout(gridBagLayout1);
    add_new_btn.setText("Добавить");
    remove_btn.setText("Удалить");
    rename_btn.setText("Переименовать");
    properties_btn.setText("Свойства");
    non_terminals_lbl.setText("Нетерминалы:");
    terminals_lbl.setText("Терминалы:");
    outputs_lbl.setText("Выходные:");
    buttons_panel.setLayout(gridLayout1);
    gridLayout1.setColumns(4);
    gridLayout1.setHgap(5);
    gridLayout1.setRows(1);
    gridLayout1.setVgap(5);
    evaluator_class_lbl.setText("Класс-обработчик символа:");
    jPanel1.setLayout(gridBagLayout2);
    evaluator_class_txfld.setText("");
    set_evaluator_btn.setText("Задать...");

    non_terminals_scrlpn.setViewportView(non_terminals_lst);
    terminals_scrlpn.setViewportView(terminals_lst);
    outputs_scrlpn.setViewportView(outputs_lst);

    this.add(buttons_panel,     new GridBagConstraints(0, 2, 3, 1, 1.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    buttons_panel.add(add_new_btn, null);
    buttons_panel.add(remove_btn, null);
    buttons_panel.add(rename_btn, null);
    buttons_panel.add(properties_btn, null);

    this.add(non_terminals_scrlpn,    new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0
            ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
    this.add(outputs_scrlpn,    new GridBagConstraints(2, 1, 1, 1, 1.0, 1.0
            ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
    this.add(terminals_scrlpn,     new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0
            ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
    this.add(outputs_lbl,     new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    this.add(terminals_lbl,     new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
    this.add(non_terminals_lbl,    new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
    this.add(jPanel1,        new GridBagConstraints(0, 3, 3, 1, 1.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    jPanel1.add(evaluator_class_txfld,              new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    jPanel1.add(evaluator_class_lbl,    new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.VERTICAL, new Insets(5, 5, 0, 5), 0, 0));
    jPanel1.add(set_evaluator_btn, new GridBagConstraints(1, 1, 1, 1, 0.1, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
  }
  }