1 11 12 package org.jivesoftware.messenger.launcher; 13 14 import org.jdesktop.jdic.tray.SystemTray; 15 import org.jdesktop.jdic.tray.TrayIcon; 16 import org.jivesoftware.util.WebManager; 17 import org.jivesoftware.util.XMLProperties; 18 19 import javax.swing.BorderFactory ; 20 import javax.swing.ImageIcon ; 21 import javax.swing.JButton ; 22 import javax.swing.JDialog ; 23 import javax.swing.JFrame ; 24 import javax.swing.JLabel ; 25 import javax.swing.JMenuItem ; 26 import javax.swing.JOptionPane ; 27 import javax.swing.JPanel ; 28 import javax.swing.JPopupMenu ; 29 import javax.swing.JProgressBar ; 30 import javax.swing.JScrollPane ; 31 import javax.swing.JTextPane ; 32 import javax.swing.UIManager ; 33 import javax.swing.text.BadLocationException ; 34 import javax.swing.text.SimpleAttributeSet ; 35 import javax.swing.text.StyleConstants ; 36 37 import java.awt.BorderLayout ; 38 import java.awt.CardLayout ; 39 import java.awt.Color ; 40 import java.awt.Cursor ; 41 import java.awt.Dimension ; 42 import java.awt.Frame ; 43 import java.awt.Graphics ; 44 import java.awt.GridBagConstraints ; 45 import java.awt.GridBagLayout ; 46 import java.awt.Insets ; 47 import java.awt.event.ActionEvent ; 48 import java.awt.event.ActionListener ; 49 import java.awt.event.WindowAdapter ; 50 import java.awt.event.WindowEvent ; 51 import java.io.File ; 52 import java.io.FileNotFoundException ; 53 import java.io.IOException ; 54 import java.io.InputStream ; 55 56 61 public class Launcher { 62 63 private String appName; 64 private File binDir; 65 private Process messengerd; 66 private String configFile; 67 private JPanel toolbar = new JPanel (); 68 69 private ImageIcon offIcon; 70 private ImageIcon onIcon; 71 private TrayIcon trayIcon; 72 private JFrame frame; 73 private JPanel cardPanel = new JPanel (); 74 private CardLayout cardLayout = new CardLayout (); 75 76 private JTextPane pane; 77 private boolean freshStart = true; 78 79 82 public Launcher() { 83 final SystemTray tray = SystemTray.getDefaultSystemTray(); 85 try { 87 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 88 } 89 catch (Exception e) { 90 e.printStackTrace(); 91 } 92 93 if (System.getProperty("app.name") != null) { 94 appName = System.getProperty("app.name"); 95 } 96 else { 97 appName = "Jive Messenger"; 98 } 99 100 binDir = new File ("").getAbsoluteFile(); 101 if (System.getProperty("appdir") != null) { 103 binDir = new File (System.getProperty("appdir")); 104 } 105 106 configFile = new File (new File (binDir.getParent(), "conf"), 107 "jive-messenger.xml").getAbsolutePath(); 108 109 frame = new DroppableFrame() { 110 public void fileDropped(File file) { 111 String fileName = file.getName(); 112 if (fileName.endsWith(".jar") || fileName.endsWith(".war")) { 113 installPlugin(file); 114 } 115 } 116 }; 117 118 frame.setTitle(appName); 119 120 ImageIcon splash = null; 121 JPanel mainPanel = new JPanel (); 122 JLabel splashLabel = null; 123 124 cardPanel.setLayout(cardLayout); 125 126 try { 128 splash = new ImageIcon (getClass().getClassLoader().getResource("splash.gif")); 129 splashLabel = new JLabel ("", splash, JLabel.CENTER); 130 131 onIcon = new ImageIcon (getClass().getClassLoader().getResource("messenger_on-16x16.gif")); 132 offIcon = new ImageIcon (getClass().getClassLoader().getResource("messenger_off-16x16.gif")); 133 frame.setIconImage(offIcon.getImage()); 134 } 135 catch (Exception e) { 136 } 137 138 mainPanel.setLayout(new BorderLayout ()); 139 cardPanel.setBackground(Color.white); 140 141 final JButton startButton = new JButton ("Start"); 143 startButton.setActionCommand("Start"); 144 145 final JButton stopButton = new JButton ("Stop"); 146 stopButton.setActionCommand("Stop"); 147 148 final JButton browserButton = new JButton ("Launch Admin"); 149 browserButton.setActionCommand("Launch Admin"); 150 151 final JButton quitButton = new JButton ("Quit"); 152 quitButton.setActionCommand("Quit"); 153 154 toolbar.setLayout(new GridBagLayout ()); 155 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)); 156 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)); 157 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)); 158 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)); 159 160 mainPanel.add(cardPanel, BorderLayout.CENTER); 161 mainPanel.add(toolbar, BorderLayout.SOUTH); 162 163 JPopupMenu menu = new JPopupMenu (appName + " Menu"); 165 166 final JMenuItem showMenuItem = new JMenuItem ("Hide"); 167 showMenuItem.setActionCommand("Hide/Show"); 168 menu.add(showMenuItem); 169 170 final JMenuItem startMenuItem = new JMenuItem ("Start"); 171 startMenuItem.setActionCommand("Start"); 172 menu.add(startMenuItem); 173 174 final JMenuItem stopMenuItem = new JMenuItem ("Stop"); 175 stopMenuItem.setActionCommand("Stop"); 176 menu.add(stopMenuItem); 177 178 final JMenuItem browserMenuItem = new JMenuItem ("Launch Admin"); 179 browserMenuItem.setActionCommand("Launch Admin"); 180 menu.add(browserMenuItem); 181 182 menu.addSeparator(); 183 184 final JMenuItem quitMenuItem = new JMenuItem ("Quit"); 185 quitMenuItem.setActionCommand("Quit"); 186 menu.add(quitMenuItem); 187 188 browserButton.setEnabled(false); 189 stopButton.setEnabled(false); 190 browserMenuItem.setEnabled(false); 191 stopMenuItem.setEnabled(false); 192 193 ActionListener actionListener = new ActionListener () { 194 public void actionPerformed(ActionEvent e) { 195 if ("Start".equals(e.getActionCommand())) { 196 frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 197 startButton.setEnabled(false); 199 stopButton.setEnabled(true); 200 startMenuItem.setEnabled(false); 201 stopMenuItem.setEnabled(true); 202 203 startApplication(); 205 206 frame.setIconImage(onIcon.getImage()); 208 trayIcon.setIcon(onIcon); 209 210 Thread thread = new Thread () { 212 public void run() { 213 try { 214 sleep(8000); 215 } 216 catch (Exception e) { 217 } 218 if (stopButton.isEnabled()) { 221 browserButton.setEnabled(true); 222 browserMenuItem.setEnabled(true); 223 frame.setCursor(Cursor.getDefaultCursor()); 224 } 225 } 226 }; 227 thread.start(); 228 } 229 else if ("Stop".equals(e.getActionCommand())) { 230 stopApplication(); 231 frame.setIconImage(offIcon.getImage()); 233 trayIcon.setIcon(offIcon); 234 frame.setCursor(Cursor.getDefaultCursor()); 236 browserButton.setEnabled(false); 237 startButton.setEnabled(true); 238 stopButton.setEnabled(false); 239 browserMenuItem.setEnabled(false); 240 startMenuItem.setEnabled(true); 241 stopMenuItem.setEnabled(false); 242 } 243 else if ("Launch Admin".equals(e.getActionCommand())) { 244 launchBrowser(); 245 } 246 else if ("Quit".equals(e.getActionCommand())) { 247 stopApplication(); 248 System.exit(0); 249 } 250 else if ("Hide/Show".equals(e.getActionCommand()) || "PressAction".equals(e.getActionCommand())) { 251 if (frame.isVisible()) { 254 frame.setVisible(false); 255 frame.setState(Frame.ICONIFIED); 256 showMenuItem.setText("Show"); 257 } 258 else { 259 frame.setVisible(true); 260 frame.setState(Frame.NORMAL); 261 showMenuItem.setText("Hide"); 262 } 263 } 264 } 265 }; 266 267 startButton.addActionListener(actionListener); 269 stopButton.addActionListener(actionListener); 270 browserButton.addActionListener(actionListener); 271 quitButton.addActionListener(actionListener); 272 273 quitMenuItem.addActionListener(actionListener); 275 browserMenuItem.addActionListener(actionListener); 276 stopMenuItem.addActionListener(actionListener); 277 startMenuItem.addActionListener(actionListener); 278 showMenuItem.addActionListener(actionListener); 279 280 trayIcon = new TrayIcon(offIcon, appName, menu); 282 trayIcon.setIconAutoSize(true); 283 trayIcon.addActionListener(actionListener); 284 285 tray.addTrayIcon(trayIcon); 286 287 frame.addWindowListener(new WindowAdapter () { 288 public void windowClosing(WindowEvent e) { 289 stopApplication(); 290 System.exit(0); 291 } 292 293 public void windowIconified(WindowEvent e) { 294 frame.setVisible(false); 296 showMenuItem.setText("Show"); 297 } 298 }); 299 300 cardPanel.add("main", splashLabel); 301 frame.getContentPane().add(mainPanel, BorderLayout.CENTER); 302 frame.pack(); 303 304 frame.setSize(400, 300); 305 frame.setResizable(true); 306 307 GraphicUtils.centerWindowOnScreen(frame); 308 309 frame.setVisible(true); 310 311 final ImageIcon icon = new ImageIcon (getClass().getClassLoader().getResource("splash2.gif")); 313 pane = new DroppableTextPane() { 314 public void paintComponent(Graphics g) { 315 final Dimension size = pane.getSize(); 316 317 int x = (size.width - icon.getIconWidth()) / 2; 318 int y = (size.height - icon.getIconHeight()) / 2; 319 g.setColor(Color.white); 321 g.fillRect(0, 0, size.width, size.height); 322 g.drawImage(icon.getImage(), x, y, null); 323 324 325 setOpaque(false); 326 super.paintComponent(g); 327 } 328 329 public void fileDropped(File file) { 330 String fileName = file.getName(); 331 if (fileName.endsWith(".jar") || fileName.endsWith(".war")) { 332 installPlugin(file); 333 } 334 } 335 }; 336 337 pane.setEditable(false); 338 339 final JPanel bevelPanel = new JPanel (); 340 bevelPanel.setBackground(Color.white); 341 bevelPanel.setLayout(new BorderLayout ()); 342 bevelPanel.setBorder(BorderFactory.createLoweredBevelBorder()); 343 bevelPanel.add(new JScrollPane (pane), BorderLayout.CENTER); 344 cardPanel.add("running", bevelPanel); 345 346 startButton.doClick(); 348 } 349 350 353 public static void main(String [] args) { 354 new Launcher(); 355 } 356 357 private synchronized void startApplication() { 358 if (messengerd == null) { 359 try { 360 File windowsExe = new File (binDir, "messengerd.exe"); 361 File unixExe = new File (binDir, "messengerd"); 362 if (windowsExe.exists()) { 363 messengerd = Runtime.getRuntime().exec(new String []{windowsExe.toString()}); 364 } 365 else if (unixExe.exists()) { 366 messengerd = Runtime.getRuntime().exec(new String []{unixExe.toString()}); 367 } 368 else { 369 throw new FileNotFoundException (); 370 } 371 } 372 catch (Exception e) { 373 try { 375 File libDir = new File (binDir.getParentFile(), "lib").getAbsoluteFile(); 376 messengerd = Runtime.getRuntime().exec(new String []{ 377 "java", "-jar", new File (libDir, "startup.jar").toString() 378 }); 379 } 380 catch (Exception ex) { 381 ex.printStackTrace(); 382 JOptionPane.showMessageDialog(null, 383 "Launcher could not start,\n" + appName, 384 "File not found", JOptionPane.ERROR_MESSAGE); 385 } 386 } 387 388 final SimpleAttributeSet styles = new SimpleAttributeSet (); 389 SwingWorker inputWorker = new SwingWorker() { 390 public Object construct() { 391 if (messengerd != null) { 392 try { 393 InputStream in = messengerd.getInputStream(); 395 int c; 396 while ((c = in.read()) != -1) { 397 try { 398 StyleConstants.setFontFamily(styles, "courier new"); 399 pane.getDocument().insertString(pane.getDocument().getLength(), 400 "" + (char)c, styles); 401 } 402 catch (BadLocationException e) { 403 } 404 } 405 in.close(); 406 } 407 catch (IOException e) { 408 e.printStackTrace(); 409 } 410 } 411 return "ok"; 412 } 413 }; 414 inputWorker.start(); 415 416 417 SwingWorker errorWorker = new SwingWorker() { 418 public Object construct() { 419 if (messengerd != null) { 420 try { 421 InputStream in = messengerd.getErrorStream(); 423 int c; 424 while ((c = in.read()) != -1) { 425 try { 426 StyleConstants.setForeground(styles, Color.red); 427 pane.getDocument().insertString(pane.getDocument().getLength(), "" + (char)c, styles); 428 } 429 catch (BadLocationException e) { 430 } 431 } 432 in.close(); 433 } 434 catch (IOException e) { 435 e.printStackTrace(); 436 } 437 } 438 return "ok"; 439 } 440 }; 441 errorWorker.start(); 442 443 if (freshStart) { 444 try { 445 Thread.sleep(1000); 446 cardLayout.show(cardPanel, "running"); 447 } 448 catch (Exception ex) { 449 450 } 451 freshStart = false; 452 } 453 else { 454 pane.setText(""); 456 cardLayout.show(cardPanel, "running"); 457 } 458 459 } 460 } 461 462 private synchronized void stopApplication() { 463 if (messengerd != null) { 464 try { 465 messengerd.destroy(); 466 messengerd.waitFor(); 467 cardLayout.show(cardPanel, "main"); 468 } 469 catch (Exception e) { 470 e.printStackTrace(); 471 } 472 } 473 474 messengerd = null; 475 } 476 477 private synchronized void launchBrowser() { 478 try { 479 XMLProperties props = new XMLProperties(configFile); 480 String port = props.getProperty("adminConsole.port"); 481 BrowserLauncher.openURL("http://127.0.0.1:" + port + "/index.html"); 482 } 483 catch (Exception e) { 484 JOptionPane.showMessageDialog(new JFrame (), configFile + " " + e.getMessage()); 485 } 486 } 487 488 private void installPlugin(final File plugin) { 489 final JDialog dialog = new JDialog (frame, "Installing Plugin", true); 490 dialog.getContentPane().setLayout(new BorderLayout ()); 491 JProgressBar bar = new JProgressBar (); 492 bar.setIndeterminate(true); 493 bar.setString("Installing Plugin. Please wait..."); 494 bar.setStringPainted(true); 495 dialog.getContentPane().add(bar, BorderLayout.CENTER); 496 dialog.pack(); 497 dialog.setSize(225, 55); 498 499 final SwingWorker installerThread = new SwingWorker() { 500 public Object construct() { 501 File pluginsDir = new File (binDir.getParentFile(), "plugins"); 502 String tempName = plugin.getName() + ".part"; 503 File tempPluginsFile = new File (pluginsDir, tempName); 504 505 File realPluginsFile = new File (pluginsDir, plugin.getName()); 506 507 try { 509 Thread.sleep(2000); 511 512 WebManager.copy(plugin.toURL(), tempPluginsFile); 513 514 tempPluginsFile.renameTo(realPluginsFile); 516 } 517 catch (Exception e) { 518 e.printStackTrace(); 519 } 520 return realPluginsFile; 521 } 522 523 public void finished() { 524 dialog.setVisible(false); 525 } 526 }; 527 528 installerThread.start(); 530 531 dialog.setLocationRelativeTo(frame); 532 dialog.setVisible(true); 533 } 534 } | Popular Tags |