package gui;
import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import run.*;

import java.util.*;

import j3d.*;

import javax.swing.table.*;

public class Processor_Find extends JPanel {
   JTextArea text;
   GridLayout gridLayout1 = new GridLayout();
   JLabel jLabel1 = new JLabel();
   JTextField tFind = new JTextField();
   JButton jButton1 = new JButton();
   JLabel jLabel2 = new JLabel();
   JTextField tReplace = new JTextField();
   JButton jButton2 = new JButton();
   private JLabel lb_cmd = new JLabel();
   private JCheckBox cb_all = new JCheckBox();
   public Processor_Find(JTextArea t) {
      super();
      try {
         this.text = t;
         jbInit();
      }  catch (Exception e) {
         e.printStackTrace();
      }
   }
   private void jbInit() throws Exception {
      this.jLabel1.setText("Text to find");
      this.gridLayout1.setRows(3);
      this.gridLayout1.setColumns(3);
      this.setLayout(this.gridLayout1);
      this.jButton1.setText("Find");
      this.jButton1.addActionListener(new java.awt.event.ActionListener(){
         
         public void actionPerformed(ActionEvent e) {
            jButton1_actionPerformed(e);
         }
      });
      this.jLabel2.setText("Replace with");
      this.jButton2.setText("Replace");
      this.jButton2.addActionListener(new java.awt.event.ActionListener(){
         
         public void actionPerformed(ActionEvent e) {
            jButton2_actionPerformed(e);
         }
      });
      this.cb_all.setText("Replace All");
      this.add(this.jLabel1, null);
      this.add(this.tFind, null);
      this.add(this.jButton1, null);
      this.add(this.jLabel2, null);
      this.add(this.tReplace, null);
      this.add(this.jButton2, null);
      this.add(this.lb_cmd, null);
      this.add(this.cb_all, null);
   }
   void jButton1_actionPerformed(ActionEvent e) {
      String f_st = this.tFind.getText();
      if (f_st.length() == 0) return;
      boolean stop = false;
      int pos = this.text.getCaretPosition();
      try {
         while (pos != this.text.getText().length() && !stop) {
            this.text.setSelectionStart(pos);
            this.text.setSelectionEnd(pos + f_st.length());
            if (this.text.getSelectedText().equals(f_st)) stop = true;
            pos++;
         }
      }  catch (Exception e1) {
      }
   }
   void jButton2_actionPerformed(ActionEvent e) {
      String r_st = this.tReplace.getText();
      String f_st = this.tFind.getText();
      if (f_st.length() == 0) return;
      try {
         if (this.cb_all.isSelected()) {
            this.text.setSelectionStart(this.text.getCaretPosition());
            this.text.setSelectionEnd(this.text.getText().length());
            if (this.text.getSelectedText() != null && this.text.getSelectedText().trim().length() != 0) this.text.replaceSelection(this.text.getSelectedText().replaceAll(f_st, r_st));
         } else {
            if (this.text.getSelectedText() != null && this.text.getSelectedText().trim().length() != 0) {
               if (!this.text.getSelectedText().equals(f_st)) this.text.setCaretPosition(this.text.getSelectionStart()); else this.text.replaceSelection(this.tReplace.getText());
            }
            jButton1_actionPerformed(e);
         }
      }  catch (Exception e1) {
      }
   }
}
