package osa;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;

public class Tab6
{
  private MainFrame tFrame;
  private BorderLayout tBorderLayout = new BorderLayout();

  private Action aFileNewProject;
  private Action aFileOpenProject;
  private Action aFileSaveProject;
  private Action aFileSaveProjectAs;
  private Action aFileExit;
  private Action aFunctionsNew;
  private Action aFunctionsEdit;
  private Action aFunctionsDelete;
  private Action aHelpHelpTopics;
  private Action aHelpAbout;

  private JLabel ErrorLabel = new JLabel();
  private JPanel ErrorPanel = new JPanel();
  private JLabel ErrorTLabel = new JLabel();
  private JTextArea ErrorTextArea = new JTextArea();
  private JScrollPane ErrorScrollPane = new JScrollPane();
  private JLabel ErrorBLabel = new JLabel();

  private JPanel t6Panel = new JPanel();
  private GridLayout tGridLayout = new GridLayout(2,1);
  private JPanel t6tPanel = new JPanel();
  private BorderLayout t6tBorderLayout = new BorderLayout();
  private JLabel t6tTLabel = new JLabel();
  public  JList t6List = new JList();
  private JScrollPane t6tScrollPane = new JScrollPane();
  private JLabel t6tBLabel = new JLabel();
  private JPanel t6bPanel = new JPanel();
  private BorderLayout t6bBorderLayout = new BorderLayout();
  private JLabel t6bTLabel = new JLabel();
  private JTextArea t6TextArea = new JTextArea();
  private JScrollPane t6bScrollPane = new JScrollPane();

