Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 19.75 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package secondaprova;
  7. import java.util.*;
  8. import java.time.*;
  9. import javax.swing.*;
  10. import java.io.*;
  11. /**
  12.  *
  13.  * @author foggia
  14.  */
  15. public class ReminderFrame extends javax.swing.JFrame {
  16.     private SortedMap<LocalDateTime, Reminder> map;
  17.     private DefaultListModel<Reminder> listModel;
  18.     private final static String FILENAME="reminder.dat";
  19.  
  20.     /**
  21.      * Creates new form ReminderFrame
  22.      */
  23.     public ReminderFrame() {
  24.         map=new TreeMap<>();
  25.         listModel=new DefaultListModel<>();
  26.         initComponents();
  27.         LocalDateTime now=LocalDateTime.now();
  28.         dayText.setText(""+now.getDayOfMonth());
  29.         monthText.setText(""+now.getMonthValue());
  30.         yearText.setText(""+now.getYear());
  31.         hourText.setText(""+now.getHour());
  32.         minuteText.setText(""+now.getMinute());
  33.         Thread t=new Thread(this::checkLoop);
  34.         t.start();
  35.     }
  36.  
  37.     /**
  38.      * This method is called from within the constructor to initialize the form.
  39.      * WARNING: Do NOT modify this code. The content of this method is always
  40.      * regenerated by the Form Editor.
  41.      */
  42.     @SuppressWarnings("unchecked")
  43.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  44.     private void initComponents() {
  45.  
  46.         jLabel1 = new javax.swing.JLabel();
  47.         jLabel2 = new javax.swing.JLabel();
  48.         jLabel3 = new javax.swing.JLabel();
  49.         jLabel4 = new javax.swing.JLabel();
  50.         jLabel5 = new javax.swing.JLabel();
  51.         dayText = new javax.swing.JTextField();
  52.         monthText = new javax.swing.JTextField();
  53.         yearText = new javax.swing.JTextField();
  54.         hourText = new javax.swing.JTextField();
  55.         minuteText = new javax.swing.JTextField();
  56.         descriptionText = new javax.swing.JTextField();
  57.         jLabel6 = new javax.swing.JLabel();
  58.         createButton = new javax.swing.JButton();
  59.         removeButton = new javax.swing.JButton();
  60.         jScrollPane1 = new javax.swing.JScrollPane();
  61.         reminderList = new javax.swing.JList<>();
  62.         saveButton = new javax.swing.JButton();
  63.         loadButton = new javax.swing.JButton();
  64.  
  65.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  66.         setTitle("Promemoria");
  67.  
  68.         jLabel1.setText("Giorno");
  69.  
  70.         jLabel2.setText("Mese");
  71.  
  72.         jLabel3.setText("Anno");
  73.  
  74.         jLabel4.setText("Ora");
  75.  
  76.         jLabel5.setText("Minuto");
  77.  
  78.         dayText.setText("jTextField1");
  79.  
  80.         monthText.setText("jTextField1");
  81.  
  82.         yearText.setText("jTextField1");
  83.  
  84.         hourText.setText("jTextField1");
  85.  
  86.         minuteText.setText("jTextField1");
  87.  
  88.         jLabel6.setText("Descrizione");
  89.  
  90.         createButton.setText("Crea promemoria");
  91.         createButton.addActionListener(new java.awt.event.ActionListener() {
  92.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  93.                 createButtonActionPerformed(evt);
  94.             }
  95.         });
  96.  
  97.         removeButton.setText("Rimuovi promemoria");
  98.         removeButton.addActionListener(new java.awt.event.ActionListener() {
  99.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  100.                 removeButtonActionPerformed(evt);
  101.             }
  102.         });
  103.  
  104.         reminderList.setModel(listModel);
  105.         jScrollPane1.setViewportView(reminderList);
  106.  
  107.         saveButton.setText("Salva file");
  108.         saveButton.addActionListener(new java.awt.event.ActionListener() {
  109.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  110.                 saveButtonActionPerformed(evt);
  111.             }
  112.         });
  113.  
  114.         loadButton.setText("Carica file");
  115.         loadButton.addActionListener(new java.awt.event.ActionListener() {
  116.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  117.                 loadButtonActionPerformed(evt);
  118.             }
  119.         });
  120.  
  121.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  122.         getContentPane().setLayout(layout);
  123.         layout.setHorizontalGroup(
  124.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  125.             .addGroup(layout.createSequentialGroup()
  126.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  127.                     .addGroup(layout.createSequentialGroup()
  128.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  129.                             .addGroup(layout.createSequentialGroup()
  130.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  131.                                     .addGroup(layout.createSequentialGroup()
  132.                                         .addGap(28, 28, 28)
  133.                                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  134.                                             .addComponent(dayText, javax.swing.GroupLayout.PREFERRED_SIZE, 1, Short.MAX_VALUE)
  135.                                             .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
  136.                                     .addGroup(layout.createSequentialGroup()
  137.                                         .addContainerGap()
  138.                                         .addComponent(jLabel6)))
  139.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  140.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  141.                                     .addGroup(layout.createSequentialGroup()
  142.                                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  143.                                             .addComponent(monthText, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
  144.                                             .addComponent(jLabel2))
  145.                                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  146.                                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  147.                                             .addGroup(layout.createSequentialGroup()
  148.                                                 .addComponent(yearText, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
  149.                                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  150.                                                 .addComponent(hourText, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
  151.                                             .addGroup(layout.createSequentialGroup()
  152.                                                 .addComponent(jLabel3)
  153.                                                 .addGap(42, 42, 42)
  154.                                                 .addComponent(jLabel4)))
  155.                                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  156.                                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  157.                                             .addComponent(jLabel5)
  158.                                             .addComponent(minuteText, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)))
  159.                                     .addComponent(descriptionText, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)))
  160.                             .addGroup(layout.createSequentialGroup()
  161.                                 .addGap(12, 12, 12)
  162.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  163.                                     .addComponent(loadButton)
  164.                                     .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 374, javax.swing.GroupLayout.PREFERRED_SIZE))))
  165.                         .addGap(0, 8, Short.MAX_VALUE))
  166.                     .addGroup(layout.createSequentialGroup()
  167.                         .addContainerGap()
  168.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  169.                             .addGroup(layout.createSequentialGroup()
  170.                                 .addComponent(createButton)
  171.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  172.                                 .addComponent(removeButton))
  173.                             .addGroup(layout.createSequentialGroup()
  174.                                 .addComponent(saveButton)
  175.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))))
  176.                 .addContainerGap())
  177.         );
  178.         layout.setVerticalGroup(
  179.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  180.             .addGroup(layout.createSequentialGroup()
  181.                 .addContainerGap()
  182.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  183.                     .addComponent(jLabel1)
  184.                     .addComponent(jLabel2)
  185.                     .addComponent(jLabel3)
  186.                     .addComponent(jLabel4)
  187.                     .addComponent(jLabel5))
  188.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  189.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  190.                     .addComponent(dayText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  191.                     .addComponent(monthText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  192.                     .addComponent(yearText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  193.                     .addComponent(hourText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  194.                     .addComponent(minuteText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  195.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  196.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  197.                     .addComponent(descriptionText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  198.                     .addComponent(jLabel6))
  199.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  200.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  201.                     .addComponent(createButton)
  202.                     .addComponent(removeButton))
  203.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  204.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
  205.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  206.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  207.                     .addComponent(saveButton)
  208.                     .addComponent(loadButton))
  209.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  210.         );
  211.  
  212.         pack();
  213.     }// </editor-fold>//GEN-END:initComponents
  214.  
  215.     private void createButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createButtonActionPerformed
  216.         LocalDateTime dateTime=getDateTime();
  217.         if (dateTime==null) {
  218.             error("Formato data/ora non valido!");
  219.             return;
  220.         }
  221.         if (!dateTime.isAfter(LocalDateTime.now())) {
  222.             error("Non puoi usare una data/ora già passata!");
  223.             return;
  224.         }
  225.         synchronized (map) {
  226.             if (map.containsKey(dateTime)) {
  227.                 error("Data/ora già presente!");
  228.                 return;
  229.             }
  230.         }
  231.         String text=descriptionText.getText().trim();
  232.         if ("".equals(text)) {
  233.             error("Devi specificare una descrizione!");
  234.             return;
  235.         }
  236.        
  237.         Reminder rem=new Reminder(dateTime, text);
  238.         addReminder(rem);
  239.     }//GEN-LAST:event_createButtonActionPerformed
  240.  
  241.     private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
  242.         LocalDateTime dateTime=getDateTime();
  243.         if (dateTime==null) {
  244.             error("Formato data/ora non valido!");
  245.             return;
  246.         }
  247.         Reminder rem;
  248.         synchronized (map) {
  249.             rem=map.get(dateTime);
  250.         }
  251.         if (rem==null) {
  252.             error("Data/ora non presente!");
  253.             return;
  254.         }
  255.         if (confirm("Confermi la cancellazione?", rem.toString()))
  256.             removeReminder(dateTime);
  257.     }//GEN-LAST:event_removeButtonActionPerformed
  258.  
  259.     private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
  260.         synchronized (this) {
  261.         try {
  262.             ObjectOutputStream oos=
  263.                     new ObjectOutputStream(
  264.                         new FileOutputStream(FILENAME));
  265.             oos.writeObject(map);
  266.             oos.close();
  267.         } catch (IOException exc) {
  268.             error(exc.toString());
  269.         }
  270.         }
  271.     }//GEN-LAST:event_saveButtonActionPerformed
  272.  
  273.     private void loadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadButtonActionPerformed
  274.         synchronized (this) {
  275.         try {
  276.             ObjectInputStream ois=
  277.                     new ObjectInputStream(
  278.                         new FileInputStream(FILENAME));
  279.             map=(SortedMap<LocalDateTime,Reminder>)ois.readObject();
  280.             ois.close();
  281.         } catch (IOException | ClassNotFoundException exc) {
  282.             error(exc.toString());
  283.         }
  284.         }
  285.         SwingUtilities.invokeLater(this::updateList);
  286.     }//GEN-LAST:event_loadButtonActionPerformed
  287.  
  288.     /**
  289.      * @param args the command line arguments
  290.      */
  291.     public static void main(String args[]) {
  292.         /* Set the Nimbus look and feel */
  293.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  294.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  295.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  296.          */
  297.         try {
  298.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  299.                 if ("Nimbus".equals(info.getName())) {
  300.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  301.                     break;
  302.                 }
  303.             }
  304.         } catch (ClassNotFoundException ex) {
  305.             java.util.logging.Logger.getLogger(ReminderFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  306.         } catch (InstantiationException ex) {
  307.             java.util.logging.Logger.getLogger(ReminderFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  308.         } catch (IllegalAccessException ex) {
  309.             java.util.logging.Logger.getLogger(ReminderFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  310.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  311.             java.util.logging.Logger.getLogger(ReminderFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  312.         }
  313.         //</editor-fold>
  314.  
  315.         /* Create and display the form */
  316.         java.awt.EventQueue.invokeLater(new Runnable() {
  317.             public void run() {
  318.                 new ReminderFrame().setVisible(true);
  319.             }
  320.         });
  321.     }
  322.    
  323.     private LocalDateTime getDateTime() {
  324.         try {
  325.             int day=Integer.parseInt(dayText.getText());
  326.             int month=Integer.parseInt(monthText.getText());
  327.             int year=Integer.parseInt(yearText.getText());
  328.             int hour=Integer.parseInt(hourText.getText());
  329.             int minute=Integer.parseInt(minuteText.getText());
  330.             return LocalDateTime.of(year, month, day, hour, minute);
  331.         } catch (NumberFormatException | DateTimeException exc) {
  332.             return null;
  333.         }
  334.     }
  335.    
  336.     private void error(String message) {
  337.         JOptionPane.showMessageDialog(this, message, "Errore",
  338.                 JOptionPane.ERROR_MESSAGE);
  339.     }
  340.    
  341.     private void info(String message) {
  342.         JOptionPane.showMessageDialog(this, message, "Promemoria",
  343.                 JOptionPane.INFORMATION_MESSAGE);
  344.     }
  345.    
  346.     private boolean confirm(String message, String subject) {
  347.         Object body;
  348.         if (subject!=null) {
  349.             body=new String[] { subject, message };
  350.         } else {
  351.             body=message;            
  352.         }
  353.        
  354.         int answer=JOptionPane.showConfirmDialog(this, body, "Conferma",
  355.                 JOptionPane.YES_NO_OPTION);
  356.         return answer==JOptionPane.YES_OPTION;
  357.     }
  358.    
  359.     private void updateList() {
  360.         listModel.clear();
  361.         synchronized (this) {
  362.             for(Reminder rem : map.values())
  363.                 listModel.addElement(rem);
  364.         }
  365.     }
  366.    
  367.     public void addReminder(Reminder rem) {
  368.         synchronized (this) {
  369.             map.put(rem.getDateTime(), rem);
  370.         }
  371.         SwingUtilities.invokeLater(this::updateList);
  372.     }
  373.    
  374.     public Reminder removeReminder(LocalDateTime dateTime) {
  375.         Reminder rem;
  376.         synchronized (this) {
  377.             rem=map.remove(dateTime);
  378.         }
  379.         SwingUtilities.invokeLater(this::updateList);
  380.         return rem;
  381.     }
  382.    
  383.     private void checkLoop() {
  384.         while (true) {
  385.             try {
  386.                 checkExpiration();
  387.                 Thread.sleep(15*1000);
  388.             } catch (InterruptedException exc) { }
  389.         }
  390.     }
  391.    
  392.     private void checkExpiration() {
  393.         LocalDateTime first;
  394.         synchronized (this) {
  395.             if (map.size()==0)
  396.                 return;
  397.             first=map.firstKey();
  398.         }
  399.         if (!first.isAfter(LocalDateTime.now())) {
  400.             Reminder rem=removeReminder(first);
  401.             info(rem.toString());
  402.         }
  403.     }
  404.  
  405.     // Variables declaration - do not modify//GEN-BEGIN:variables
  406.     private javax.swing.JButton createButton;
  407.     private javax.swing.JTextField dayText;
  408.     private javax.swing.JTextField descriptionText;
  409.     private javax.swing.JTextField hourText;
  410.     private javax.swing.JLabel jLabel1;
  411.     private javax.swing.JLabel jLabel2;
  412.     private javax.swing.JLabel jLabel3;
  413.     private javax.swing.JLabel jLabel4;
  414.     private javax.swing.JLabel jLabel5;
  415.     private javax.swing.JLabel jLabel6;
  416.     private javax.swing.JScrollPane jScrollPane1;
  417.     private javax.swing.JButton loadButton;
  418.     private javax.swing.JTextField minuteText;
  419.     private javax.swing.JTextField monthText;
  420.     private javax.swing.JList<Reminder> reminderList;
  421.     private javax.swing.JButton removeButton;
  422.     private javax.swing.JButton saveButton;
  423.     private javax.swing.JTextField yearText;
  424.     // End of variables declaration//GEN-END:variables
  425. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement