Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
JStenografer - JStenografer.java

JStenografer.java

Caricato da:
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.  * JStenografer.java
  8.  *
  9.  * Created on 6-ott-2010, 14.16.46
  10.  */
  11. package jstenografer.graphics;
  12.  
  13. import com.sun.awt.AWTUtilities;
  14. import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
  15. import java.io.File;
  16. import java.io.IOException;
  17. import javax.swing.JFileChooser;
  18. import javax.swing.JOptionPane;
  19. import javax.swing.UIManager;
  20. import javax.swing.UnsupportedLookAndFeelException;
  21. import javax.swing.filechooser.FileFilter;
  22. import jstenografer.Stenografer;
  23. import jstenografer.exception.NotBMPDestinationFile;
  24. import jstenografer.exception.TooLongStringException;
  25.  
  26. /**
  27.  *
  28.  * @author Paolo
  29.  */
  30. public class JStenografer extends javax.swing.JFrame {
  31.  
  32.     private File choose;
  33.     private JFileChooser chooser;
  34.     private JFileChooser save;
  35.  
  36.     /** Creates new form JStenografer */
  37.     public JStenografer() {
  38.         try {
  39.             UIManager.setLookAndFeel(new NimbusLookAndFeel());
  40.         } catch (UnsupportedLookAndFeelException ex) {
  41.         }
  42.         initComponents();
  43.         this.initChooser();
  44.     }
  45.  
  46.     private void initChooser() {
  47.         this.chooser = new JFileChooser(System.getProperty("user.home"));
  48.         this.chooser.removeChoosableFileFilter(this.chooser.getAcceptAllFileFilter());
  49.         this.chooser.addChoosableFileFilter(new FileFilter() {
  50.  
  51.             @Override
  52.             public boolean accept(File f) {
  53.                 String ext = f.getPath().substring(f.getPath().length() - 3);
  54.                 if (f.isDirectory() || ext.equalsIgnoreCase("png") || ext.equalsIgnoreCase("bmp") || ext.equalsIgnoreCase("gif") || ext.equalsIgnoreCase("jpg") || ext.equalsIgnoreCase("jpeg")) {
  55.                     return true;
  56.                 }
  57.                 return false;
  58.             }
  59.  
  60.             @Override
  61.             public String getDescription() {
  62.                 return "Immagini (bmp,gif,png,jpg)";
  63.             }
  64.         });
  65.         this.save = new JFileChooser(System.getProperty("user.home"));
  66.         this.save.removeChoosableFileFilter(this.chooser.getAcceptAllFileFilter());
  67.         this.save.addChoosableFileFilter(new FileFilter() {
  68.  
  69.             @Override
  70.             public boolean accept(File f) {
  71.                 String ext = f.getPath().substring(f.getPath().length() - 3);
  72.                 if (f.isDirectory() || ext.equalsIgnoreCase("bmp")) {
  73.                     return true;
  74.                 }
  75.                 return false;
  76.             }
  77.  
  78.             @Override
  79.             public String getDescription() {
  80.                 return "Immagini (bmp)";
  81.             }
  82.         });
  83.     }
  84.  
  85.     /** This method is called from within the constructor to
  86.      * initialize the form.
  87.      * WARNING: Do NOT modify this code. The content of this method is
  88.      * always regenerated by the Form Editor.
  89.      */
  90.     @SuppressWarnings("unchecked")
  91.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  92.     private void initComponents() {
  93.  
  94.         panel = new javax.swing.JPanel();
  95.         immLabel = new javax.swing.JLabel();
  96.         immPath = new javax.swing.JLabel();
  97.         searchButton = new javax.swing.JButton();
  98.         writeButton = new javax.swing.JButton();
  99.         readButton = new javax.swing.JButton();
  100.         toWriteLabel = new javax.swing.JLabel();
  101.         scroll1 = new javax.swing.JScrollPane();
  102.         toWrite = new javax.swing.JTextArea();
  103.         readLabel = new javax.swing.JLabel();
  104.         scroll2 = new javax.swing.JScrollPane();
  105.         read = new javax.swing.JTextArea();
  106.  
  107.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  108.         setTitle("JStenografer - by Paolo Ricciuti (C)");
  109.         setResizable(false);
  110.  
  111.         immLabel.setText("Immagine: ");
  112.  
  113.         immPath.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
  114.  
  115.         searchButton.setText("Sfoglia");
  116.         searchButton.addActionListener(new java.awt.event.ActionListener() {
  117.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  118.                 searchButtonActionPerformed(evt);
  119.             }
  120.         });
  121.  
  122.         writeButton.setText("Scrivi messaggio");
  123.         writeButton.setEnabled(false);
  124.         writeButton.addActionListener(new java.awt.event.ActionListener() {
  125.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  126.                 writeButtonActionPerformed(evt);
  127.             }
  128.         });
  129.  
  130.         readButton.setText("Leggi messaggio");
  131.         readButton.setEnabled(false);
  132.         readButton.addActionListener(new java.awt.event.ActionListener() {
  133.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  134.                 readButtonActionPerformed(evt);
  135.             }
  136.         });
  137.  
  138.         toWriteLabel.setText("Da scrivere: ");
  139.  
  140.         toWrite.setColumns(20);
  141.         toWrite.setRows(5);
  142.         scroll1.setViewportView(toWrite);
  143.  
  144.         readLabel.setText("Letto: ");
  145.  
  146.         read.setColumns(20);
  147.         read.setEditable(false);
  148.         read.setRows(5);
  149.         scroll2.setViewportView(read);
  150.  
  151.         javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
  152.         panel.setLayout(panelLayout);
  153.         panelLayout.setHorizontalGroup(
  154.             panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  155.             .addGroup(panelLayout.createSequentialGroup()
  156.                 .addContainerGap()
  157.                 .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  158.                     .addGroup(panelLayout.createSequentialGroup()
  159.                         .addComponent(immLabel)
  160.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  161.                         .addComponent(immPath, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
  162.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  163.                         .addComponent(searchButton))
  164.                     .addGroup(panelLayout.createSequentialGroup()
  165.                         .addComponent(writeButton)
  166.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  167.                         .addComponent(readButton))
  168.                     .addGroup(panelLayout.createSequentialGroup()
  169.                         .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  170.                             .addComponent(readLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
  171.                             .addComponent(toWriteLabel))
  172.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  173.                         .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  174.                             .addComponent(scroll1, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
  175.                             .addComponent(scroll2, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE))))
  176.                 .addGap(38, 38, 38))
  177.         );
  178.         panelLayout.setVerticalGroup(
  179.             panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  180.             .addGroup(panelLayout.createSequentialGroup()
  181.                 .addContainerGap()
  182.                 .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  183.                     .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  184.                         .addComponent(immLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
  185.                         .addComponent(immPath, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
  186.                     .addComponent(searchButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  187.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  188.                 .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  189.                     .addComponent(writeButton)
  190.                     .addComponent(readButton))
  191.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  192.                 .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  193.                     .addComponent(toWriteLabel)
  194.                     .addComponent(scroll1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
  195.                 .addGap(18, 18, 18)
  196.                 .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  197.                     .addComponent(readLabel)
  198.                     .addComponent(scroll2, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
  199.                 .addGap(35, 35, 35))
  200.         );
  201.  
  202.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  203.         getContentPane().setLayout(layout);
  204.         layout.setHorizontalGroup(
  205.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  206.             .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  207.         );
  208.         layout.setVerticalGroup(
  209.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  210.             .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  211.         );
  212.  
  213.         java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
  214.         setBounds((screenSize.width-416)/2, (screenSize.height-338)/2, 416, 338);
  215.     }// </editor-fold>//GEN-END:initComponents
  216.  
  217.     private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
  218.         int retval = this.chooser.showOpenDialog(this);
  219.         if (retval == JFileChooser.APPROVE_OPTION) {
  220.             this.choose = this.chooser.getSelectedFile();
  221.             this.immPath.setText(this.choose.getAbsolutePath());
  222.             String name = this.choose.getAbsolutePath();
  223.             String ext = name.substring(name.length() - 4);
  224.             if (!ext.equalsIgnoreCase(".bmp")) {
  225.                 this.writeButton.setEnabled(true);
  226.                 this.readButton.setEnabled(false);
  227.             } else {
  228.                 this.writeButton.setEnabled(false);
  229.                 this.readButton.setEnabled(true);
  230.             }
  231.         }
  232.     }//GEN-LAST:event_searchButtonActionPerformed
  233.  
  234.     private void writeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_writeButtonActionPerformed
  235.         int retval = this.save.showSaveDialog(this);
  236.         if (retval == JFileChooser.APPROVE_OPTION) {
  237.             String toSteno = this.toWrite.getText();
  238.             try {
  239.                 String name = this.save.getSelectedFile().getAbsolutePath();
  240.                 String ext = name.substring(name.length() - 4);
  241.                 File dest = this.save.getSelectedFile();
  242.                 if (!ext.equalsIgnoreCase(".bmp")) {
  243.                     dest = new File(name + ".bmp");
  244.                 }
  245.                 Stenografer.write(this.choose, toSteno, dest);
  246.             } catch (IOException ex) {
  247.             } catch (TooLongStringException ex) {
  248.                 JOptionPane.showMessageDialog(this, "Stringa troppo lunga per essere salvata in questa foto.");
  249.             } catch (NotBMPDestinationFile ex) {
  250.                 JOptionPane.showMessageDialog(this, "Il file di destinazione deve essere un file BMP");
  251.             }
  252.             JOptionPane.showMessageDialog(this, "Stenografia completata!");
  253.         }
  254.     }//GEN-LAST:event_writeButtonActionPerformed
  255.  
  256.     private void readButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_readButtonActionPerformed
  257.         try {
  258.             this.read.setText(Stenografer.read(this.choose));
  259.         } catch (IOException ex) {
  260.         }
  261.     }//GEN-LAST:event_readButtonActionPerformed
  262.  
  263.     /**
  264.      * @param args the command line arguments
  265.      */
  266.     public static void main(String args[]) {
  267.         java.awt.EventQueue.invokeLater(new Runnable() {
  268.  
  269.             public void run() {
  270.                 new JStenografer().setVisible(true);
  271.             }
  272.         });
  273.     }
  274.     // Variables declaration - do not modify//GEN-BEGIN:variables
  275.     private javax.swing.JLabel immLabel;
  276.     private javax.swing.JLabel immPath;
  277.     private javax.swing.JPanel panel;
  278.     private javax.swing.JTextArea read;
  279.     private javax.swing.JButton readButton;
  280.     private javax.swing.JLabel readLabel;
  281.     private javax.swing.JScrollPane scroll1;
  282.     private javax.swing.JScrollPane scroll2;
  283.     private javax.swing.JButton searchButton;
  284.     private javax.swing.JTextArea toWrite;
  285.     private javax.swing.JLabel toWriteLabel;
  286.     private javax.swing.JButton writeButton;
  287.     // End of variables declaration//GEN-END:variables
  288. }