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

public class Tab3
{
  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 aHelpHelpTopics;
  private Action aHelpAbout;
  private JLabel ErrorLabel = new JLabel();
  private JPanel t3Panel = new JPanel();
  private JPanel t3lPanel = new JPanel();
  private BorderLayout t3lBorderLayout = new BorderLayout();
  private JLabel t3lTLabel = new JLabel();
  private JTextArea t3lTextArea = new JTextArea();
  private JScrollPane t3lScrollPane = new JScrollPane();
  private JLabel t3lBLabel = new JLabel();
  private JPanel t3rPanel = new JPanel();
  private BorderLayout t3rBorderLayout = new BorderLayout();
  private JLabel t3rTLabel = new JLabel();
  private JTextArea t3rTextArea = new JTextArea();
  private JScrollPane t3rScrollPane = new JScrollPane();
  private JLabel t3rBLabel = new JLabel();

  private ArrayList o;
  private int os;

  public Tab3(MainFrame pMainFrame)
  {
   tFrame=pMainFrame;
   tFrame.mPanel3.setLayout(tBorderLayout);

   aFileNewProject = new AbstractAction("New Project",new ImageIcon(Tab3.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(Tab3.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(Tab3.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(Tab3.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(); }};
   aHelpHelpTopics = new AbstractAction("Help Topics",new ImageIcon(Tab3.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(0x3); }};
   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);
   t3Panel.setLayout(new BorderLayout());
   t3Panel.add(t3lPanel,BorderLayout.WEST);
   t3lPanel.setLayout(t3lBorderLayout);
   t3lTLabel.setFont(tFrame.mFont);
   t3lTLabel.setText("  ");
   t3lPanel.add(t3lTLabel,BorderLayout.NORTH);
   t3lTextArea.setFont(tFrame.mFont);
   t3lTextArea.setTabSize(0x08);
   t3lTextArea.setEditable(false);
   t3lTextArea.getCaret().setVisible(false);
   t3lTextArea.getCaret().setSelectionVisible(true);
   t3lScrollPane.setViewportView(t3lTextArea);
   t3lPanel.add(t3lScrollPane,BorderLayout.CENTER);
   t3lBLabel.setFont(tFrame.mFont);
   t3lBLabel.setText("  ");
   t3lPanel.add(t3lBLabel,BorderLayout.SOUTH);
   JPanel t3lEastPanel = new JPanel();
   t3lEastPanel.setLayout(new BorderLayout());
   t3lEastPanel.add(t3rPanel,BorderLayout.WEST);
   t3Panel.add(t3lEastPanel,BorderLayout.CENTER);
   t3rPanel.setLayout(t3rBorderLayout);
   t3rTLabel.setFont(tFrame.mFont);
   t3rTLabel.setText("  ");
   t3rPanel.add(t3rTLabel,BorderLayout.NORTH);
   t3rTextArea.setFont(tFrame.mFont);
   t3rTextArea.setTabSize(0x08);
   t3rTextArea.setEditable(false);
   t3rTextArea.getCaret().setVisible(false);
   t3rTextArea.getCaret().setSelectionVisible(true);
   t3rScrollPane.setViewportView(t3rTextArea);
   t3rPanel.add(t3rScrollPane,BorderLayout.CENTER);
   t3rBLabel.setFont(tFrame.mFont);
   t3rBLabel.setText("  ");
   t3rPanel.add(t3rBLabel,BorderLayout.SOUTH);
  }

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

  public void tSelect()
  { if(tFrame.reAnalyse) tFrame.Analyse();
    switch(tFrame.AnalyseReturn)
    { case 0: ErrorLabel.setText("СОСТОЯНИЕ ГРАММАТИКИ ОПРЕДЕЛИТЬ НЕ УДАЛОСЬ");
        tFrame.mPanel3.add(ErrorLabel,BorderLayout.NORTH); break;
      case 1: ErrorLabel.setText("В ГРАММАТИКЕ ОБНАРУЖЕНЫ СИНТАКСИЧЕСКИЕ ОШИБКИ");
        tFrame.mPanel3.add(ErrorLabel,BorderLayout.NORTH); break;
      case 2: ErrorLabel.setText("ГРАММАТИКА НЕ ПРИВЕДЕНА");
        tFrame.mPanel3.add(ErrorLabel,BorderLayout.NORTH); break; }
    switch(tFrame.AnalyseReturn)
    { case 2:
        if(tFrame.reListTerminal) {
          t3lTLabel.setText("  Непроизводящие Нетерминалы:   ");
          t3rTLabel.setText("  Недостижимые Символы:         ");
          t3lTextArea.selectAll();
          t3lTextArea.replaceSelection(tFrame.ArrayListToString(tFrame.alNonTerminalList));
          t3lTextArea.setCaretPosition(0);
          t3rTextArea.selectAll();
          t3rTextArea.replaceSelection(tFrame.ArrayListToString(tFrame.alTerminalList));
          t3rTextArea.setCaretPosition(0);
          t3lBLabel.setText("  К-во Непроизводящих: "+tFrame.alNonTerminalList.size());
          t3rBLabel.setText("  К-во Недостижимых: "+tFrame.alTerminalList.size());
          tFrame.reListTerminal=false; }
        tFrame.mPanel3.add(t3Panel,BorderLayout.CENTER); break;
      case 3: case 4:
        if(tFrame.reListTerminal) { int i,mos=0;
          t3lTLabel.setText("  Нетерминалы Грамматики:       ");
          t3rTLabel.setText("  Терминалы Грамматики:         ");
          t3lTextArea.selectAll();
          t3lTextArea.replaceSelection(tFrame.ArrayListToString(tFrame.alNonTerminalList));
          t3lTextArea.setCaretPosition(0);
          os=tFrame.alTerminalList.size();
          if(os>0) { mos=os-1; o = new ArrayList(mos);
            for(i=1;i<os;++i) o.add(tFrame.alTerminalList.get(i));
            t3rTextArea.selectAll();
            t3rTextArea.replaceSelection(tFrame.ArrayListToString(o));
            t3rTextArea.setCaretPosition(0); o.clear();
          } else { t3rTextArea.selectAll(); t3rTextArea.replaceSelection(""); }
          t3lBLabel.setText("  К-во Нетерминалов: "+tFrame.alNonTerminalList.size());
          t3rBLabel.setText("  К-во Терминалов: "+mos);
          tFrame.reListTerminal=false; }
        tFrame.mPanel3.add(t3Panel,BorderLayout.CENTER); 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));
      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);
    }
    tFrame.validate();
  }
}
