1 5 6 package java.awt; 7 8 import java.util.Vector ; 9 import java.awt.peer.SystemTrayPeer; 10 import java.beans.PropertyChangeListener ; 11 import java.beans.PropertyChangeSupport ; 12 import sun.awt.AppContext; 13 import sun.awt.SunToolkit; 14 import sun.awt.HeadlessToolkit; 15 import sun.security.util.SecurityConstants; 16 17 102 public class SystemTray { 103 private static SystemTray systemTray; 104 private int currentIconID = 0; 106 transient private SystemTrayPeer peer; 107 108 112 private SystemTray() { 113 addNotify(); 114 } 115 116 142 public static SystemTray getSystemTray() { 143 checkSystemTrayAllowed(); 144 if (GraphicsEnvironment.isHeadless()) { 145 throw new HeadlessException (); 146 } 147 148 initializeSystemTrayIfNeeded(); 149 150 if (!isSupported()) { 151 throw new UnsupportedOperationException ( 152 "The system tray is not supported on the current platform."); 153 } 154 155 return systemTray; 156 } 157 158 183 public static boolean isSupported() { 184 initializeSystemTrayIfNeeded(); 185 186 if (Toolkit.getDefaultToolkit() instanceof SunToolkit) { 187 188 return ((SunToolkit)Toolkit.getDefaultToolkit()).isTraySupported(); 189 190 } else if (Toolkit.getDefaultToolkit() instanceof HeadlessToolkit) { 191 192 return ((HeadlessToolkit)Toolkit.getDefaultToolkit()).isTraySupported(); 193 } 194 return false; 195 } 196 197 218 public void add(TrayIcon trayIcon) throws AWTException { 219 if (trayIcon == null) { 220 throw new NullPointerException ("adding null TrayIcon"); 221 } 222 TrayIcon [] oldArray = null, newArray = null; 223 Vector <TrayIcon > icons = null; 224 synchronized (this) { 225 oldArray = systemTray.getTrayIcons(); 226 icons = (Vector <TrayIcon >)AppContext.getAppContext().get(TrayIcon .class); 227 if (icons == null) { 228 icons = new Vector <TrayIcon >(3); 229 AppContext.getAppContext().put(TrayIcon .class, icons); 230 231 } else if (icons.contains(trayIcon)) { 232 throw new IllegalArgumentException ("adding TrayIcon that is already added"); 233 } 234 icons.add(trayIcon); 235 newArray = systemTray.getTrayIcons(); 236 237 trayIcon.setID(++currentIconID); 238 } 239 try { 240 trayIcon.addNotify(); 241 } catch (AWTException e) { 242 icons.remove(trayIcon); 243 throw e; 244 } 245 firePropertyChange("trayIcons", oldArray, newArray); 246 } 247 248 264 public void remove(TrayIcon trayIcon) { 265 if (trayIcon == null) { 266 return; 267 } 268 TrayIcon [] oldArray = null, newArray = null; 269 synchronized (this) { 270 oldArray = systemTray.getTrayIcons(); 271 Vector <TrayIcon > icons = (Vector <TrayIcon >)AppContext.getAppContext().get(TrayIcon .class); 272 if (icons == null || !icons.remove(trayIcon)) { 274 return; 275 } 276 trayIcon.removeNotify(); 277 newArray = systemTray.getTrayIcons(); 278 } 279 firePropertyChange("trayIcons", oldArray, newArray); 280 } 281 282 301 public TrayIcon [] getTrayIcons() { 302 Vector <TrayIcon > icons = (Vector <TrayIcon >)AppContext.getAppContext().get(TrayIcon .class); 303 if (icons != null) { 304 return (TrayIcon [])icons.toArray(new TrayIcon [icons.size()]); 305 } 306 return new TrayIcon [0]; 307 } 308 309 321 public Dimension getTrayIconSize() { 322 return peer.getTrayIconSize(); 323 } 324 325 350 public synchronized void addPropertyChangeListener(String propertyName, 351 PropertyChangeListener listener) 352 { 353 if (listener == null) { 354 return; 355 } 356 getCurrentChangeSupport().addPropertyChangeListener(propertyName, listener); 357 } 358 359 374 public synchronized void removePropertyChangeListener(String propertyName, 375 PropertyChangeListener listener) 376 { 377 if (listener == null) { 378 return; 379 } 380 getCurrentChangeSupport().removePropertyChangeListener(propertyName, listener); 381 } 382 383 398 public synchronized PropertyChangeListener [] getPropertyChangeListeners(String propertyName) { 399 return getCurrentChangeSupport().getPropertyChangeListeners(propertyName); 400 } 401 402 403 406 407 417 private void firePropertyChange(String propertyName, 418 Object oldValue, Object newValue) 419 { 420 if (oldValue != null && newValue != null && oldValue.equals(newValue)) { 421 return; 422 } 423 getCurrentChangeSupport().firePropertyChange(propertyName, oldValue, newValue); 424 } 425 426 432 private synchronized PropertyChangeSupport getCurrentChangeSupport() { 433 PropertyChangeSupport changeSupport = 434 (PropertyChangeSupport )AppContext.getAppContext().get(SystemTray .class); 435 436 if (changeSupport == null) { 437 changeSupport = new PropertyChangeSupport (this); 438 AppContext.getAppContext().put(SystemTray .class, changeSupport); 439 } 440 return changeSupport; 441 } 442 443 synchronized void addNotify() { 444 if (peer == null) { 445 peer = ((SunToolkit)Toolkit.getDefaultToolkit()).createSystemTray(this); 446 } 447 } 448 449 static void checkSystemTrayAllowed() { 450 SecurityManager security = System.getSecurityManager(); 451 if (security != null) { 452 security.checkPermission(SecurityConstants.ACCESS_SYSTEM_TRAY_PERMISSION); 453 } 454 } 455 456 private static void initializeSystemTrayIfNeeded() { 457 synchronized (SystemTray .class) { 458 if (systemTray == null) { 459 systemTray = new SystemTray (); 460 } 461 } 462 } 463 } 464 | Popular Tags |