Skip to content
This repository has been archived by the owner on Apr 19, 2021. It is now read-only.

Improved the feature #25 #35

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/main/java/com/nilhcem/fakesmtp/gui/MainFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import org.slf4j.Logger;

/**
* Provides the main window of the application.
Expand All @@ -26,6 +29,8 @@ public final class MainFrame {
private final MenuBar menu = new MenuBar(this);
private final MainPanel panel = new MainPanel(menu);

private static final Logger LOGGER = LoggerFactory.getLogger(MainFrame.class);

/**
* Creates the main window and makes it visible.
* <p>
Expand All @@ -52,9 +57,17 @@ public MainFrame() {
getClass().getResource(Configuration.INSTANCE.get("application.icon.path")));

MainWindowListener windowListener = new MainWindowListener(this);

mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
mainFrame.addWindowListener(windowListener); // for catching windowClosing event

mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

mainFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
close();
}
});

mainFrame.addWindowStateListener(windowListener); // used for TrayIcon
mainFrame.setSize(frameSize);
mainFrame.setMinimumSize(frameSize);

Expand Down Expand Up @@ -93,20 +106,22 @@ public void run() {

public void close() {
// Save configuration

LOGGER.debug("Closing the application and saving the configuration");

Configuration.INSTANCE.set("smtp.default.port", panel.getPortText().get().getText());
Configuration.INSTANCE.set("emails.default.dir", panel.getSaveMsgTextField().get().getText());

try {
Configuration.INSTANCE.saveToUserProfile();
} catch (IOException ex) {
LoggerFactory.getLogger(MainFrame.class).error("Could not save configuration", ex);
LOGGER.error("Could not save configuration", ex);
}
// Check for SMTP server running and stop it
if (SMTPServerHandler.INSTANCE.getSmtpServer().isRunning()) {
SMTPServerHandler.INSTANCE.getSmtpServer().stop();
}

mainFrame.dispose();
System.exit(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ public ExitActionListener(MainFrame mainFrame) {
@Override
public void actionPerformed(ActionEvent e) {
mainFrame.close();
System.exit(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@
*/
public class MainWindowListener extends WindowAdapter {

private final MainFrame mainFrame;
private TrayIcon trayIcon = null;
private final boolean useTray;

private static final Logger LOGGER = LoggerFactory.getLogger(MainWindowListener.class);

/**
* @param mainFrame The MainFrame class used for closing actions.
* @param mainFrame The MainFrame class used for closing actions from
* TrayPopup.
*/
public MainWindowListener(final MainFrame mainFrame) {
this.mainFrame = mainFrame;
useTray = (SystemTray.isSupported() && Boolean.parseBoolean(Configuration.INSTANCE.get("application.tray.use")));

if (useTray) {
Expand All @@ -53,62 +52,42 @@ public MainWindowListener(final MainFrame mainFrame) {
}

@Override
public void windowIconified(final WindowEvent e) {
super.windowIconified(e);
public void windowStateChanged(WindowEvent e) {
super.windowStateChanged(e);

if (useTray) {
minimizeFrameIntoTray((JFrame) e.getSource());
}
}

@Override
public void windowClosing(WindowEvent e) {
if (useTray && minimizeFrameIntoTray((JFrame) e.getSource())) {
if (!useTray) {
return;
}

mainFrame.close();
}
final SystemTray tray = SystemTray.getSystemTray();
final JFrame frame = (JFrame) e.getSource();

/**
* Minimizes the specified frame into the tray. When tray icon receives
* an action, the frame is restored to the previous state.
*
* @return a boolean value whether the window has been minimized or not.
*/
private boolean minimizeFrameIntoTray(final JFrame frame) {
if (!useTray) {
LOGGER.warn("It is not allowed to use the system tray");
if ((e.getNewState() & Frame.ICONIFIED) != 0) {
try {
/* Displays the window when the icon is clicked twice */
trayIcon.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
int state = frame.getExtendedState();
state &= ~Frame.ICONIFIED;

return false;
}
frame.setExtendedState(state);
frame.setVisible(true);

final SystemTray tray = SystemTray.getSystemTray();
tray.remove(trayIcon);

/* Displays the window when the icon is clicked twice */
trayIcon.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
int state = frame.getExtendedState();
state &= ~Frame.ICONIFIED;
trayIcon.removeActionListener(this);
}
});

frame.setVisible(true);
frame.setExtendedState(state);
tray.remove(trayIcon);
tray.add(trayIcon);

trayIcon.removeActionListener(this);
frame.dispose();
} catch (AWTException ex) {
LOGGER.error("Couldn't create a tray icon, the minimizing is not possible", ex);
}
});

try {
tray.add(trayIcon);
frame.setVisible(false);
} catch (AWTException ex) {
LOGGER.error("Couldn't create a tray icon, the minimizing is not possible", ex);

return false;
} else {
frame.setVisible(true);
}

return true;
}
}