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