JNotepad - JNotePad.java
Cerca
 











JNotePad.java

Caricato da: Paoloricciuti
Scarica il programma completo

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. /*
  7.  * JNotePad.java
  8.  *
  9.  * Created on 10-giu-2010, 12.21.40
  10.  */
  11. package jnotepad.graphics;
  12.  
  13. import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
  14. import java.awt.Toolkit;
  15. import java.awt.datatransfer.Clipboard;
  16. import java.awt.datatransfer.DataFlavor;
  17. import java.awt.datatransfer.Transferable;
  18. import java.awt.datatransfer.UnsupportedFlavorException;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.util.ArrayList;
  22. import java.util.Calendar;
  23. import java.util.Date;
  24. import java.util.GregorianCalendar;
  25. import java.util.Locale;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28. import javax.swing.ImageIcon;
  29. import javax.swing.JFileChooser;
  30. import javax.swing.JFrame;
  31. import javax.swing.JLabel;
  32. import javax.swing.JOptionPane;
  33. import javax.swing.KeyStroke;
  34. import javax.swing.TransferHandler;
  35. import javax.swing.UIManager;
  36. import javax.swing.UnsupportedLookAndFeelException;
  37. import javax.swing.filechooser.FileFilter;
  38. import jnotepad.FileManager;
  39.  
  40. /**
  41.  *
  42.  * @author Paolo
  43.  */
  44. public class JNotePad extends javax.swing.JFrame {
  45.  
  46.     private FileManager manager;
  47.     private boolean salvato;
  48.     private boolean creato;
  49.     private boolean getUndo;
  50.     private ArrayList<String> storiaContent;
  51.     private int idContent;
  52.  
  53.     /** Creates new form JNotePad */
  54.     public JNotePad() {
  55.         this.manager = new FileManager();
  56.         this.salvato = false;
  57.         this.getUndo = true;
  58.         try {
  59.             UIManager.setLookAndFeel(new WindowsLookAndFeel());
  60.         } catch (UnsupportedLookAndFeelException ex) {
  61.         }
  62.         initComponents();
  63.         this.removeControlH();
  64.         this.newFile();
  65.         this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/jnotepad/graphics/images/Notepad.png")));
  66.     }
  67.  
  68.     private void removeControlH() {
  69.         KeyStroke ks = KeyStroke.getKeyStroke("control H");
  70.         this.textArea.getInputMap().put(ks, "none");
  71.     }
  72.  
  73.     private void newFile() {
  74.         this.setTitle("Senza nome - JNotepad");
  75.         this.textArea.setText("");
  76.         this.manager = new FileManager();
  77.         this.salvato = false;
  78.         this.creato = true;
  79.         this.storiaContent = new ArrayList<String>();
  80.         this.storiaContent.add("");
  81.         this.idContent = 0;
  82.     }
  83.  
  84.     private boolean checkSave() {
  85.         if (!this.salvato && !this.textArea.getText().isEmpty()) {
  86.             int retval = JOptionPane.showConfirmDialog(this, "Il file non è stato salvato. Salvare?");
  87.             if (retval == 0) {
  88.                 this.salvaActionPerformed(null);
  89.                 return true;
  90.             } else if (retval == 1) {
  91.                 return true;
  92.             } else {
  93.                 return false;
  94.             }
  95.         }
  96.         return true;
  97.     }
  98.  
  99.     /** This method is called from within the constructor to
  100.      * initialize the form.
  101.      * WARNING: Do NOT modify this code. The content of this method is
  102.      * always regenerated by the Form Editor.
  103.      */
  104.     @SuppressWarnings("unchecked")
  105.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  106.     private void initComponents() {
  107.  
  108.         gruppo = new javax.swing.ButtonGroup();
  109.         trovaFrame = new javax.swing.JFrame();
  110.         panello = new javax.swing.JPanel();
  111.         trovaLabel = new javax.swing.JLabel();
  112.         daTrovare = new javax.swing.JTextField();
  113.         upperLower = new javax.swing.JCheckBox();
  114.         jPanel1 = new javax.swing.JPanel();
  115.         su = new javax.swing.JRadioButton();
  116.         giu = new javax.swing.JRadioButton();
  117.         trovaButton = new javax.swing.JButton();
  118.         annullaButton = new javax.swing.JButton();
  119.         sostituisciLabel = new javax.swing.JLabel();
  120.         daSostituire = new javax.swing.JTextField();
  121.         sostituisciButton = new javax.swing.JButton();
  122.         sostituisciTutto = new javax.swing.JButton();
  123.         panel = new javax.swing.JPanel();
  124.         jScrollPane1 = new javax.swing.JScrollPane();
  125.         textArea = new javax.swing.JTextArea();
  126.         Menu = new javax.swing.JMenuBar();
  127.         File = new javax.swing.JMenu();
  128.         file = new javax.swing.JMenuItem();
  129.         apri = new javax.swing.JMenuItem();
  130.         salva = new javax.swing.JMenuItem();
  131.         salvanome = new javax.swing.JMenuItem();
  132.         jSeparator1 = new javax.swing.JSeparator();
  133.         esci = new javax.swing.JMenuItem();
  134.         Modifica = new javax.swing.JMenu();
  135.         annulla = new javax.swing.JMenuItem();
  136.         jSeparator2 = new javax.swing.JSeparator();
  137.         taglia = new javax.swing.JMenuItem();
  138.         copia = new javax.swing.JMenuItem();
  139.         incolla = new javax.swing.JMenuItem();
  140.         jSeparator3 = new javax.swing.JSeparator();
  141.         trova = new javax.swing.JMenuItem();
  142.         sostituisci = new javax.swing.JMenuItem();
  143.         selezionaTutto = new javax.swing.JMenuItem();
  144.         oradata = new javax.swing.JMenuItem();
  145.         Formato = new javax.swing.JMenu();
  146.         aCapo = new javax.swing.JCheckBoxMenuItem();
  147.         Help = new javax.swing.JMenu();
  148.         info = new javax.swing.JMenuItem();
  149.  
  150.         trovaLabel.setText("Trova: ");
  151.  
  152.         daTrovare.addCaretListener(new javax.swing.event.CaretListener() {
  153.             public void caretUpdate(javax.swing.event.CaretEvent evt) {
  154.                 daTrovareCaretUpdate(evt);
  155.             }
  156.         });
  157.  
  158.         upperLower.setText("Maiuscole/Minuscole");
  159.  
  160.         jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Direzione"));
  161.  
  162.         gruppo.add(su);
  163.         su.setSelected(true);
  164.         su.setText("Su");
  165.  
  166.         gruppo.add(giu);
  167.         giu.setText("Giu");
  168.  
  169.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  170.         jPanel1.setLayout(jPanel1Layout);
  171.         jPanel1Layout.setHorizontalGroup(
  172.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  173.             .addGroup(jPanel1Layout.createSequentialGroup()
  174.                 .addContainerGap()
  175.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  176.                     .addComponent(su)
  177.                     .addComponent(giu))
  178.                 .addContainerGap(41, Short.MAX_VALUE))
  179.         );
  180.         jPanel1Layout.setVerticalGroup(
  181.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  182.             .addGroup(jPanel1Layout.createSequentialGroup()
  183.                 .addComponent(su)
  184.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
  185.                 .addComponent(giu))
  186.         );
  187.  
  188.         trovaButton.setText("Trova successivo");
  189.         trovaButton.setEnabled(false);
  190.         trovaButton.addActionListener(new java.awt.event.ActionListener() {
  191.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  192.                 trovaButtonActionPerformed(evt);
  193.             }
  194.         });
  195.  
  196.         annullaButton.setText("Annulla");
  197.         annullaButton.addActionListener(new java.awt.event.ActionListener() {
  198.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  199.                 annullaButtonActionPerformed(evt);
  200.             }
  201.         });
  202.  
  203.         sostituisciLabel.setText("Sostituisci: ");
  204.  
  205.         sostituisciButton.setText("Sostituisci");
  206.         sostituisciButton.setEnabled(false);
  207.         sostituisciButton.addActionListener(new java.awt.event.ActionListener() {
  208.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  209.                 sostituisciButtonActionPerformed(evt);
  210.             }
  211.         });
  212.  
  213.         sostituisciTutto.setText("Sostituisci tutto");
  214.         sostituisciTutto.setEnabled(false);
  215.         sostituisciTutto.addActionListener(new java.awt.event.ActionListener() {
  216.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  217.                 sostituisciTuttoActionPerformed(evt);
  218.             }
  219.         });
  220.  
  221.         javax.swing.GroupLayout panelloLayout = new javax.swing.GroupLayout(panello);
  222.         panello.setLayout(panelloLayout);
  223.         panelloLayout.setHorizontalGroup(
  224.             panelloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  225.             .addGroup(panelloLayout.createSequentialGroup()
  226.                 .addContainerGap()
  227.                 .addGroup(panelloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  228.                     .addComponent(upperLower)
  229.                     .addGroup(panelloLayout.createSequentialGroup()
  230.                         .addGroup(panelloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  231.                             .addComponent(sostituisciLabel)
  232.                             .addComponent(trovaLabel))
  233.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  234.                         .addGroup(panelloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  235.                             .addComponent(daSostituire)
  236.                             .addComponent(daTrovare, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))))
  237.                 .addGap(27, 27, 27)
  238.                 .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  239.                 .addGap(48, 48, 48)
  240.                 .addGroup(panelloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  241.                     .addComponent(sostituisciTutto, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  242.                     .addComponent(sostituisciButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  243.                     .addComponent(annullaButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  244.                     .addComponent(trovaButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  245.                 .addContainerGap(20, Short.MAX_VALUE))
  246.         );
  247.         panelloLayout.setVerticalGroup(
  248.             panelloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  249.             .addGroup(panelloLayout.createSequentialGroup()
  250.                 .addContainerGap()
  251.                 .addGroup(panelloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  252.                     .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  253.                     .addGroup(panelloLayout.createSequentialGroup()
  254.                         .addComponent(trovaButton)
  255.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  256.                         .addComponent(annullaButton)
  257.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  258.                         .addComponent(sostituisciButton)
  259.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  260.                         .addComponent(sostituisciTutto))
  261.                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelloLayout.createSequentialGroup()
  262.                         .addGroup(panelloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  263.                             .addComponent(trovaLabel)
  264.                             .addComponent(daTrovare, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  265.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  266.                         .addGroup(panelloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  267.                             .addComponent(sostituisciLabel)
  268.                             .addComponent(daSostituire, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  269.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
  270.                         .addComponent(upperLower)))
  271.                 .addGap(24, 24, 24))
  272.         );
  273.  
  274.         javax.swing.GroupLayout trovaFrameLayout = new javax.swing.GroupLayout(trovaFrame.getContentPane());
  275.         trovaFrame.getContentPane().setLayout(trovaFrameLayout);
  276.         trovaFrameLayout.setHorizontalGroup(
  277.             trovaFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  278.             .addComponent(panello, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  279.         );
  280.         trovaFrameLayout.setVerticalGroup(
  281.             trovaFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  282.             .addComponent(panello, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  283.         );
  284.  
  285.         setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
  286.         addWindowListener(new java.awt.event.WindowAdapter() {
  287.             public void windowClosing(java.awt.event.WindowEvent evt) {
  288.                 formWindowClosing(evt);
  289.             }
  290.         });
  291.  
  292.         textArea.setColumns(20);
  293.         textArea.setRows(5);
  294.         textArea.addCaretListener(new javax.swing.event.CaretListener() {
  295.             public void caretUpdate(javax.swing.event.CaretEvent evt) {
  296.                 textAreaCaretUpdate(evt);
  297.             }
  298.         });
  299.         jScrollPane1.setViewportView(textArea);
  300.  
  301.         javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
  302.         panel.setLayout(panelLayout);
  303.         panelLayout.setHorizontalGroup(
  304.             panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  305.             .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 955, Short.MAX_VALUE)
  306.         );
  307.         panelLayout.setVerticalGroup(
  308.             panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  309.             .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 626, Short.MAX_VALUE)
  310.         );
  311.  
  312.         File.setText("File");
  313.  
  314.         file.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
  315.         file.setText("Nuovo");
  316.         file.addActionListener(new java.awt.event.ActionListener() {
  317.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  318.                 fileActionPerformed(evt);
  319.             }
  320.         });
  321.         File.add(file);
  322.  
  323.         apri.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F12, java.awt.event.InputEvent.CTRL_MASK));
  324.         apri.setText("Apri");
  325.         apri.addActionListener(new java.awt.event.ActionListener() {
  326.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  327.                 apriActionPerformed(evt);
  328.             }
  329.         });
  330.         File.add(apri);
  331.  
  332.         salva.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F12, java.awt.event.InputEvent.SHIFT_MASK));
  333.         salva.setText("Salva");
  334.         salva.addActionListener(new java.awt.event.ActionListener() {
  335.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  336.                 salvaActionPerformed(evt);
  337.             }
  338.         });
  339.         File.add(salva);
  340.  
  341.         salvanome.setText("Salva con nome...");
  342.         salvanome.addActionListener(new java.awt.event.ActionListener() {
  343.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  344.                 salvanomeActionPerformed(evt);
  345.             }
  346.         });
  347.         File.add(salvanome);
  348.         File.add(jSeparator1);
  349.  
  350.         esci.setText("Esci");
  351.         esci.addActionListener(new java.awt.event.ActionListener() {
  352.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  353.                 esciActionPerformed(evt);
  354.             }
  355.         });
  356.         File.add(esci);
  357.  
  358.         Menu.add(File);
  359.  
  360.         Modifica.setText("Modifica");
  361.  
  362.         annulla.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Z, java.awt.event.InputEvent.CTRL_MASK));
  363.         annulla.setText("Annulla");
  364.         annulla.addActionListener(new java.awt.event.ActionListener() {
  365.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  366.                 annullaActionPerformed(evt);
  367.             }
  368.         });
  369.         Modifica.add(annulla);
  370.         Modifica.add(jSeparator2);
  371.  
  372.         taglia.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
  373.         taglia.setText("Taglia");
  374.         taglia.addActionListener(new java.awt.event.ActionListener() {
  375.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  376.                 tagliaActionPerformed(evt);
  377.             }
  378.         });
  379.         Modifica.add(taglia);
  380.  
  381.         copia.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
  382.         copia.setText("Copia");
  383.         copia.addActionListener(new java.awt.event.ActionListener() {
  384.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  385.                 copiaActionPerformed(evt);
  386.             }
  387.         });
  388.         Modifica.add(copia);
  389.  
  390.         incolla.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
  391.         incolla.setText("Incolla");
  392.         incolla.addActionListener(new java.awt.event.ActionListener() {
  393.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  394.                 incollaActionPerformed(evt);
  395.             }
  396.         });
  397.         Modifica.add(incolla);
  398.         Modifica.add(jSeparator3);
  399.  
  400.         trova.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.CTRL_MASK));
  401.         trova.setText("Trova");
  402.         trova.addActionListener(new java.awt.event.ActionListener() {
  403.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  404.                 trovaActionPerformed(evt);
  405.             }
  406.         });
  407.         Modifica.add(trova);
  408.  
  409.         sostituisci.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_H, java.awt.event.InputEvent.CTRL_MASK));
  410.         sostituisci.setText("Sostituisci");
  411.         sostituisci.addActionListener(new java.awt.event.ActionListener() {
  412.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  413.                 sostituisciActionPerformed(evt);
  414.             }
  415.         });
  416.         Modifica.add(sostituisci);
  417.  
  418.         selezionaTutto.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
  419.         selezionaTutto.setText("Seleziona tutto");
  420.         selezionaTutto.addActionListener(new java.awt.event.ActionListener() {
  421.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  422.                 selezionaTuttoActionPerformed(evt);
  423.             }
  424.         });
  425.         Modifica.add(selezionaTutto);
  426.  
  427.         oradata.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F5, 0));
  428.         oradata.setText("Ora/Data");
  429.         oradata.addActionListener(new java.awt.event.ActionListener() {
  430.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  431.                 oradataActionPerformed(evt);
  432.             }
  433.         });
  434.         Modifica.add(oradata);
  435.  
  436.         Menu.add(Modifica);
  437.  
  438.         Formato.setText("Formato");
  439.  
  440.         aCapo.setText("A capo automatico");
  441.         aCapo.addActionListener(new java.awt.event.ActionListener() {
  442.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  443.                 aCapoActionPerformed(evt);
  444.             }
  445.         });
  446.         Formato.add(aCapo);
  447.  
  448.         Menu.add(Formato);
  449.  
  450.         Help.setText("?");
  451.  
  452.         info.setText("Informazioni su JNotepad");
  453.         info.addActionListener(new java.awt.event.ActionListener() {
  454.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  455.                 infoActionPerformed(evt);
  456.             }
  457.         });
  458.         Help.add(info);
  459.  
  460.         Menu.add(Help);
  461.  
  462.         setJMenuBar(Menu);
  463.  
  464.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  465.         getContentPane().setLayout(layout);
  466.         layout.setHorizontalGroup(
  467.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  468.             .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  469.         );
  470.         layout.setVerticalGroup(
  471.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  472.             .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  473.         );
  474.  
  475.         pack();
  476.     }// </editor-fold>//GEN-END:initComponents
  477.  
  478.     private void fileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileActionPerformed
  479.         if (this.checkSave()) {
  480.             this.newFile();
  481.         }
  482.     }//GEN-LAST:event_fileActionPerformed
  483.  
  484.     private void apriActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_apriActionPerformed
  485.         JFileChooser apriFile = new JFileChooser();
  486.         apriFile.removeChoosableFileFilter(apriFile.getAcceptAllFileFilter());
  487.         apriFile.setFileFilter(new FileFilter() {
  488.  
  489.             @Override
  490.             public boolean accept(File f) {
  491.                 String path = f.getPath();
  492.                 String estensione = path.substring((path.length() - 3), path.length());
  493.                 if (estensione.toLowerCase().equals("txt") || f.isDirectory()) {
  494.                     return true;
  495.                 }
  496.                 return false;
  497.             }
  498.  
  499.             @Override
  500.             public String getDescription() {
  501.                 return "File di testo (txt)";
  502.             }
  503.         });
  504.         int retval = apriFile.showOpenDialog(this);
  505.         if (retval == JFileChooser.APPROVE_OPTION) {
  506.             if (this.checkSave()) {
  507.                 this.apriFile(apriFile.getSelectedFile());
  508.             }
  509.         }
  510.     }//GEN-LAST:event_apriActionPerformed
  511.  
  512.     public void apriFile(File pFile) {
  513.         this.manager.setFile(pFile);
  514.         String contenutoFile = this.manager.leggi();
  515.         this.textArea.setText(contenutoFile);
  516.         this.storiaContent = new ArrayList<String>();
  517.         this.storiaContent.add(contenutoFile);
  518.         this.idContent = 0;
  519.         this.setTitle(pFile.getName() + " - JNotepad");
  520.         this.creato = false;
  521.  
  522.     }
  523.  
  524.     private void salvaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salvaActionPerformed
  525.         if (this.creato) {
  526.             this.salvanomeActionPerformed(null);
  527.         } else {
  528.             this.manager.scrivi(this.textArea.getText());
  529.             this.salvato = true;
  530.         }
  531.     }//GEN-LAST:event_salvaActionPerformed
  532.  
  533.     private void salvanomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salvanomeActionPerformed
  534.         JFileChooser salvaFile = new JFileChooser();
  535.         salvaFile.removeChoosableFileFilter(salvaFile.getAcceptAllFileFilter());
  536.         salvaFile.setFileFilter(new FileFilter() {
  537.  
  538.             @Override
  539.             public boolean accept(File f) {
  540.                 String path = f.getPath();
  541.                 String estensione = path.substring((path.length() - 3), path.length());
  542.                 if (estensione.toLowerCase().equals("txt") || f.isDirectory()) {
  543.                     return true;
  544.                 }
  545.                 return false;
  546.             }
  547.  
  548.             @Override
  549.             public String getDescription() {
  550.                 return "File di testo (txt)";
  551.             }
  552.         });
  553.         int retval = salvaFile.showSaveDialog(this);
  554.         if (retval == JFileChooser.APPROVE_OPTION) {
  555.             File daSalvare = salvaFile.getSelectedFile();
  556.             String est = daSalvare.getName().substring(daSalvare.getName().length() - 4, daSalvare.getName().length());
  557.             if (!est.equals(".txt")) {
  558.                 daSalvare = new File(daSalvare.getAbsoluteFile() + ".txt");
  559.             }
  560.             if (daSalvare.exists()) {
  561.                 int risposta = JOptionPane.showConfirmDialog(this, "Il file esiste già: sovrascrivere?");
  562.                 if (risposta == 0) {
  563.                     this.manager.setFile(daSalvare);
  564.                     this.manager.scrivi(this.textArea.getText());
  565.                     this.salvato = true;
  566.                     this.creato = false;
  567.                 } else if (risposta == 1) {
  568.                     this.salvanomeActionPerformed(null);
  569.                 }
  570.             } else {
  571.                 this.manager.setFile(daSalvare);
  572.                 this.manager.scrivi(this.textArea.getText());
  573.                 this.salvato = true;
  574.                 this.creato = false;
  575.             }
  576.             this.setTitle(daSalvare.getName() + " - JNotepad");
  577.         }
  578.     }//GEN-LAST:event_salvanomeActionPerformed
  579.  
  580.     private void esciActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_esciActionPerformed
  581.         this.formWindowClosing(null);
  582.     }//GEN-LAST:event_esciActionPerformed
  583.  
  584.     private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
  585.         if (this.checkSave()) {
  586.             System.exit(0);
  587.         }
  588.     }//GEN-LAST:event_formWindowClosing
  589.  
  590.     private void annullaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_annullaActionPerformed
  591.         if (this.idContent > 0) {
  592.             this.idContent--;
  593.             this.getUndo = false;
  594.             this.textArea.setText(this.storiaContent.get(this.idContent));
  595.             this.getUndo = true;
  596.         }
  597.     }//GEN-LAST:event_annullaActionPerformed
  598.  
  599.     private void textAreaCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_textAreaCaretUpdate
  600.         if (this.getUndo) {
  601.             int attuale = this.textArea.getText().length();
  602.             int vecchia = 0;
  603.             if (this.idContent != 0) {
  604.                 this.storiaContent.get(this.idContent - 1).length();
  605.             }
  606.             if (attuale != vecchia) {
  607.                 this.idContent++;
  608.                 if (this.storiaContent.size() > this.idContent + 1) {
  609.                     this.storiaContent.set(this.idContent + 1, this.textArea.getText());
  610.                 } else {
  611.                     this.storiaContent.add(this.textArea.getText());
  612.                 }
  613.             }
  614.         }
  615.     }//GEN-LAST:event_textAreaCaretUpdate
  616.  
  617.     private void tagliaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tagliaActionPerformed
  618.         TransferHandler cut = textArea.getTransferHandler();
  619.         Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
  620.         cut.exportToClipboard(textArea, clip, TransferHandler.MOVE);
  621.     }//GEN-LAST:event_tagliaActionPerformed
  622.  
  623.     private void copiaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_copiaActionPerformed
  624.         TransferHandler copy = textArea.getTransferHandler();
  625.         Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
  626.         copy.exportToClipboard(textArea, clip, TransferHandler.COPY);
  627.     }//GEN-LAST:event_copiaActionPerformed
  628.  
  629.     private void incollaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_incollaActionPerformed
  630.         Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
  631.         Transferable data = clip.getContents(null);
  632.         try {
  633.             String testoIncollato = (String) data.getTransferData(DataFlavor.stringFlavor);
  634.             if (textArea.getSelectedText() == null) {
  635.                 textArea.insert(testoIncollato, textArea.getCaretPosition());
  636.             } else {
  637.                 textArea.replaceSelection(testoIncollato);
  638.             }
  639.         } catch (UnsupportedFlavorException ex) {
  640.         } catch (IOException ex) {
  641.         }
  642.  
  643.     }//GEN-LAST:event_incollaActionPerformed
  644.  
  645.     private void trovaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_trovaActionPerformed
  646.         this.trovaFrame.setVisible(true);
  647.         this.daSostituire.setVisible(false);
  648.         this.sostituisciButton.setVisible(false);
  649.         this.sostituisciLabel.setVisible(false);
  650.         this.trovaFrame.setBounds(this.getLocation().x + 100, this.getLocation().y + 100, 500, 160);
  651.     }//GEN-LAST:event_trovaActionPerformed
  652.  
  653.     private void sostituisciActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sostituisciActionPerformed
  654.         this.trovaFrame.setVisible(true);
  655.         this.daSostituire.setVisible(true);
  656.         this.sostituisciButton.setVisible(true);
  657.         this.sostituisciLabel.setVisible(true);
  658.         this.trovaFrame.setBounds(this.getLocation().x + 100, this.getLocation().y + 100, 500, 160);
  659.     }//GEN-LAST:event_sostituisciActionPerformed
  660.  
  661.     private void selezionaTuttoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selezionaTuttoActionPerformed
  662.         this.textArea.setSelectionStart(0);
  663.         this.textArea.setSelectionEnd(this.textArea.getText().length());
  664.     }//GEN-LAST:event_selezionaTuttoActionPerformed
  665.  
  666.     private void oradataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_oradataActionPerformed
  667.         GregorianCalendar calendar = new GregorianCalendar();
  668.         int anno = calendar.get(GregorianCalendar.YEAR);
  669.         int mese = calendar.get(GregorianCalendar.MONTH);
  670.         int giorno = calendar.get(GregorianCalendar.DAY_OF_MONTH);
  671.         int ora = calendar.get(GregorianCalendar.HOUR_OF_DAY);
  672.         int minuti = calendar.get(GregorianCalendar.MINUTE);
  673.         int secondi = calendar.get(GregorianCalendar.SECOND);
  674.         String data = ora + ":" + minuti + ":" + secondi + " " + giorno + "/" + mese + "/" + anno;
  675.         this.textArea.append(data);
  676.     }//GEN-LAST:event_oradataActionPerformed
  677.  
  678.     private void aCapoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aCapoActionPerformed
  679.         this.textArea.setLineWrap(this.aCapo.isSelected());
  680.         this.textArea.repaint();
  681.     }//GEN-LAST:event_aCapoActionPerformed
  682.  
  683.     private void infoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_infoActionPerformed
  684.         JFrame frame = new JFrame("Informazioni su JNotepad");
  685.         frame.setResizable(false);
  686.         frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
  687.         frame.setBounds(this.getLocation().x + 100, this.getLocation().y + 100, 400, 300);
  688.         frame.setVisible(true);
  689.         JLabel content = new JLabel();
  690.         content.setIcon(new ImageIcon(getClass().getResource("/jnotepad/graphics/images/logo.png")));
  691.         content.setText("<html>" +
  692.                 "Creato da Paolo Ricciuti senza alcun scopo di lucro per la " +
  693.                 "community PieroTofy a scopo illustrativo dei principali " +
  694.                 "metodi e classi del package swing." +
  695.                 "</html>");
  696.         frame.add(content);
  697.     }//GEN-LAST:event_infoActionPerformed
  698.  
  699.     private void daTrovareCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_daTrovareCaretUpdate
  700.         if (this.daTrovare.getText().length() != 0) {
  701.             this.trovaButton.setEnabled(true);
  702.             this.sostituisciButton.setEnabled(true);
  703.             this.sostituisciTutto.setEnabled(true);
  704.         } else {
  705.             this.trovaButton.setEnabled(false);
  706.         }
  707.     }//GEN-LAST:event_daTrovareCaretUpdate
  708.  
  709.     private void trovaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_trovaButtonActionPerformed
  710.         if (this.su.isSelected()) {
  711.             String testo = this.textArea.getText();
  712.             String trovare = this.daTrovare.getText();
  713.             if (this.upperLower.isSelected()) {
  714.                 testo = testo.toLowerCase();
  715.                 trovare = trovare.toLowerCase();
  716.             }
  717.             if (this.textArea.getSelectedText() != null) {
  718.                 if (this.textArea.getSelectionEnd() == this.textArea.getCaretPosition()) {
  719.                     this.textArea.setCaretPosition(this.textArea.getCaretPosition() - trovare.length());
  720.                 }
  721.             }
  722.             testo = testo.substring(0, this.textArea.getCaretPosition());
  723.             int index = testo.lastIndexOf(trovare);
  724.             if (index != -1) {
  725.                 this.textArea.select(index, index + trovare.length());
  726.             } else {
  727.                 JOptionPane.showMessageDialog(this, "Ricerca completata");
  728.             }
  729.         } else {
  730.             String testo = this.textArea.getText();
  731.             String trovare = this.daTrovare.getText();
  732.             if (this.upperLower.isSelected()) {
  733.                 testo = testo.toLowerCase();
  734.                 trovare = trovare.toLowerCase();
  735.             }
  736.             int index = testo.indexOf(trovare, this.textArea.getCaretPosition());
  737.             if (index != -1) {
  738.                 this.textArea.setCaretPosition(index + trovare.length());
  739.                 this.textArea.select(index, index + trovare.length());
  740.             } else {
  741.                 JOptionPane.showMessageDialog(this, "Ricerca completata");
  742.             }
  743.         }
  744.     }//GEN-LAST:event_trovaButtonActionPerformed
  745.  
  746.     private void annullaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_annullaButtonActionPerformed
  747.         this.trovaFrame.dispose();
  748.     }//GEN-LAST:event_annullaButtonActionPerformed
  749.  
  750.     private void sostituisciButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sostituisciButtonActionPerformed
  751.         this.textArea.replaceSelection(this.daSostituire.getText());
  752.     }//GEN-LAST:event_sostituisciButtonActionPerformed
  753.  
  754.     private void sostituisciTuttoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sostituisciTuttoActionPerformed
  755.         String testo = this.textArea.getText();
  756.         testo = testo.replaceAll(this.daTrovare.getText(), this.daSostituire.getText());
  757.         this.textArea.setText(testo);
  758.     }//GEN-LAST:event_sostituisciTuttoActionPerformed
  759.  
  760.     /**
  761.      * @param args the command line arguments
  762.      */
  763.     public static void main(final String args[]) {
  764.         java.awt.EventQueue.invokeLater(new Runnable() {
  765.  
  766.             public void run() {
  767.                 JNotePad pad = new JNotePad();
  768.                 pad.setLocation(50, 50);
  769.                 pad.setVisible(true);
  770.                 if (args.length == 1) {
  771.                     if (args[0].toLowerCase().endsWith(".txt")) {
  772.                         pad.apriFile(new File(args[0]));
  773.                     }
  774.                 }
  775.             }
  776.         });
  777.     }
  778.     // Variables declaration - do not modify//GEN-BEGIN:variables
  779.     private javax.swing.JMenu File;
  780.     private javax.swing.JMenu Formato;
  781.     private javax.swing.JMenu Help;
  782.     private javax.swing.JMenuBar Menu;
  783.     private javax.swing.JMenu Modifica;
  784.     private javax.swing.JCheckBoxMenuItem aCapo;
  785.     private javax.swing.JMenuItem annulla;
  786.     private javax.swing.JButton annullaButton;
  787.     private javax.swing.JMenuItem apri;
  788.     private javax.swing.JMenuItem copia;
  789.     private javax.swing.JTextField daSostituire;
  790.     private javax.swing.JTextField daTrovare;
  791.     private javax.swing.JMenuItem esci;
  792.     private javax.swing.JMenuItem file;
  793.     private javax.swing.JRadioButton giu;
  794.     private javax.swing.ButtonGroup gruppo;
  795.     private javax.swing.JMenuItem incolla;
  796.     private javax.swing.JMenuItem info;
  797.     private javax.swing.JPanel jPanel1;
  798.     private javax.swing.JScrollPane jScrollPane1;
  799.     private javax.swing.JSeparator jSeparator1;
  800.     private javax.swing.JSeparator jSeparator2;
  801.     private javax.swing.JSeparator jSeparator3;
  802.     private javax.swing.JMenuItem oradata;
  803.     private javax.swing.JPanel panel;
  804.     private javax.swing.JPanel panello;
  805.     private javax.swing.JMenuItem salva;
  806.     private javax.swing.JMenuItem salvanome;
  807.     private javax.swing.JMenuItem selezionaTutto;
  808.     private javax.swing.JMenuItem sostituisci;
  809.     private javax.swing.JButton sostituisciButton;
  810.     private javax.swing.JLabel sostituisciLabel;
  811.     private javax.swing.JButton sostituisciTutto;
  812.     private javax.swing.JRadioButton su;
  813.     private javax.swing.JMenuItem taglia;
  814.     private javax.swing.JTextArea textArea;
  815.     private javax.swing.JMenuItem trova;
  816.     private javax.swing.JButton trovaButton;
  817.     private javax.swing.JFrame trovaFrame;
  818.     private javax.swing.JLabel trovaLabel;
  819.     private javax.swing.JCheckBox upperLower;
  820.     // End of variables declaration//GEN-END:variables
  821. }
 

Creative Commons License
Il layout di questo sito è concesso sotto licenza Creative Commons.
Per maggiori informazioni sulle licenze dei contenuti del sito, clicca.