KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > launcher > Launcher


1 /**
2  * $RCSfile: Launcher.java,v $
3  * $Revision: 1.26 $
4  * $Date: 2005/07/28 03:58:07 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

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 JavaDoc;
20 import javax.swing.ImageIcon JavaDoc;
21 import javax.swing.JButton JavaDoc;
22 import javax.swing.JDialog JavaDoc;
23 import javax.swing.JFrame JavaDoc;
24 import javax.swing.JLabel JavaDoc;
25 import javax.swing.JMenuItem JavaDoc;
26 import javax.swing.JOptionPane JavaDoc;
27 import javax.swing.JPanel JavaDoc;
28 import javax.swing.JPopupMenu JavaDoc;
29 import javax.swing.JProgressBar JavaDoc;
30 import javax.swing.JScrollPane JavaDoc;
31 import javax.swing.JTextPane JavaDoc;
32 import javax.swing.UIManager JavaDoc;
33 import javax.swing.text.BadLocationException JavaDoc;
34 import javax.swing.text.SimpleAttributeSet JavaDoc;
35 import javax.swing.text.StyleConstants JavaDoc;
36
37 import java.awt.BorderLayout JavaDoc;
38 import java.awt.CardLayout JavaDoc;
39 import java.awt.Color JavaDoc;
40 import java.awt.Cursor JavaDoc;
41 import java.awt.Dimension JavaDoc;
42 import java.awt.Frame JavaDoc;
43 import java.awt.Graphics JavaDoc;
44 import java.awt.GridBagConstraints JavaDoc;
45 import java.awt.GridBagLayout JavaDoc;
46 import java.awt.Insets JavaDoc;
47 import java.awt.event.ActionEvent JavaDoc;
48 import java.awt.event.ActionListener JavaDoc;
49 import java.awt.event.WindowAdapter JavaDoc;
50 import java.awt.event.WindowEvent JavaDoc;
51 import java.io.File JavaDoc;
52 import java.io.FileNotFoundException JavaDoc;
53 import java.io.IOException JavaDoc;
54 import java.io.InputStream JavaDoc;
55
56 /**
57  * Graphical launcher for Jive Messenger.
58  *
59  * @author Matt Tucker
60  */

