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

public class Tab4
{
  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 aChainGenerate;
  private Action aChainNew;
  private Action aChainEdit;
  private Action aChainDelete;
  private Action aHelpHelpTopics;
  private Action aHelpAbout;
  private JLabel ErrorLabel = new JLabel();
  private JPanel t4Panel = new JPanel();
  private GridLayout tGridLayout = new GridLayout(2,1);
  private JPanel t4tPanel = new JPanel();
  private BorderLayout t4tBorderLayout = new BorderLayout();
  private JLabel t4tTLabel = new JLabel();
  public  JList t4List = new JList();
  private JScrollPane t4tScrollPane = new JScrollPane();
  private JLabel t4tBLabel = new JLabel();
  private JPanel t4bPanel = new JPanel();
  private BorderLayout t4bBorderLayout = new BorderLayout();
  private JLabel t4bTLabel = new JLabel();
  private JTextArea t4TextArea = new JTextArea();
  private JScrollPane t4bScrollPane = new JScrollPane();

  public Tab4(MainFrame pMainFrame)
  {
    tFrame=pMainFrame;
    tFrame.mPanel4.setLayout(tBorderLayout);

    aFileNewProject = new AbstractAction("New Project",new ImageIcon(Tab4.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(Tab4.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(Tab4.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(Tab4.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(); }};
    aChainGenerate = new AbstractAction("Generate",new ImageIcon(Tab4.class.getResource("generate.gif")))
    {{ putValue(Action.SHORT_DESCRIPTION,"Generate"); }
    // putValue(Action.MNEMONIC_KEY,new Integer(KeyEvent.VK_G));
    public void actionPerformed(ActionEvent e)
    { tFrame.isNew=true; tFrame.OpenBottom(0x4B); }};
    aChainNew = new AbstractAction("New",new ImageIcon(Tab4.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(0x4A); }};
    aChainEdit = new AbstractAction("Edit",new ImageIcon(Tab4.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(0x4A); }};
    aChainDelete = new AbstractAction("Delete",new ImageIcon(Tab4.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.CurrentChainIndex,j;
      if((i>=0)&&(i<tFrame.alChainName.size())&&(i<tFrame.alChainText.size()))
      { tFrame.alChainName.remove(i); tFrame.alChainText.remove(i); t4SetListData();
        tFrame.reSaveChains=true; j=tFrame.alChainName.size(); if(i>=j) i=j-1;
        if(i>=0) { t4List.setSelectedIndex(i); t4List.ensureIndexIsVisible(i); }
      } }};
    aHelpHelpTopics = new AbstractAction("Help Topics",new ImageIcon(Tab4.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(0x4); }};
    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);
    t4Panel.setLayout(tGridLayout);
    t4Panel.add(t4tPanel);
    t4Panel.add(t4bPanel);

    t4tPanel.setLayout(t4tBorderLayout);
    t4tTLabel.setFont(tFrame.mFont);
    t4tTLabel.setText("  Èìåíà Öåïî÷åê:");
    t4tPanel.add(t4tTLabel,BorderLayout.NORTH);
    t4List.setFont(tFrame.mFont);
    t4List.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    t4List.addListSelectionListener(new ListSelectionListener()
    { public void valueChanged(ListSelectionEvent e) {
      t4ListMenuUpdate(); t4TextArea.selectAll(); t4TextArea.replaceSelection("");
      tFrame.CurrentChainIndex=t4List.getSelectedIndex();
      if((tFrame.CurrentChainIndex>=0)&&(tFrame.CurrentChainIndex<tFrame.alChainText.size()))
      { t4TextArea.selectAll(); t4TextArea.replaceSelection(tFrame.ArrayListToString
        ((ArrayList)tFrame.alChainText.get(tFrame.CurrentChainIndex)));
        t4TextArea.setCaretPosition(0);
      } tFrame.reTranslate=true; tFrame.TranslatorUpdate(3); }});
    t4tScrollPane.setViewportView(t4List);
    t4tPanel.add(t4tScrollPane,BorderLayout.CENTER);
    t4tBLabel.setFont(tFrame.mFont);
    t4tBLabel.setText("  ");
    t4tPanel.add(t4tBLabel,BorderLayout.SOUTH);

    t4bPanel.setLayout(t4bBorderLayout);
    t4bTLabel.setFont(tFrame.mFont);
    t4bTLabel.setText("  Áûñòðûé Ïðîñìîòð:");
    t4bPanel.add(t4bTLabel,BorderLayout.NORTH);
    t4TextArea.setFont(tFrame.mFont);
    t4TextArea.setTabSize(0x08);
    t4TextArea.setEditable(false);
    t4TextArea.getCaret().setVisible(false);
    t4TextArea.getCaret().setSelectionVisible(true);
    t4bScrollPane.setViewportView(t4TextArea);
    t4bPanel.add(t4bScrollPane,BorderLayout.CENTER);

    tFrame.reSaveChains=false;
    tFrame.CurrentChainIndex=-1;
    t4SetListData();
  }

  public boolean SaveChain(String name,String s,ArrayList al)
  { boolean r=false; int i,j=-1,k=-1,q=tFrame.alChainName.size(),op;
    if(q==tFrame.alChainText.size())
    { r=true; for(i=0;i<q;++i) {
        if(s.equals((String)tFrame.alChainName.get(i))) j=i;
        if(s.compareTo((String)tFrame.alChainName.get(i))>0) k=i; } ++k;
      if(j<0) { tFrame.alChainName.add(k,s); tFrame.alChainText.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.alChainText.remove(j); tFrame.alChainText.add(j,al); } }
      if(r) { t4SetListData();
        t4List.setSelectedIndex(k); t4List.ensureIndexIsVisible(k); }
    } return(r);
  }

  public void t4SetListData()
  { int i,q=tFrame.alChainName.size();
    Object o[] = new Object[q];
    for(i=0;i<q;++i) o[i]=tFrame.alChainName.get(i);
    t4List.setListData(o);
    t4ListMenuUpdate();
    t4TextArea.selectAll(); t4TextArea.replaceSelection("");
    t4tBLabel.setText("  Êîëè÷åñòâî Öåïî÷åê: "+q);
  }

  private void t4ListMenuUpdate()
  { int i=t4List.getSelectedIndex();
    if((i>=0)&&(i<tFrame.alChainName.size())&&(i<tFrame.alChainText.size()))
    { aChainEdit.setEnabled(true); aChainDelete.setEnabled(true); }
    else { aChainEdit.setEnabled(false); aChainDelete.setEnabled(false); }
    if(tFrame.isBottomComponentActive)
    { aChainGenerate.setEnabled(false); aChainNew.setEnabled(false); }
    else { aChainGenerate.setEnabled(true); aChainNew.setEnabled(true); }
  }

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

  public void tSelect()
  { boolean Error=false; if(tFrame.reAnalyse) tFrame.Analyse();
    switch(tFrame.AnalyseReturn)
    { case 0: ErrorLabel.setText("ÑÎÑÒÎßÍÈÅ ÃÐÀÌÌÀÒÈÊÈ ÎÏÐÅÄÅËÈÒÜ ÍÅ ÓÄÀËÎÑÜ");
        tFrame.mPanel4.add(ErrorLabel,BorderLayout.NORTH); Error=true; break;
      case 1: ErrorLabel.setText("Â ÃÐÀÌÌÀÒÈÊÅ ÎÁÍÀÐÓÆÅÍÛ ÑÈÍÒÀÊÑÈ×ÅÑÊÈÅ ÎØÈÁÊÈ");
        tFrame.mPanel4.add(ErrorLabel,BorderLayout.NORTH); Error=true; break;
      case 2: ErrorLabel.setText("ÃÐÀÌÌÀÒÈÊÀ ÍÅ ÏÐÈÂÅÄÅÍÀ");
        tFrame.mPanel4.add(ErrorLabel,BorderLayout.NORTH); Error=true; break; }
    switch(tFrame.AnalyseReturn)
    { case 3: case 4: tFrame.mPanel4.add(t4Panel,BorderLayout.CENTER); break; }

    if(!tFrame.isBottomComponentActive)
    { t4ListMenuUpdate();
      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("Chain");
        jMenu.setMnemonic(KeyEvent.VK_C);
        tFrame.mMenuBar.add(jMenu);
        jMenu.add(aChainGenerate).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE,0));
        jMenu.add(aChainNew).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F2,Event.CTRL_MASK));
        jMenu.add(aChainEdit).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3,Event.CTRL_MASK));
        jMenu.add(aChainDelete).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(aChainGenerate);
        jButton.setFocusable(false); jButton.setMargin(jInsets);
        jButton=tFrame.mToolBar.add(aChainNew);
        jButton.setFocusable(false); jButton.setMargin(jInsets);
        jButton=tFrame.mToolBar.add(aChainEdit);
        jButton.setFocusable(false); jButton.setMargin(jInsets);
        jButton=tFrame.mToolBar.add(aChainDelete);
        jButton.setFocusable(false); jButton.setMargin(jInsets); }
    }
    tFrame.validate();
  }
}
