| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ClearAllButton |
|
| 2.25;2.25 | ||||
| ClearAllButton$1 |
|
| 2.25;2.25 |
| 1 | package com.nilhcem.fakesmtp.gui.info; | |
| 2 | ||
| 3 | import com.nilhcem.fakesmtp.core.Configuration; | |
| 4 | import com.nilhcem.fakesmtp.core.I18n; | |
| 5 | import com.nilhcem.fakesmtp.server.MailSaver; | |
| 6 | import com.nilhcem.fakesmtp.server.SMTPServerHandler; | |
| 7 | ||
| 8 | import javax.swing.JButton; | |
| 9 | import javax.swing.JOptionPane; | |
| 10 | import java.awt.event.ActionEvent; | |
| 11 | import java.awt.event.ActionListener; | |
| 12 | import java.util.Observable; | |
| 13 | import java.util.Observer; | |
| 14 | ||
| 15 | /** | |
| 16 | * Button to clear all the information from the main panel. | |
| 17 | * <p> | |
| 18 | * The button will ask the user if he wants to delete the received emails or not.<br> | |
| 19 | * If yes, emails will be deleted from file system. | |
| 20 | * </p> | |
| 21 | * | |
| 22 | * @author Nilhcem | |
| 23 | * @since 1.0 | |
| 24 | */ | |
| 25 | 0 | public final class ClearAllButton extends Observable implements Observer { |
| 26 | ||
| 27 | 0 | private final I18n i18n = I18n.INSTANCE; |
| 28 | 0 | private final JButton button = new JButton(i18n.get("clearall.button")); |
| 29 | ||
| 30 | /** | |
| 31 | * Creates the "clear all" button" | |
| 32 | * <p> | |
| 33 | * The button will be disabled by default, since no email is received when the application starts.<br> | |
| 34 | * The button will display a confirmation dialog to know if it needs to delete the received emails or not.<br> | |
| 35 | * If yes, emails will be deleted from the file system. | |
| 36 | * </p> | |
| 37 | */ | |
| 38 | 0 | public ClearAllButton() { |
| 39 | 0 | button.setToolTipText(i18n.get("clearall.tooltip")); |
| 40 | 0 | button.addActionListener(new ActionListener() { |
| 41 | @Override | |
| 42 | public void actionPerformed(ActionEvent e) { | |
| 43 | 0 | int answer = JOptionPane.showConfirmDialog(button.getParent(), i18n.get("clearall.delete.email"), |
| 44 | 0 | String.format(i18n.get("clearall.title"), Configuration.INSTANCE.get("application.name")), |
| 45 | JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); | |
| 46 | 0 | if (answer == JOptionPane.CLOSED_OPTION) { |
| 47 | 0 | return; |
| 48 | } | |
| 49 | ||
| 50 | 0 | synchronized (SMTPServerHandler.INSTANCE.getMailSaver().getLock()) { |
| 51 | // Note: Should delete emails before calling observers, since observers will clean the model. | |
| 52 | 0 | if (answer == JOptionPane.YES_OPTION) { |
| 53 | 0 | SMTPServerHandler.INSTANCE.getMailSaver().deleteEmails(); |
| 54 | } | |
| 55 | 0 | setChanged(); |
| 56 | 0 | notifyObservers(); |
| 57 | 0 | button.setEnabled(false); |
| 58 | 0 | } |
| 59 | 0 | } |
| 60 | }); | |
| 61 | 0 | button.setEnabled(false); |
| 62 | 0 | } |
| 63 | ||
| 64 | /** | |
| 65 | * Returns the JButton object. | |
| 66 | * | |
| 67 | * @return the JButton object. | |
| 68 | */ | |
| 69 | public JButton get() { | |
| 70 | 0 | return button; |
| 71 | } | |
| 72 | ||
| 73 | /** | |
| 74 | * Enables the button, so that the user can clear/delete emails. | |
| 75 | * <p> | |
| 76 | * This method will be called by a {@link MailSaver} object when an email will be received. | |
| 77 | * </p> | |
| 78 | */ | |
| 79 | @Override | |
| 80 | public void update(Observable o, Object arg) { | |
| 81 | 0 | if (o instanceof MailSaver && !button.isEnabled()) { |
| 82 | 0 | button.setEnabled(true); |
| 83 | } | |
| 84 | 0 | } |
| 85 | } |