package com.nilhcem.fakesmtp.gui.info;

import java.awt.Font;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JLabel;

import com.nilhcem.fakesmtp.model.UIModel;
import com.nilhcem.fakesmtp.server.MailSaver;

import com.apple.eawt.Application;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Label class to display the number of received emails.
 *
 * @author Nilhcem
 * @since 1.0
 */
public final class NbReceivedLabel implements Observer {
    private static final Logger LOGGER = LoggerFactory.getLogger(NbReceivedLabel.class);

	private final JLabel nbReceived = new JLabel("0");

	/**
	 * Creates the label class (with a bold font).
	 */
	public NbReceivedLabel() {
		Font boldFont = new Font(nbReceived.getFont().getName(), Font.BOLD, nbReceived.getFont().getSize());
		nbReceived.setFont(boldFont);
	}

	/**
	 * Returns the JLabel object.
	 *
	 * @return the JLabel object.
	 */
	public JLabel get() {
		return nbReceived;
	}

	/**
	 * Actions which will be done when the component will be notified by an Observable object.
	 * <ul>
	 *   <li>If the observable element is a {@link MailSaver} object, the method will increment
	 *   the number of received messages and update the {@link UIModel};</li>
	 *   <li>If the observable element is a {@link ClearAllButton}, the method will reinitialize
	 *   the number of received messages and update the {@link UIModel}.</li>
     *   <li>When running on OS X the method will also update the Dock Icon with the number of
     *   received messages.</li>
	 * </ul>
	 *
	 * @param o the observable element which will notify this class.
	 * @param arg optional parameters (not used).
	 */
	@Override
	public void update(Observable o, Object arg) {
		if (o instanceof MailSaver) {
			UIModel model = UIModel.INSTANCE;
			int countMsg = model.getNbMessageReceived() + 1;
			model.setNbMessageReceived(countMsg);
            updateDockIconBadge(Integer.toString(countMsg));
			nbReceived.setText(Integer.toString(countMsg));
		} else if (o instanceof ClearAllButton) {
			UIModel.INSTANCE.setNbMessageReceived(0);
            updateDockIconBadge("");
			nbReceived.setText("0");
		}
	}

    private void updateDockIconBadge(String badgeValue) {
        try {
            Application.getApplication().setDockIconBadge(badgeValue);
        } catch (RuntimeException e) {
            LOGGER.debug("Error: {} - This is probably because we run on a non-Mac platform and these components are not implemented", e.getMessage());
        } catch (Exception e) {
            LOGGER.error("", e);
        }
    }
}

@@ -481,20 +481,18 @@
 erver %7B%0A
-    
+%0A%09
 private 
@@ -1411,19 +1411,16 @@
 del%7D
-.
+;
 %3C/li%3E%0A
-    
+%09
  *  
@@ -1506,20 +1506,17 @@
 mber of%0A
-    
+%09
  *   rec
@@ -1848,16 +1848,69 @@
 () + 1;%0A
+%09%09%09String countMsgStr = Integer.toString(countMsg);%0A%0A
 %09%09%09model
@@ -1942,28 +1942,19 @@
 ntMsg);%0A
-            
+%09%09%09
 updateDo
@@ -1969,33 +1969,16 @@
 dge(
-Integer.toString(
 countMsg
 ));%0A
@@ -1969,25 +1969,27 @@
 dge(countMsg
-)
+Str
 );%0A%09%09%09nbRece
@@ -2005,33 +2005,16 @@
 ext(
-Integer.toString(
 countMsg
 ));%0A
@@ -2009,17 +2009,19 @@
 countMsg
-)
+Str
 );%0A%09%09%7D e
@@ -2104,28 +2104,19 @@
 ved(0);%0A
-            
+%09%09%09
 updateDo
@@ -2168,20 +2168,17 @@
 %09%09%7D%0A%09%7D%0A%0A
-    
+%09
 private 
@@ -2227,34 +2227,19 @@
 ) %7B%0A
-        try %7B%0A            
+%09%09try %7B%0A%09%09%09
 Appl
@@ -2293,24 +2293,18 @@
 Value);%0A
-        
+%09%09
 %7D catch 
@@ -2322,36 +2322,27 @@
 eption e) %7B%0A
-            
+%09%09%09
 LOGGER.debug
@@ -2469,24 +2469,18 @@
 age());%0A
-        
+%09%09
 %7D catch 
@@ -2499,20 +2499,11 @@
 ) %7B%0A
-            
+%09%09%09
 LOGG
@@ -2523,22 +2523,13 @@
 e);%0A
-        %7D%0A    
+%09%09%7D%0A%09
 %7D%0A%7D%0A

Format code