KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > SysTray


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2;
6
7 import java.sql.SQLException JavaDoc;
8
9 import org.h2.message.Message;
10 import org.h2.tools.Server;
11 import org.h2.util.StartBrowser;
12
13 import snoozesoft.systray4j.SysTrayMenu;
14 import snoozesoft.systray4j.SysTrayMenuEvent;
15 import snoozesoft.systray4j.SysTrayMenuIcon;
16 import snoozesoft.systray4j.SysTrayMenuItem;
17 import snoozesoft.systray4j.SysTrayMenuListener;
18
19 public class SysTray implements SysTrayMenuListener {
20
21     Server tcp;
22     Server web;
23     Server odbc;
24
25     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
26         new SysTray().run(args);
27     }
28
29     private void startBrowser() {
30         if(web != null) {
31             StartBrowser.openURL(web.getURL());
32         }
33     }
34     
35     private void run(String JavaDoc[] args) {
36         try {
37             web = Server.createWebServer(args);
38             web.start();
39             tcp = Server.createTcpServer(args).start();
40             odbc = Server.createOdbcServer(args).start();
41             createMenu();
42         } catch(SQLException JavaDoc e) {
43             if(e.getErrorCode() == Message.EXCEPTION_OPENING_PORT_1) {
44                 System.out.println("Port is in use, maybe another server server already running on " + web.getURL());
45             } else {
46                 e.printStackTrace();
47             }
48         }
49         // start browser anyway (even if the server is already running)
50
// because some people don't look at the output,
51
// but are wondering why nothing happens
52
StartBrowser.openURL(web.getURL());
53         if(!web.isRunning()) {
54             System.exit(1);
55         }
56     }
57
58     public void menuItemSelected(SysTrayMenuEvent e) {
59         if (e.getActionCommand().equals("exit")) {
60             System.exit(0);
61         } else if (e.getActionCommand().equals("open")) {
62             startBrowser();
63         }
64     }
65
66     public void iconLeftClicked(SysTrayMenuEvent e) {
67         startBrowser();
68     }
69
70     public void iconLeftDoubleClicked(SysTrayMenuEvent e) {
71         startBrowser();
72     }
73
74     void createMenu() {
75         SysTrayMenuItem itemExit = new SysTrayMenuItem("Exit", "exit");
76         itemExit.addSysTrayMenuListener(this);
77         SysTrayMenuItem itemOpen = new SysTrayMenuItem("H2 Console", "open");
78         itemOpen.addSysTrayMenuListener(this);
79         SysTrayMenuIcon icon;
80         icon = new SysTrayMenuIcon(getClass().getResource("/org/h2/h2.ico"));
81         SysTrayMenu menu = new SysTrayMenu(icon, "H2 Console");
82         icon.addSysTrayMenuListener(this);
83         menu.addItem(itemExit);
84         menu.addSeparator();
85         menu.addItem(itemOpen);
86     }
87
88 }
89
Popular Tags