Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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 secondaprova;
- import java.util.*;
- import java.time.*;
- import javax.swing.*;
- import java.io.*;
- /**
- *
- * @author foggia
- */
- public class ReminderFrame extends javax.swing.JFrame {
- private SortedMap<LocalDateTime, Reminder> map;
- private DefaultListModel<Reminder> listModel;
- private final static String FILENAME="reminder.dat";
- /**
- * Creates new form ReminderFrame
- */
- public ReminderFrame() {
- map=new TreeMap<>();
- listModel=new DefaultListModel<>();
- initComponents();
- LocalDateTime now=LocalDateTime.now();
- dayText.setText(""+now.getDayOfMonth());
- monthText.setText(""+now.getMonthValue());
- yearText.setText(""+now.getYear());
- hourText.setText(""+now.getHour());
- minuteText.setText(""+now.getMinute());
- Thread t=new Thread(this::checkLoop);
- t.start();
- }
- /**
- * 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() {
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- jLabel3 = new javax.swing.JLabel();
- jLabel4 = new javax.swing.JLabel();
- jLabel5 = new javax.swing.JLabel();
- dayText = new javax.swing.JTextField();
- monthText = new javax.swing.JTextField();
- yearText = new javax.swing.JTextField();
- hourText = new javax.swing.JTextField();
- minuteText = new javax.swing.JTextField();
- descriptionText = new javax.swing.JTextField();
- jLabel6 = new javax.swing.JLabel();
- createButton = new javax.swing.JButton();
- removeButton = new javax.swing.JButton();
- jScrollPane1 = new javax.swing.JScrollPane();
- reminderList = new javax.swing.JList<>();
- saveButton = new javax.swing.JButton();
- loadButton = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setTitle("Promemoria");
- jLabel1.setText("Giorno");
- jLabel2.setText("Mese");
- jLabel3.setText("Anno");
- jLabel4.setText("Ora");
- jLabel5.setText("Minuto");
- dayText.setText("jTextField1");
- monthText.setText("jTextField1");
- yearText.setText("jTextField1");
- hourText.setText("jTextField1");
- minuteText.setText("jTextField1");
- jLabel6.setText("Descrizione");
- createButton.setText("Crea promemoria");
- createButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- createButtonActionPerformed(evt);
- }
- });
- removeButton.setText("Rimuovi promemoria");
- removeButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- removeButtonActionPerformed(evt);
- }
- });
- reminderList.setModel(listModel);
- jScrollPane1.setViewportView(reminderList);
- saveButton.setText("Salva file");
- saveButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- saveButtonActionPerformed(evt);
- }
- });
- loadButton.setText("Carica file");
- loadButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- loadButtonActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(28, 28, 28)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(dayText, javax.swing.GroupLayout.PREFERRED_SIZE, 1, Short.MAX_VALUE)
- .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jLabel6)))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(monthText, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jLabel2))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(yearText, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(hourText, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addComponent(jLabel3)
- .addGap(42, 42, 42)
- .addComponent(jLabel4)))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel5)
- .addComponent(minuteText, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addComponent(descriptionText, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addGroup(layout.createSequentialGroup()
- .addGap(12, 12, 12)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
- .addComponent(loadButton)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 374, javax.swing.GroupLayout.PREFERRED_SIZE))))
- .addGap(0, 8, Short.MAX_VALUE))
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(createButton)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(removeButton))
- .addGroup(layout.createSequentialGroup()
- .addComponent(saveButton)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))))
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel1)
- .addComponent(jLabel2)
- .addComponent(jLabel3)
- .addComponent(jLabel4)
- .addComponent(jLabel5))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(dayText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(monthText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(yearText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(hourText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(minuteText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(descriptionText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jLabel6))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(createButton)
- .addComponent(removeButton))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(saveButton)
- .addComponent(loadButton))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>//GEN-END:initComponents
- private void createButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createButtonActionPerformed
- LocalDateTime dateTime=getDateTime();
- if (dateTime==null) {
- error("Formato data/ora non valido!");
- return;
- }
- if (!dateTime.isAfter(LocalDateTime.now())) {
- error("Non puoi usare una data/ora già passata!");
- return;
- }
- synchronized (map) {
- if (map.containsKey(dateTime)) {
- error("Data/ora già presente!");
- return;
- }
- }
- String text=descriptionText.getText().trim();
- if ("".equals(text)) {
- error("Devi specificare una descrizione!");
- return;
- }
- Reminder rem=new Reminder(dateTime, text);
- addReminder(rem);
- }//GEN-LAST:event_createButtonActionPerformed
- private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
- LocalDateTime dateTime=getDateTime();
- if (dateTime==null) {
- error("Formato data/ora non valido!");
- return;
- }
- Reminder rem;
- synchronized (map) {
- rem=map.get(dateTime);
- }
- if (rem==null) {
- error("Data/ora non presente!");
- return;
- }
- if (confirm("Confermi la cancellazione?", rem.toString()))
- removeReminder(dateTime);
- }//GEN-LAST:event_removeButtonActionPerformed
- private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
- synchronized (this) {
- try {
- ObjectOutputStream oos=
- new ObjectOutputStream(
- new FileOutputStream(FILENAME));
- oos.writeObject(map);
- oos.close();
- } catch (IOException exc) {
- error(exc.toString());
- }
- }
- }//GEN-LAST:event_saveButtonActionPerformed
- private void loadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadButtonActionPerformed
- synchronized (this) {
- try {
- ObjectInputStream ois=
- new ObjectInputStream(
- new FileInputStream(FILENAME));
- map=(SortedMap<LocalDateTime,Reminder>)ois.readObject();
- ois.close();
- } catch (IOException | ClassNotFoundException exc) {
- error(exc.toString());
- }
- }
- SwingUtilities.invokeLater(this::updateList);
- }//GEN-LAST:event_loadButtonActionPerformed
- /**
- * @param args the command line arguments
- */
- 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(ReminderFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(ReminderFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(ReminderFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(ReminderFrame.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 ReminderFrame().setVisible(true);
- }
- });
- }
- private LocalDateTime getDateTime() {
- try {
- int day=Integer.parseInt(dayText.getText());
- int month=Integer.parseInt(monthText.getText());
- int year=Integer.parseInt(yearText.getText());
- int hour=Integer.parseInt(hourText.getText());
- int minute=Integer.parseInt(minuteText.getText());
- return LocalDateTime.of(year, month, day, hour, minute);
- } catch (NumberFormatException | DateTimeException exc) {
- return null;
- }
- }
- private void error(String message) {
- JOptionPane.showMessageDialog(this, message, "Errore",
- JOptionPane.ERROR_MESSAGE);
- }
- private void info(String message) {
- JOptionPane.showMessageDialog(this, message, "Promemoria",
- JOptionPane.INFORMATION_MESSAGE);
- }
- private boolean confirm(String message, String subject) {
- Object body;
- if (subject!=null) {
- body=new String[] { subject, message };
- } else {
- body=message;
- }
- int answer=JOptionPane.showConfirmDialog(this, body, "Conferma",
- JOptionPane.YES_NO_OPTION);
- return answer==JOptionPane.YES_OPTION;
- }
- private void updateList() {
- listModel.clear();
- synchronized (this) {
- for(Reminder rem : map.values())
- listModel.addElement(rem);
- }
- }
- public void addReminder(Reminder rem) {
- synchronized (this) {
- map.put(rem.getDateTime(), rem);
- }
- SwingUtilities.invokeLater(this::updateList);
- }
- public Reminder removeReminder(LocalDateTime dateTime) {
- Reminder rem;
- synchronized (this) {
- rem=map.remove(dateTime);
- }
- SwingUtilities.invokeLater(this::updateList);
- return rem;
- }
- private void checkLoop() {
- while (true) {
- try {
- checkExpiration();
- Thread.sleep(15*1000);
- } catch (InterruptedException exc) { }
- }
- }
- private void checkExpiration() {
- LocalDateTime first;
- synchronized (this) {
- if (map.size()==0)
- return;
- first=map.firstKey();
- }
- if (!first.isAfter(LocalDateTime.now())) {
- Reminder rem=removeReminder(first);
- info(rem.toString());
- }
- }
- // Variables declaration - do not modify//GEN-BEGIN:variables
- private javax.swing.JButton createButton;
- private javax.swing.JTextField dayText;
- private javax.swing.JTextField descriptionText;
- private javax.swing.JTextField hourText;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JLabel jLabel3;
- private javax.swing.JLabel jLabel4;
- private javax.swing.JLabel jLabel5;
- private javax.swing.JLabel jLabel6;
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.JButton loadButton;
- private javax.swing.JTextField minuteText;
- private javax.swing.JTextField monthText;
- private javax.swing.JList<Reminder> reminderList;
- private javax.swing.JButton removeButton;
- private javax.swing.JButton saveButton;
- private javax.swing.JTextField yearText;
- // End of variables declaration//GEN-END:variables
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement