/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package scratchbench;


import com.lowagie.text.Document;
import com.lowagie.text.pdf.DefaultFontMapper;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
import static java.awt.Toolkit.getDefaultToolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.EditorKit;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.UndoManager;
import jsyntaxpane.DefaultSyntaxKit;
import jsyntaxpane.SyntaxDocument;
import jsyntaxpane.Token;
import jsyntaxpane.actions.ActionUtils;
import jsyntaxpane.actions.CaretMonitor;
/**
 *
 * @author Kp Saluja
 */

public class MainMenu extends javax.swing.JFrame implements CaretListener{
public static ArrayList<JScrollPane> panes;
public static ArrayList<JEditorPane> editorPanes;

public UndoManager undo = new UndoManager();
private JEditorPane jep;
private JScrollPane jsp;
private String k;
public static int ct;
public static String path;
                JTabbedPane newTabbedPane= new JTabbedPane();
                 int inch = Toolkit.getDefaultToolkit().getScreenResolution();

  float pixelToPoint = (float) 72 / (float) inch;
    /**
     * Creates new form MainMenu
     */
public void caretUpdate(CaretEvent ce) {
           
         SyntaxDocument sDoc = ActionUtils.getSyntaxDocument(editorPanes.get(tabbedPane.getSelectedIndex()));
         if (sDoc != null) {
             Token t = sDoc.getTokenAt(ce.getDot());
             if (t != null) {
                 CharSequence tData = t.getText(sDoc);
                 if (t.length > 40) {
                     tData = tData.subSequence(0, 40);
                 }
                 caretPosLabel.setText(t.toString() + ": " + tData);
             } else {
                 // null token, remove the status
//				lblToken.setText(java.util.ResourceBundle.getBundle("jsyntaxpane/Bundle").getString("NO_TOKEN_AT_CURSOR"));
             }
         }
              }
  private void createTab()
  {
  jep=new JEditorPane();
  jsp=new JScrollPane(jep);
  panes.add(jsp);
 jep.setContentType("text/plain");
typeOFileLabel.setText("Plain Text File");
  editorPanes.add(jep);
  new CaretMonitor(jep, caretPosLabel);
     jep.addCaretListener(this);
jep.getDocument().addUndoableEditListener((UndoableEditEvent uee) -> {
    undo.addEdit(uee.getEdit());   });
  tabbedPane.addTab("*Tab "+(ct+1),jsp);
  tabbedPane.setSelectedIndex(ct);
  tabbedPane.setTabComponentAt(ct,new ButtonTabComponent(tabbedPane));
  ct++;
  setToolbar();
  }
   
    public MainMenu()
    {
       
        ct=0;
        DefaultSyntaxKit.initKit();
       panes=new ArrayList<JScrollPane>();
       editorPanes=new ArrayList<JEditorPane>();
       
        initComponents();
       // new CaretMonitor(jep, caretPosLabel);
createTab();
createsplitPane();
createPopupMenu();
jSplitPane1.getRightComponent().setMinimumSize(new Dimension());
jSplitPane1.setDividerLocation(1.0d);
 //Thread t=new Thread(new MyTimerTask());
    }
        

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        statusPanel = new javax.swing.JPanel();
        typeOFileLabel = new javax.swing.JLabel();
        caretPosLabel = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jSplitPane1 = new javax.swing.JSplitPane();
        tabbedPane = new javax.swing.JTabbedPane();
        jToolBar1 = new javax.swing.JToolBar();
        jSeparator4 = new javax.swing.JSeparator();
        menuBar = new javax.swing.JMenuBar();
        newMenu = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem9 = new javax.swing.JMenuItem();
        jMenuItem10 = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JPopupMenu.Separator();
        jMenuItem11 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem13 = new javax.swing.JMenuItem();
        jMenuItem14 = new javax.swing.JMenuItem();
        jMenuItem15 = new javax.swing.JMenuItem();
        jSeparator5 = new javax.swing.JPopupMenu.Separator();
        jMenuItem16 = new javax.swing.JMenuItem();
        jMenuItem17 = new javax.swing.JMenuItem();
        jMenu5 = new javax.swing.JMenu();
        jMenuItem18 = new javax.swing.JMenuItem();
        jMenuItem19 = new javax.swing.JMenuItem();
        cMenu1 = new javax.swing.JMenuItem();
        cppMenu1 = new javax.swing.JMenuItem();
        javaMenu1 = new javax.swing.JMenuItem();
        javaScript = new javax.swing.JMenuItem();
        jMenuItem21 = new javax.swing.JMenuItem();
        Properties = new javax.swing.JMenuItem();
        jMenuItem22 = new javax.swing.JMenuItem();
        jMenuItem23 = new javax.swing.JMenuItem();
        xmlMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem24 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Scratch|Bench");
        setIconImage(getDefaultToolkit().getImage("src/resources/icon.jpg"));
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });

        typeOFileLabel.setText("typeOFileLabel");

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(typeOFileLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(237, 237, 237)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 70, Short.MAX_VALUE)
                .addComponent(caretPosLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(typeOFileLabel)
                .addGap(0, 0, Short.MAX_VALUE))
            .addComponent(jSeparator2)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(caretPosLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jSplitPane1.setDividerLocation(2000);
        jSplitPane1.setResizeWeight(0.5);

        tabbedPane.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                tabbedPaneFocusGained(evt);
            }
        });
        jSplitPane1.setLeftComponent(tabbedPane);

        jToolBar1.setBorder(null);
        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        newMenu.setText("File");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("New");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        newMenu.add(jMenuItem1);

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setText("Open");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        newMenu.add(jMenuItem2);

        jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem5.setText("Save");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        newMenu.add(jMenuItem5);

        jMenuItem9.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem9.setText("Close");
        jMenuItem9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem9ActionPerformed(evt);
            }
        });
        newMenu.add(jMenuItem9);

        jMenuItem10.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem10.setText("Close All");
        jMenuItem10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem10ActionPerformed(evt);
            }
        });
        newMenu.add(jMenuItem10);
        newMenu.add(jSeparator3);

        jMenuItem11.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem11.setText("Export To Pdf");
        jMenuItem11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem11ActionPerformed(evt);
            }
        });
        newMenu.add(jMenuItem11);

        menuBar.add(newMenu);

        jMenu2.setText("Edit");

        jMenuItem13.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem13.setText("Cut");
        jMenuItem13.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem13ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem13);

        jMenuItem14.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem14.setText("Copy");
        jMenuItem14.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem14ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem14);

        jMenuItem15.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem15.setText("Paste");
        jMenuItem15.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem15ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem15);
        jMenu2.add(jSeparator5);

        jMenuItem16.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Z, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem16.setText("Undo");
        jMenuItem16.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem16ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem16);

        jMenuItem17.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Y, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem17.setText("Redo");
        jMenuItem17.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem17ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem17);

        menuBar.add(jMenu2);

        jMenu5.setText("Language");

        jMenuItem18.setText("Bash");
        jMenuItem18.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem18ActionPerformed(evt);
            }
        });
        jMenu5.add(jMenuItem18);

        jMenuItem19.setText("Batch");
        jMenuItem19.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem19ActionPerformed(evt);
            }
        });
        jMenu5.add(jMenuItem19);

        cMenu1.setText("C");
        cMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cMenu1ActionPerformed(evt);
            }
        });
        jMenu5.add(cMenu1);

        cppMenu1.setText("C++");
        cppMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cppMenu1ActionPerformed(evt);
            }
        });
        jMenu5.add(cppMenu1);

        javaMenu1.setText("Java");
        javaMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                javaMenu1ActionPerformed(evt);
            }
        });
        jMenu5.add(javaMenu1);

        javaScript.setText("Java Script");
        javaScript.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                javaScriptActionPerformed(evt);
            }
        });
        jMenu5.add(javaScript);

        jMenuItem21.setText("Python");
        jMenuItem21.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem21ActionPerformed(evt);
            }
        });
        jMenu5.add(jMenuItem21);

        Properties.setText("Properties");
        Properties.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PropertiesActionPerformed(evt);
            }
        });
        jMenu5.add(Properties);

        jMenuItem22.setText("Ruby");
        jMenuItem22.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem22ActionPerformed(evt);
            }
        });
        jMenu5.add(jMenuItem22);

        jMenuItem23.setText("SQL");
        jMenuItem23.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem23ActionPerformed(evt);
            }
        });
        jMenu5.add(jMenuItem23);

        xmlMenuItem1.setText("XML");
        xmlMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                xmlMenuItem1ActionPerformed(evt);
            }
        });
        jMenu5.add(xmlMenuItem1);

        jMenuItem24.setText("Clear Formatting");
        jMenuItem24.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem24ActionPerformed(evt);
            }
        });
        jMenu5.add(jMenuItem24);

        menuBar.add(jMenu5);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jSeparator4)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jSplitPane1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 1, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed

        createTab();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
     JFileChooser open = new JFileChooser(); // open up a file chooser (a dialog for the user to browse files to open)
			int option = open.showOpenDialog(this); // get the option that the user selected (approve or cancel)
			// NOTE: because we are OPENing a file, we call showOpenDialog~
			// if the user clicked OK, we have "APPROVE_OPTION"
			// so we want to open the file
			if (option == JFileChooser.APPROVE_OPTION) {
				jep.setText(""); // clear the TextArea before applying the file contents
				try {
					// create a scanner to read the file (getSelectedFile().getPath() will get the path to the file)                                      
                                    String fname=open.getSelectedFile().getName();
                                        String extension=getExtension(fname);
                                        path=open.getSelectedFile().getPath();
                                        System.out.println(path);
                                        Scanner scan=null;
                                        if(id.testint==1)
                                        {
                                        if(extension.equalsIgnoreCase("des"))
                                        {
                                         FileDecryption.decrypt(path);
						path=path+".txt";
						System.out.println("After ddecrypption"+path);
						 scan = new Scanner(new FileReader(path));
					//	while (scan.hasNext())  
                                        // jep.setText(jep.getText()+scan.nextLine()+ "\n");    
                                        }
                                        else
                                        {
                                         scan = new Scanner(new FileReader(path));
                                        }
                                        }
                                        else
                                        {
                                         scan = new Scanner(new FileReader(path));
                                        } 
                                        createTab();
                                        int index=tabbedPane.getSelectedIndex();
                                        tabbedPane.setTitleAt(index, fname);
                                        if(extension.equalsIgnoreCase("java"))
                                        {
                                        editorPanes.get(index).setContentType("text/java");
                                        typeOFileLabel.setText("Java");
                                        } else
                                        if(extension.equalsIgnoreCase("sh"))
                                        {
                                        editorPanes.get(index).setContentType("text/bash");
                                        typeOFileLabel.setText("Linux Shell Script");
                                        } else
                                        if(extension.equalsIgnoreCase("bat")||extension.equalsIgnoreCase("cmd"))
                                        {
                                        editorPanes.get(index).setContentType("text/dosbatch");
                                        typeOFileLabel.setText("Dos File (Windows)");
                                        }else 
                                        if(extension.equalsIgnoreCase("c"))
                                        {
                                        editorPanes.get(index).setContentType("text/c");
                                        typeOFileLabel.setText("C File");
                                        }else
                                        if(extension.equalsIgnoreCase("cpp"))
                                        {
                                        editorPanes.get(index).setContentType("text/cpp");
                                        typeOFileLabel.setText("C++ File");
                                        }else
                                        if(extension.equalsIgnoreCase("js"))
                                        {
                                        editorPanes.get(index).setContentType("text/javascript");
                                        typeOFileLabel.setText("JavaScript File");
                                        }else
                                        if(extension.equalsIgnoreCase("py"))
                                        {
                                        editorPanes.get(index).setContentType("text/python");
                                        typeOFileLabel.setText("Python File");
                                        }else
                                        if(extension.equalsIgnoreCase("properties"))
                                        {
                                        editorPanes.get(index).setContentType("text/properties");
                                        typeOFileLabel.setText("Properties File");
                                        }else
                                        if(extension.equalsIgnoreCase("rb"))
                                        {
                                        editorPanes.get(index).setContentType("text/ruby");
                                        typeOFileLabel.setText("Ruby File");
                                        }else
                                        if(extension.equalsIgnoreCase("sql"))
                                        {
                                        editorPanes.get(index).setContentType("text/sql");
                                         typeOFileLabel.setText("SQL File");
                                        }else
                                        if(extension.equalsIgnoreCase("xml"))
                                        {
                                        editorPanes.get(index).setContentType("text/xml");
                                         typeOFileLabel.setText("XML File");
                                        }else{
                                             editorPanes.get(index).setContentType("text/plain");
                                         typeOFileLabel.setText("Plain Text");
                                        }
                                        setToolbar();
					while (scan.hasNext()||scan.hasNextLine()) // while there's still something to read
                                        {
                                        jep.setText(jep.getText()+scan.nextLine()+ "\n");    
                                        }
                                        setToolbar();
                                        
						//jep.append(scan.nextLine() + "\n"); // append the line to the TextArea
				} catch (Exception ex) { // catch any exceptions, and...
					// ...write to the debug console
					System.out.println(ex.getMessage());
				}
			}
            
       
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void cMenu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cMenu1ActionPerformed
        int i=tabbedPane.getSelectedIndex();
        k=editorPanes.get(i).getText();
        editorPanes.get(i).setContentType("text/c");
        editorPanes.get(i).setText(k);   // TODO add your handling code here:
         setToolbar();
         typeOFileLabel.setText("C File");
    }//GEN-LAST:event_cMenu1ActionPerformed

    private void cppMenu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cppMenu1ActionPerformed
       int i=tabbedPane.getSelectedIndex();
        k=editorPanes.get(i).getText();
        editorPanes.get(i).setContentType("text/cpp");
        editorPanes.get(i).setText(k);
         setToolbar();// TODO add your handling code here:
         typeOFileLabel.setText("C++ File");
    }//GEN-LAST:event_cppMenu1ActionPerformed

    private void javaMenu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_javaMenu1ActionPerformed
        int i=tabbedPane.getSelectedIndex();
        k=editorPanes.get(i).getText();
        editorPanes.get(i).setContentType("text/java");
        editorPanes.get(i).setText(k); 
        typeOFileLabel.setText("Java File");
    }//GEN-LAST:event_javaMenu1ActionPerformed

    private void javaScriptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_javaScriptActionPerformed
        int i=tabbedPane.getSelectedIndex();
        k=editorPanes.get(i).getText();
        editorPanes.get(i).setContentType("text/javascript");
        editorPanes.get(i).setText(k);  
         setToolbar();// TODO add your handling code here:
         typeOFileLabel.setText("Java Script File");
    }//GEN-LAST:event_javaScriptActionPerformed

    private void jMenuItem21ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem21ActionPerformed
       int i=tabbedPane.getSelectedIndex();
        k=editorPanes.get(i).getText();
        editorPanes.get(i).setContentType("text/python");
        editorPanes.get(i).setText(k); 
         setToolbar();// TODO add your handling code here:
         typeOFileLabel.setText("Python File");
    }//GEN-LAST:event_jMenuItem21ActionPerformed

    private void PropertiesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PropertiesActionPerformed
       int i=tabbedPane.getSelectedIndex();
        k=editorPanes.get(i).getText();
        editorPanes.get(i).setContentType("text/properties");
        editorPanes.get(i).setText(k); // TODO add your handling code here:
         setToolbar();
         typeOFileLabel.setText("properties File");
    }//GEN-LAST:event_PropertiesActionPerformed

    private void jMenuItem22ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem22ActionPerformed
        int i=tabbedPane.getSelectedIndex();
        k=editorPanes.get(i).getText();
        editorPanes.get(i).setContentType("text/ruby");
        editorPanes.get(i).setText(k);
         setToolbar();// TODO add your handling code here:
         typeOFileLabel.setText("Ruby File");
    }//GEN-LAST:event_jMenuItem22ActionPerformed

    private void jMenuItem23ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem23ActionPerformed
      int i=tabbedPane.getSelectedIndex();
        k=editorPanes.get(i).getText();
        editorPanes.get(i).setContentType("text/sql");
        editorPanes.get(i).setText(k); 
         setToolbar();// TODO add your handling code here:
         typeOFileLabel.setText("SQL file");
    }//GEN-LAST:event_jMenuItem23ActionPerformed

    private void xmlMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_xmlMenuItem1ActionPerformed
      int i=tabbedPane.getSelectedIndex();
        k=editorPanes.get(i).getText();
        editorPanes.get(i).setContentType("text/xml");
        editorPanes.get(i).setText(k);
         setToolbar();
         typeOFileLabel.setText("XML File");
    }//GEN-LAST:event_xmlMenuItem1ActionPerformed

    private void jMenuItem24ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem24ActionPerformed
       int i=tabbedPane.getSelectedIndex();
        k=editorPanes.get(i).getText();
        editorPanes.get(i).setContentType("text/plain");
        editorPanes.get(i).setText(k);
         setToolbar();
         typeOFileLabel.setText("Plain Text");
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem24ActionPerformed

    private void jMenuItem18ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem18ActionPerformed
        int i=tabbedPane.getSelectedIndex();
        k=editorPanes.get(i).getText();
        editorPanes.get(i).setContentType("text/bash");
        editorPanes.get(i).setText(k);  // TODO add your handling code here:
        setToolbar();
        typeOFileLabel.setText("Linux Shell Script");
    }//GEN-LAST:event_jMenuItem18ActionPerformed

    private void jMenuItem19ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem19ActionPerformed
     int i=tabbedPane.getSelectedIndex();
        k=editorPanes.get(i).getText();
        editorPanes.get(i).setContentType("text/dosbatch");
        editorPanes.get(i).setText(k);  
        // TODO add your handling code here:
         setToolbar();
         typeOFileLabel.setText("Dos File (Windows)");
    }//GEN-LAST:event_jMenuItem19ActionPerformed

    private void jMenuItem13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem13ActionPerformed
editorPanes.get(tabbedPane.getSelectedIndex()).cut();        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem13ActionPerformed

    private void jMenuItem14ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem14ActionPerformed
editorPanes.get(tabbedPane.getSelectedIndex()).copy();        // TODO add your handling code here:
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem14ActionPerformed

    private void jMenuItem15ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem15ActionPerformed
editorPanes.get(tabbedPane.getSelectedIndex()).paste();       // TODO add your handling code here:
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem15ActionPerformed

    private void jMenuItem16ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem16ActionPerformed
 try {
            undo.undo();
        } catch (Exception e) {
           
        }      // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem16ActionPerformed

    private void jMenuItem17ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem17ActionPerformed
 try {
            undo.redo();
        } catch (Exception cre) {
            
        }      // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem17ActionPerformed

    private void jMenuItem9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem9ActionPerformed
int i=tabbedPane.getSelectedIndex();
        tabbedPane.remove(i);
panes.remove(i);
editorPanes.remove(i);
ct++;
// TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem9ActionPerformed

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed

			JFileChooser save = new JFileChooser(); // again, open a file chooser
			int option = save.showSaveDialog(this); // similar to the open file, only this time we call
			// showSaveDialog instead of showOpenDialog
			// if the user clicked OK (and not cancel)
			if (option == JFileChooser.APPROVE_OPTION) {
				try {
					// create a buffered writer to write to a file
                                    path=save.getSelectedFile().getPath();
                                    System.out.println(path);
					BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));
                                        System.out.println(editorPanes.get(tabbedPane.getSelectedIndex()).getText());
					out.write(editorPanes.get(tabbedPane.getSelectedIndex()).getText()); // write the contents of the TextArea to the file
					out.close(); // close the file stream
                                        if(id.testint==1)
                                       {
                                       FileEncryption.encrypt1(path);
                                         JOptionPane.showMessageDialog (null, "File Encrypted Successfully."+"\n"+"Kindly delete the file at "+path+" if needed", "File Saved", JOptionPane.INFORMATION_MESSAGE);
                                       }
                                      
				} catch (Exception ex) { // again, catch any exceptions and...
					// ...write to the debug console
					System.out.println(ex.getMessage());
				}
			}
		        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem5ActionPerformed

    private void jMenuItem10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem10ActionPerformed
