2009-07-21 04:39:18 -04:00
|
|
|
/*
|
|
|
|
* DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
|
|
|
|
* Copyright (C) 2009 Mickael Guessant
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
2009-04-03 03:38:31 -04:00
|
|
|
package davmail.ui.tray;
|
2009-02-11 10:33:58 -05:00
|
|
|
|
2009-04-23 10:54:06 -04:00
|
|
|
import davmail.BundleMessage;
|
2009-05-17 18:24:43 -04:00
|
|
|
import davmail.DavGateway;
|
2011-06-08 15:58:29 -04:00
|
|
|
import davmail.Settings;
|
2009-02-11 14:51:31 -05:00
|
|
|
import davmail.ui.AboutFrame;
|
|
|
|
import davmail.ui.SettingsFrame;
|
2009-06-22 09:37:47 -04:00
|
|
|
import org.apache.log4j.Level;
|
2011-06-08 15:58:29 -04:00
|
|
|
import org.apache.log4j.Logger;
|
2009-02-11 10:33:58 -05: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;
|
2009-02-11 14:51:31 -05:00
|
|
|
import java.awt.event.ActionListener;
|
2009-02-11 10:33:58 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Failover GUI for Java 1.5 without SWT
|
|
|
|
*/
|
|
|
|
public class FrameGatewayTray implements DavGatewayTrayInterface {
|
|
|
|
protected FrameGatewayTray() {
|
|
|
|
}
|
|
|
|
|
2009-09-18 08:17:57 -04:00
|
|
|
static JFrame mainFrame;
|
|
|
|
static AboutFrame aboutFrame;
|
|
|
|
static SettingsFrame settingsFrame;
|
|
|
|
LogBrokerMonitor logBrokerMonitor;
|
2009-04-16 18:20:30 -04:00
|
|
|
private static JEditorPane errorArea;
|
|
|
|
private static JLabel errorLabel;
|
|
|
|
private static JEditorPane messageArea;
|
|
|
|
private static Image image;
|
|
|
|
private static Image image2;
|
|
|
|
private static Image inactiveImage;
|
2009-02-11 10:33:58 -05:00
|
|
|
private boolean isActive = true;
|
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Return AWT Image icon for frame title.
|
|
|
|
*
|
|
|
|
* @return frame icon
|
|
|
|
*/
|
2009-02-11 10:33:58 -05:00
|
|
|
public Image getFrameIcon() {
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Switch tray icon between active and standby icon.
|
|
|
|
*/
|
2009-02-11 10:33:58 -05:00
|
|
|
public void switchIcon() {
|
|
|
|
isActive = true;
|
|
|
|
SwingUtilities.invokeLater(new Runnable() {
|
|
|
|
public void run() {
|
2009-05-07 04:45:45 -04:00
|
|
|
if (mainFrame.getIconImage().equals(image)) {
|
2009-02-11 10:33:58 -05:00
|
|
|
mainFrame.setIconImage(image2);
|
|
|
|
} else {
|
|
|
|
mainFrame.setIconImage(image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Set tray icon to inactive (network down)
|
|
|
|
*/
|
2009-02-11 10:33:58 -05:00
|
|
|
public void resetIcon() {
|
|
|
|
SwingUtilities.invokeLater(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
mainFrame.setIconImage(image);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Set tray icon to inactive (network down)
|
|
|
|
*/
|
2009-02-11 10:33:58 -05:00
|
|
|
public void inactiveIcon() {
|
|
|
|
isActive = false;
|
|
|
|
SwingUtilities.invokeLater(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
mainFrame.setIconImage(inactiveImage);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Check if current tray status is inactive (network down).
|
|
|
|
*
|
|
|
|
* @return true if inactive
|
|
|
|
*/
|
2009-02-11 10:33:58 -05:00
|
|
|
public boolean isActive() {
|
|
|
|
return isActive;
|
|
|
|
}
|
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Log and display balloon message according to log level.
|
|
|
|
*
|
|
|
|
* @param message text message
|
|
|
|
* @param level log level
|
|
|
|
*/
|
2009-06-22 09:37:47 -04:00
|
|
|
public void displayMessage(final String message, final Level level) {
|
2009-02-11 10:33:58 -05:00
|
|
|
SwingUtilities.invokeLater(new Runnable() {
|
|
|
|
public void run() {
|
2010-07-19 05:02:14 -04:00
|
|
|
if (errorArea != null && messageArea != null) {
|
2009-06-22 09:37:47 -04:00
|
|
|
if (level.equals(Level.INFO)) {
|
2009-02-11 14:51:31 -05:00
|
|
|
errorLabel.setIcon(UIManager.getIcon("OptionPane.informationIcon"));
|
|
|
|
errorArea.setText(message);
|
2009-06-22 09:37:47 -04:00
|
|
|
} else if (level.equals(Level.WARN)) {
|
2009-02-11 14:51:31 -05:00
|
|
|
errorLabel.setIcon(UIManager.getIcon("OptionPane.warningIcon"));
|
|
|
|
errorArea.setText(message);
|
2009-06-22 09:37:47 -04:00
|
|
|
} else if (level.equals(Level.ERROR)) {
|
2009-02-11 14:51:31 -05:00
|
|
|
errorLabel.setIcon(UIManager.getIcon("OptionPane.errorIcon"));
|
2009-02-11 10:33:58 -05:00
|
|
|
errorArea.setText(message);
|
|
|
|
}
|
2009-05-10 18:20:17 -04:00
|
|
|
messageArea.setText(message);
|
2009-02-11 10:33:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Open about window
|
|
|
|
*/
|
2009-02-11 17:38:46 -05:00
|
|
|
public void about() {
|
2009-02-11 10:33:58 -05:00
|
|
|
SwingUtilities.invokeLater(new Runnable() {
|
|
|
|
public void run() {
|
2009-02-11 17:38:46 -05:00
|
|
|
aboutFrame.update();
|
|
|
|
aboutFrame.setVisible(true);
|
2011-10-02 17:49:39 -04:00
|
|
|
aboutFrame.toFront();
|
|
|
|
aboutFrame.requestFocus();
|
2009-02-11 10:33:58 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Open settings window
|
|
|
|
*/
|
2009-02-11 17:38:46 -05:00
|
|
|
public void preferences() {
|
|
|
|
SwingUtilities.invokeLater(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
settingsFrame.reload();
|
|
|
|
settingsFrame.setVisible(true);
|
2011-10-02 17:49:39 -04:00
|
|
|
settingsFrame.toFront();
|
2012-06-01 06:39:14 -04:00
|
|
|
settingsFrame.repaint();
|
2011-10-02 17:49:39 -04:00
|
|
|
settingsFrame.requestFocus();
|
2009-02-11 17:38:46 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2009-02-11 10:33:58 -05:00
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Open logging window.
|
|
|
|
*/
|
2009-02-11 17:38:46 -05:00
|
|
|
public void showLogs() {
|
|
|
|
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()) {
|
2009-05-10 18:20:17 -04:00
|
|
|
@Override
|
|
|
|
protected void closeAfterConfirm() {
|
2009-02-11 17:38:46 -05:00
|
|
|
hide();
|
|
|
|
}
|
2009-05-17 18:24:43 -04:00
|
|
|
};
|
|
|
|
lf5Appender = new LF5Appender(logBrokerMonitor);
|
2009-02-11 17:38:46 -05:00
|
|
|
lf5Appender.setName("LF5Appender");
|
|
|
|
rootLogger.addAppender(lf5Appender);
|
2009-02-11 14:51:31 -05:00
|
|
|
}
|
2009-02-11 17:38:46 -05:00
|
|
|
lf5Appender.getLogBrokerMonitor().show();
|
|
|
|
}
|
2009-02-11 14:51:31 -05:00
|
|
|
|
2009-09-04 04:46:39 -04:00
|
|
|
/**
|
|
|
|
* Create tray icon and register frame listeners.
|
|
|
|
*/
|
2009-02-11 17:38:46 -05:00
|
|
|
public void init() {
|
|
|
|
SwingUtilities.invokeLater(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
createAndShowGUI();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2009-02-11 10:33:58 -05:00
|
|
|
|
2009-02-11 17:38:46 -05:00
|
|
|
protected void buildMenu() {
|
2009-02-11 10:33:58 -05:00
|
|
|
// create a popup menu
|
2009-05-10 18:20:17 -04:00
|
|
|
JMenu menu = new JMenu(BundleMessage.format("UI_DAVMAIL_GATEWAY"));
|
2009-02-11 10:33:58 -05:00
|
|
|
JMenuBar menuBar = new JMenuBar();
|
|
|
|
menuBar.add(menu);
|
|
|
|
mainFrame.setJMenuBar(menuBar);
|
|
|
|
|
|
|
|
// create an action settingsListener to listen for settings action executed on the tray icon
|
|
|
|
ActionListener aboutListener = new ActionListener() {
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
2009-02-11 17:38:46 -05:00
|
|
|
about();
|
2009-02-11 10:33:58 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
// create menu item for the default action
|
2009-05-10 18:20:17 -04:00
|
|
|
JMenuItem aboutItem = new JMenuItem(BundleMessage.format("UI_ABOUT"));
|
2009-02-11 10:33:58 -05:00
|
|
|
aboutItem.addActionListener(aboutListener);
|
|
|
|
menu.add(aboutItem);
|
|
|
|
|
2009-02-11 17:38:46 -05:00
|
|
|
|
2009-02-11 10:33:58 -05: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-02-11 17:38:46 -05:00
|
|
|
preferences();
|
2009-02-11 10:33:58 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
// create menu item for the default action
|
2009-05-10 18:20:17 -04:00
|
|
|
JMenuItem defaultItem = new JMenuItem(BundleMessage.format("UI_SETTINGS"));
|
2009-02-11 10:33:58 -05:00
|
|
|
defaultItem.addActionListener(settingsListener);
|
|
|
|
menu.add(defaultItem);
|
|
|
|
|
2009-05-10 18:20:17 -04:00
|
|
|
JMenuItem logItem = new JMenuItem(BundleMessage.format("UI_SHOW_LOGS"));
|
2009-02-11 10:33:58 -05:00
|
|
|
logItem.addActionListener(new ActionListener() {
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
2009-02-11 17:38:46 -05:00
|
|
|
showLogs();
|
2009-02-11 10:33:58 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
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) {
|
2009-06-16 04:55:39 -04:00
|
|
|
try {
|
2009-09-04 04:46:39 -04:00
|
|
|
DavGateway.stop();
|
|
|
|
// dispose frames
|
|
|
|
settingsFrame.dispose();
|
|
|
|
aboutFrame.dispose();
|
|
|
|
if (logBrokerMonitor != null) {
|
|
|
|
logBrokerMonitor.dispose();
|
|
|
|
}
|
2009-06-16 04:55:39 -04:00
|
|
|
} catch (Exception exc) {
|
|
|
|
DavGatewayTray.error(exc);
|
|
|
|
}
|
2009-05-20 06:36:08 -04:00
|
|
|
// make sure we do exit
|
|
|
|
System.exit(0);
|
2009-02-11 10:33:58 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
// create menu item for the exit action
|
2009-05-10 18:20:17 -04:00
|
|
|
JMenuItem exitItem = new JMenuItem(BundleMessage.format("UI_EXIT"));
|
2009-02-11 10:33:58 -05:00
|
|
|
exitItem.addActionListener(exitListener);
|
|
|
|
menu.add(exitItem);
|
2009-02-11 17:38:46 -05:00
|
|
|
}
|
|
|
|
|
2011-05-16 04:50:33 -04:00
|
|
|
protected void createAndShowGUI() {
|
2011-05-16 16:37:36 -04:00
|
|
|
System.setProperty("swing.defaultlaf", UIManager.getSystemLookAndFeelClassName());
|
2009-02-11 17:38:46 -05:00
|
|
|
|
|
|
|
image = DavGatewayTray.loadImage("tray.png");
|
2009-11-24 17:38:20 -05:00
|
|
|
image2 = DavGatewayTray.loadImage(AwtGatewayTray.TRAY_ACTIVE_PNG);
|
|
|
|
inactiveImage = DavGatewayTray.loadImage(AwtGatewayTray.TRAY_INACTIVE_PNG);
|
2009-02-11 17:38:46 -05:00
|
|
|
|
|
|
|
mainFrame = new JFrame();
|
2012-07-06 17:59:01 -04:00
|
|
|
mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
2009-05-10 18:20:17 -04:00
|
|
|
mainFrame.setTitle(BundleMessage.format("UI_DAVMAIL_GATEWAY"));
|
2009-02-11 17:38:46 -05:00
|
|
|
mainFrame.setIconImage(image);
|
|
|
|
|
|
|
|
JPanel errorPanel = new JPanel();
|
2009-05-10 18:20:17 -04:00
|
|
|
errorPanel.setBorder(BorderFactory.createTitledBorder(BundleMessage.format("UI_LAST_MESSAGE")));
|
2009-02-11 17:38:46 -05:00
|
|
|
errorPanel.setLayout(new BoxLayout(errorPanel, BoxLayout.X_AXIS));
|
|
|
|
errorArea = new JTextPane();
|
|
|
|
errorArea.setEditable(false);
|
|
|
|
errorArea.setBackground(mainFrame.getBackground());
|
|
|
|
errorLabel = new JLabel();
|
|
|
|
errorPanel.add(errorLabel);
|
|
|
|
errorPanel.add(errorArea);
|
|
|
|
|
|
|
|
JPanel messagePanel = new JPanel();
|
2009-05-10 18:20:17 -04:00
|
|
|
messagePanel.setBorder(BorderFactory.createTitledBorder(BundleMessage.format("UI_LAST_LOG")));
|
2009-02-11 17:38:46 -05:00
|
|
|
messagePanel.setLayout(new BoxLayout(messagePanel, BoxLayout.X_AXIS));
|
|
|
|
|
|
|
|
messageArea = new JTextPane();
|
2009-05-10 18:20:17 -04:00
|
|
|
messageArea.setText(BundleMessage.format("LOG_STARTING_DAVMAIL"));
|
2009-02-11 17:38:46 -05:00
|
|
|
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);
|
|
|
|
|
|
|
|
aboutFrame = new AboutFrame();
|
|
|
|
settingsFrame = new SettingsFrame();
|
|
|
|
buildMenu();
|
2009-02-11 10:33:58 -05:00
|
|
|
|
2009-02-11 14:51:31 -05:00
|
|
|
mainFrame.setMinimumSize(new Dimension(400, 180));
|
2009-02-11 10:33:58 -05:00
|
|
|
mainFrame.pack();
|
2009-02-11 14:51:31 -05:00
|
|
|
// workaround MacOSX
|
|
|
|
if (mainFrame.getSize().width < 400 || mainFrame.getSize().height < 180) {
|
2009-05-10 18:20:17 -04:00
|
|
|
mainFrame.setSize(Math.max(mainFrame.getSize().width, 400),
|
|
|
|
Math.max(mainFrame.getSize().height, 180));
|
2009-02-11 14:51:31 -05:00
|
|
|
}
|
2009-02-11 10:33:58 -05:00
|
|
|
// 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);
|
2009-02-11 14:51:31 -05:00
|
|
|
|
|
|
|
// display settings frame on first start
|
|
|
|
if (Settings.isFirstStart()) {
|
|
|
|
settingsFrame.setVisible(true);
|
2011-10-02 17:49:39 -04:00
|
|
|
settingsFrame.toFront();
|
2012-06-01 06:39:14 -04:00
|
|
|
settingsFrame.repaint();
|
2011-10-02 17:49:39 -04:00
|
|
|
settingsFrame.requestFocus();
|
2009-02-11 14:51:31 -05:00
|
|
|
}
|
2009-02-11 10:33:58 -05:00
|
|
|
}
|
|
|
|
}
|