  public Tab6(MainFrame pMainFrame)
  {
    tFrame=pMainFrame;
    tFrame.mPanel6.setLayout(tBorderLayout);

    aFileNewProject = new AbstractAction("New Project",new ImageIcon(Tab6.class.getResource("new.gif")))
    {{ putValue(Action.SHORT_DESCRIPTION,"New Project"); }
    // putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_N));
    public void actionPerformed(ActionEvent e)
    { tFrame.FileNewProject(); }};
    aFileOpenProject = new AbstractAction("Open Project...",new ImageIcon(Tab6.class.getResource("open.gif")))
    {{ putValue(Action.SHORT_DESCRIPTION,"Open Project..."); }
    // putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_O));
    public void actionPerformed(ActionEvent e)
    { tFrame.FileOpenProject(); }};
    aFileSaveProject = new AbstractAction("Save Project",new ImageIcon(Tab6.class.getResource("save.gif")))
    {{ putValue(Action.SHORT_DESCRIPTION,"Save Project"); }
    // putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_S));
    public void actionPerformed(ActionEvent e)
    { tFrame.FileSaveProject(); }};
    aFileSaveProjectAs = new AbstractAction("Save Project As...",new ImageIcon(Tab6.class.getResource("saveas.gif")))
    {{ putValue(Action.SHORT_DESCRIPTION,"Save Project As..."); }
    // putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_A));
    public void actionPerformed(ActionEvent e)
    { tFrame.FileSaveProjectAs(); }};
    aFileExit = new AbstractAction("Exit")
    {{ putValue(Action.SHORT_DESCRIPTION,"Exit"); }
    // putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_X));
    public void actionPerformed(ActionEvent e)
    { tFrame.FileExit(); }};
    aFunctionsNew = new AbstractAction("New",new ImageIcon(Tab6.class.getResource("create.gif")))
    {{ putValue(Action.SHORT_DESCRIPTION,"New"); }
    // putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_N));
    public void actionPerformed(ActionEvent e)
    { tFrame.isNew=true; tFrame.OpenBottom(0x6A); }};
    aFunctionsEdit = new AbstractAction("Edit",new ImageIcon(Tab6.class.getResource("edit.gif")))
    {{ putValue(Action.SHORT_DESCRIPTION,"Edit"); }
    // putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_E));
    public void actionPerformed(ActionEvent e)
    { tFrame.isNew=false; tFrame.OpenBottom(0x6A); }};
    aFunctionsDelete = new AbstractAction("Delete",new ImageIcon(Tab6.class.getResource("delete.gif")))
    {{ putValue(Action.SHORT_DESCRIPTION,"Delete"); }
    // putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_D));
    public void actionPerformed(ActionEvent e)
    { int i=tFrame.CurrentFunctionIndex,j;
      if((i>=0)&&(i<tFrame.alFunctionName.size())&&(i<tFrame.alFunctionText.size()))
      { tFrame.alFunctionName.remove(i); tFrame.alFunctionText.remove(i); t6SetListData();
        tFrame.reSaveFunctions=true; j=tFrame.alFunctionName.size(); if(i>=j) i=j-1;
        if(i>=0) { t6List.setSelectedIndex(i); t6List.ensureIndexIsVisible(i); }
      } }};
    aHelpHelpTopics = new AbstractAction("Help Topics",new ImageIcon(Tab6.class.getResource("help.gif")))
    {{ putValue(Action.SHORT_DESCRIPTION,"Help Topics"); }
    // putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_H));
    public void actionPerformed(ActionEvent e)
    { tFrame.HelpHelpTopics(0x6); }};
    aHelpAbout = new AbstractAction("About...")
    {{ putValue(Action.SHORT_DESCRIPTION,"About..."); }
    // putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_A));
    public void actionPerformed(ActionEvent e)
    { tFrame.HelpAbout(); }};

    ErrorLabel.setFont(tFrame.mFont);
    ErrorLabel.setForeground(Color.red);
    ErrorLabel.setHorizontalAlignment(JLabel.CENTER);
    ErrorPanel.setLayout(new BorderLayout());
    ErrorTLabel.setFont(tFrame.mFont);
    ErrorTLabel.setText("  Êîíôëèêòû Ãðàììàòèêè:");
    ErrorPanel.add(ErrorTLabel,BorderLayout.NORTH);
    ErrorTextArea.setFont(tFrame.mFont);
    ErrorTextArea.setTabSize(0x08);
    ErrorTextArea.setEditable(false);
    ErrorTextArea.getCaret().setVisible(false);
    ErrorTextArea.getCaret().setSelectionVisible(true);
    ErrorScrollPane.setViewportView(ErrorTextArea);
    ErrorPanel.add(ErrorScrollPane,BorderLayout.CENTER);
    ErrorBLabel.setFont(tFrame.mFont);
    ErrorBLabel.setText("  ");
    ErrorPanel.add(ErrorBLabel,BorderLayout.SOUTH);

    t6Panel.setLayout(tGridLayout);
    t6Panel.add(t6tPanel);
    t6Panel.add(t6bPanel);

    t6tPanel.setLayout(t6tBorderLayout);
    t6tTLabel.setFont(tFrame.mFont);
    t6tTLabel.setText("  Èìåíà Íàáîðîâ Ôóíêöèé Ïåðåõîäîâ:");
    t6tPanel.add(t6tTLabel,BorderLayout.NORTH);
    t6List.setFont(tFrame.mFont);
    t6List.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    t6List.addListSelectionListener(new ListSelectionListener()
    { public void valueChanged(ListSelectionEvent e) {
      t6ListMenuUpdate(); t6TextArea.selectAll(); t6TextArea.replaceSelection("");
      tFrame.CurrentFunctionIndex=t6List.getSelectedIndex();
      if((tFrame.CurrentFunctionIndex>=0)&&(tFrame.CurrentFunctionIndex<tFrame.alFunctionText.size()))
      { t6TextArea.selectAll(); t6TextArea.replaceSelection(tFrame.ArrayListToString
        ((ArrayList)tFrame.alFunctionText.get(tFrame.CurrentFunctionIndex)));
        t6TextArea.setCaretPosition(0);
      } tFrame.reTranslate=true; tFrame.TranslatorUpdate(5); }});
    t6tScrollPane.setViewportView(t6List);
    t6tPanel.add(t6tScrollPane,BorderLayout.CENTER);
    t6tBLabel.setFont(tFrame.mFont);
    t6tBLabel.setText("  ");
    t6tPanel.add(t6tBLabel,BorderLayout.SOUTH);

    t6bPanel.setLayout(t6bBorderLayout);
    t6bTLabel.setFont(tFrame.mFont);
    t6bTLabel.setText("  Áûñòðûé Ïðîñìîòð:");
    t6bPanel.add(t6bTLabel,BorderLayout.NORTH);
    t6TextArea.setFont(tFrame.mFont);
    t6TextArea.setTabSize(0x08);
    t6TextArea.setEditable(false);
    t6TextArea.getCaret().setVisible(false);
    t6TextArea.getCaret().setSelectionVisible(true);
    t6bScrollPane.setViewportView(t6TextArea);
    t6bPanel.add(t6bScrollPane,BorderLayout.CENTER);

    tFrame.reSaveFunctions=false;
    tFrame.CurrentFunctionIndex=-1;
    t6SetListData();
  }

