385N/A/*
385N/A * CDDL HEADER START
385N/A *
385N/A * The contents of this file are subject to the terms of the
385N/A * Common Development and Distribution License (the "License").
385N/A * You may not use this file except in compliance with the License.
385N/A *
385N/A * See LICENSE.txt included in this distribution for the specific
385N/A * language governing permissions and limitations under the License.
385N/A *
385N/A * When distributing Covered Code, include this CDDL HEADER in each
385N/A * file and include the License file at LICENSE.txt.
385N/A * If applicable, add the following below this CDDL HEADER, with the
385N/A * fields enclosed by brackets "[]" replaced with your own identifying
385N/A * information: Portions Copyright [yyyy] [name of copyright owner]
385N/A *
385N/A * CDDL HEADER END
385N/A */
407N/A
385N/Apackage org.opensolaris.opengrok.management.client;
385N/A
385N/Aimport java.awt.AWTException;
385N/Aimport java.awt.Image;
385N/Aimport java.awt.MenuItem;
385N/Aimport java.awt.PopupMenu;
385N/Aimport java.awt.SystemTray;
385N/Aimport java.awt.Toolkit;
385N/Aimport java.awt.TrayIcon;
385N/Aimport java.awt.event.ActionEvent;
385N/Aimport java.awt.event.ActionListener;
385N/Aimport java.awt.event.MouseEvent;
385N/Aimport java.awt.event.MouseListener;
385N/Aimport java.awt.event.WindowEvent;
385N/Aimport java.awt.event.WindowListener;
385N/Aimport java.io.IOException;
385N/Aimport java.util.logging.Level;
385N/Aimport java.util.logging.Logger;
1327N/A
385N/Aimport org.opensolaris.opengrok.OpenGrokLogger;
385N/A
385N/A/**
385N/A *
385N/A * @author Jan S Berg
385N/A */
385N/Apublic class OpenGrokTrayApp {
385N/A
1327N/A private static final Logger log = Logger.getLogger(OpenGrokTrayApp.class.getName());
385N/A private static TrayIcon trayIcon;
385N/A private static String cfgfile;
456N/A private final SettingsPersistence settings;
385N/A public static final String OPENGROKICONURL = "opengrok.gif";
385N/A public static final String INDEXWARNINGICONURL = "opengrok_indexwarning.gif";
385N/A public static final String NOCONNECTIONICONURL = "opengrok_noconnection.gif";
385N/A private AgentConnection agent = null;
385N/A private boolean agentConnected = false;
385N/A private Image opengrokImage;
385N/A private Image indexWarningImage;
385N/A private Image noConnectionImage;
385N/A private Image currentIcon;
385N/A
385N/A /**
385N/A * @param args the command line arguments
385N/A */
434N/A @SuppressWarnings("PMD.SystemPrintln")
385N/A public static void main(String[] args) {
385N/A try {
385N/A for (int i = 0; i < args.length; i++) {
385N/A if (args[i].equals("-config")) {
385N/A if (++i < args.length) {
385N/A cfgfile = args[i];
385N/A } else {
385N/A System.err.println("-config, argument missing: config file");
385N/A System.exit(1);
385N/A }
385N/A }
385N/A }
385N/A OpenGrokTrayApp ogta = new OpenGrokTrayApp(cfgfile);
385N/A ogta.enableSystemTray();
385N/A } catch (IOException ex) {
1327N/A log.severe(ex.getMessage());
1327N/A log.log(Level.FINE, "main", ex);
385N/A }
385N/A }
385N/A
385N/A public OpenGrokTrayApp(String cfgfile) throws IOException {
385N/A settings = new SettingsPersistence(cfgfile);
385N/A loadImages();
385N/A OpenGrokLogger.setupLogger(settings.getProperty(SettingsPersistence.LOGGINGPATHKEY),
385N/A settings.getFileLogLevel(),
385N/A settings.getConsoleLogLevel());
385N/A if (tryAgentConnect()) {
385N/A currentIcon = opengrokImage;
385N/A } else {
385N/A currentIcon = noConnectionImage;
385N/A }
385N/A
385N/A }
385N/A
596N/A private ActionListener getAgentActionListener() {
596N/A
596N/A ActionListener actionListener = new ActionListener() {
596N/A
596N/A public void actionPerformed(ActionEvent e) {
596N/A log.info("Got Event " + e.getActionCommand());
596N/A trayIcon.displayMessage("OpenGrok Indexer", "Files have been deleted/added", TrayIcon.MessageType.INFO);
596N/A trayIcon.setImage(indexWarningImage);
596N/A }
596N/A };
596N/A
596N/A return actionListener;
596N/A }
596N/A
596N/A private ActionListener getExitListener() {
596N/A ActionListener exitListener = new ActionListener() {
596N/A
596N/A @Override
596N/A public void actionPerformed(ActionEvent e) {
596N/A log.info("Exiting...");
596N/A if (agent != null && agent.isConnected()) {
596N/A agent.unregister();
596N/A }
596N/A System.exit(0);
596N/A }
596N/A };
596N/A
596N/A return exitListener;
596N/A }
1190N/A
639N/A private ActionListener getConfigurationListener() {
639N/A ActionListener configListener = new ActionListener() {
1190N/A
639N/A public void actionPerformed(ActionEvent e) {
639N/A log.finer("Config...");
639N/A ConfigurationsFrame sf;
639N/A try {
639N/A sf = new ConfigurationsFrame(agent);
639N/A sf.setVisible(true);
639N/A } catch (IOException ex) {
1327N/A log.warning(ex.getLocalizedMessage());
1327N/A log.log(Level.FINE, "actionPerformed", e);
639N/A }
1190N/A
639N/A log.finer("Done config");
639N/A }
639N/A };
639N/A return configListener;
639N/A }
596N/A
596N/A private MouseListener getMouseListener() {
596N/A MouseListener mouseListener = new MouseListener() {
596N/A
596N/A @Override
596N/A public void mouseClicked(MouseEvent e) {
596N/A log.finest("Tray Icon - Mouse clicked!");
596N/A }
596N/A
596N/A @Override
596N/A public void mouseEntered(MouseEvent e) {
596N/A log.finest("Tray Icon - Mouse entered!");
596N/A }
596N/A
596N/A @Override
596N/A public void mouseExited(MouseEvent e) {
596N/A log.finest("Tray Icon - Mouse exited!");
596N/A }
596N/A
596N/A @Override
596N/A public void mousePressed(MouseEvent e) {
596N/A log.finest("Tray Icon - Mouse pressed!");
596N/A }
596N/A
596N/A @Override
596N/A public void mouseReleased(MouseEvent e) {
596N/A log.finest("Tray Icon - Mouse released!");
596N/A }
596N/A };
596N/A
596N/A return mouseListener;
596N/A }
596N/A
596N/A private WindowListener getNotificationWindowListener() {
596N/A
596N/A final WindowListener notificationWindowListener = new WindowListener() {
596N/A
596N/A public void windowOpened(WindowEvent arg0) {
596N/A log.finest("Received window opened");
596N/A }
596N/A
596N/A public void windowClosing(WindowEvent arg0) {
596N/A log.finest("Received window closing");
596N/A }
596N/A
596N/A public void windowClosed(WindowEvent arg0) {
596N/A log.info("Received window closing");
596N/A if (agent != null && agent.isConnected()) {
596N/A currentIcon = opengrokImage;
596N/A } else {
596N/A currentIcon = noConnectionImage;
596N/A }
596N/A trayIcon.setImage(currentIcon);
596N/A }
596N/A
596N/A public void windowIconified(WindowEvent arg0) {
596N/A log.finest("Received window iconified");
596N/A }
596N/A
596N/A public void windowDeiconified(WindowEvent arg0) {
596N/A log.finest("Received window deiconified");
596N/A }
596N/A
596N/A public void windowActivated(WindowEvent arg0) {
596N/A log.finest("Received window activated");
596N/A }
596N/A
596N/A public void windowDeactivated(WindowEvent arg0) {
596N/A log.finest("Received window deactivated");
596N/A }
596N/A };
596N/A
596N/A return notificationWindowListener;
596N/A }
596N/A
596N/A private ActionListener getSettingsListener(final WindowListener settingsWindowListener) {
596N/A
596N/A ActionListener settingsListener = new ActionListener() {
596N/A public void actionPerformed(ActionEvent e) {
596N/A log.finer("Settings...");
596N/A SettingsFrame sf = new SettingsFrame(settings);
596N/A sf.addWindowListener(settingsWindowListener);
596N/A sf.setVisible(true);
596N/A log.finer("Done settings");
596N/A }
596N/A };
596N/A
596N/A return settingsListener;
596N/A }
596N/A
596N/A private WindowListener getSettingsWindowListener() {
596N/A final WindowListener settingsWindowListener = new WindowListener() {
596N/A @Override
596N/A public void windowOpened(WindowEvent arg0) {
596N/A log.finest("Received window opened");
596N/A }
596N/A
596N/A @Override
596N/A public void windowClosing(WindowEvent arg0) {
596N/A log.finest("Received window closing " + arg0);
596N/A }
596N/A
596N/A @Override
596N/A public void windowClosed(WindowEvent arg0) {
596N/A log.info("Received window closing");
596N/A if (!agentConnected) {
596N/A tryAgentConnect();
596N/A }
596N/A if (agentConnected) {
596N/A currentIcon = opengrokImage;
596N/A } else {
596N/A currentIcon = noConnectionImage;
596N/A }
596N/A trayIcon.setImage(currentIcon);
596N/A }
596N/A
596N/A @Override
596N/A public void windowIconified(WindowEvent arg0) {
596N/A log.finest("Received window iconified");
596N/A }
596N/A
596N/A public void windowDeiconified(WindowEvent arg0) {
596N/A log.finest("Received window deiconified");
596N/A }
596N/A
596N/A public void windowActivated(WindowEvent arg0) {
596N/A log.finest("Received window activated");
596N/A }
596N/A
596N/A public void windowDeactivated(WindowEvent arg0) {
596N/A log.finest("Received window deactivated");
596N/A }
596N/A };
596N/A
596N/A return settingsWindowListener;
596N/A }
596N/A
596N/A private ActionListener getShowNotificationListener(final WindowListener notificationWindowListener) {
596N/A ActionListener showNotificationsListener = new ActionListener() {
596N/A public void actionPerformed(ActionEvent e) {
596N/A log.info("Notifications...");
596N/A String notifs = "";
596N/A long starttime = 0;
596N/A long endtime = 0;
596N/A if (agentConnected && agent != null) {
596N/A notifs = agent.getFilesInfo();
596N/A agent.clearFilesInfo();
596N/A starttime = agent.getStartTime();
596N/A endtime = agent.getEndTime();
596N/A }
596N/A NotificationsFrame sf = new NotificationsFrame(notifs, starttime, endtime);
596N/A sf.addWindowListener(notificationWindowListener);
596N/A sf.setVisible(true);
596N/A log.finest("Done Notifications");
596N/A }
596N/A };
596N/A
596N/A return showNotificationsListener;
596N/A }
596N/A
385N/A private void loadImages() {
385N/A java.net.URL imageUrl;
385N/A imageUrl = OpenGrokTrayApp.class.getResource(OPENGROKICONURL);
385N/A opengrokImage = Toolkit.getDefaultToolkit().getImage(imageUrl);
385N/A imageUrl = OpenGrokTrayApp.class.getResource(INDEXWARNINGICONURL);
385N/A indexWarningImage = Toolkit.getDefaultToolkit().getImage(imageUrl);
385N/A imageUrl = OpenGrokTrayApp.class.getResource(NOCONNECTIONICONURL);
385N/A noConnectionImage = Toolkit.getDefaultToolkit().getImage(imageUrl);
385N/A }
385N/A
385N/A private boolean tryAgentConnect() {
385N/A boolean retval = false;
385N/A try {
385N/A if (agent == null) {
385N/A agent = new AgentConnection(settings.getAgentUrl());
385N/A }
385N/A agent.setUrl(settings.getAgentUrl());
385N/A
385N/A agent.connect();
385N/A agent.registerListener();
385N/A retval = true;
385N/A agentConnected = true;
385N/A } catch (Exception ex) {
385N/A log.log(Level.WARNING, "Could not connect ", ex);
385N/A }
385N/A return retval;
385N/A }
385N/A
385N/A private void enableSystemTray() {
385N/A
385N/A if (SystemTray.isSupported()) {
385N/A
385N/A SystemTray tray = SystemTray.getSystemTray();
596N/A MouseListener mouseListener = getMouseListener();
385N/A
385N/A log.info("Creating listeners");
385N/A
596N/A final ActionListener exitListener = getExitListener();
596N/A final WindowListener settingsWindowListener = getSettingsWindowListener();
596N/A final WindowListener notificationWindowListener = getNotificationWindowListener();
596N/A final ActionListener settingsListener = getSettingsListener(settingsWindowListener);
596N/A final ActionListener showNotificationsListener = getShowNotificationListener(notificationWindowListener);
639N/A final ActionListener configListener = this.getConfigurationListener();
385N/A
385N/A log.info("Creating popup and tray icon ");
385N/A
385N/A PopupMenu popup = new PopupMenu();
385N/A MenuItem exitItem = new MenuItem("Exit");
385N/A exitItem.addActionListener(exitListener);
639N/A MenuItem settingsItem = new MenuItem("Connection Settings");
385N/A settingsItem.addActionListener(settingsListener);
639N/A MenuItem configsItem = new MenuItem("Configurations");
639N/A configsItem.addActionListener(configListener);
385N/A MenuItem notifyItem = new MenuItem("Show notifications");
385N/A notifyItem.addActionListener(showNotificationsListener);
385N/A
385N/A popup.add(settingsItem);
639N/A popup.add(configsItem);
385N/A popup.add(notifyItem);
385N/A popup.addSeparator();
385N/A popup.add(exitItem);
385N/A
385N/A trayIcon = new TrayIcon(opengrokImage, "OpenGrok Agent Client", popup);
385N/A
596N/A final ActionListener actionListener = getAgentActionListener();
385N/A
385N/A if (agent == null || !agent.isConnected()) {
385N/A trayIcon.setImage(noConnectionImage);
385N/A } else {
385N/A agent.setActionListener(actionListener);
385N/A }
385N/A
385N/A trayIcon.setImageAutoSize(true);
385N/A trayIcon.addActionListener(actionListener);
385N/A trayIcon.addMouseListener(mouseListener);
385N/A
385N/A try {
385N/A tray.add(trayIcon);
385N/A } catch (AWTException e) {
1327N/A log.log(Level.WARNING, "TrayIcon could not be added: " + e.getMessage());
1327N/A log.log(Level.FINE, "enableSystemTray", e);
385N/A }
385N/A log.info("Created, ready for action");
385N/A
385N/A } else {
385N/A // System Tray is not supported
434N/A log.severe("System Tray is NOT supported");
385N/A }
385N/A }
396N/A}