package osa.attribute.atgrammar.gui.dialogs;

import java.util.*;

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 com.borland.jbcl.layout.*;

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

public class TypesEditingPanel
  extends JPanel
  {
  protected class TypesListSelectionListener
    implements ListSelectionListener
    {
    public void valueChanged(ListSelectionEvent e)
      {
      JList source_list=(JList)e.getSource();
      int selected_index=source_list.getSelectedIndex();
      if(source_list.getSelectedIndex()>-1)
        {
        remove_btn.setEnabled(true);
        rename_btn.setEnabled(true);
        }
      else
        {
        remove_btn.setEnabled(false);
        rename_btn.setEnabled(false);
        }
      }
    }

  protected class AddBtnActionListener
    implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
      {

      }
    }

  protected class RemoveBtnActionListener
    implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
      {

      }
    }

  protected class RenameBtnActionListener
    implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
      {
      AttributeType selected_type=(AttributeType)user_types_list.getSelectedValue();
      if(selected_type.getName().equals("void"))
        {
        }
      }
    }

  protected class TypesListModel
    extends AbstractListModel
    {
    ArrayList list_model;

    public TypesListModel(ArrayList list)
      {
/*      for(int i=0;i<source_list_size;i++)
         {

         }
      this.list_model=list_model;*/
      }

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

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

  JScrollPane user_types_list_scroll_pane = new JScrollPane();
  JPanel buttons_pane = new JPanel();
  GridLayout gridLayout1 = new GridLayout();
  JButton add_btn = new JButton();
  JButton rename_btn = new JButton();
  JButton remove_btn = new JButton();
  JLabel system_types_lbl = new JLabel();
  JList user_types_list = new JList();

  protected AttributeTypeSystem type_system;

  JLabel user_types_lbl = new JLabel();
  JScrollPane system_types_list_scroll_pane = new JScrollPane();
  JList system_types_list = new JList();
  JPanel ok_cancel_panel = new JPanel();
  GridLayout gridLayout2 = new GridLayout();
  JButton ok_button = new JButton();
  JButton cancel_button = new JButton();
  Component component1;
  Component component2;
  GridBagLayout gridBagLayout1 = new GridBagLayout();

  public TypesEditingPanel( AttributeTypeSystem type_system )
    {
    this.type_system=type_system;

    ArrayList all_types=type_system.getTypesList();

    ArrayList user_types=new ArrayList();
    ArrayList system_types=new ArrayList();

    int types_count=all_types.size();
    AttributeType temp_type;
    for(int i=0;i<types_count;i++)
       {
       temp_type=(AttributeType)all_types.get(i);
       if(temp_type.isBuiltIn())
         {
         system_types.add(temp_type);
         }
       else
         {
         user_types.add(temp_type);
         }
       }


    user_types_list.setModel(new TypesListModel(user_types));
    user_types_list.addListSelectionListener(new TypesListSelectionListener());

    system_types_list.setModel(new TypesListModel(system_types));


    add_btn.addActionListener(new AddBtnActionListener());

    remove_btn.addActionListener(new RemoveBtnActionListener());
    rename_btn.addActionListener(new RenameBtnActionListener());

    add_btn.setEnabled(true);
    remove_btn.setEnabled(false);
    rename_btn.setEnabled(false);

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

  private void jbInit()
    throws Exception
    {
    component1 = Box.createHorizontalStrut(8);
    component2 = Box.createHorizontalStrut(8);
    ok_cancel_panel.setLayout(gridLayout2);
    ok_button.setText("OK");
    cancel_button.setText("Cancel");
    gridLayout2.setColumns(4);
    gridLayout2.setHgap(5);
    gridLayout2.setRows(1);
    gridLayout2.setVgap(5);
    buttons_pane.add(add_btn, null);
    buttons_pane.add(remove_btn, null);
    buttons_pane.add(rename_btn, null);
    this.add(ok_cancel_panel,     new GridBagConstraints(0, 5, 1, 1, 1.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(10, 5, 5, 5), 0, 0));
    system_types_list_scroll_pane.getViewport().add(system_types_list, null);
    ok_cancel_panel.add(component1, null);
    ok_cancel_panel.add(ok_button, null);
    ok_cancel_panel.add(cancel_button, null);
    ok_cancel_panel.add(component2, null);
    buttons_pane.setLayout(gridLayout1);
    add_btn.setText("Добавить");
    rename_btn.setText("Переименовать");
    remove_btn.setText("Удалить");
    system_types_lbl.setText("Встроенные типы:");
    gridLayout1.setColumns(3);
    gridLayout1.setHgap(5);
    gridLayout1.setRows(1);
    gridLayout1.setVgap(5);
    user_types_lbl.setText("Пользовательские типы:");
    user_types_list_scroll_pane.getViewport().add(user_types_list, null);

    this.setLayout(gridBagLayout1);
    this.add(system_types_lbl,   new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    this.add(system_types_list_scroll_pane,   new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0
            ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
    this.add(user_types_lbl,   new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    this.add(user_types_list_scroll_pane,   new GridBagConstraints(0, 3, 1, 1, 1.0, 1.0
            ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
    this.add(buttons_pane,     new GridBagConstraints(0, 4, 1, 1, 1.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 10, 5), 0, 0));



    }
}