tabbedPane.removeAll();    
panes.clear();
editorPanes.clear();
    }//GEN-LAST:event_jMenuItem10ActionPerformed

    private void tabbedPaneFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tabbedPaneFocusGained
        int i=tabbedPane.getSelectedIndex();
        editorPanes.get(i).requestFocus();
    }//GEN-LAST:event_tabbedPaneFocusGained

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
if(jSplitPane1.getRightComponent()!=newTabbedPane)
{     jSplitPane1.getRightComponent().setMinimumSize(new Dimension());
jSplitPane1.setDividerLocation(1.0d); 
}// TODO add your handling code here:
    }//GEN-LAST:event_formComponentResized

    private void jMenuItem11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem11ActionPerformed

        JFileChooser fileChooser=new JFileChooser();
		fileChooser.setDialogTitle("Speciify a file to Save");
		int userSelection=fileChooser.showSaveDialog(this);
		if(userSelection==JFileChooser.APPROVE_OPTION)
		{
			File fileToSave=fileChooser.getSelectedFile();
			fileToSave.getAbsolutePath();
        if(!fileToSave.exists())
        {
                            try {
                                fileToSave.createNewFile();
                            } catch (IOException ex) {
                                Logger.getLogger(MainMenu.class.getName()).log(Level.SEVERE, null, ex);
                            }
        }
			paintToPDF(editorPanes.get(tabbedPane.getSelectedIndex()),fileToSave);
		}
// TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem11ActionPerformed


        public float convertToPoints(int pixels) {
    return (float) (pixels * pixelToPoint);
  }

  public float convertToPixels(int points) {
    return (float) (points / pixelToPoint);
  }
	
    public void paintToPDF(JEditorPane jep,File file) {
    try {
      jep.setBounds(0, 0, (int) convertToPixels(612 - 58), (int) convertToPixels(792 - 60));

      Document document = new Document();
      FileOutputStream fos = new FileOutputStream(file);
      PdfWriter writer = PdfWriter.getInstance(document, fos);

      document.setPageSize(new com.lowagie.text.Rectangle(612, 792));
      document.open();
      PdfContentByte cb = writer.getDirectContent();

      cb.saveState();
      cb.concatCTM(1, 0, 0, 1, 0, 0);

      DefaultFontMapper mapper = new DefaultFontMapper();
      mapper.insertDirectory("c:/windows/fonts");

      Graphics2D g2 = cb.createGraphics(612, 792, mapper, true, .95f);

      AffineTransform at = new AffineTransform();
      at.translate(convertToPixels(20), convertToPixels(20));
      at.scale(pixelToPoint, pixelToPoint);

      g2.transform(at);

      g2.setColor(Color.WHITE);
      g2.fill(jep.getBounds());

      Rectangle alloc = getVisibleEditorRect(jep);
      jep.getUI().getRootView(jep).paint(g2, alloc);

      g2.setColor(Color.BLACK);
      g2.draw(jep.getBounds());

      g2.dispose();
      cb.restoreState();
      document.close();
      fos.flush();
      fos.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
    
      protected Rectangle getVisibleEditorRect(JEditorPane jep) {
    Rectangle alloc = jep.getBounds();
    if ((alloc.width > 0) && (alloc.height > 0)) {
      alloc.x = alloc.y = 0;
      Insets insets = jep.getInsets();
      alloc.x += insets.left;
      alloc.y += insets.top;
      alloc.width -= insets.left + insets.right;
      alloc.height -= insets.top + insets.bottom;
      return alloc;
    }
    return null;
  }
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainMenu().setVisible(false);
            }
        });
    }
    public String getExtension(String name)
	{
		int i=name.length();
		i--;
		while(i>=0)
		{
			if(name.charAt(i)=='.')
			{
				break;
			}
			i--;
		}
		i++;
		StringBuffer sb=new StringBuffer();
		while(i<name.length())
		{
			sb.append(name.charAt(i));
			i++;
		}
		return sb.toString();
		}
	public String getName(String name)
	{
		int i=name.length();
		i--;
		while(name.charAt(i)!='.')
		{
			i--;
		}
		StringBuffer sb=new StringBuffer();
		int j=0;
		while(j<i)
		{
			sb.append(name.charAt(j));
			j++;
		}
		return sb.toString();
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem Properties;
    private javax.swing.JMenuItem cMenu1;
    private javax.swing.JLabel caretPosLabel;
    private javax.swing.JMenuItem cppMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem11;
    private javax.swing.JMenuItem jMenuItem13;
    private javax.swing.JMenuItem jMenuItem14;
    private javax.swing.JMenuItem jMenuItem15;
    private javax.swing.JMenuItem jMenuItem16;
    private javax.swing.JMenuItem jMenuItem17;
    private javax.swing.JMenuItem jMenuItem18;
    private javax.swing.JMenuItem jMenuItem19;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem21;
    private javax.swing.JMenuItem jMenuItem22;
    private javax.swing.JMenuItem jMenuItem23;
    private javax.swing.JMenuItem jMenuItem24;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JPopupMenu.Separator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JPopupMenu.Separator jSeparator5;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JMenuItem javaMenu1;
    private javax.swing.JMenuItem javaScript;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenu newMenu;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JTabbedPane tabbedPane;
    private javax.swing.JLabel typeOFileLabel;
    private javax.swing.JMenuItem xmlMenuItem1;
    // End of variables declaration//GEN-END:variables

    private void setToolbar() {
        jToolBar1.removeAll();
       EditorKit kit = editorPanes.get(tabbedPane.getSelectedIndex()).getEditorKit();
			if (kit instanceof DefaultSyntaxKit) {
				DefaultSyntaxKit defaultSyntaxKit = (DefaultSyntaxKit) kit;
				defaultSyntaxKit.addToolBarActions(editorPanes.get(tabbedPane.getSelectedIndex()), jToolBar1);
			}
			jToolBar1.validate();
    }

    private void createsplitPane() {
      

    }

    private void createPopupMenu() {
              JPopupMenu pop_up = new JPopupMenu();
              
 JMenuItem openToSplit = new JMenuItem("Open in SplitPane");
 openToSplit.addActionListener(new ActionListener()
 {
                @Override
                  public void actionPerformed(ActionEvent ae) {
                    remove(tabbedPane);
                    int i=tabbedPane.getSelectedIndex();
                    String k=tabbedPane.getTitleAt(i);
                    tabbedPane.remove(i);
            newTabbedPane.add(k,panes.get(i));
            newTabbedPane.setTabComponentAt(newTabbedPane.getSelectedIndex(),new ButtonTabComponent(newTabbedPane));
          jSplitPane1.setLeftComponent(tabbedPane);
          jSplitPane1.setRightComponent(newTabbedPane);
      
                   // JSplitPane jSplitPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,tabbedPane,newTabbedPane);
                    
                 //   add(jSplitPane);
                  }
 
 });
 pop_up.add(openToSplit);
  tabbedPane.setComponentPopupMenu(pop_up);
    }

    void saveAll() {
        JScrollPane jspp;
        JEditorPane hepp;
        int x=0;
        File file=new File("C://sb");
        if(!file.exists())
        {
            file.mkdir();
        }
        try
        {
        
       
       while(x<tabbedPane.getTabCount())
        {

        jspp=(JScrollPane) tabbedPane.getComponentAt(x);
        hepp=(JEditorPane) jspp.getViewport().getView();
        file=new File("c:\\sb"+x+".txt");
        System.out.println("File  pointer Created");
        file.createNewFile();
        System.out.println("File Created");
        BufferedWriter out = new BufferedWriter(new FileWriter(file));
	out.write(hepp.getText()); // write the contents of the TextArea to the file
	out.close();
        x++;
    }
       System.out.println(tabbedPane.getTabCount());
        }
        catch(Exception exception)
        {
            System.out.println(exception);
        }
    }
   

}