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
17
18
19
20
21
22
23
24
25 public final class ClearAllButton extends Observable implements Observer {
26
27 private final I18n i18n = I18n.INSTANCE;
28 private final JButton button = new JButton(i18n.get("clearall.button"));
29
30
31
32
33
34
35
36
37
38 public ClearAllButton() {
39 button.setToolTipText(i18n.get("clearall.tooltip"));
40 button.addActionListener(new ActionListener() {
41 @Override
42 public void actionPerformed(ActionEvent e) {
43 int answer = JOptionPane.showConfirmDialog(button.getParent(), i18n.get("clearall.delete.email"),
44 String.format(i18n.get("clearall.title"), Configuration.INSTANCE.get("application.name")),
45 JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
46 if (answer == JOptionPane.CLOSED_OPTION) {
47 return;
48 }
49
50 synchronized (SMTPServerHandler.INSTANCE.getMailSaver().getLock()) {
51
52 if (answer == JOptionPane.YES_OPTION) {
53 SMTPServerHandler.INSTANCE.getMailSaver().deleteEmails();
54 }
55 setChanged();
56 notifyObservers();
57 button.setEnabled(false);
58 }
59 }
60 });
61 button.setEnabled(false);
62 }
63
64
65
66
67
68
69 public JButton get() {
70 return button;
71 }
72
73
74
75
76
77
78
79 @Override
80 public void update(Observable o, Object arg) {
81 if (o instanceof MailSaver && !button.isEnabled()) {
82 button.setEnabled(true);
83 }
84 }
85 }