Launcher.java 14.1 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5 6 7 8 9 10 11
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
 * Copyright (C) 2004 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
 */

Matt Tucker's avatar
Matt Tucker committed
12 13
package org.jivesoftware.messenger.launcher;

14 15 16 17
import org.jdesktop.jdic.tray.SystemTray;
import org.jdesktop.jdic.tray.TrayIcon;
import org.jivesoftware.messenger.JiveGlobals;
import org.jivesoftware.util.XMLProperties;
18
import org.jivesoftware.util.WebManager;
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JProgressBar;
import javax.swing.UIManager;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
Matt Tucker's avatar
Matt Tucker committed
37 38 39 40 41 42
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileNotFoundException;
43
import java.io.IOException;
Matt Tucker's avatar
Matt Tucker committed
44 45

/**
Matt Tucker's avatar
Matt Tucker committed
46
 * Graphical launcher for Jive Messenger.
Matt Tucker's avatar
Matt Tucker committed
47 48 49 50
 *
 * @author Matt Tucker
 */
public class Launcher {
Matt Tucker's avatar
Matt Tucker committed
51

52
    private Process messengerd;
53
    private String configFile = JiveGlobals.getMessengerHome() + File.separator + "conf" + File.separator + "jive-messenger.xml";
Matt Tucker's avatar
Matt Tucker committed
54 55
    private JPanel toolbar = new JPanel();

56 57 58
    private ImageIcon offIcon;
    private ImageIcon onIcon;
    private TrayIcon trayIcon;
59
    private JFrame frame;
60

Matt Tucker's avatar
Matt Tucker committed
61 62 63 64
    /**
     * Creates a new Launcher object.
     */
    public Launcher() {
65
        // Initialize the SystemTray now (to avoid a bug!)
66
        final SystemTray tray = SystemTray.getDefaultSystemTray();
Matt Tucker's avatar
Matt Tucker committed
67 68 69 70 71 72 73 74
        // Use the native look and feel.
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) {
            e.printStackTrace();
        }

75
        String title = "Jive Messenger";
76 77 78
        frame = new DroppableFrame() {
            public void fileDropped(File file) {
                String fileName = file.getName();
Derek DeMoro's avatar
Derek DeMoro committed
79
                if (fileName.endsWith(".jar") || fileName.endsWith(".war")) {
80 81 82 83 84 85
                    installPlugin(file);
                }
            }
        };

        frame.setTitle(title);
Matt Tucker's avatar
Matt Tucker committed
86 87 88 89 90 91 92 93

        ImageIcon splash = null;
        JLabel splashLabel = null;

