New failover GUI for Java 1.5 without SWT (e.g. MacOSX support)

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@346 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2009-02-11 15:33:58 +00:00
parent 5c47845a81
commit 1ebf517f04
3 changed files with 236 additions and 2 deletions

View File

@ -107,7 +107,7 @@ public class AwtGatewayTray implements DavGatewayTrayInterface {
// get the SystemTray instance
SystemTray tray = SystemTray.getSystemTray();
image = DavGatewayTray.loadImage("tray.png");
image2 = DavGatewayTray.loadImage("tray.png");
image2 = DavGatewayTray.loadImage("tray2.png");
inactiveImage = DavGatewayTray.loadImage("trayinactive.png");
// create a popup menu

View File

@ -140,7 +140,8 @@ public class DavGatewayTray {
}
}
if (davGatewayTray == null) {
DavGatewayTray.warn("No system tray support found (tried SWT and native java)");
davGatewayTray = new FrameGatewayTray();
davGatewayTray.init();
}
}
}

View File

@ -0,0 +1,233 @@
package davmail.tray;
import org.apache.log4j.Priority;
import org.apache.log4j.Logger;
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.ActionListener;
import java.awt.event.ActionEvent;
import davmail.ui.AboutFrame;
import davmail.ui.SettingsFrame;
import davmail.Settings;
/**
* Failover GUI for Java 1.5 without SWT
*/
public class FrameGatewayTray implements DavGatewayTrayInterface {
protected FrameGatewayTray() {
}
private static JFrame mainFrame = null;
private static JEditorPane errorArea = null;
private static JLabel errorLabel = null;
private static JEditorPane messageArea = null;
private static Image image = null;
private static Image image2 = null;
private static Image inactiveImage = null;
private boolean isActive = true;
public Image getFrameIcon() {
return image;
}
public void switchIcon() {
isActive = true;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (mainFrame.getIconImage() == image) {
mainFrame.setIconImage(image2);
} else {
mainFrame.setIconImage(image);
}
}
});
}
public void resetIcon() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
mainFrame.setIconImage(image);
}
});
}
public void inactiveIcon() {
isActive = false;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
mainFrame.setIconImage(inactiveImage);
}
});
}
public boolean isActive() {
return isActive;
}
public void displayMessage(final String message, final Priority priority) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (mainFrame != 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) {
switch (messageType) {
case ERROR:
errorLabel.setIcon(UIManager.getIcon("OptionPane.errorIcon"));
break;
case INFO:
errorLabel.setIcon(UIManager.getIcon("OptionPane.informationIcon"));
break;
case WARNING:
errorLabel.setIcon(UIManager.getIcon("OptionPane.warningIcon"));
break;
}
errorArea.setText(message);
} else {
messageArea.setText(message);
}
}
}
});
}
public void init() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
protected void createAndShowGUI() {
// set native look and feel
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
DavGatewayTray.warn("Unable to set system look and feel", e);
}
image = DavGatewayTray.loadImage("tray.png");
image2 = DavGatewayTray.loadImage("tray2.png");
inactiveImage = DavGatewayTray.loadImage("trayinactive.png");
mainFrame = new JFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setTitle("DavMail Gateway");
mainFrame.setIconImage(image);
// create a popup menu
JMenu menu = new JMenu("DavMail");
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
mainFrame.setJMenuBar(menuBar);
JPanel errorPanel = new JPanel();
errorPanel.setBorder(BorderFactory.createTitledBorder("Last message"));
errorPanel.setLayout(new BoxLayout(errorPanel, BoxLayout.X_AXIS));
mainFrame.setMinimumSize(new Dimension(400, 180));
errorArea = new JTextPane();
errorArea.setEditable(false);
errorArea.setBackground(mainFrame.getBackground());
errorLabel = new JLabel();
errorPanel.add(errorLabel);
errorPanel.add(errorArea);
JPanel messagePanel = new JPanel();
messagePanel.setBorder(BorderFactory.createTitledBorder("Last log"));
messagePanel.setLayout(new BoxLayout(messagePanel, BoxLayout.X_AXIS));
messageArea = new JTextPane();
messageArea.setText("Starting DavMail Gateway...");
messageArea.setEditable(false);
messageArea.setBackground(mainFrame.getBackground());
messagePanel.add(messageArea);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
mainPanel.add(errorPanel);
mainPanel.add(messagePanel);
mainFrame.add(mainPanel);
final AboutFrame aboutFrame = new AboutFrame();
// create an action settingsListener to listen for settings action executed on the tray icon
ActionListener aboutListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
aboutFrame.update();
aboutFrame.setVisible(true);
}
};
// create menu item for the default action
JMenuItem aboutItem = new JMenuItem("About...");
aboutItem.addActionListener(aboutListener);
menu.add(aboutItem);
final SettingsFrame settingsFrame = new SettingsFrame();
// create an action settingsListener to listen for settings action executed on the tray icon
ActionListener settingsListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
settingsFrame.reload();
settingsFrame.setVisible(true);
}
};
// create menu item for the default action
JMenuItem defaultItem = new JMenuItem("Settings...");
defaultItem.addActionListener(settingsListener);
menu.add(defaultItem);
JMenuItem logItem = new JMenuItem("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()) {
protected void closeAfterConfirm() {
hide();
}
});
lf5Appender.setName("LF5Appender");
rootLogger.addAppender(lf5Appender);
}
lf5Appender.getLogBrokerMonitor().show();
}
});
menu.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) {
//noinspection CallToSystemExit
System.exit(0);
}
};
// create menu item for the exit action
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(exitListener);
menu.add(exitItem);
// display settings frame on first start
if (Settings.isFirstStart()) {
settingsFrame.setVisible(true);
}
mainFrame.pack();
// center frame
mainFrame.setLocation(mainFrame.getToolkit().getScreenSize().width / 2 -
mainFrame.getSize().width / 2,
mainFrame.getToolkit().getScreenSize().height / 2 -
mainFrame.getSize().height / 2);
mainFrame.setVisible(true);
}
}