KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > tracelog > ui > ShellTray


1 package net.sourceforge.tracelog.ui;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.InputStreamReader JavaDoc;
5 import java.net.URL JavaDoc;
6
7 import net.sourceforge.tracelog.listeners.LogViewerHandler;
8 import net.sourceforge.tracelog.utils.Util;
9
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.events.ModifyEvent;
12 import org.eclipse.swt.graphics.Image;
13 import org.eclipse.swt.program.Program;
14 import org.eclipse.swt.widgets.Event;
15 import org.eclipse.swt.widgets.Listener;
16 import org.eclipse.swt.widgets.Menu;
17 import org.eclipse.swt.widgets.MenuItem;
18 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.swt.widgets.ToolTip;
20 import org.eclipse.swt.widgets.Tray;
21 import org.eclipse.swt.widgets.TrayItem;
22
23 public class ShellTray extends AbstractWidget {
24     // private ShellMain shellMain;
25
private Shell trayShell;
26     private Image idleImage;
27     private Image activeImage;
28     private TrayItem trayItem;
29
30     public ShellTray() {
31         super();
32         // this.shellMain = shellMain;
33
this.trayShell = new Shell(display);
34         this.idleImage = new Image(display, Util.getOwnResource(projectProperties.getShellIconPath()));
35         this.activeImage = new Image(display, Util.getOwnResource(projectProperties.getIconActive()));
36     }
37
38     public void run() {
39         final Tray tray = display.getSystemTray();
40
41         if (tray != null) {
42
43             ToolTip toolTip = new ToolTip(trayShell, SWT.BALLOON | SWT.ICON_INFORMATION);
44
45             String JavaDoc latestVersion = getLatestSoftwareVersion();
46
47             // has new version
48
if (!latestVersion.equals(appVersion)) {
49                 toolTip.setText(appTitle + " Software Update");
50                 toolTip.setAutoHide(false);
51
52                 toolTip.setMessage("A new software release, " + latestVersion + ", is now available for download. "
53                         + "The current software release is " + appVersion + ". " + "\n\nPlease click here to download the latest release.");
54                 toolTip.addListener(SWT.Selection, new Listener() {
55                     public void handleEvent(Event event) {
56                         Program.launch(projectProperties.getUpdateURL());
57                     }
58                 });
59             }
60             // no updates available
61
else {
62                 toolTip.setText(appTitle);
63                 toolTip.setMessage("This system tray icon blinks when the log viewer is updated.");
64             }
65
66             trayItem = new TrayItem(tray, SWT.NONE);
67             trayItem.setToolTipText(appTitle);
68             trayItem.setToolTip(toolTip);
69             trayItem.addListener(SWT.Selection, new Listener() {
70                 public void handleEvent(Event event) {
71                     mediator.handleEvent(ActionMediator.EVENT_DISPLAY_MAIN_SHELL);
72                 }
73             });
74
75             final Menu menu = new Menu(trayShell, SWT.POP_UP);
76             MenuItem openMenuItem = new MenuItem(menu, SWT.PUSH);
77             openMenuItem.setText("Open " + appTitle);
78             openMenuItem.addListener(SWT.Selection, new Listener() {
79                 public void handleEvent(Event event) {
80                     mediator.handleEvent(ActionMediator.EVENT_DISPLAY_MAIN_SHELL);
81                 }
82             });
83             menu.setDefaultItem(openMenuItem);
84
85             new MenuItem(menu, SWT.SEPARATOR);
86
87             MenuItem closeMenuItem = new MenuItem(menu, SWT.PUSH);
88             closeMenuItem.setText("Exit");
89             closeMenuItem.addListener(SWT.Selection, new Listener() {
90                 public void handleEvent(Event event) {
91                     tray.dispose();
92                 }
93             });
94
95             trayItem.addListener(SWT.MenuDetect, new Listener() {
96                 public void handleEvent(Event event) {
97                     menu.setVisible(true);
98                 }
99             });
100
101             trayItem.setImage(idleImage);
102
103             // position and display the tool tip
104
toolTip.setLocation(trayShell.getLocation());
105             toolTip.setVisible(true);
106
107             while (!tray.isDisposed()) {
108                 if (!display.readAndDispatch())
109                     display.sleep();
110             }
111
112             idleImage.dispose();
113             activeImage.dispose();
114             display.dispose();
115         }
116     }
117
118     private String JavaDoc getLatestSoftwareVersion() {
119         String JavaDoc latestVersion = "";
120
121         try {
122             URL JavaDoc url = new URL JavaDoc(projectProperties.getUpdateCheckerURL());
123             BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(url.openStream()));
124
125             // read only the first non-blank line for the latest version
126
while ((latestVersion = br.readLine()) != null && latestVersion.equals("")) {
127             }
128
129             // somehow the line is empty, which is strange, then just default it
130
// to current version so that it doesn't crash
131
latestVersion = (latestVersion == null || latestVersion.equals("")) ? appVersion : latestVersion.trim();
132
133             br.close();
134         }
135         // any errors, then default latest version to the current version.
136
catch (Exception JavaDoc ignored) {
137             latestVersion = appVersion;
138         }
139
140         return latestVersion;
141     }
142
143     /**
144      * Checks if the tray icon is showing active mode.
145      *
146      * @return true if it is active, false otherwise.
147      */

148     private boolean isActive() {
149         return trayItem.getImage() != null && trayItem.getImage().equals(activeImage);
150     }
151
152     /**
153      * Sets tray icon to be active mode.
154      */

155     private void setActiveImage() {
156         if (!isActive()) {
157             trayItem.setImage(activeImage);
158         }
159     }
160
161     /**
162      * Sets tray icon to be idle mode.
163      */

164     private void setIdleImage() {
165         if (isActive()) {
166             trayItem.setImage(idleImage);
167         }
168     }
169
170     public void updateTrayIcon(ModifyEvent e) {
171
172         if (e == null) {
173             return;
174         }
175
176         // has data to display
177
if (e.data == null) {
178             if (!isActive()) {
179                 setActiveImage();
180             }
181         }
182         // no more data to display
183
else if (((String JavaDoc) e.data).equals(LogViewerHandler.NO_DATA)) {
184             if (isActive()) {
185                 setIdleImage();
186             }
187         }
188     }
189 }
190
Popular Tags