1 package com.nilhcem.fakesmtp.gui.info;
2
3 import java.awt.Font;
4 import java.util.Observable;
5 import java.util.Observer;
6
7 import javax.swing.JLabel;
8
9 import com.nilhcem.fakesmtp.model.UIModel;
10 import com.nilhcem.fakesmtp.server.MailSaver;
11
12 import com.apple.eawt.Application;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16
17
18
19
20
21
22 public final class NbReceivedLabel implements Observer {
23
24 private static final Logger LOGGER = LoggerFactory.getLogger(NbReceivedLabel.class);
25
26 private final JLabel nbReceived = new JLabel("0");
27
28
29
30
31 public NbReceivedLabel() {
32 Font boldFont = new Font(nbReceived.getFont().getName(), Font.BOLD, nbReceived.getFont().getSize());
33 nbReceived.setFont(boldFont);
34 }
35
36
37
38
39
40
41 public JLabel get() {
42 return nbReceived;
43 }
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 @Override
60 public void update(Observable o, Object arg) {
61 if (o instanceof MailSaver) {
62 UIModel model = UIModel.INSTANCE;
63 int countMsg = model.getNbMessageReceived() + 1;
64 String countMsgStr = Integer.toString(countMsg);
65
66 model.setNbMessageReceived(countMsg);
67 updateDockIconBadge(countMsgStr);
68 nbReceived.setText(countMsgStr);
69 } else if (o instanceof ClearAllButton) {
70 UIModel.INSTANCE.setNbMessageReceived(0);
71 updateDockIconBadge("");
72 nbReceived.setText("0");
73 }
74 }
75
76 private void updateDockIconBadge(String badgeValue) {
77 try {
78 Application.getApplication().setDockIconBadge(badgeValue);
79 } catch (RuntimeException e) {
80 LOGGER.debug("Error: {} - This is probably because we run on a non-Mac platform and these components are not implemented", e.getMessage());
81 } catch (Exception e) {
82 LOGGER.error("", e);
83 }
84 }
85 }