Skip to content

Commit 57c6263

Browse files
VestNilhcem
authored andcommitted
Removed OS X fix, because the latest JDK (1.8.51) works with tray event better;
Forgot to save the configuration, when the main window is closed;
1 parent 840f4d3 commit 57c6263

3 files changed

Lines changed: 61 additions & 62 deletions

File tree

‎src/main/java/com/nilhcem/fakesmtp/gui/MainFrame.java‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
import java.awt.Dimension;
1313
import java.awt.Image;
1414
import java.awt.Toolkit;
15+
import java.awt.event.WindowAdapter;
16+
import java.awt.event.WindowEvent;
1517
import java.io.IOException;
18+
import org.slf4j.Logger;
1619

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

32+
private static final Logger LOGGER = LoggerFactory.getLogger(MainFrame.class);
33+
2934
/**
3035
* Creates the main window and makes it visible.
3136
* <p>
@@ -52,8 +57,16 @@ public MainFrame() {
5257
getClass().getResource(Configuration.INSTANCE.get("application.icon.path")));
5358

5459
MainWindowListener windowListener = new MainWindowListener(this);
55-
60+
5661
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
62+
63+
mainFrame.addWindowListener(new WindowAdapter() {
64+
@Override
65+
public void windowClosing(WindowEvent e) {
66+
close();
67+
}
68+
});
69+
5770
mainFrame.addWindowStateListener(windowListener); // used for TrayIcon
5871
mainFrame.setSize(frameSize);
5972
mainFrame.setMinimumSize(frameSize);
@@ -93,20 +106,22 @@ public void run() {
93106

94107
public void close() {
95108
// Save configuration
109+
110+
LOGGER.debug("Closing the application and saving the configuration");
111+
96112
Configuration.INSTANCE.set("smtp.default.port", panel.getPortText().get().getText());
97113
Configuration.INSTANCE.set("emails.default.dir", panel.getSaveMsgTextField().get().getText());
98114

99115
try {
100116
Configuration.INSTANCE.saveToUserProfile();
101117
} catch (IOException ex) {
102-
LoggerFactory.getLogger(MainFrame.class).error("Could not save configuration", ex);
118+
LOGGER.error("Could not save configuration", ex);
103119
}
104120
// Check for SMTP server running and stop it
105121
if (SMTPServerHandler.INSTANCE.getSmtpServer().isRunning()) {
106122
SMTPServerHandler.INSTANCE.getSmtpServer().stop();
107123
}
108124

109125
mainFrame.dispose();
110-
System.exit(0);
111126
}
112127
}

‎src/main/java/com/nilhcem/fakesmtp/gui/listeners/ExitActionListener.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ public ExitActionListener(MainFrame mainFrame) {
2626
@Override
2727
public void actionPerformed(ActionEvent e) {
2828
mainFrame.close();
29+
System.exit(0);
2930
}
3031
}

‎src/main/java/com/nilhcem/fakesmtp/gui/listeners/MainWindowListener.java‎

Lines changed: 42 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,13 @@ public class MainWindowListener extends WindowAdapter {
3131

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

34-
private final boolean isMac;
35-
3634
/**
37-
* @param mainFrame The MainFrame class used for closing actions
38-
* from TrayPopup.
35+
* @param mainFrame The MainFrame class used for closing actions from
36+
* TrayPopup.
3937
*/
4038
public MainWindowListener(final MainFrame mainFrame) {
4139
useTray = (SystemTray.isSupported() && Boolean.parseBoolean(Configuration.INSTANCE.get("application.tray.use")));
42-
40+
4341
if (useTray) {
4442
final TrayPopup trayPopup = new TrayPopup(mainFrame);
4543

@@ -51,60 +49,45 @@ public MainWindowListener(final MainFrame mainFrame) {
5149
trayIcon.setImageAutoSize(true);
5250
trayIcon.setPopupMenu(trayPopup.get());
5351
}
54-
55-
/* the order of AWT events on OS X is slightly different */
56-
String osName = System.getProperty("os.name").toLowerCase();
57-
isMac = (osName.indexOf("mac") >= 0);
5852
}
5953

60-
@Override
61-
public void windowStateChanged(WindowEvent e) {
62-
super.windowStateChanged(e);
63-
64-
if (!useTray) {
65-
return;
66-
}
67-
68-
final SystemTray tray = SystemTray.getSystemTray();
69-
final JFrame frame = (JFrame) e.getSource();
70-
71-
if ((e.getNewState() & Frame.ICONIFIED) != 0) {
72-
try {
73-
/* Displays the window when the icon is clicked twice */
74-
trayIcon.addActionListener(new ActionListener() {
75-
@Override
76-
public void actionPerformed(ActionEvent ae) {
77-
int state = frame.getExtendedState();
78-
state &= ~Frame.ICONIFIED;
79-
80-
frame.setExtendedState(state);
81-
frame.setVisible(true);
82-
83-
if (!isMac) {
84-
tray.remove(trayIcon);
85-
}
86-
87-
trayIcon.removeActionListener(this);
88-
}
89-
});
90-
91-
tray.add(trayIcon);
92-
93-
if (isMac) {
94-
frame.setVisible(false);
95-
}
96-
else {
97-
frame.dispose();
98-
}
99-
} catch (AWTException ex) {
100-
LOGGER.error("Couldn't create a tray icon, the minimizing is not possible", ex);
101-
}
102-
}
103-
else {
104-
if (isMac) {
105-
tray.remove(trayIcon);
106-
}
107-
frame.setVisible(true);
108-
}
109-
}
54+
@Override
55+
public void windowStateChanged(WindowEvent e) {
56+
super.windowStateChanged(e);
57+
58+
if (!useTray) {
59+
return;
60+
}
61+
62+
final SystemTray tray = SystemTray.getSystemTray();
63+
final JFrame frame = (JFrame) e.getSource();
64+
65+
if ((e.getNewState() & Frame.ICONIFIED) != 0) {
66+
try {
67+
/* Displays the window when the icon is clicked twice */
68+
trayIcon.addActionListener(new ActionListener() {
69+
@Override
70+
public void actionPerformed(ActionEvent ae) {
71+
int state = frame.getExtendedState();
72+
state &= ~Frame.ICONIFIED;
73+
74+
frame.setExtendedState(state);
75+
frame.setVisible(true);
76+
77+
tray.remove(trayIcon);
78+
79+
trayIcon.removeActionListener(this);
80+
}
81+
});
82+
83+
tray.add(trayIcon);
84+
85+
frame.dispose();
86+
} catch (AWTException ex) {
87+
LOGGER.error("Couldn't create a tray icon, the minimizing is not possible", ex);
88+
}
89+
} else {
90+
frame.setVisible(true);
91+
}
92+
}
11093
}

0 commit comments

Comments
 (0)