View Javadoc
1   package com.nilhcem.fakesmtp.gui;
2   
3   import com.nilhcem.fakesmtp.core.ArgsHandler;
4   import com.nilhcem.fakesmtp.core.Configuration;
5   import com.nilhcem.fakesmtp.core.exception.UncaughtExceptionHandler;
6   import com.nilhcem.fakesmtp.gui.listeners.MainWindowListener;
7   import com.nilhcem.fakesmtp.model.UIModel;
8   import com.nilhcem.fakesmtp.server.SMTPServerHandler;
9   import org.slf4j.LoggerFactory;
10  
11  import javax.swing.JFrame;
12  import java.awt.Dimension;
13  import java.awt.Image;
14  import java.awt.Toolkit;
15  import java.awt.event.WindowAdapter;
16  import java.awt.event.WindowEvent;
17  import java.io.IOException;
18  import org.slf4j.Logger;
19  import org.subethamail.smtp.server.SMTPServer;
20  
21  
22  /**
23   * Provides the main window of the application.
24   *
25   * @author Nilhcem
26   * @since 1.0
27   */
28  public final class MainFrame {
29  
30  	private static final Logger LOGGER = LoggerFactory.getLogger(MainFrame.class);
31  
32  	private final JFrame mainFrame = new JFrame(Configuration.INSTANCE.get("application.title"));
33  	private final MenuBar menu = new MenuBar(this);
34  	private final MainPanel panel = new MainPanel(menu);
35  
36  	/**
37  	 * Creates the main window and makes it visible.
38  	 * <p>
39  	 * First, assigns the main panel to the default uncaught exception handler to display exceptions in this panel.<br><br>
40  	 * Before creating the main window, the application will have to set some elements, such as:
41  	 * </p>
42  	 * <ul>
43  	 *   <li>The minimum and default size;</li>
44  	 *   <li>The menu bar and the main panel;</li>
45  	 *   <li>An icon image;</li>
46  	 *   <li>A shutdown hook to stop the server, once the main window is closed.</li>
47  	 * </ul><br>
48  	 * <p>
49  	 * The icon of the application is a modified version from the one provided in "{@code WebAppers.com}"
50  	 * <i>(Creative Commons Attribution 3.0 License)</i>.
51  	 * </p>
52  	 */
53  	public MainFrame() {
54  		((UncaughtExceptionHandler) Thread.getDefaultUncaughtExceptionHandler()).setParentComponent(panel.get());
55  		Dimension frameSize = new Dimension(Integer.parseInt(Configuration.INSTANCE.get("application.min.width")),
56  			Integer.parseInt(Configuration.INSTANCE.get("application.min.height")));
57  
58  		Image iconImage = Toolkit.getDefaultToolkit().getImage(
59  			getClass().getResource(Configuration.INSTANCE.get("application.icon.path")));
60  
61  		MainWindowListener windowListener = new MainWindowListener(this);
62  
63  		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
64  		mainFrame.addWindowListener(new WindowAdapter() {
65  			@Override
66  			public void windowClosing(WindowEvent e) {
67  				close();
68  			}
69  		});
70  
71  		mainFrame.addWindowStateListener(windowListener); // used for TrayIcon
72  		mainFrame.setSize(frameSize);
73  		mainFrame.setMinimumSize(frameSize);
74  
75  		mainFrame.setJMenuBar(menu.get());
76  		mainFrame.getContentPane().add(panel.get());
77  		mainFrame.setLocationRelativeTo(null); // Center main frame
78  		mainFrame.setIconImage(iconImage);
79  
80  		// Add shutdown hook to stop server if enabled
81  		Runtime.getRuntime().addShutdownHook(new Thread() {
82  			@Override
83  			public void run() {
84  				SMTPServerHandler.INSTANCE.stopServer();
85  			}
86  		});
87  
88  		// Restore last saved smtp port (if not overridden by the user)
89  		String smtpPort = ArgsHandler.INSTANCE.getPort();
90  		if (smtpPort == null) {
91  			smtpPort = Configuration.INSTANCE.get("smtp.default.port");
92  		}
93  		panel.getPortText().setText(smtpPort);
94  
95  		// Restore last emails directory (if not overridden by the user)
96  		String emailsDir = ArgsHandler.INSTANCE.getOutputDirectory();
97  		if (emailsDir == null) {
98  			emailsDir = Configuration.INSTANCE.get("emails.default.dir");
99  		}
100 		if (emailsDir != null && !emailsDir.isEmpty()) {
101 			panel.getSaveMsgTextField().get().setText(emailsDir);
102 			UIModel.INSTANCE.setSavePath(emailsDir);
103 		}
104 
105 		mainFrame.setVisible(true);
106 	}
107 
108 	public void close() {
109 		LOGGER.debug("Closing the application and saving the configuration");
110 
111 		Configuration.INSTANCE.set("smtp.default.port", panel.getPortText().get().getText());
112 		Configuration.INSTANCE.set("emails.default.dir", panel.getSaveMsgTextField().get().getText());
113 
114 		try {
115 			Configuration.INSTANCE.saveToUserProfile();
116 		} catch (IOException ex) {
117 			LOGGER.error("Could not save configuration", ex);
118 		}
119 		// Check for SMTP server running and stop it
120 		SMTPServer smtpServer = SMTPServerHandler.INSTANCE.getSmtpServer();
121 		if (smtpServer != null && smtpServer.isRunning()) {
122 			smtpServer.stop();
123 		}
124 
125 		mainFrame.dispose();
126 	}
127 }