001: package net.sourceforge.tracelog.ui;
002:
003: import java.io.BufferedReader;
004: import java.io.InputStreamReader;
005: import java.net.URL;
006:
007: import net.sourceforge.tracelog.utils.Util;
008:
009: import org.eclipse.swt.SWT;
010: import org.eclipse.swt.graphics.Image;
011: import org.eclipse.swt.program.Program;
012: import org.eclipse.swt.widgets.Event;
013: import org.eclipse.swt.widgets.Listener;
014: import org.eclipse.swt.widgets.Menu;
015: import org.eclipse.swt.widgets.MenuItem;
016: import org.eclipse.swt.widgets.Shell;
017: import org.eclipse.swt.widgets.ToolTip;
018: import org.eclipse.swt.widgets.Tray;
019: import org.eclipse.swt.widgets.TrayItem;
020:
021: public class ShellTray extends AbstractWidget {
022: private Image activeImage;
023: private Image idleImage;
024: private TrayItem trayItem;
025: private Shell trayShell;
026:
027: public ShellTray() {
028: super ();
029: this .trayShell = new Shell(display);
030: this .idleImage = new Image(display, Util
031: .getOwnResource(projectProperties.getShellIconPath()));
032: this .activeImage = new Image(display, Util
033: .getOwnResource(projectProperties.getIconActive()));
034: }
035:
036: public void run() {
037: final Tray tray = display.getSystemTray();
038:
039: if (tray != null) {
040:
041: ToolTip toolTip = new ToolTip(trayShell, SWT.BALLOON
042: | SWT.ICON_INFORMATION);
043:
044: String latestVersion = getLatestSoftwareVersion();
045:
046: // has new version
047: if (!latestVersion.equals(appVersion)) {
048: toolTip.setText(appTitle + " Software Update");
049: toolTip.setAutoHide(false);
050:
051: toolTip
052: .setMessage("A new software release, "
053: + latestVersion
054: + ", is now available for download. "
055: + "The current software release is "
056: + appVersion
057: + ". "
058: + Util.LINE_BREAK
059: + Util.LINE_BREAK
060: + "Please click here to download the latest release.");
061: toolTip.addListener(SWT.Selection, new Listener() {
062: public void handleEvent(Event event) {
063: Program
064: .launch(projectProperties
065: .getUpdateURL());
066: }
067: });
068: }
069: // no updates available
070: else {
071: toolTip.setText(appTitle);
072: toolTip
073: .setMessage("This system tray icon blinks when the log viewer is updated.");
074: }
075:
076: trayItem = new TrayItem(tray, SWT.NONE);
077: trayItem.setToolTipText(appTitle);
078: trayItem.setToolTip(toolTip);
079: trayItem.addListener(SWT.Selection, new Listener() {
080: public void handleEvent(Event event) {
081: mediator
082: .handleEvent(ActionMediator.EVENT_DISPLAY_MAIN_SHELL);
083: }
084: });
085:
086: final Menu menu = new Menu(trayShell, SWT.POP_UP);
087: MenuItem openMenuItem = new MenuItem(menu, SWT.PUSH);
088: openMenuItem.setText("Open " + appTitle);
089: openMenuItem.addListener(SWT.Selection, new Listener() {
090: public void handleEvent(Event event) {
091: mediator
092: .handleEvent(ActionMediator.EVENT_DISPLAY_MAIN_SHELL);
093: }
094: });
095: menu.setDefaultItem(openMenuItem);
096:
097: new MenuItem(menu, SWT.SEPARATOR);
098:
099: MenuItem closeMenuItem = new MenuItem(menu, SWT.PUSH);
100: closeMenuItem.setText("Exit");
101: closeMenuItem.addListener(SWT.Selection, new Listener() {
102: public void handleEvent(Event event) {
103: tray.dispose();
104: }
105: });
106:
107: trayItem.addListener(SWT.MenuDetect, new Listener() {
108: public void handleEvent(Event event) {
109: menu.setVisible(true);
110: }
111: });
112:
113: trayItem.setImage(idleImage);
114:
115: // position and display the tool tip
116: toolTip.setLocation(trayShell.getLocation());
117: toolTip.setVisible(true);
118:
119: while (!tray.isDisposed()) {
120: if (!display.readAndDispatch())
121: display.sleep();
122: }
123:
124: idleImage.dispose();
125: activeImage.dispose();
126: display.dispose();
127: }
128: }
129:
130: private String getLatestSoftwareVersion() {
131: String latestVersion = "";
132: BufferedReader br = null;
133:
134: try {
135: URL url = new URL(projectProperties.getUpdateCheckerURL());
136:
137: br = new BufferedReader(new InputStreamReader(url
138: .openStream()));
139:
140: // read only the first non-blank line for the latest version
141: while ((latestVersion = br.readLine()) != null
142: && latestVersion.equals("")) {
143: }
144:
145: // somehow the line is empty, which is strange, then just default it
146: // to current version so that it doesn't crash
147: latestVersion = (latestVersion == null || latestVersion
148: .equals("")) ? appVersion : latestVersion.trim();
149:
150: }
151: // any errors, then default latest version to the current version.
152: catch (Exception ignored) {
153: latestVersion = appVersion;
154: } finally {
155: try {
156: br.close();
157: } catch (Exception ignored) {
158: }
159: }
160:
161: return latestVersion;
162: }
163:
164: // /**
165: // * Checks if the tray icon is showing active mode.
166: // *
167: // * @return true if it is active, false otherwise.
168: // */
169: // private boolean isActive() {
170: // return trayItem.getImage() != null &&
171: // trayItem.getImage().equals(activeImage);
172: // }
173: //
174: // /**
175: // * Sets tray icon to be active mode.
176: // */
177: // private void setActiveImage() {
178: // if (!isActive()) {
179: // trayItem.setImage(activeImage);
180: // }
181: // }
182: //
183: // /**
184: // * Sets tray icon to be idle mode.
185: // */
186: // private void setIdleImage() {
187: // if (isActive()) {
188: // trayItem.setImage(idleImage);
189: // }
190: // }
191: //
192: // public void updateTrayIcon(ModifyEvent e) {
193: //
194: // if (e == null) {
195: // return;
196: // }
197: //
198: // // has data to display
199: // if (e.data == null) {
200: // if (!isActive()) {
201: // setActiveImage();
202: // }
203: // }
204: // // no more data to display
205: // else if (((String) e.data).equals(LogViewerHandler.NO_DATA)) {
206: // if (isActive()) {
207: // setIdleImage();
208: // }
209: // }
210: // }
211:
212: }
|