61 public class Launcher {
62
63     private String JavaDoc appName;
64     private File JavaDoc binDir;
65     private Process JavaDoc messengerd;
66     private String JavaDoc configFile;
67     private JPanel JavaDoc toolbar = new JPanel JavaDoc();
68
69     private ImageIcon JavaDoc offIcon;
70     private ImageIcon JavaDoc onIcon;
71     private TrayIcon trayIcon;
72     private JFrame JavaDoc frame;
73     private JPanel JavaDoc cardPanel = new JPanel JavaDoc();
74     private CardLayout JavaDoc cardLayout = new CardLayout JavaDoc();
75
76     private JTextPane JavaDoc pane;
77     private boolean freshStart = true;
78
79     /**
80      * Creates a new Launcher object.
81      */

82     public Launcher() {
83         // Initialize the SystemTray now (to avoid a bug!)
84
final SystemTray tray = SystemTray.getDefaultSystemTray();
85         // Use the native look and feel.
86
try {
87             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
88         }
89         catch (Exception JavaDoc 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 JavaDoc("").getAbsoluteFile();
101         // See if the appdir property is set. If so, use it to find the executable.
102
if (System.getProperty("appdir") != null) {
103             binDir = new File JavaDoc(System.getProperty("appdir"));
104         }
105
106         configFile = new File JavaDoc(new File JavaDoc(binDir.getParent(), "conf"),
107                 "jive-messenger.xml").getAbsolutePath();
108
109         frame = new DroppableFrame() {
110             public void fileDropped(File JavaDoc file) {
111                 String JavaDoc 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 JavaDoc splash = null;
121         JPanel JavaDoc mainPanel = new JPanel JavaDoc();
122         JLabel JavaDoc splashLabel = null;
123
124         cardPanel.setLayout(cardLayout);
125
126         // Set the icon.
127
try {
128             splash = new ImageIcon JavaDoc(getClass().getClassLoader().getResource("splash.gif"));
129             splashLabel = new JLabel JavaDoc("", splash, JLabel.CENTER);
130
131             onIcon = new ImageIcon JavaDoc(getClass().getClassLoader().getResource("messenger_on-16x16.gif"));
132             offIcon = new ImageIcon JavaDoc(getClass().getClassLoader().getResource("messenger_off-16x16.gif"));
133             frame.setIconImage(offIcon.getImage());
134         }
135         catch (Exception JavaDoc e) {
136         }
137
138         mainPanel.setLayout(new BorderLayout JavaDoc());
139         cardPanel.setBackground(Color.white);
140
141         // Add buttons
142
final JButton JavaDoc startButton = new JButton JavaDoc("Start");
143         startButton.setActionCommand("Start");
144
145         final JButton JavaDoc stopButton = new JButton JavaDoc("Stop");
146         stopButton.setActionCommand("Stop");
147
148         final JButton JavaDoc browserButton = new JButton JavaDoc("Launch Admin");
149         browserButton.setActionCommand("Launch Admin");
150
151         final JButton JavaDoc quitButton = new JButton JavaDoc("Quit");
152         quitButton.setActionCommand("Quit");
153
154         toolbar.setLayout(new GridBagLayout JavaDoc());
155         toolbar.add(startButton, new GridBagConstraints JavaDoc(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets JavaDoc(5, 5, 5, 5), 0, 0));
156         toolbar.add(stopButton, new GridBagConstraints JavaDoc(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets JavaDoc(5, 5, 5, 5), 0, 0));
157         toolbar.add(browserButton, new GridBagConstraints JavaDoc(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets JavaDoc(5, 5, 5, 5), 0, 0));
158         toolbar.add(quitButton, new GridBagConstraints JavaDoc(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets JavaDoc(5, 5, 5, 5), 0, 0));
159
160         mainPanel.add(cardPanel, BorderLayout.CENTER);
161         mainPanel.add(toolbar, BorderLayout.SOUTH);
162
163         // create the main menu of the system tray icon
164
JPopupMenu JavaDoc menu = new JPopupMenu JavaDoc(appName + " Menu");
165
166         final JMenuItem JavaDoc showMenuItem = new JMenuItem JavaDoc("Hide");
167         showMenuItem.setActionCommand("Hide/Show");
168         menu.add(showMenuItem);
169
170         final JMenuItem JavaDoc startMenuItem = new JMenuItem JavaDoc("Start");
171         startMenuItem.setActionCommand("Start");
172         menu.add(startMenuItem);
173
174         final JMenuItem JavaDoc stopMenuItem = new JMenuItem JavaDoc("Stop");
175         stopMenuItem.setActionCommand("Stop");
176         menu.add(stopMenuItem);
177
178         final JMenuItem JavaDoc browserMenuItem = new JMenuItem JavaDoc("Launch Admin");
179         browserMenuItem.setActionCommand("Launch Admin");
180         menu.add(browserMenuItem);
181
182         menu.addSeparator();
183
184         final JMenuItem JavaDoc quitMenuItem = new JMenuItem JavaDoc("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 JavaDoc actionListener = new ActionListener JavaDoc() {
194             public void actionPerformed(ActionEvent JavaDoc e) {
195                 if ("Start".equals(e.getActionCommand())) {
196                     frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
197                     // Adjust button and menu items.
198
startButton.setEnabled(false);
199                     stopButton.setEnabled(true);
200                     startMenuItem.setEnabled(false);
201                     stopMenuItem.setEnabled(true);
202
203                     // Startup Application
204
startApplication();
205
206                     // Change to the "on" icon.
207
frame.setIconImage(onIcon.getImage());
208                     trayIcon.setIcon(onIcon);
209
210                     // Start a thread to enable the admin button after 8 seconds.
211
Thread JavaDoc thread = new Thread JavaDoc() {
212                         public void run() {
213                             try {
214                                 sleep(8000);
215                             }
216                             catch (Exception JavaDoc e) {
217                             }
218                             // Enable the Launch Admin button/menu item only if the
219
// server has started.
220
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                     // Change to the "off" button.
232
frame.setIconImage(offIcon.getImage());
233                     trayIcon.setIcon(offIcon);
234                     // Adjust buttons and menu items.
235
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                     // Hide/Unhide the window if the user clicked in the system tray icon or
252
// selected the menu option
253
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         // Register a listener for the radio buttons.
268
startButton.addActionListener(actionListener);
269         stopButton.addActionListener(actionListener);
270         browserButton.addActionListener(actionListener);
271         quitButton.addActionListener(actionListener);
272
273         // Register a listener for the menu items.
274
quitMenuItem.addActionListener(actionListener);
275         browserMenuItem.addActionListener(actionListener);
276         stopMenuItem.addActionListener(actionListener);
277         startMenuItem.addActionListener(actionListener);
278         showMenuItem.addActionListener(actionListener);
279
280         // Set the system tray icon with the menu
281
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 JavaDoc() {
288             public void windowClosing(WindowEvent JavaDoc e) {
289                 stopApplication();
290                 System.exit(0);
291             }
292
293             public void windowIconified(WindowEvent JavaDoc e) {
294                 // Make the window disappear when minimized
295
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         // Setup command area
312
final ImageIcon JavaDoc icon = new ImageIcon JavaDoc(getClass().getClassLoader().getResource("splash2.gif"));
313         pane = new DroppableTextPane() {
314             public void paintComponent(Graphics JavaDoc g) {
315                 final Dimension JavaDoc size = pane.getSize();
316
317                 int x = (size.width - icon.getIconWidth()) / 2;
318                 int y = (size.height - icon.getIconHeight()) / 2;
319                 // Approach 1: Dispaly image at at full size
320
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 JavaDoc file) {
330                 String JavaDoc 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 JavaDoc bevelPanel = new JPanel JavaDoc();
340         bevelPanel.setBackground(Color.white);
341         bevelPanel.setLayout(new BorderLayout JavaDoc());
342         bevelPanel.setBorder(BorderFactory.createLoweredBevelBorder());
343         bevelPanel.add(new JScrollPane JavaDoc(pane), BorderLayout.CENTER);
344         cardPanel.add("running", bevelPanel);
345
346         // Start the app.
347
startButton.doClick();
348     }
349
350     /**
351      * Creates a new GUI launcher instance.
352      */

353     public static void main(String JavaDoc[] args) {
354         new Launcher();
355     }
356
357     private synchronized void startApplication() {
358         if (messengerd == null) {
359             try {
360                 File JavaDoc windowsExe = new File JavaDoc(binDir, "messengerd.exe");
361                 File JavaDoc unixExe = new File JavaDoc(binDir, "messengerd");
362                 if (windowsExe.exists()) {
363                     messengerd = Runtime.getRuntime().exec(new String JavaDoc[]{windowsExe.toString()});
364                 }
365                 else if (unixExe.exists()) {
366                     messengerd = Runtime.getRuntime().exec(new String JavaDoc[]{unixExe.toString()});
367                 }
368                 else {
369                     throw new FileNotFoundException JavaDoc();
370                 }
371             }
372             catch (Exception JavaDoc e) {
373                 // Try one more time using the jar and hope java is on the path
374
try {
375                     File JavaDoc libDir = new File JavaDoc(binDir.getParentFile(), "lib").getAbsoluteFile();
376                     messengerd = Runtime.getRuntime().exec(new String JavaDoc[]{
377                         "java", "-jar", new File JavaDoc(libDir, "startup.jar").toString()
378                     });
379                 }
380                 catch (Exception JavaDoc 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 JavaDoc styles = new SimpleAttributeSet JavaDoc();
389             SwingWorker inputWorker = new SwingWorker() {
390                 public Object JavaDoc construct() {
391                     if (messengerd != null) {
392                         try {
393                             // Get the input stream and read from it
394
InputStream JavaDoc 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 JavaDoc e) {
403                                 }
404                             }
405                             in.close();
406                         }
407                         catch (IOException JavaDoc e) {
408                             e.printStackTrace();
409                         }
410                     }
411                     return "ok";
412                 }
413             };
414             inputWorker.start();
415
416
417             SwingWorker errorWorker = new SwingWorker() {
418                 public Object JavaDoc construct() {
419                     if (messengerd != null) {
420                         try {
421                             // Get the input stream and read from it
422
InputStream JavaDoc 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 JavaDoc e) {
430                                 }
431                             }
432                             in.close();
433                         }
434                         catch (IOException JavaDoc 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 JavaDoc ex) {
449
450                 }
451                 freshStart = false;
452             }
453             else {
454                  // Clear Text
455
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 JavaDoc 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 JavaDoc port = props.getProperty("adminConsole.port");
481             BrowserLauncher.openURL("http://127.0.0.1:" + port + "/index.html");
482         }
483         catch (Exception JavaDoc e) {
484             JOptionPane.showMessageDialog(new JFrame JavaDoc(), configFile + " " + e.getMessage());
485         }
486     }
487
488     private void installPlugin(final File JavaDoc plugin) {
489         final JDialog JavaDoc dialog = new JDialog JavaDoc(frame, "Installing Plugin", true);
490         dialog.getContentPane().setLayout(new BorderLayout JavaDoc());
491         JProgressBar JavaDoc bar = new JProgressBar JavaDoc();
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 JavaDoc construct() {
501                 File JavaDoc pluginsDir = new File JavaDoc(binDir.getParentFile(), "plugins");
502                 String JavaDoc tempName = plugin.getName() + ".part";
503                 File JavaDoc tempPluginsFile = new File JavaDoc(pluginsDir, tempName);
504
505                 File JavaDoc realPluginsFile = new File JavaDoc(pluginsDir, plugin.getName());
506
507                 // Copy Plugin into Dir.
508
try {
509                     // Just for fun. Show no matter what for two seconds.
510
Thread.sleep(2000);
511
512                     WebManager.copy(plugin.toURL(), tempPluginsFile);
513
514                     // If successfull, rename to real plugin name.
515
tempPluginsFile.renameTo(realPluginsFile);
516                 }
517                 catch (Exception JavaDoc e) {
518                     e.printStackTrace();
519                 }
520                 return realPluginsFile;
521             }
522
523             public void finished() {
524                 dialog.setVisible(false);
525             }
526         };
527
528         // Start installation
529
installerThread.start();
530
531         dialog.setLocationRelativeTo(frame);
532         dialog.setVisible(true);
533     }
534 }
Popular Tags