View Javadoc
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  	private final JScrollPane logsPane = new JScrollPane();
28  	private final SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss a");
29  	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  	public LogsPane() {
35  		logsArea.setEditable(false);
36  		logsPane.getViewport().add(logsArea, null);
37  		addObserverToSmtpLogAppender();
38  	}
39  
40  	/**
41  	 * Returns the JScrollPane object.
42  	 *
43  	 * @return the JScrollPane object.
44  	 */
45  	public JScrollPane get() {
46  		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  		Logger smtpLogger = LoggerFactory.getLogger(org.subethamail.smtp.server.Session.class);
58  		String appenderName = Configuration.INSTANCE.get("logback.appender.name");
59  
60  		@SuppressWarnings("unchecked")
61  		SMTPLogsAppender<ILoggingEvent> appender = (SMTPLogsAppender<ILoggingEvent>)
62  			((AppenderAttachable<ILoggingEvent>) smtpLogger).getAppender(appenderName);
63  		if (appender == null) {
64  			LoggerFactory.getLogger(LogsPane.class).error("Can't find logger: {}", appenderName);
65  		} else {
66  			appender.getObservable().addObserver(this);
67  		}
68  	}
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  		if (o instanceof SMTPLogsObservable) {
86  			// Update and scroll pane to the bottom
87  			logsArea.append(String.format("%s - %s%n", dateFormat.format(new Date()), log));
88  			logsArea.setCaretPosition(logsArea.getText().length());
89  		} else if (o instanceof ClearAllButton) {
90  			// Remove text
91  			logsArea.setText("");
92  		}
93  	}
94  }