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;
|
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;
|
|
|
|
import org.apache.log4j.Priority;
|
|
|
|
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;
|
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() {
|
|
|
|
if (trayIcon.getImage() == image) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void displayMessage(final String message, final Priority priority) {
|
|
|
|
SwingUtilities.invokeLater(new Runnable() {
|
|
|
|
public void run() {
|
2009-04-02 11:08:34 -04:00
|
|
|
if (trayIcon != null) {
|
|
|
|
TrayIcon.MessageType messageType = null;
|
|
|
|
if (priority == Priority.INFO) {
|
|
|
|
messageType = TrayIcon.MessageType.INFO;
|
|
|
|
} else if (priority == Priority.WARN) {
|
|
|
|
messageType = TrayIcon.MessageType.WARNING;
|
|
|
|
} else if (priority == Priority.ERROR) {
|
|
|
|
messageType = TrayIcon.MessageType.ERROR;
|
|
|
|
}
|
|
|
|
if (messageType != null) {
|
|
|
|
trayIcon.displayMessage("DavMail gateway", message, messageType);
|
|
|
|
}
|
|
|
|
trayIcon.setToolTip("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) {
|
2008-01-31 18:40:26 -05:00
|
|
|
DavGatewayTray.warn("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
|
|
|
|
MenuItem aboutItem = new MenuItem("About...");
|
|
|
|
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
|
|
|
|
MenuItem defaultItem = new MenuItem("Settings...");
|
|
|
|
defaultItem.addActionListener(settingsListener);
|
|
|
|
popup.add(defaultItem);
|
|
|
|
|
|
|
|
MenuItem logItem = new MenuItem("Show logs...");
|
|
|
|
logItem.addActionListener(new ActionListener() {
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
Logger rootLogger = Logger.getRootLogger();
|
|
|
|
LF5Appender lf5Appender = (LF5Appender) rootLogger.getAppender("LF5Appender");
|
|
|
|
if (lf5Appender == null) {
|
|
|
|
lf5Appender = new LF5Appender(new LogBrokerMonitor(LogLevel.getLog4JLevels()) {
|
2009-04-16 17:52:17 -04:00
|
|
|
@Override protected void closeAfterConfirm() {
|
2007-10-09 05:48:59 -04:00
|
|
|
hide();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
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) {
|
|
|
|
SystemTray.getSystemTray().remove(trayIcon);
|
|
|
|
//noinspection CallToSystemExit
|
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// create menu item for the exit action
|
|
|
|
MenuItem exitItem = new MenuItem("Exit");
|
|
|
|
exitItem.addActionListener(exitListener);
|
|
|
|
popup.add(exitItem);
|
|
|
|
|
|
|
|
/// ... add other items
|
|
|
|
// construct a TrayIcon
|
|
|
|
trayIcon = new TrayIcon(image, "DavMail Gateway", popup);
|
|
|
|
// set the TrayIcon properties
|
|
|
|
trayIcon.addActionListener(settingsListener);
|
|
|
|
// ...
|
|
|
|
// add the tray image
|
|
|
|
try {
|
|
|
|
tray.add(trayIcon);
|
|
|
|
} catch (AWTException e) {
|
2008-01-31 18:40:26 -05:00
|
|
|
DavGatewayTray.warn("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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|