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

public class Tab5
{
  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 aFunctionsTraining;
  private Action aHelpHelpTopics;
  private Action aHelpAbout;
  private JLabel ErrorLabel = new JLabel();
  private JPanel t5Panel = new JPanel();
  private JPanel t5CPanel = new JPanel();
  private JLabel t5TLabel = new JLabel();
  private JList t5List = new JList();
  private JScrollPane t5ScrollPane = new JScrollPane();
  private JPanel t5RPanel = new JPanel();
  private JLabel t5RTLabel = new JLabel();
  private JTextArea t5RTextArea = new JTextArea();
  private JScrollPane t5RScrollPane = new JScrollPane();

  public Tab5(MainFrame pMainFrame)
  { tFrame=pMainFrame;
    tFrame.mPanel5.setLayout(new BorderLayout());

    aFileNewProject = new AbstractAction("New Project",new ImageIcon(Tab5.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(Tab5.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(Tab5.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(Tab5.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 Functions As Unicode File...",new ImageIcon(Tab5.class.getResource("usave.gif")))
    {{ putValue(Action.SHORT_DESCRIPTION,"Save Functions As Unicode File..."); }
    // putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_U));
    public void actionPerformed(ActionEvent e)
    { ArrayList al=getTSelection();
      tFrame.SaveText("Ôóíêöèè Âûáîðà UTF-16 (*.sfu)",".sfu",al,-1); }};
    aFileSaveANSI = new AbstractAction("Save Functions As ANSI File...")
    {{ putValue(Action.SHORT_DESCRIPTION,"Save Functions As ANSI File..."); }
    // putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_I));
    public void actionPerformed(ActionEvent e)
    { ArrayList al=getTSelection();
      tFrame.SaveText("Ôóíêöèè Âûáîðà ANSI (*.sfa)",".sfa",al,0); }};
    aFileSaveASCII = new AbstractAction("Save Functions As ASCII File...")
    {{ putValue(Action.SHORT_DESCRIPTION,"Save Functions As ASCII File..."); }
    // putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_C));
    public void actionPerformed(ActionEvent e)
    { ArrayList al=getTSelection();
      tFrame.SaveText("Ôóíêöèè Âûáîðà ASCII (*.sfc)",".sfc",al,1); }};
    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(); }};
    aFunctionsTraining = new AbstractAction("Training",new ImageIcon(Tab5.class.getResource("training.gif")))
    {{ putValue(Action.SHORT_DESCRIPTION,"Training"); }
    // putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_T));
    public void actionPerformed(ActionEvent e)
    { tFrame.isNew=true; tFrame.OpenBottom(0x5A); }};
    aHelpHelpTopics = new AbstractAction("Help Topics",new ImageIcon(Tab5.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(0x5); }};
    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);
    t5Panel.setLayout(new BorderLayout());
    t5Panel.add(t5CPanel,BorderLayout.CENTER);
    t5CPanel.setLayout(new BorderLayout());
    t5TLabel.setFont(tFrame.mFont);
    t5TLabel.setText("  Ïðàâèëà Ãðàììàòèêè:");
    t5CPanel.add(t5TLabel,BorderLayout.NORTH);
    t5List.setFont(tFrame.mFont);
    t5List.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    t5List.addListSelectionListener(new ListSelectionListener()
    { public void valueChanged(ListSelectionEvent e) { int i,j,q; ArrayList al,ta;
      t5RTextArea.selectAll(); t5RTextArea.replaceSelection("");
      i=t5List.getSelectedIndex(); if((i>=0)&&(i<tFrame.alSelection.size()))
      { al=(ArrayList)tFrame.alSelection.get(i); q=al.size(); ta = new ArrayList(q);
        for(i=0;i<q;++i) { j=((Integer)al.get(i)).intValue();
        if((j>=0)&&(j<tFrame.alTerminalList.size()))
          ta.add((String)tFrame.alTerminalList.get(j)); else ta.add(""); }
      t5RTextArea.selectAll();
      t5RTextArea.replaceSelection(tFrame.ArrayListToString(ta));
      t5RTextArea.setCaretPosition(0); ta.clear(); ta=null; } }});
    t5ScrollPane.setViewportView(t5List);
    t5CPanel.add(t5ScrollPane,BorderLayout.CENTER);
    t5Panel.add(t5RPanel,BorderLayout.EAST);
    t5RPanel.setLayout(new BorderLayout());
    t5RTLabel.setFont(tFrame.mFont);
    t5RTLabel.setText("  Ôóíêöèÿ Âûáîðà:       ");
    t5RPanel.add(t5RTLabel,BorderLayout.NORTH);
    t5RTextArea.setFont(tFrame.mFont);
    t5RTextArea.setTabSize(0x08);
    t5RTextArea.setEditable(false);
    t5RTextArea.getCaret().setVisible(false);
    t5RTextArea.getCaret().setSelectionVisible(true);
    t5RScrollPane.setViewportView(t5RTextArea);
    t5RPanel.add(t5RScrollPane,BorderLayout.CENTER);

    tFrame.SelectionSecurity=false;
  }

  private ArrayList getTSelection()
  { int i,j,k,l,q; ArrayList al,ta; q=tFrame.alRuleList.size(); ta = new ArrayList(q<<4);
    for(i=0;i<q;++i) { ta.add(tFrame.alRuleList.get(i));
      if((i>=0)&&(i<tFrame.alSelection.size()))
      { al=(ArrayList)tFrame.alSelection.get(i); k=al.size();
        for(j=0;j<k;++j) { l=((Integer)al.get(j)).intValue();
          if((l>=0)&&(l<tFrame.alTerminalList.size()))
            ta.add("\t"+(String)tFrame.alTerminalList.get(l)); } }
      ta.add(""); } return(ta); }

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

  public void tSelect()
  { boolean Error=false; if(tFrame.reAnalyse) tFrame.Analyse();
    switch(tFrame.AnalyseReturn)
    { case 0: ErrorLabel.setText("ÑÎÑÒÎßÍÈÅ ÃÐÀÌÌÀÒÈÊÈ ÎÏÐÅÄÅËÈÒÜ ÍÅ ÓÄÀËÎÑÜ");
        tFrame.mPanel5.add(ErrorLabel,BorderLayout.NORTH); Error=true; break;
      case 1: ErrorLabel.setText("Â ÃÐÀÌÌÀÒÈÊÅ ÎÁÍÀÐÓÆÅÍÛ ÑÈÍÒÀÊÑÈ×ÅÑÊÈÅ ÎØÈÁÊÈ");
        tFrame.mPanel5.add(ErrorLabel,BorderLayout.NORTH); Error=true; break;
      case 2: ErrorLabel.setText("ÃÐÀÌÌÀÒÈÊÀ ÍÅ ÏÐÈÂÅÄÅÍÀ");
        tFrame.mPanel5.add(ErrorLabel,BorderLayout.NORTH); Error=true; break; }
      switch(tFrame.AnalyseReturn)
      { case 3: case 4: if(tFrame.reListSelection)
        { t5RTextArea.selectAll(); t5RTextArea.replaceSelection("");
          t5List.setListData(tFrame.alRuleList.toArray()); tFrame.reListSelection=false; }
        tFrame.mPanel5.add(t5Panel,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));
      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));
      if(!Error) {
        jMenu = new JMenu("Functions");
        jMenu.setMnemonic(KeyEvent.VK_U);
        tFrame.mMenuBar.add(jMenu);
        jMenu.add(aFunctionsTraining).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE,0));
      } 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.mToolBar.addSeparator();
        jButton=tFrame.mToolBar.add(aFunctionsTraining);
        jButton.setFocusable(false); jButton.setMargin(jInsets); }
    }
    tFrame.validate();
  }
}
