2009-04-03 03:38:31 -04:00
|
|
|
package davmail.ui.tray;
|
2007-10-09 05:48:59 -04:00
|
|
|
|
2007-10-20 14:35:02 -04:00
|
|
|
import davmail.Settings;
|
2009-04-23 10:54:06 -04:00
|
|
|
import davmail.BundleMessage;
|
2009-05-17 18:24:43 -04:00
|
|
|
import davmail.DavGateway;
|
2008-11-03 05:56:57 -05:00
|
|
|
import davmail.ui.AboutFrame;
|
|
|
|
import davmail.ui.SettingsFrame;
|
2007-10-09 05:48:59 -04:00
|
|
|
import org.apache.log4j.Logger;
|
2009-06-22 09:37:47 -04:00
|
|
|
import org.apache.log4j.Level;
|
2007-10-09 05:48:59 -04:00
|
|
|
import org.apache.log4j.lf5.LF5Appender;
|
|
|
|
import org.apache.log4j.lf5.LogLevel;
|
|
|
|
import org.apache.log4j.lf5.viewer.LogBrokerMonitor;
|
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tray icon handler based on java 1.6
|
|
|
|
*/
|
|
|
|
public class AwtGatewayTray implements DavGatewayTrayInterface {
|
2009-04-07 08:39:09 -04:00
|
|
|
protected static final String TRAY2_PNG = "tray2.png";
|
|
|
|
protected static final String TRAY_PNG = "tray.png";
|
|
|
|
protected static final String TRAYINACTIVE_PNG = "trayinactive.png";
|
|
|
|
|
2007-10-09 05:48:59 -04:00
|
|
|
protected AwtGatewayTray() {
|
|
|
|
}
|
|
|
|
|
2009-04-16 17:16:40 -04:00
|
|
|
static AboutFrame aboutFrame;
|
|
|
|
static SettingsFrame settingsFrame;
|
2009-04-02 11:08:34 -04:00
|
|
|
|
2009-04-16 18:20:30 -04:00
|
|
|
private static TrayIcon trayIcon;
|
|
|
|
private static Image image;
|
|
|
|
private static Image image2;
|
|
|
|
private static Image inactiveImage;
|
2009-05-17 18:24:43 -04:00
|
|
|
protected static LogBrokerMonitor logBrokerMonitor;
|
2008-11-24 07:35:58 -05:00
|
|
|
private boolean isActive = true;
|
2007-10-09 05:48:59 -04:00
|
|
|
|
2008-11-03 05:56:57 -05:00
|
|
|
public Image getFrameIcon() {
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
2007-10-09 05:48:59 -04:00
|
|
|
public void switchIcon() {
|
2008-11-24 07:35:58 -05:00
|
|
|
isActive = true;
|
|
|
|
SwingUtilities.invokeLater(new Runnable() {
|
|
|
|
public void run() {
|
2009-05-07 04:45:45 -04:00
|
|
|
if (trayIcon.getImage().equals(image)) {
|
2008-11-24 07:35:58 -05:00
|
|
|
trayIcon.setImage(image2);
|
|
|
|
} else {
|
|
|
|
trayIcon.setImage(image);
|
|
|
|
}
|
2007-10-09 05:48:59 -04:00
|
|
|
}
|
2008-11-24 07:35:58 -05:00
|
|
|
});
|
2007-10-09 05:48:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void resetIcon() {
|
2008-11-24 07:35:58 -05:00
|
|
|
SwingUtilities.invokeLater(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
trayIcon.setImage(image);
|
|
|
|
}
|
|
|
|
});
|
2007-10-09 05:48:59 -04:00
|
|
|
}
|
|
|
|
|
2008-11-24 07:35:58 -05:00
|
|
|
public void inactiveIcon() {
|
|
|
|
isActive = false;
|
|
|
|
SwingUtilities.invokeLater(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
trayIcon.setImage(inactiveImage);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isActive() {
|
|
|
|
return isActive;
|
|
|
|
}
|
|
|
|
|
2009-06-22 09:37:47 -04:00
|
|
|
public void displayMessage(final String message, final Level level) {
|
2008-11-24 07:35:58 -05:00
|
|
|
SwingUtilities.invokeLater(new Runnable() {
|
|
|
|
public void run() {
|
2009-04-02 11:08:34 -04:00
|
|
|
if (trayIcon != null) {
|
|
|
|
TrayIcon.MessageType messageType = null;
|
2009-06-22 09:37:47 -04:00
|
|
|
if (level.equals(Level.INFO)) {
|
2009-04-02 11:08:34 -04:00
|
|
|
messageType = TrayIcon.MessageType.INFO;
|
2009-06-22 09:37:47 -04:00
|
|
|
} else if (level.equals(Level.WARN)) {
|
2009-04-02 11:08:34 -04:00
|
|
|
messageType = TrayIcon.MessageType.WARNING;
|
2009-06-22 09:37:47 -04:00
|
|
|
} else if (level.equals(Level.ERROR)) {
|
2009-04-02 11:08:34 -04:00
|
|
|
messageType = TrayIcon.MessageType.ERROR;
|
|
|
|
}
|
|
|
|
if (messageType != null) {
|
2009-04-23 10:54:06 -04:00
|
|
|
trayIcon.displayMessage(BundleMessage.format("UI_DAVMAIL_GATEWAY"), message, messageType);
|
2009-04-02 11:08:34 -04:00
|
|
|
}
|
2009-04-23 10:54:06 -04:00
|
|
|
trayIcon.setToolTip(BundleMessage.format("UI_DAVMAIL_GATEWAY") + '\n' + message);
|
2007-10-09 05:48:59 -04:00
|
|
|
}
|
|
|
|
}
|
2009-04-02 11:08:34 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void about() {
|
|
|
|
SwingUtilities.invokeLater(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
aboutFrame.update();
|
|
|
|
aboutFrame.setVisible(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void preferences() {
|
|
|
|
SwingUtilities.invokeLater(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
settingsFrame.reload();
|
|
|
|
settingsFrame.setVisible(true);
|
2008-11-24 07:35:58 -05:00
|
|
|
}
|
|
|
|
});
|
2007-10-09 05:48:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void init() {
|
2008-11-07 13:25:49 -05:00
|
|
|
SwingUtilities.invokeLater(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
createAndShowGUI();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2008-11-24 07:35:58 -05:00
|
|
|
|
|
|
|
protected void createAndShowGUI() {
|
2007-10-09 05:48:59 -04:00
|
|
|
// set native look and feel
|
|
|
|
try {
|
|
|
|
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
|
|
|
} catch (Exception e) {
|
2009-04-23 10:54:06 -04:00
|
|
|
DavGatewayTray.warn(new BundleMessage("LOG_UNABLE_TO_SET_SYSTEM_LOOK_AND_FEEL"), e);
|
2007-10-09 05:48:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// get the SystemTray instance
|
|
|
|
SystemTray tray = SystemTray.getSystemTray();
|
2009-04-07 08:39:09 -04:00
|
|
|
image = DavGatewayTray.loadImage(TRAY_PNG);
|
|
|
|
image2 = DavGatewayTray.loadImage(TRAY2_PNG);
|
|
|
|
inactiveImage = DavGatewayTray.loadImage(TRAYINACTIVE_PNG);
|
2008-11-03 05:56:57 -05:00
|
|
|
|
2007-10-09 05:48:59 -04:00
|
|
|
// create a popup menu
|
|
|
|
PopupMenu popup = new PopupMenu();
|
2008-10-19 17:00:22 -04:00
|
|
|
|
2009-04-02 11:08:34 -04:00
|
|
|
aboutFrame = new AboutFrame();
|
2008-10-19 17:00:22 -04:00
|
|
|
// create an action settingsListener to listen for settings action executed on the tray icon
|
|
|
|
ActionListener aboutListener = new ActionListener() {
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
2009-04-02 11:08:34 -04:00
|
|
|
about();
|
2008-10-19 17:00:22 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
// create menu item for the default action
|
2009-04-23 10:54:06 -04:00
|
|
|
MenuItem aboutItem = new MenuItem(BundleMessage.format("UI_ABOUT"));
|
2008-10-19 17:00:22 -04:00
|
|
|
aboutItem.addActionListener(aboutListener);
|
|
|
|
popup.add(aboutItem);
|
|
|
|
|
2009-04-02 11:08:34 -04:00
|
|
|
settingsFrame = new SettingsFrame();
|
2007-10-09 05:48:59 -04:00
|
|
|
// create an action settingsListener to listen for settings action executed on the tray icon
|
|
|
|
ActionListener settingsListener = new ActionListener() {
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
2009-04-02 11:08:34 -04:00
|
|
|
preferences();
|
2007-10-09 05:48:59 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
// create menu item for the default action
|
2009-04-23 10:54:06 -04:00
|
|
|
MenuItem defaultItem = new MenuItem(BundleMessage.format("UI_SETTINGS"));
|
2007-10-09 05:48:59 -04:00
|
|
|
defaultItem.addActionListener(settingsListener);
|
|
|
|
popup.add(defaultItem);
|
|
|
|
|
2009-04-23 10:54:06 -04:00
|
|
|
MenuItem logItem = new MenuItem(BundleMessage.format("UI_SHOW_LOGS"));
|
2007-10-09 05:48:59 -04:00
|
|
|
logItem.addActionListener(new ActionListener() {
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
Logger rootLogger = Logger.getRootLogger();
|
|
|
|
LF5Appender lf5Appender = (LF5Appender) rootLogger.getAppender("LF5Appender");
|
|
|
|
if (lf5Appender == null) {
|
2009-05-17 18:24:43 -04:00
|
|
|
logBrokerMonitor = new LogBrokerMonitor(LogLevel.getLog4JLevels()) {
|
|
|
|
@Override
|
|
|
|
protected void closeAfterConfirm() {
|
2007-10-09 05:48:59 -04:00
|
|
|
hide();
|
|
|
|
}
|
2009-05-17 18:24:43 -04:00
|
|
|
};
|
|
|
|
lf5Appender = new LF5Appender(logBrokerMonitor);
|
2007-10-09 05:48:59 -04:00
|
|
|
lf5Appender.setName("LF5Appender");
|
|
|
|
rootLogger.addAppender(lf5Appender);
|
|
|
|
}
|
|
|
|
lf5Appender.getLogBrokerMonitor().show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
popup.add(logItem);
|
|
|
|
|
|
|
|
// create an action exitListener to listen for exit action executed on the tray icon
|
|
|
|
ActionListener exitListener = new ActionListener() {
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
2009-06-16 04:55:39 -04:00
|
|
|
try {
|
|
|
|
DavGateway.stop();
|
|
|
|
SystemTray.getSystemTray().remove(trayIcon);
|
|
|
|
|
|
|
|
// dispose frames
|
|
|
|
settingsFrame.dispose();
|
|
|
|
aboutFrame.dispose();
|
|
|
|
if (logBrokerMonitor != null) {
|
|
|
|
logBrokerMonitor.dispose();
|
|
|
|
}
|
|
|
|
} catch (Exception exc) {
|
|
|
|
DavGatewayTray.error(exc);
|
2009-05-17 18:24:43 -04:00
|
|
|
}
|
2009-05-20 06:36:08 -04:00
|
|
|
// make sure we do exit
|
|
|
|
System.exit(0);
|
2007-10-09 05:48:59 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
// create menu item for the exit action
|
2009-04-23 10:54:06 -04:00
|
|
|
MenuItem exitItem = new MenuItem(BundleMessage.format("UI_EXIT"));
|
2007-10-09 05:48:59 -04:00
|
|
|
exitItem.addActionListener(exitListener);
|
|
|
|
popup.add(exitItem);
|
|
|
|
|
|
|
|
/// ... add other items
|
|
|
|
// construct a TrayIcon
|
2009-04-23 10:54:06 -04:00
|
|
|
trayIcon = new TrayIcon(image, BundleMessage.format("UI_DAVMAIL_GATEWAY"), popup);
|
2007-10-09 05:48:59 -04:00
|
|
|
// set the TrayIcon properties
|
|
|
|
trayIcon.addActionListener(settingsListener);
|
|
|
|
// ...
|
|
|
|
// add the tray image
|
|
|
|
try {
|
|
|
|
tray.add(trayIcon);
|
|
|
|
} catch (AWTException e) {
|
2009-04-23 10:54:06 -04:00
|
|
|
DavGatewayTray.warn(new BundleMessage("LOG_UNABLE_TO_CREATE_TRAY"), e);
|
2007-10-09 05:48:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// display settings frame on first start
|
|
|
|
if (Settings.isFirstStart()) {
|
|
|
|
settingsFrame.setVisible(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|