1 package net.sourceforge.tracelog.ui; 2 3 import java.io.BufferedReader ; 4 import java.io.InputStreamReader ; 5 import java.net.URL ; 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 Shell trayShell; 26 private Image idleImage; 27 private Image activeImage; 28 private TrayItem trayItem; 29 30 public ShellTray() { 31 super(); 32 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 latestVersion = getLatestSoftwareVersion(); 46 47 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 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 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 getLatestSoftwareVersion() { 119 String latestVersion = ""; 120 121 try { 122 URL url = new URL (projectProperties.getUpdateCheckerURL()); 123 BufferedReader br = new BufferedReader (new InputStreamReader (url.openStream())); 124 125 while ((latestVersion = br.readLine()) != null && latestVersion.equals("")) { 127 } 128 129 latestVersion = (latestVersion == null || latestVersion.equals("")) ? appVersion : latestVersion.trim(); 132 133 br.close(); 134 } 135 catch (Exception ignored) { 137 latestVersion = appVersion; 138 } 139 140 return latestVersion; 141 } 142 143 148 private boolean isActive() { 149 return trayItem.getImage() != null && trayItem.getImage().equals(activeImage); 150 } 151 152 155 private void setActiveImage() { 156 if (!isActive()) { 157 trayItem.setImage(activeImage); 158 } 159 } 160 161 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 if (e.data == null) { 178 if (!isActive()) { 179 setActiveImage(); 180 } 181 } 182 else if (((String ) e.data).equals(LogViewerHandler.NO_DATA)) { 184 if (isActive()) { 185 setIdleImage(); 186 } 187 } 188 } 189 } 190 | Popular Tags |