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
24
25
26
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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);
72 mainFrame.setSize(frameSize);
73 mainFrame.setMinimumSize(frameSize);
74
75 mainFrame.setJMenuBar(menu.get());
76 mainFrame.getContentPane().add(panel.get());
77 mainFrame.setLocationRelativeTo(null);
78 mainFrame.setIconImage(iconImage);
79
80
81 Runtime.getRuntime().addShutdownHook(new Thread() {
82 @Override
83 public void run() {
84 SMTPServerHandler.INSTANCE.stopServer();
85 }
86 });
87
88
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
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
120 SMTPServer smtpServer = SMTPServerHandler.INSTANCE.getSmtpServer();
121 if (smtpServer != null && smtpServer.isRunning()) {
122 smtpServer.stop();
123 }
124
125 mainFrame.dispose();
126 }
127 }