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

public class Tab7
{
  private MainFrame tFrame;

  private Action aFileNewProject;
  private Action aFileOpenProject;
  private Action aFileSaveProject;
  private Action aFileSaveProjectAs;
  private Action aFileSaveUnicode;
  private Action aFileSaveANSI;
  private Action aFileSaveASCII;
  private Action aFileExit;
  private Action aHelpHelpTopics;
  private Action aHelpAbout;

  private JLabel ErrorLabel = new JLabel();
  private JPanel ErrorPanel = new JPanel();
  private JLabel ErrorTLabel = new JLabel();
  private JList  ErrorList = new JList();
  private JScrollPane ErrorScrollPane = new JScrollPane();
  private JLabel ErrorBLabel = new JLabel();

  private JPanel t7Panel = new JPanel();
  private JPanel t7StepPanel = new JPanel();
  private JLabel t7StepTLabel = new JLabel();
  private JList t7StepList = new JList();
  private JScrollPane t7StepScrollPane = new JScrollPane();
  private JLabel t7StepBLabel = new JLabel();
  private JPanel t7StackPanel = new JPanel();
  private JLabel t7StackTLabel = new JLabel();
  private JTextArea t7StackTextArea = new JTextArea();
  private JScrollPane t7StackScrollPane = new JScrollPane();

  private JPanel t7CenterPanel = new JPanel();
  private JPanel t7InOutPanel = new JPanel();
  private JPanel t7InputPanel = new JPanel();
  private JLabel t7InputTLabel = new JLabel();
  private JTextArea t7InputTextArea = new JTextArea();
  private JScrollPane t7InputScrollPane = new JScrollPane();
  private JPanel t7OutputPanel = new JPanel();
  private JLabel t7OutputTLabel = new JLabel();
  private JTextArea t7OutputTextArea = new JTextArea();
  private JScrollPane t7OutputScrollPane = new JScrollPane();
  private JPanel t7NorthPanel = new JPanel();
  private JPanel t7NorthTopPanel = new JPanel();
  private JLabel t7NorthTopLabel = new JLabel();
  private JPanel t7StatePanel = new JPanel();
  private JLabel t7StateLabel = new JLabel();
  private JTextField t7StateTextField = new JTextField();
  private JTextField t7FunctionTextField = new JTextField();

  private int CCIndex;
  private int CFIndex;
  private JPanel cPanel;
  private boolean isTranslator;

