Coverage Report - com.nilhcem.fakesmtp.gui.tab.LogsPane
 
Classes in this File Line Coverage Branch Coverage Complexity
LogsPane
0%
0/23
0%
0/6
1.75
 
 1  
 package com.nilhcem.fakesmtp.gui.tab;
 2  
 
 3  
 import ch.qos.logback.classic.spi.ILoggingEvent;
 4  
 import ch.qos.logback.core.spi.AppenderAttachable;
 5  
 import com.nilhcem.fakesmtp.core.Configuration;
 6  
 import com.nilhcem.fakesmtp.gui.info.ClearAllButton;
 7  
 import com.nilhcem.fakesmtp.log.SMTPLogsAppender;
 8  
 import com.nilhcem.fakesmtp.log.SMTPLogsObservable;
 9  
 import org.slf4j.Logger;
 10  
 import org.slf4j.LoggerFactory;
 11  
 
 12  
 import javax.swing.JScrollPane;
 13  
 import javax.swing.JTextArea;
 14  
 import java.text.SimpleDateFormat;
 15  
 import java.util.Date;
 16  
 import java.util.Observable;
 17  
 import java.util.Observer;
 18  
 
 19  
 /**
 20  
  * Scrolled text area where will be displayed the SMTP logs.
 21  
  *
 22  
  * @author Nilhcem
 23  
  * @since 1.0
 24  
  */
 25  
 public final class LogsPane implements Observer {
 26  
 
 27  0
         private final JScrollPane logsPane = new JScrollPane();
 28  0
         private final SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss a");
 29  0
         private final JTextArea logsArea = new JTextArea();
 30  
 
 31  
         /**
 32  
          * Creates the text area, sets it as non-editable and sets an observer to intercept logs.
 33  
          */
 34  0
         public LogsPane() {
 35  0
                 logsArea.setEditable(false);
 36  0
                 logsPane.getViewport().add(logsArea, null);
 37  0
                 addObserverToSmtpLogAppender();
 38  0
         }
 39  
 
 40  
         /**
 41  
          * Returns the JScrollPane object.
 42  
          *
 43  
          * @return the JScrollPane object.
 44  
          */
 45  
         public JScrollPane get() {
 46  0
                 return logsPane;
 47  
         }
 48  
 
 49  
         /**
 50  
          * Adds this object to the SMTP logs appender observable, to intercept logs.
 51  
          * <p>
 52  
          * The goal is to be informed when the log appender will received some debug SMTP logs.<br>
 53  
          * When a log is written, the appender will notify this class which will display it in the text area.
 54  
          * </p>
 55  
          */
 56  
         private void addObserverToSmtpLogAppender() {
 57  0
                 Logger smtpLogger = LoggerFactory.getLogger(org.subethamail.smtp.server.Session.class);
 58  0
                 String appenderName = Configuration.INSTANCE.get("logback.appender.name");
 59  
 
 60  
                 @SuppressWarnings("unchecked")
 61  0
                 SMTPLogsAppender<ILoggingEvent> appender = (SMTPLogsAppender<ILoggingEvent>)
 62  0
                         ((AppenderAttachable<ILoggingEvent>) smtpLogger).getAppender(appenderName);
 63  0
                 if (appender == null) {
 64  0
                         LoggerFactory.getLogger(LogsPane.class).error("Can't find logger: {}", appenderName);
 65  
                 } else {
 66  0
                         appender.getObservable().addObserver(this);
 67  
                 }
 68  0
         }
 69  
 
 70  
         /**
 71  
          * Updates the content of the text area.
 72  
          * <p>
 73  
          * This method will be called by an observable element.
 74  
      * </p>
 75  
          * <ul>
 76  
          *   <li>If the observable is a {@link SMTPLogsObservable} object, the text area will display the received log.</li>
 77  
          *   <li>If the observable is a {@link ClearAllButton} object, the text area will be cleared.</li>
 78  
          * </ul>
 79  
          *
 80  
          * @param o the observable element which will notify this class.
 81  
          * @param log optional parameter (a {@code String} object, when the observable is a {@code SMTPLogsObservable} object, which will contain the log).
 82  
          */
 83  
         @Override
 84  
         public void update(Observable o, Object log) {
 85  0
                 if (o instanceof SMTPLogsObservable) {
 86  
                         // Update and scroll pane to the bottom
 87  0
                         logsArea.append(String.format("%s - %s%n", dateFormat.format(new Date()), log));
 88  0
                         logsArea.setCaretPosition(logsArea.getText().length());
 89  0
                 } else if (o instanceof ClearAllButton) {
 90  
                         // Remove text
 91  0
                         logsArea.setText("");
 92  
                 }
 93  0
         }
 94  
 }