| 1 |
package org.astrogrid.samp.gui; |
| 2 |
|
| 3 |
import java.awt.AWTException; |
| 4 |
import java.awt.Image; |
| 5 |
import java.awt.PopupMenu; |
| 6 |
import java.awt.event.ActionListener; |
| 7 |
import java.lang.reflect.Constructor; |
| 8 |
import java.lang.reflect.InvocationTargetException; |
| 9 |
import java.lang.reflect.Method; |
| 10 |
import java.util.logging.Logger; |
| 11 |
|
| 12 |
/** |
| 13 |
* Provides basic access to the windowing system's System Tray. |
| 14 |
* This is a facade for a subset of the Java 1.6 java.awt.SystemTray |
| 15 |
* functionality. When running in a J2SE1.6 JRE it will use reflection |
| 16 |
* to access the underlying classes. In an earlier JRE, it will report |
| 17 |
* lack of support. |
| 18 |
* |
| 19 |
* @author Mark Taylor |
| 20 |
* @since 20 Jul 2010 |
| 21 |
*/ |
| 22 |
public abstract class SysTray { |
| 23 |
|
| 24 |
/** |
| 25 |
* Name of system property ({@value}) to inhibit use of system tray; |
| 26 |
* if set "true" the system tray will not be used even if it is |
| 27 |
* apparently supported. */ |
| 28 |
public static final String NOTRAY_PROP = "jsamp.nosystray"; |
| 29 |
|
| 30 |
private static SysTray instance_; |
| 31 |
private static final Logger logger_ = |
| 32 |
Logger.getLogger( SysTray.class.getName() ); |
| 33 |
|
| 34 |
/** |
| 35 |
* Indicates whether system tray functionality is available. |
| 36 |
* |
| 37 |
* @return true iff the addIcon/removeIcon methods are expected to work |
| 38 |
*/ |
| 39 |
public abstract boolean isSupported(); |
| 40 |
|
| 41 |
/** |
| 42 |
* Adds an icon to the system tray. |
| 43 |
* |
| 44 |
* @param im image for display |
| 45 |
* @param tooltip tooltip text, or null |
| 46 |
* @param popup popup menu, or null |
| 47 |
* @param iconListener listener triggered when icon is activated, or null |
| 48 |
* @return tray icon object, may be used for later removal |
| 49 |
*/ |
| 50 |
public abstract Object addIcon( Image im, String tooltip, PopupMenu popup, |
| 51 |
ActionListener iconListener ) |
| 52 |
throws AWTException; |
| 53 |
|
| 54 |
/** |
| 55 |
* Removes a previously-added icon from the tray. |
| 56 |
* |
| 57 |
* @param trayIcon object obtained from a previous invocation of |
| 58 |
* addIcon |
| 59 |
*/ |
| 60 |
public abstract void removeIcon( Object trayIcon ) |
| 61 |
throws AWTException; |
| 62 |
|
| 63 |
/** |
| 64 |
* Returns an instance of this class. |
| 65 |
* |
| 66 |
* @return instance |
| 67 |
*/ |
| 68 |
public static SysTray getInstance() { |
| 69 |
if ( instance_ == null ) { |
| 70 |
String jvers = System.getProperty( "java.specification.version" ); |
| 71 |
boolean isJava6 = |
| 72 |
jvers != null && jvers.matches( "^[0-9]+\\.[0-9]+$" ) |
| 73 |
&& Double.parseDouble( jvers ) > 1.5999; |
| 74 |
if ( ! isJava6 ) { |
| 75 |
logger_.info( "Not expecting system tray support" |
| 76 |
+ " (java version < 1.6)" ); |
| 77 |
} |
| 78 |
SysTray instance; |
| 79 |
String notrayProp = System.getProperty( NOTRAY_PROP ); |
| 80 |
if ( "true".equalsIgnoreCase( notrayProp ) ) { |
| 81 |
logger_.info( "No system tray support by explicit request" |
| 82 |
+ " (" + NOTRAY_PROP + ")" ); |
| 83 |
instance = new NoSysTray(); |
| 84 |
} |
| 85 |
else { |
| 86 |
try { |
| 87 |
instance = new Java6SysTray(); |
| 88 |
} |
| 89 |
catch ( Throwable e ) { |
| 90 |
if ( isJava6 ) { |
| 91 |
logger_.info( "No system tray support: " + e ); |
| 92 |
} |
| 93 |
instance = new NoSysTray(); |
| 94 |
} |
| 95 |
} |
| 96 |
instance_ = instance; |
| 97 |
} |
| 98 |
return instance_; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Implementation which provides no system tray access. |
| 103 |
*/ |
| 104 |
private static class NoSysTray extends SysTray { |
| 105 |
public boolean isSupported() { |
| 106 |
return false; |
| 107 |
} |
| 108 |
public Object addIcon( Image im, String tooltip, PopupMenu popup, |
| 109 |
ActionListener iconListener ) { |
| 110 |
throw new UnsupportedOperationException(); |
| 111 |
} |
| 112 |
public void removeIcon( Object trayIcon ) { |
| 113 |
throw new UnsupportedOperationException(); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Implementation which provides system tray access using J2SE 1.6 classes |
| 119 |
* by reflection. |
| 120 |
*/ |
| 121 |
private static class Java6SysTray extends SysTray { |
| 122 |
private final Class systemTrayClass_; |
| 123 |
private final Method addMethod_; |
| 124 |
private final Method removeMethod_; |
| 125 |
private final Class trayIconClass_; |
| 126 |
private final Constructor trayIconConstructor_; |
| 127 |
private final Method setImageAutoSizeMethod_; |
| 128 |
private final Method addActionListenerMethod_; |
| 129 |
private final Object systemTrayInstance_; |
| 130 |
|
| 131 |
/** |
| 132 |
* Constructor. |
| 133 |
*/ |
| 134 |
Java6SysTray() throws ClassNotFoundException, IllegalAccessException, |
| 135 |
NoSuchMethodException, InvocationTargetException { |
| 136 |
systemTrayClass_ = Class.forName( "java.awt.SystemTray" ); |
| 137 |
trayIconClass_ = Class.forName( "java.awt.TrayIcon" ); |
| 138 |
addMethod_ = |
| 139 |
systemTrayClass_ |
| 140 |
.getMethod( "add", new Class[] { trayIconClass_ } ); |
| 141 |
removeMethod_ = |
| 142 |
systemTrayClass_ |
| 143 |
.getMethod( "remove", new Class[] { trayIconClass_ } ); |
| 144 |
trayIconConstructor_ = |
| 145 |
trayIconClass_ |
| 146 |
.getConstructor( new Class[] { Image.class, String.class, |
| 147 |
PopupMenu.class } ); |
| 148 |
setImageAutoSizeMethod_ = |
| 149 |
trayIconClass_ |
| 150 |
.getMethod( "setImageAutoSize", new Class[] { boolean.class } ); |
| 151 |
addActionListenerMethod_ = |
| 152 |
trayIconClass_ |
| 153 |
.getMethod( "addActionListener", |
| 154 |
new Class[] { ActionListener.class } ); |
| 155 |
boolean isSupported = |
| 156 |
Boolean.TRUE |
| 157 |
.equals( systemTrayClass_ |
| 158 |
.getMethod( "isSupported", new Class[ 0 ] ) |
| 159 |
.invoke( null, new Object[ 0 ] ) ); |
| 160 |
systemTrayInstance_ = |
| 161 |
isSupported ? systemTrayClass_ |
| 162 |
.getMethod( "getSystemTray", new Class[ 0 ] ) |
| 163 |
.invoke( null, new Object[ 0 ] ) |
| 164 |
: null; |
| 165 |
} |
| 166 |
|
| 167 |
public boolean isSupported() { |
| 168 |
return systemTrayInstance_ != null; |
| 169 |
} |
| 170 |
|
| 171 |
public Object addIcon( Image im, String tooltip, PopupMenu popup, |
| 172 |
ActionListener iconListener ) |
| 173 |
throws AWTException { |
| 174 |
try { |
| 175 |
Object trayIcon = |
| 176 |
trayIconConstructor_ |
| 177 |
.newInstance( new Object[] { im, tooltip, popup } ); |
| 178 |
setImageAutoSizeMethod_ |
| 179 |
.invoke( trayIcon, new Object[] { Boolean.TRUE } ); |
| 180 |
if ( iconListener != null ) { |
| 181 |
addActionListenerMethod_ |
| 182 |
.invoke( trayIcon, new Object[] { iconListener } ); |
| 183 |
} |
| 184 |
addMethod_.invoke( systemTrayInstance_, |
| 185 |
new Object[] { trayIcon } ); |
| 186 |
return trayIcon; |
| 187 |
} |
| 188 |
catch ( InvocationTargetException e ) { |
| 189 |
String msg = e.getCause() instanceof AWTException |
| 190 |
? e.getCause().getMessage() |
| 191 |
: "Add tray icon invocation failed"; |
| 192 |
throw (AWTException) new AWTException( msg ).initCause( e ); |
| 193 |
} |
| 194 |
catch ( Exception e ) { |
| 195 |
throw (AWTException) |
| 196 |
new AWTException( "Add tray icon invocation failed" ) |
| 197 |
.initCause( e ); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
public void removeIcon( Object trayIcon ) throws AWTException { |
| 202 |
try { |
| 203 |
removeMethod_.invoke( systemTrayInstance_, |
| 204 |
new Object[] { trayIcon } ); |
| 205 |
} |
| 206 |
catch ( Exception e ) { |
| 207 |
throw (AWTException) |
| 208 |
new AWTException( "Remove tray icon invocation failed" ) |
| 209 |
.initCause( e ); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
} |