  public Tab7(MainFrame pMainFrame)
  { tFrame=pMainFrame;
    tFrame.mPanel7.setLayout(new BorderLayout());
    tFrame.mPanel8.setLayout(new BorderLayout());

    aFileNewProject = new AbstractAction("New Project",new ImageIcon(Tab7.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(Tab7.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(Tab7.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(Tab7.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(); }};
    aFileSaveUnicode = new AbstractAction("Save Log As Unicode File...",new ImageIcon(Tab7.class.getResource("usave.gif")))
    {{ putValue(Action.SHORT_DESCRIPTION,"Save Log As Unicode File..."); }
    // putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_U));
    public void actionPerformed(ActionEvent e)
    { tFrame.mWasp.getTLog();
      tFrame.SaveText("Ïðîòîêîë UTF-16 (*.lgu)",".lgu",tFrame.alTLog,-1);
      tFrame.alTLog.clear(); }};
    aFileSaveANSI = new AbstractAction("Save Log As ANSI File...")
    {{ putValue(Action.SHORT_DESCRIPTION,"Save Log As ANSI File..."); }
    // putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_I));
    public void actionPerformed(ActionEvent e)
    { tFrame.mWasp.getTLog();
      tFrame.SaveText("Ïðîòîêîë ANSI (*.lga)",".lga",tFrame.alTLog,0);
      tFrame.alTLog.clear(); }};
    aFileSaveASCII = new AbstractAction("Save Log As ASCII File...")
    {{ putValue(Action.SHORT_DESCRIPTION,"Save Log As ASCII File..."); }
    // putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_C));
    public void actionPerformed(ActionEvent e)
    { tFrame.mWasp.getTLog();
      tFrame.SaveText("Ïðîòîêîë ASCII (*.lgc)",".lgc",tFrame.alTLog,1);
      tFrame.alTLog.clear(); }};
    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(); }};
    aHelpHelpTopics = new AbstractAction("Help Topics",new ImageIcon(Tab7.class.getResource("help.gif")))
    {{ putValue(Action.SHORT_DESCRIPTION,"Help Topics"); }
    // putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_H));
    public void actionPerformed(ActionEvent e)
    { if(isTranslator) tFrame.HelpHelpTopics(0x8);
      else tFrame.HelpHelpTopics(0x7); }};
    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);
    ErrorList.setFont(tFrame.mFont);
    ErrorList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    ErrorScrollPane.setViewportView(ErrorList);
    ErrorPanel.add(ErrorScrollPane,BorderLayout.CENTER);
    ErrorBLabel.setFont(tFrame.mFont);
    ErrorBLabel.setText("  ");
    ErrorPanel.add(ErrorBLabel,BorderLayout.SOUTH);

    t7Panel.setLayout(new BorderLayout());
    t7Panel.add(t7StepPanel,BorderLayout.WEST);
    t7StepPanel.setLayout(new BorderLayout());
    t7StepTLabel.setFont(tFrame.mFont);
    t7StepTLabel.setText(" Òàêòû: ");
    t7StepPanel.add(t7StepTLabel,BorderLayout.NORTH);
    t7StepList.setFont(tFrame.mFont);
    t7StepList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    t7StepList.addListSelectionListener(new ListSelectionListener()
    { public void valueChanged(ListSelectionEvent e) { int i; ClearText();
      i=t7StepList.getSelectedIndex(); if((i>=0)&&(i<tFrame.TStepQ))
      { tFrame.mWasp.getTStep(i);
        t7StackTextArea.selectAll();
        t7StackTextArea.replaceSelection(tFrame.ArrayListToString(tFrame.alTStack));
        t7StackTextArea.setCaretPosition(0);
        t7InputTextArea.selectAll();
        t7InputTextArea.replaceSelection(tFrame.ArrayListToString(tFrame.alInputChain));
        t7InputTextArea.setCaretPosition(0);
        t7OutputTextArea.selectAll();
        t7OutputTextArea.replaceSelection(tFrame.ArrayListToString(tFrame.alOutputChain));
        t7OutputTextArea.setCaretPosition(0);
        t7FunctionTextField.selectAll();
        t7FunctionTextField.replaceSelection(tFrame.TFunction);
        t7FunctionTextField.setCaretPosition(0);
        t7StateTextField.selectAll();
        t7StateTextField.replaceSelection(tFrame.TState);
        t7StateTextField.setCaretPosition(0); } }});
    t7StepScrollPane.setViewportView(t7StepList);
    t7StepPanel.add(t7StepScrollPane,BorderLayout.CENTER);
    t7StepBLabel.setFont(tFrame.mFont);
    t7StepBLabel.setText("  ");
    t7StepPanel.add(t7StepBLabel,BorderLayout.SOUTH);
    t7Panel.add(t7StackPanel,BorderLayout.EAST);
    t7StackPanel.setLayout(new BorderLayout());
    t7StackTLabel.setFont(tFrame.mFont);
    t7StackTLabel.setText("  Ìàãàçèí:              ");
    t7StackPanel.add(t7StackTLabel,BorderLayout.NORTH);
    t7StackTextArea.setFont(tFrame.mFont);
    t7StackTextArea.setTabSize(0x08);
    t7StackTextArea.setEditable(false);
    t7StackTextArea.getCaret().setVisible(false);
    t7StackTextArea.getCaret().setSelectionVisible(true);
    t7StackScrollPane.setViewportView(t7StackTextArea);
    t7StackPanel.add(t7StackScrollPane,BorderLayout.CENTER);
    t7Panel.add(t7CenterPanel,BorderLayout.CENTER);
    t7CenterPanel.setLayout(new BorderLayout());
    t7CenterPanel.add(t7InOutPanel,BorderLayout.CENTER);
    t7InOutPanel.setLayout(new GridLayout(2,1));
    t7InOutPanel.add(t7InputPanel);
    t7InputPanel.setLayout(new BorderLayout());
    t7InputTLabel.setFont(tFrame.mFont);
    t7InputTLabel.setText("  Âõîäíàÿ Öåïî÷êà:");
    t7InputPanel.add(t7InputTLabel,BorderLayout.NORTH);
    t7InputTextArea.setFont(tFrame.mFont);
    t7InputTextArea.setTabSize(0x08);
    t7InputTextArea.setEditable(false);
    t7InputTextArea.getCaret().setVisible(false);
    t7InputTextArea.getCaret().setSelectionVisible(true);
    t7InputScrollPane.setViewportView(t7InputTextArea);
    t7InputPanel.add(t7InputScrollPane,BorderLayout.CENTER);
    //t7InOutPanel.add(t7OutputPanel);
    t7OutputPanel.setLayout(new BorderLayout());
    t7OutputTLabel.setFont(tFrame.mFont);
    t7OutputTLabel.setText("  Âûõîäíàÿ Öåïî÷êà:");
    t7OutputPanel.add(t7OutputTLabel,BorderLayout.NORTH);
    t7OutputTextArea.setFont(tFrame.mFont);
    t7OutputTextArea.setTabSize(0x08);
    t7OutputTextArea.setEditable(false);
    t7OutputTextArea.getCaret().setVisible(false);
    t7OutputTextArea.getCaret().setSelectionVisible(true);
    t7OutputScrollPane.setViewportView(t7OutputTextArea);
    t7OutputPanel.add(t7OutputScrollPane,BorderLayout.CENTER);
    t7CenterPanel.add(t7NorthPanel,BorderLayout.NORTH);
    t7NorthPanel.setLayout(new BorderLayout());
    t7NorthPanel.add(t7NorthTopPanel,BorderLayout.NORTH);
    t7NorthTopPanel.setLayout(new BorderLayout());
    t7NorthTopLabel.setFont(tFrame.mFont);
    t7NorthTopLabel.setText("  Ôóíêöèÿ Ïåðåõîäà:");
    t7NorthTopPanel.add(t7NorthTopLabel,BorderLayout.WEST);
    t7NorthTopPanel.add(t7StatePanel,BorderLayout.CENTER);
    t7StatePanel.setLayout(new BorderLayout());
    t7StateLabel.setFont(tFrame.mFont);
    t7StateLabel.setText("    Ñîñòîÿíèå: ");
    t7StatePanel.add(t7StateLabel,BorderLayout.WEST);
    t7StateTextField.setFont(tFrame.mFont);
    t7StateTextField.setEditable(false);
    t7StateTextField.getCaret().setVisible(false);
    t7StateTextField.getCaret().setSelectionVisible(true);
    t7StatePanel.add(t7StateTextField,BorderLayout.CENTER);
    t7FunctionTextField.setFont(tFrame.mFont);
    t7FunctionTextField.setEditable(false);
    t7FunctionTextField.getCaret().setVisible(false);
    t7FunctionTextField.getCaret().setSelectionVisible(true);
    t7NorthPanel.add(t7FunctionTextField,BorderLayout.CENTER);

    tFrame.reTranslate=true;
  }