  public boolean SaveFunction(String name,String s,ArrayList al)
  { boolean r=false; int i,j=-1,k=-1,q=tFrame.alFunctionName.size(),op;
    if(q==tFrame.alFunctionText.size())
    { r=true; for(i=0;i<q;++i) {
        if(s.equals((String)tFrame.alFunctionName.get(i))) j=i;
        if(s.compareTo((String)tFrame.alFunctionName.get(i))>0) k=i; } ++k;
      if(j<0) { tFrame.alFunctionName.add(k,s); tFrame.alFunctionText.add(k,al); }
      else { k=j; if(!name.equals(s)) {
        op=JOptionPane.showConfirmDialog(tFrame,
          s+" already exists.\nDo you want to replace it?","Save",
          JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
        if(op!=JOptionPane.YES_OPTION) r=false; }
        if(r) { tFrame.alFunctionText.remove(j); tFrame.alFunctionText.add(j,al); } }
      if(r) { t6SetListData();
        t6List.setSelectedIndex(k); t6List.ensureIndexIsVisible(k); }
    } return(r);
  }

  public void t6SetListData()
  { int i,q=tFrame.alFunctionName.size();
    Object o[] = new Object[q];
    for(i=0;i<q;++i) o[i]=tFrame.alFunctionName.get(i);
    t6List.setListData(o);
    t6ListMenuUpdate();
    t6TextArea.selectAll(); t6TextArea.replaceSelection("");
    t6tBLabel.setText("  Êîëè÷åñòâî Íàáîðîâ Ôóíêöèé Ïåðåõîäîâ: "+q);
  }

  private void t6ListMenuUpdate()
  { int i=t6List.getSelectedIndex();
    if((i>=0)&&(i<tFrame.alFunctionName.size())&&(i<tFrame.alFunctionText.size()))
    { aFunctionsEdit.setEnabled(true); aFunctionsDelete.setEnabled(true); }
    else { aFunctionsEdit.setEnabled(false); aFunctionsDelete.setEnabled(false); }
    if(tFrame.isBottomComponentActive)
    { aFunctionsNew.setEnabled(false); }
    else { aFunctionsNew.setEnabled(true); }
  }

  public void tDeSelect()
  { if(!tFrame.isBottomComponentActive)
    { tFrame.mMenuBar.removeAll();
      tFrame.mToolBar.removeAll(); }
    tFrame.mPanel6.removeAll();
    tFrame.validate(); }

  public void tSelect()
  { boolean Error=false; if(tFrame.reAnalyse) tFrame.Analyse();
    switch(tFrame.AnalyseReturn)
    { case 0: ErrorLabel.setText("ÑÎÑÒÎßÍÈÅ ÃÐÀÌÌÀÒÈÊÈ ÎÏÐÅÄÅËÈÒÜ ÍÅ ÓÄÀËÎÑÜ");
        tFrame.mPanel6.add(ErrorLabel,BorderLayout.NORTH); Error=true; break;
      case 1: ErrorLabel.setText("Â ÃÐÀÌÌÀÒÈÊÅ ÎÁÍÀÐÓÆÅÍÛ ÑÈÍÒÀÊÑÈ×ÅÑÊÈÅ ÎØÈÁÊÈ");
        tFrame.mPanel6.add(ErrorLabel,BorderLayout.NORTH); Error=true; break;
      case 2: ErrorLabel.setText("ÃÐÀÌÌÀÒÈÊÀ ÍÅ ÏÐÈÂÅÄÅÍÀ");
        tFrame.mPanel6.add(ErrorLabel,BorderLayout.NORTH); Error=true; break;
      case 3: ErrorLabel.setText("ÍÀÉÄÅÍÛ ÊÎÍÔËÈÊÒÛ - ÃÐÀÌÌÀÒÈÊÀ ÍÅ LL(1)");
        tFrame.mPanel6.add(ErrorLabel,BorderLayout.NORTH); Error=true;
        if(tFrame.reListFunctions)
        { ErrorTextArea.selectAll();
          ErrorTextArea.replaceSelection(tFrame.ArrayListToString(tFrame.alGeneratedFinctions));
          ErrorTextArea.setCaretPosition(0); tFrame.reListFunctions=false; }
        ErrorBLabel.setText("  Êîëè÷åñòâî Êîíôëèêòîâ: "+tFrame.alGeneratedFinctions.size());
        tFrame.mPanel6.add(ErrorPanel,BorderLayout.CENTER); break; }
    switch(tFrame.AnalyseReturn)
    { case 4: tFrame.mPanel6.add(t6Panel,BorderLayout.CENTER); break; }

    if(!tFrame.isBottomComponentActive)
    { t6ListMenuUpdate();
      JMenu jMenu;
      jMenu = new JMenu("File");
      jMenu.setMnemonic(KeyEvent.VK_F);
      tFrame.mMenuBar.add(jMenu);
      jMenu.add(aFileNewProject).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,Event.CTRL_MASK));
      jMenu.add(aFileOpenProject).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,Event.CTRL_MASK));
      jMenu.addSeparator();
      jMenu.add(aFileSaveProject).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,Event.CTRL_MASK));
      jMenu.add(aFileSaveProjectAs).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,Event.CTRL_MASK|Event.SHIFT_MASK));
      jMenu.addSeparator();
      jMenu.add(aFileExit).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,Event.ALT_MASK));
      if(!Error) {
        jMenu = new JMenu("Functions");
        jMenu.setMnemonic(KeyEvent.VK_U);
        tFrame.mMenuBar.add(jMenu);
        jMenu.add(aFunctionsNew).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F2,Event.CTRL_MASK));
        jMenu.add(aFunctionsEdit).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3,Event.CTRL_MASK));
        jMenu.add(aFunctionsDelete).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,Event.CTRL_MASK));
      } jMenu = new JMenu("Help");
      jMenu.setMnemonic(KeyEvent.VK_H);
      tFrame.mMenuBar.add(jMenu);
      jMenu.add(aHelpHelpTopics).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1,0));
      jMenu.addSeparator();
      jMenu.add(aHelpAbout);//.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,Event.ALT_MASK));
      JButton jButton; Insets jInsets=new Insets(2,2,2,2);
      jButton=tFrame.mToolBar.add(aFileNewProject);
      jButton.setFocusable(false); jButton.setMargin(jInsets);
      jButton=tFrame.mToolBar.add(aFileOpenProject);
      jButton.setFocusable(false); jButton.setMargin(jInsets);
      jButton=tFrame.mToolBar.add(aFileSaveProject);
      jButton.setFocusable(false); jButton.setMargin(jInsets);
      jButton=tFrame.mToolBar.add(aFileSaveProjectAs);
      jButton.setFocusable(false); jButton.setMargin(jInsets);
      if(!Error) {
        tFrame.mToolBar.addSeparator();
        jButton=tFrame.mToolBar.add(aFunctionsNew);
        jButton.setFocusable(false); jButton.setMargin(jInsets);
        jButton=tFrame.mToolBar.add(aFunctionsEdit);
        jButton.setFocusable(false); jButton.setMargin(jInsets);
        jButton=tFrame.mToolBar.add(aFunctionsDelete);
        jButton.setFocusable(false); jButton.setMargin(jInsets); }
    }
    tFrame.validate();
  }
}
