forked from Nilhcem/FakeSMTP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 25: The feature "minimize to tray" is implemented
Created separate ActionListeners for About and Exit actions; Created TrayPopup for the icon tray popup menu (it utilizes the actions mentioned above); Created MainWindow listener that is responsible for the window hiding + restoration from/to the system tray; Slightly formatted Javadocs of existing classes; Not available: No configuration for the feature (it is always enabled if the platform allows this);
- Loading branch information
Showing
6 changed files
with
321 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.nilhcem.fakesmtp.gui; | ||
|
||
import com.nilhcem.fakesmtp.core.I18n; | ||
import com.nilhcem.fakesmtp.gui.listeners.AboutActionListener; | ||
import com.nilhcem.fakesmtp.gui.listeners.ExitActionListener; | ||
import java.awt.MenuItem; | ||
import java.awt.PopupMenu; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Provides the popup menu for the SystemTray icon. | ||
* | ||
* @author Vest | ||
* @since 2.1 | ||
*/ | ||
public class TrayPopup { | ||
|
||
private final I18n i18n = I18n.INSTANCE; | ||
private static final Logger LOGGER = LoggerFactory.getLogger(TrayPopup.class); | ||
|
||
private final PopupMenu popup = new PopupMenu(); | ||
|
||
/** | ||
* The popup menu used by the icon in the system tray. | ||
* | ||
* @param mainFrame MainFrame class. | ||
*/ | ||
public TrayPopup(final MainFrame mainFrame) { | ||
// Create a popup menu components | ||
final MenuItem aboutItem = new MenuItem(i18n.get("menubar.about")); | ||
aboutItem.addActionListener(new AboutActionListener(null)); | ||
|
||
final MenuItem exitItem = new MenuItem(i18n.get("menubar.exit")); | ||
exitItem.addActionListener(new ExitActionListener(mainFrame)); | ||
|
||
popup.add(aboutItem); | ||
popup.addSeparator(); | ||
popup.add(exitItem); | ||
} | ||
|
||
/** | ||
* Returns the PopupMenu object. | ||
* | ||
* @return the PopupMenu object. | ||
*/ | ||
public PopupMenu get() { | ||
return popup; | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
src/main/java/com/nilhcem/fakesmtp/gui/listeners/AboutActionListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.nilhcem.fakesmtp.gui.listeners; | ||
|
||
import com.nilhcem.fakesmtp.core.Configuration; | ||
import com.nilhcem.fakesmtp.core.I18n; | ||
import java.awt.Container; | ||
import java.awt.Desktop; | ||
import java.awt.Font; | ||
import java.awt.event.ActionEvent; | ||
import java.awt.event.ActionListener; | ||
import java.net.URI; | ||
import javax.swing.JEditorPane; | ||
import javax.swing.JLabel; | ||
import javax.swing.JOptionPane; | ||
import javax.swing.event.HyperlinkEvent; | ||
import javax.swing.event.HyperlinkListener; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Implements the About action. | ||
* | ||
* @author Vest | ||
* @since 2.1 | ||
*/ | ||
public class AboutActionListener implements ActionListener { | ||
|
||
private final I18n i18n = I18n.INSTANCE; | ||
private final Container parent; | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(AboutActionListener.class); | ||
|
||
/** | ||
* @param parent The parent container that is used for the About dialog | ||
* window | ||
*/ | ||
public AboutActionListener(final Container parent) { | ||
this.parent = parent; | ||
} | ||
|
||
@Override | ||
public void actionPerformed(ActionEvent e) { | ||
// for copying style | ||
final JLabel label = new JLabel(); | ||
final Font font = label.getFont(); | ||
|
||
// create some css from the label's font | ||
final StringBuffer style = new StringBuffer("font-family:") | ||
.append(font.getFamily()) | ||
.append(";font-weight:"); | ||
if (font.isBold()) { | ||
style.append("bold"); | ||
} else { | ||
style.append("normal"); | ||
} | ||
style.append(";font-size:") | ||
.append(font.getSize()) | ||
.append("pt;"); | ||
|
||
// html content | ||
final String link = i18n.get("menubar.about.dialog.link"); | ||
final JEditorPane ep = new JEditorPane("text/html", | ||
String.format("<html><body style=\"%s\">%s<br /><a href=\"%s\">%s</a></body></html>", | ||
style, i18n.get("menubar.about.dialog"), link, link)); | ||
|
||
// handle link events | ||
ep.addHyperlinkListener(new HyperlinkListener() { | ||
@Override | ||
public void hyperlinkUpdate(HyperlinkEvent e) { | ||
if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) { | ||
AboutActionListener.launchUrl(e.getURL().toString()); | ||
} | ||
} | ||
}); | ||
ep.setEditable(false); | ||
ep.setBackground(label.getBackground()); | ||
|
||
// show | ||
JOptionPane.showMessageDialog(parent, ep, String.format(i18n.get("menubar.about.title"), | ||
Configuration.INSTANCE.get("application.name")), JOptionPane.INFORMATION_MESSAGE); | ||
} | ||
|
||
/** | ||
* Opens a web browser to launch the URL specified in parameters. | ||
* | ||
* @param url the URL to launch. | ||
*/ | ||
private static void launchUrl(String url) { | ||
if (Desktop.isDesktopSupported()) { | ||
try { | ||
Desktop desktop = Desktop.getDesktop(); | ||
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) { | ||
desktop.browse(new URI(url)); | ||
} | ||
} catch (final Exception e) { | ||
LOGGER.error("", e); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.