From 1c33971524f9fa72026cc14253a522228f01627e Mon Sep 17 00:00:00 2001 From: clemenso Date: Fri, 4 Mar 2011 13:58:24 +0000 Subject: TrayIcon new git-svn-id: https://joinup.ec.europa.eu/svn/mocca/trunk@914 8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4 --- .../bku/webstart/gui/BKUControllerInterface.java | 8 + .../at/gv/egiz/bku/webstart/gui/MOCCAIcon.java | 191 +++++++++++++++++++++ .../bku/webstart/gui/PINManagementInvoker.java | 72 -------- .../gv/egiz/bku/webstart/gui/StatusNotifier.java | 54 ++++++ 4 files changed, 253 insertions(+), 72 deletions(-) create mode 100644 BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/gui/MOCCAIcon.java delete mode 100644 BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/gui/PINManagementInvoker.java create mode 100644 BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/gui/StatusNotifier.java (limited to 'BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/gui') diff --git a/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/gui/BKUControllerInterface.java b/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/gui/BKUControllerInterface.java index 886b55f7..6355de92 100644 --- a/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/gui/BKUControllerInterface.java +++ b/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/gui/BKUControllerInterface.java @@ -16,8 +16,16 @@ */ package at.gv.egiz.bku.webstart.gui; +import java.util.Locale; + public interface BKUControllerInterface { public void shutDown(); + public String getVersion(); + + public void showHelp(Locale locale); + + public void pinManagement(Locale locale); + } diff --git a/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/gui/MOCCAIcon.java b/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/gui/MOCCAIcon.java new file mode 100644 index 00000000..5c44dac3 --- /dev/null +++ b/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/gui/MOCCAIcon.java @@ -0,0 +1,191 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package at.gv.egiz.bku.webstart.gui; + +import java.awt.AWTException; +import java.awt.Image; +import java.awt.MenuItem; +import java.awt.PopupMenu; +import java.awt.SystemTray; +import java.awt.TrayIcon; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.WindowAdapter; +import java.io.IOException; +import java.text.MessageFormat; +import java.util.Locale; +import java.util.ResourceBundle; +import javax.imageio.ImageIO; +import javax.swing.JFrame; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author clemens + */ +public class MOCCAIcon implements StatusNotifier, ActionListener { + + public static final String LABEL_SHUTDOWN = "tray.label.shutdown"; + public static final String LABEL_PIN = "tray.label.pin"; + public static final String LABEL_HELP = "tray.label.help"; + public static final String LABEL_ABOUT = "tray.label.about"; + public static final String TOOLTIP_DEFAULT = "tray.tooltip.default"; + /** action commands for tray menu */ + public static final String SHUTDOWN_COMMAND = "shutdown"; + public static final String PIN_COMMAND = "pin"; + public static final String ABOUT_COMMAND = "about"; + public static final String HELP_COMMAND = "help"; + private static final Logger log = LoggerFactory.getLogger(MOCCAIcon.class); + protected BKUControllerInterface controller; + protected TrayIcon trayIcon; + protected ResourceBundle messages; + + private AboutDialog aboutDialog; + + public MOCCAIcon(BKUControllerInterface controller) { + this.controller = controller; + messages = ResourceBundle.getBundle(MESSAGES_RESOURCE, Locale.getDefault()); + this.trayIcon = initTrayIcon(); + } + + private TrayIcon initTrayIcon() { + if (SystemTray.isSupported()) { + try { + // get the SystemTray instance + SystemTray tray = SystemTray.getSystemTray(); + log.debug("TrayIcon size: " + tray.getTrayIconSize()); + + String iconResource; + if (tray.getTrayIconSize().height < 17) { + iconResource = TRAYICON_RESOURCE + "16.png"; + } else if (tray.getTrayIconSize().height < 25) { + iconResource = TRAYICON_RESOURCE + "24.png"; + } else if (tray.getTrayIconSize().height < 33) { + iconResource = TRAYICON_RESOURCE + "32.png"; + } else { + iconResource = TRAYICON_RESOURCE + "48.png"; + } + Image image = ImageIO.read(getClass().getResourceAsStream(iconResource)); + + PopupMenu menu = new PopupMenu(); + + MenuItem helpItem = new MenuItem(messages.getString(LABEL_HELP)); + helpItem.addActionListener(this); + helpItem.setActionCommand(HELP_COMMAND); + menu.add(helpItem); + + MenuItem pinItem = new MenuItem(messages.getString(LABEL_PIN)); + pinItem.addActionListener(this); + pinItem.setActionCommand(PIN_COMMAND); + menu.add(pinItem); + + MenuItem shutdownItem = new MenuItem(messages.getString(LABEL_SHUTDOWN)); + shutdownItem.addActionListener(this); + shutdownItem.setActionCommand(SHUTDOWN_COMMAND); + menu.add(shutdownItem); + + menu.addSeparator(); + + MenuItem aboutItem = new MenuItem(messages.getString(LABEL_ABOUT)); + aboutItem.setActionCommand(ABOUT_COMMAND); + aboutItem.addActionListener(this); + menu.add(aboutItem); + + TrayIcon ti = new TrayIcon(image, messages.getString(TOOLTIP_DEFAULT), menu); + ti.setImageAutoSize(true); + ti.addActionListener(this); + tray.add(ti); + return ti; + } catch (AWTException ex) { + log.error("Failed to init tray icon", ex); + } catch (IOException ex) { + log.error("Failed to load tray icon image", ex); + } + } else { + log.error("No system tray support"); + } + return null; + } + + @Override + public void error(String msgKey) { + if (trayIcon != null) { + trayIcon.displayMessage( + messages.getString(CAPTION_ERROR), + messages.getString(msgKey), + TrayIcon.MessageType.ERROR); + } else { + log.error(messages.getString(msgKey)); + } + } + + @Override + public void error(String msgPatternKey, Object... argument) { + if (trayIcon != null) { + trayIcon.displayMessage( + messages.getString(CAPTION_ERROR), + MessageFormat.format(messages.getString(msgPatternKey), argument), + TrayIcon.MessageType.ERROR); + } else { + log.error(MessageFormat.format(messages.getString(msgPatternKey), argument)); + } + } + + @Override + public void info(String msgKey) { + if (trayIcon != null) { + trayIcon.displayMessage( + messages.getString(CAPTION_DEFAULT), + messages.getString(msgKey), + TrayIcon.MessageType.INFO); + } else { + log.info(messages.getString(msgKey)); + } + } + + @Override + public Locale getLocale() { + return messages.getLocale(); + } + + /** + * Listen for TrayMenu actions (display error messages on trayIcon) + * @param e + */ + @Override + public void actionPerformed(ActionEvent e) { + if (SHUTDOWN_COMMAND.equals(e.getActionCommand())) { + log.debug("shutdown requested via tray menu"); + controller.shutDown(); + } else if (ABOUT_COMMAND.equals(e.getActionCommand())) { + log.debug("about dialog requested via tray menu"); + + if (aboutDialog == null) { + aboutDialog = new AboutDialog(new JFrame(), true, controller.getVersion()); + aboutDialog.addWindowListener(new WindowAdapter() { + + @Override + public void windowClosing(java.awt.event.WindowEvent e) { + aboutDialog.setVisible(false); + } + }); + } + aboutDialog.setLocationByPlatform(true); + aboutDialog.setVisible(true); + + } else if (PIN_COMMAND.equals(e.getActionCommand())) { + log.debug("pin management dialog requested via tray menu"); + controller.pinManagement(messages.getLocale()); + + } else if (HELP_COMMAND.equals(e.getActionCommand())) { + log.debug("help page requested via tray menu"); + controller.showHelp(messages.getLocale()); + + } else { + log.error("unknown tray menu command: " + e.getActionCommand()); + } + } +} diff --git a/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/gui/PINManagementInvoker.java b/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/gui/PINManagementInvoker.java deleted file mode 100644 index 1f14d751..00000000 --- a/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/gui/PINManagementInvoker.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2008 Federal Chancellery Austria and - * Graz University of Technology - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package at.gv.egiz.bku.webstart.gui; - -import at.gv.egiz.bku.webstart.Launcher; -import java.awt.TrayIcon; -import java.io.IOException; -import java.net.HttpURLConnection; -import java.util.ResourceBundle; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * GUI is painted using SwingUtilities.invokeLater, but TrayIcon ActionListener Thread (== webstart thread) joined Jetty Thread - * - * @author Clemens Orthacker - */ -public class PINManagementInvoker implements Runnable { - - private static final Logger log = LoggerFactory.getLogger(PINManagementInvoker.class); - - TrayIcon trayIcon; - ResourceBundle messages; - - public PINManagementInvoker(TrayIcon trayIcon, ResourceBundle messages) { - this.trayIcon = trayIcon; - this.messages = messages; - } - - @Override - public void run() { - HttpURLConnection connection = null; - try { - log.debug("Connecting to: " + Launcher.PIN_MANAGEMENT_URL); - - connection = (HttpURLConnection) Launcher.PIN_MANAGEMENT_URL.openConnection(); - - connection.setRequestMethod("GET"); - connection.setReadTimeout(0); - connection.connect(); - - if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { - log.debug("pin management dialog returned"); - } else { - log.error("unexpected response from pin management: " + connection.getResponseMessage()); - } - } catch (IOException ex) { - log.error("Failed to connect to PIN Management", ex); - trayIcon.displayMessage(messages.getString(Launcher.CAPTION_ERROR), - messages.getString(Launcher.ERROR_PIN), TrayIcon.MessageType.ERROR); - } finally { - if (connection != null) { - connection.disconnect(); - } - } - } -} diff --git a/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/gui/StatusNotifier.java b/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/gui/StatusNotifier.java new file mode 100644 index 00000000..1259ed36 --- /dev/null +++ b/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/gui/StatusNotifier.java @@ -0,0 +1,54 @@ +/* + * Copyright 2008 Federal Chancellery Austria and + * Graz University of Technology + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package at.gv.egiz.bku.webstart.gui; + +import java.util.Locale; + +/** + * + * @author clemens + */ +public interface StatusNotifier { + + /** no leading slash for messages, but for image */ + public static final String MESSAGES_RESOURCE = "at/gv/egiz/bku/webstart/messages"; + public static final String TRAYICON_RESOURCE = "/at/gv/egiz/bku/webstart/chip"; + /** resource bundle messages */ + public static final String CAPTION_DEFAULT = "tray.caption.default"; + public static final String CAPTION_ERROR = "tray.caption.error"; + public static final String MESSAGE_START = "tray.message.start"; + public static final String MESSAGE_START_OFFLINE = "tray.message.start.offline"; + public static final String MESSAGE_CONFIG = "tray.message.config"; + public static final String MESSAGE_CERTS = "tray.message.certs"; + public static final String MESSAGE_FINISHED = "tray.message.finished"; + public static final String MESSAGE_SHUTDOWN = "tray.message.shutdown"; + public static final String ERROR_START = "tray.error.start"; + public static final String ERROR_BIND = "tray.error.bind"; + public static final String ERROR_CONFIG = "tray.error.config"; + public static final String ERROR_PIN = "tray.error.pin.connect"; + public static final String ERROR_OPEN_URL = "tray.error.open.url"; + + + public void error(String msgKey); + + public void error(String msgPatternKey, Object... argument); + + public void info(String msgKey); + + public Locale getLocale(); +} -- cgit v1.2.3