| 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 | 0 | private static final Logger LOGGER = LoggerFactory.getLogger(NbReceivedLabel.class); |
| 25 | |
|
| 26 | 0 | private final JLabel nbReceived = new JLabel("0"); |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | 0 | public NbReceivedLabel() { |
| 32 | 0 | Font boldFont = new Font(nbReceived.getFont().getName(), Font.BOLD, nbReceived.getFont().getSize()); |
| 33 | 0 | nbReceived.setFont(boldFont); |
| 34 | 0 | } |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
public JLabel get() { |
| 42 | 0 | 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 | 0 | if (o instanceof MailSaver) { |
| 62 | 0 | UIModel model = UIModel.INSTANCE; |
| 63 | 0 | int countMsg = model.getNbMessageReceived() + 1; |
| 64 | 0 | String countMsgStr = Integer.toString(countMsg); |
| 65 | |
|
| 66 | 0 | model.setNbMessageReceived(countMsg); |
| 67 | 0 | updateDockIconBadge(countMsgStr); |
| 68 | 0 | nbReceived.setText(countMsgStr); |
| 69 | 0 | } else if (o instanceof ClearAllButton) { |
| 70 | 0 | UIModel.INSTANCE.setNbMessageReceived(0); |
| 71 | 0 | updateDockIconBadge(""); |
| 72 | 0 | nbReceived.setText("0"); |
| 73 | |
} |
| 74 | 0 | } |
| 75 | |
|
| 76 | |
private void updateDockIconBadge(String badgeValue) { |
| 77 | |
try { |
| 78 | 0 | Application.getApplication().setDockIconBadge(badgeValue); |
| 79 | 0 | } catch (RuntimeException e) { |
| 80 | 0 | LOGGER.debug("Error: {} - This is probably because we run on a non-Mac platform and these components are not implemented", e.getMessage()); |
| 81 | 0 | } catch (Exception e) { |
| 82 | 0 | LOGGER.error("", e); |
| 83 | 0 | } |
| 84 | 0 | } |
| 85 | |
} |