        // Set the icon.
        try {
            splash = new ImageIcon(getClass().getClassLoader().getResource("splash.gif"));
            splashLabel = new JLabel("", splash, JLabel.LEFT);
94 95 96 97

            onIcon = new ImageIcon(getClass().getClassLoader().getResource("messenger_on-16x16.gif"));
            offIcon = new ImageIcon(getClass().getClassLoader().getResource("messenger_off-16x16.gif"));
            frame.setIconImage(offIcon.getImage());
Matt Tucker's avatar
Matt Tucker committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
        }
        catch (Exception e) {
        }

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());

        // Add buttons
        final JButton startButton = new JButton("Start");
        startButton.setActionCommand("Start");

        final JButton stopButton = new JButton("Stop");
        stopButton.setActionCommand("Stop");

        final JButton browserButton = new JButton("Launch Admin");
        browserButton.setActionCommand("Launch Admin");

        final JButton quitButton = new JButton("Quit");
        quitButton.setActionCommand("Quit");

        toolbar.setLayout(new GridBagLayout());
        toolbar.add(startButton, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        toolbar.add(stopButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        toolbar.add(browserButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        toolbar.add(quitButton, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
123

Matt Tucker's avatar
Matt Tucker committed
124 125 126 127 128 129
        if (splashLabel != null) {
            mainPanel.add(splashLabel, BorderLayout.CENTER);
        }

        mainPanel.add(toolbar, BorderLayout.SOUTH);

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
        // create the main menu of the system tray icon
        JPopupMenu menu = new JPopupMenu("Messenger Menu");

        final JMenuItem showMenuItem = new JMenuItem("Hide");
        showMenuItem.setActionCommand("Hide/Show");
        menu.add(showMenuItem);

        final JMenuItem startMenuItem = new JMenuItem("Start");
        startMenuItem.setActionCommand("Start");
        menu.add(startMenuItem);

        final JMenuItem stopMenuItem = new JMenuItem("Stop");
        stopMenuItem.setActionCommand("Stop");
        menu.add(stopMenuItem);

        final JMenuItem browserMenuItem = new JMenuItem("Launch Admin");
        browserMenuItem.setActionCommand("Launch Admin");
        menu.add(browserMenuItem);

        menu.addSeparator();

        final JMenuItem quitMenuItem = new JMenuItem("Quit");
        quitMenuItem.setActionCommand("Quit");
        menu.add(quitMenuItem);

        browserButton.setEnabled(false);
        stopButton.setEnabled(false);
        browserMenuItem.setEnabled(false);
        stopMenuItem.setEnabled(false);
Matt Tucker's avatar
Matt Tucker committed
159 160 161

        ActionListener actionListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
162
                if ("Start".equals(e.getActionCommand())) {
Matt Tucker's avatar
Matt Tucker committed
163
                    frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
Matt Tucker's avatar
Matt Tucker committed
164
                    // Adjust button and menu items.
Matt Tucker's avatar
Matt Tucker committed
165 166 167
                    startApplication();
                    startButton.setEnabled(false);
                    stopButton.setEnabled(true);
168 169
                    startMenuItem.setEnabled(false);
                    stopMenuItem.setEnabled(true);
Matt Tucker's avatar
Matt Tucker committed
170 171 172 173
                    // Change to the "on" icon.
                    frame.setIconImage(onIcon.getImage());
                    trayIcon.setIcon(onIcon);
                    // Start a thread to enable the admin button after 8 seconds.
Matt Tucker's avatar
Matt Tucker committed
174 175
                    Thread thread = new Thread() {
                        public void run() {
176 177 178 179 180
                            try {
                                sleep(8000);
                            }
                            catch (Exception e) {
                            }
Matt Tucker's avatar
Matt Tucker committed
181 182
                            // Enable the Launch Admin button/menu item only if the
                            // server has started.
183 184 185
                            if (stopButton.isEnabled()) {
                                browserButton.setEnabled(true);
                                browserMenuItem.setEnabled(true);
Matt Tucker's avatar
Matt Tucker committed
186
                                frame.setCursor(Cursor.getDefaultCursor());
187
                            }
Matt Tucker's avatar
Matt Tucker committed
188 189 190 191 192
                        }
                    };

                    thread.start();
                }
193
                else if ("Stop".equals(e.getActionCommand())) {
Matt Tucker's avatar
Matt Tucker committed
194
                    stopApplication();
Matt Tucker's avatar
Matt Tucker committed
195
                    // Change to the "off" button.
196 197
                    frame.setIconImage(offIcon.getImage());
                    trayIcon.setIcon(offIcon);
Matt Tucker's avatar
Matt Tucker committed
198
                    // Adjust buttons and menu items.
199
                    frame.setCursor(Cursor.getDefaultCursor());
Matt Tucker's avatar
Matt Tucker committed
200 201
                    browserButton.setEnabled(false);
                    startButton.setEnabled(true);
202 203 204 205
                    stopButton.setEnabled(false);
                    browserMenuItem.setEnabled(false);
                    startMenuItem.setEnabled(true);
                    stopMenuItem.setEnabled(false);
Matt Tucker's avatar
Matt Tucker committed
206
                }
207
                else if ("Launch Admin".equals(e.getActionCommand())) {
Matt Tucker's avatar
Matt Tucker committed
208 209
                    launchBrowser();
                }
210
                else if ("Quit".equals(e.getActionCommand())) {
Matt Tucker's avatar
Matt Tucker committed
211 212 213
                    stopApplication();
                    System.exit(0);
                }
214
                else if ("Hide/Show".equals(e.getActionCommand()) || "PressAction".equals(e.getActionCommand())) {
215 216 217 218 219 220 221 222 223 224 225 226 227
                    // Hide/Unhide the window if the user clicked in the system tray icon or
                    // selected the menu option
                    if (frame.isVisible()) {
                        frame.setVisible(false);
                        frame.setState(Frame.ICONIFIED);
                        showMenuItem.setText("Show");
                    }
                    else {
                        frame.setVisible(true);
                        frame.setState(Frame.NORMAL);
                        showMenuItem.setText("Hide");
                    }
                }
Matt Tucker's avatar
Matt Tucker committed
228 229 230 231 232 233 234 235 236
            }
        };

        // Register a listener for the radio buttons.
        startButton.addActionListener(actionListener);
        stopButton.addActionListener(actionListener);
        browserButton.addActionListener(actionListener);
        quitButton.addActionListener(actionListener);

237 238 239 240 241 242 243 244
        // Register a listener for the menu items.
        quitMenuItem.addActionListener(actionListener);
        browserMenuItem.addActionListener(actionListener);
        stopMenuItem.addActionListener(actionListener);
        startMenuItem.addActionListener(actionListener);
        showMenuItem.addActionListener(actionListener);

        // Set the system tray icon with the menu
245 246 247 248 249
        trayIcon = new TrayIcon(offIcon, "Jive Messenger", menu);
        trayIcon.setIconAutoSize(true);
        trayIcon.addActionListener(actionListener);

        tray.addTrayIcon(trayIcon);
250 251 252 253 254 255 256 257 258 259 260 261 262 263

        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                stopApplication();
                System.exit(0);
            }

            public void windowIconified(WindowEvent e) {
                // Make the window disappear when minimized
                frame.setVisible(false);
                showMenuItem.setText("Show");
            }
        });

Matt Tucker's avatar
Matt Tucker committed
264 265 266 267 268 269 270 271
        frame.getContentPane().add(mainPanel);
        frame.pack();
        // frame.setSize(539,418);
        frame.setResizable(false);

        GraphicUtils.centerWindowOnScreen(frame);

        frame.setVisible(true);
272 273 274

        // Start the app.
        startButton.doClick();
Matt Tucker's avatar
Matt Tucker committed
275 276 277
    }

    /**
Matt Tucker's avatar
Matt Tucker committed
278
     * Creates a new GUI launcher instance.
Matt Tucker's avatar
Matt Tucker committed
279 280 281 282 283 284 285 286
     */
    public static void main(String[] args) {
        new Launcher();
    }

    private synchronized void startApplication() {
        if (messengerd == null) {
            try {
Matt Tucker's avatar
Matt Tucker committed
287 288 289
                File windowsExe = new File(new File("").getAbsoluteFile(), "messengerd.exe");
                File unixExe = new File(new File("").getAbsoluteFile(), "messengerd");
                if (windowsExe.exists()) {
290
                    messengerd = Runtime.getRuntime().exec(new String[]{windowsExe.toString()});
Matt Tucker's avatar
Matt Tucker committed
291 292
                }
                else if (unixExe.exists()) {
293
                    messengerd = Runtime.getRuntime().exec(new String[]{unixExe.toString()});
294 295
                }
                else {
Matt Tucker's avatar
Matt Tucker committed
296
                    throw new FileNotFoundException();
Matt Tucker's avatar
Matt Tucker committed
297 298 299 300 301
                }
            }
            catch (Exception e) {
                // Try one more time using the jar and hope java is on the path
                try {
Matt Tucker's avatar
Matt Tucker committed
302 303 304 305
                    File libDir = new File("../lib").getAbsoluteFile();
                    messengerd = Runtime.getRuntime().exec(new String[]{
                        "java", "-jar", new File(libDir, "startup.jar").toString()
                    });
Matt Tucker's avatar
Matt Tucker committed
306 307 308 309
                }
                catch (Exception ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(null,
Matt Tucker's avatar
Matt Tucker committed
310
                            "Launcher could not start,\nJive Messenger",
Matt Tucker's avatar
Matt Tucker committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
                            "File not found", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }

    private synchronized void stopApplication() {
        if (messengerd != null) {
            try {
                messengerd.destroy();
                messengerd.waitFor();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }

        messengerd = null;
    }

    private synchronized void launchBrowser() {
        try {
            XMLProperties props = new XMLProperties(configFile);
334
            String port = props.getProperty("adminConsole.port");
335
            BrowserLauncher.openURL("http://127.0.0.1:" + port + "/index.html");
Matt Tucker's avatar
Matt Tucker committed
336 337
        }
        catch (Exception e) {
338
            JOptionPane.showMessageDialog(new JFrame(), configFile + " " + e.getMessage());
Matt Tucker's avatar
Matt Tucker committed
339 340
        }
    }
341 342

    private void installPlugin(final File plugin) {
343
        final JDialog dialog = new JDialog(frame, "Installing Plugin", true);
344 345 346
        dialog.getContentPane().setLayout(new BorderLayout());
        JProgressBar bar = new JProgressBar();
        bar.setIndeterminate(true);
347
        bar.setString("Installing Plugin.  Please wait...");
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
        bar.setStringPainted(true);
        dialog.getContentPane().add(bar, BorderLayout.CENTER);
        dialog.pack();
        dialog.setSize(225, 55);

        final SwingWorker installerThread = new SwingWorker() {
            public Object construct() {
                File pluginsDir = new File(JiveGlobals.getMessengerHome(), "plugins");
                String tempName = plugin.getName() + ".part";
                File tempPluginsFile = new File(pluginsDir, tempName);

                File realPluginsFile = new File(pluginsDir, plugin.getName());

                // Copy Plugin into Dir.
                try {
Derek DeMoro's avatar
Derek DeMoro committed
363 364 365
                    // Just for fun. Show no matter what for two seconds.
                    Thread.sleep(2000);

366
                    WebManager.copy(plugin.toURL(), tempPluginsFile);
367 368 369 370

                    // If successfull, rename to real plugin name.
                    tempPluginsFile.renameTo(realPluginsFile);
                }
Derek DeMoro's avatar
Derek DeMoro committed
371
                catch (Exception e) {
372 373 374 375 376 377 378 379 380 381 382 383 384
                    e.printStackTrace();
                }
                return realPluginsFile;
            }

            public void finished() {
                dialog.setVisible(false);
            }
        };

        // Start installation
        installerThread.start();

Derek DeMoro's avatar
Derek DeMoro committed
385 386
        dialog.setLocationRelativeTo(frame);
        dialog.setVisible(true);
387
    }
388 389
}