  private void TranslatorOff()
  { t7InOutPanel.removeAll(); t7InOutPanel.add(t7InputPanel); }

  private void TranslatorOn()
  { t7InOutPanel.removeAll();
    t7InOutPanel.add(t7InputPanel); t7InOutPanel.add(t7OutputPanel); }

  private void ClearText()
  { t7StackTextArea.selectAll(); t7StackTextArea.replaceSelection("");
    t7InputTextArea.selectAll(); t7InputTextArea.replaceSelection("");
    t7OutputTextArea.selectAll(); t7OutputTextArea.replaceSelection("");
    t7FunctionTextField.selectAll(); t7FunctionTextField.replaceSelection("");
    t7StateTextField.selectAll(); t7StateTextField.replaceSelection(""); }

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

  public void tSelect(boolean p)
  { isTranslator=p; if(isTranslator) { TranslatorOn(); cPanel=tFrame.mPanel8; }
    else cPanel=tFrame.mPanel7;
    boolean Error=false,ToLast=false; if(tFrame.reAnalyse) tFrame.Analyse();
    switch(tFrame.AnalyseReturn)
    { case 0: ErrorLabel.setText("ÑÎÑÒÎßÍÈÅ ÃÐÀÌÌÀÒÈÊÈ ÎÏÐÅÄÅËÈÒÜ ÍÅ ÓÄÀËÎÑÜ");
        cPanel.add(ErrorLabel,BorderLayout.NORTH); Error=true; break;
      case 1: ErrorLabel.setText("Â ÃÐÀÌÌÀÒÈÊÅ ÎÁÍÀÐÓÆÅÍÛ ÑÈÍÒÀÊÑÈ×ÅÑÊÈÅ ÎØÈÁÊÈ");
        cPanel.add(ErrorLabel,BorderLayout.NORTH); Error=true; break;
      case 2: ErrorLabel.setText("ÃÐÀÌÌÀÒÈÊÀ ÍÅ ÏÐÈÂÅÄÅÍÀ");
        cPanel.add(ErrorLabel,BorderLayout.NORTH); Error=true; break;
      case 3: ErrorLabel.setText("ÍÀÉÄÅÍÛ ÊÎÍÔËÈÊÒÛ - ÃÐÀÌÌÀÒÈÊÀ ÍÅ LL(1)");
        cPanel.add(ErrorLabel,BorderLayout.NORTH); Error=true; break; }
    switch(tFrame.AnalyseReturn)
    { case 4: CCIndex=tFrame.CurrentChainIndex; CFIndex=tFrame.CurrentFunctionIndex;
        if((CCIndex>=0)&&(CCIndex<tFrame.alChainName.size())
          &&(CCIndex<tFrame.alChainText.size()))
        { if((CFIndex>=0)&&(CFIndex<tFrame.alFunctionName.size())
            &&(CFIndex<tFrame.alFunctionText.size()))
          { if(tFrame.reTranslate) tFrame.Translate();
            switch(tFrame.TranslateReturn)
            { case 0: ErrorLabel.setText("ÍÅ ÓÄÀËÎÑÜ ÎÁÐÀÁÎÒÀÒÜ ÂÕÎÄÍÛÅ ÄÀÍÍÛÅ");
                cPanel.add(ErrorLabel,BorderLayout.NORTH); Error=true; break;
              case 1: ErrorLabel.setText(
                "Â ÍÀÁÎÐÅ ÔÓÍÊÖÈÉ ÏÅÐÅÕÎÄÎÂ ÎÁÍÀÐÓÆÅÍÛ ÑÈÍÒÀÊÑÈ×ÅÑÊÈÅ ÎØÈÁÊÈ");
                cPanel.add(ErrorLabel,BorderLayout.NORTH); Error=true;
                if(tFrame.reListTranslator)
                { ErrorList.setListData(tFrame.alTStack.toArray());
                  tFrame.reListTranslator=false; }
                ErrorBLabel.setText("  Êîëè÷åñòâî Îøèáîê: "+tFrame.alTStack.size());
                cPanel.add(ErrorPanel,BorderLayout.CENTER); break;
              case 2: ErrorLabel.setText("ÔÓÍÊÖÈß ÏÅÐÅÕÎÄÎÂ ÍÅ ÎÏÐÅÄÅËÅÍÀ");
                cPanel.add(ErrorLabel,BorderLayout.NORTH); ToLast=true;
              case 3: if(tFrame.reListTranslator)
              { int i; ClearText(); if(tFrame.TStepQ<0) tFrame.TStepQ=0;
                Object o[] = new Object[tFrame.TStepQ];
                for(i=0;i<tFrame.TStepQ;++i) o[i]=Integer.toString(i);
                t7StepList.setListData(o); i=tFrame.TStepQ-1; if((ToLast)&&(i>=0))
                { t7StepList.setSelectedIndex(i); t7StepList.ensureIndexIsVisible(i); }
                tFrame.reListTranslator=false; }
                t7StepBLabel.setText(" :"+tFrame.TStepQ);
                cPanel.add(t7Panel,BorderLayout.CENTER); } break; }
          else { ErrorLabel.setText("ÍÓÆÍÎ ÂÛÁÐÀÒÜ ÍÀÁÎÐ ÔÓÍÊÖÈÉ ÏÅÐÅÕÎÄÎÂ");
            cPanel.add(ErrorLabel,BorderLayout.NORTH); Error=true; } }
        else { ErrorLabel.setText("ÍÓÆÍÎ ÂÛÁÐÀÒÜ ÂÕÎÄÍÓÞ ÖÅÏÎ×ÊÓ");
          cPanel.add(ErrorLabel,BorderLayout.NORTH); Error=true; } break; }

    if(!tFrame.isBottomComponentActive)
    {
      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));
      if(!Error) {
        jMenu.addSeparator();
        jMenu.add(aFileSaveUnicode).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U,Event.CTRL_MASK|Event.SHIFT_MASK));
        jMenu.add(aFileSaveANSI).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,Event.CTRL_MASK|Event.SHIFT_MASK));
        jMenu.add(aFileSaveASCII).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK|Event.SHIFT_MASK));
      } jMenu.addSeparator();
      jMenu.add(aFileExit).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,Event.ALT_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(aFileSaveUnicode);
        jButton.setFocusable(false); jButton.setMargin(jInsets); }
    }
    tFrame.validate();
  }
}
