242 *
243 * @param trayIcon the <code>TrayIcon</code> to be added
244 * @throws NullPointerException if <code>trayIcon</code> is
245 * <code>null</code>
246 * @throws IllegalArgumentException if the same instance of
247 * a <code>TrayIcon</code> is added more than once
248 * @throws AWTException if the desktop system tray is missing
249 * @see #remove(TrayIcon)
250 * @see #getSystemTray
251 * @see TrayIcon
252 * @see java.awt.Image
253 */
254 public void add(TrayIcon trayIcon) throws AWTException {
255 if (trayIcon == null) {
256 throw new NullPointerException("adding null TrayIcon");
257 }
258 TrayIcon[] oldArray = null, newArray = null;
259 Vector<TrayIcon> icons = null;
260 synchronized (this) {
261 oldArray = systemTray.getTrayIcons();
262 icons = (Vector<TrayIcon>)AppContext.getAppContext().get(TrayIcon.class);
263 if (icons == null) {
264 icons = new Vector<TrayIcon>(3);
265 AppContext.getAppContext().put(TrayIcon.class, icons);
266
267 } else if (icons.contains(trayIcon)) {
268 throw new IllegalArgumentException("adding TrayIcon that is already added");
269 }
270 icons.add(trayIcon);
271 newArray = systemTray.getTrayIcons();
272
273 trayIcon.setID(++currentIconID);
274 }
275 try {
276 trayIcon.addNotify();
277 } catch (AWTException e) {
278 icons.remove(trayIcon);
279 throw e;
280 }
281 firePropertyChange("trayIcons", oldArray, newArray);
282 }
287 *
288 * <p> All icons added by the application are automatically
289 * removed from the <code>SystemTray</code> upon application exit
290 * and also when the desktop system tray becomes unavailable.
291 *
292 * <p> If <code>trayIcon</code> is <code>null</code> or was not
293 * added to the system tray, no exception is thrown and no action
294 * is performed.
295 *
296 * @param trayIcon the <code>TrayIcon</code> to be removed
297 * @see #add(TrayIcon)
298 * @see TrayIcon
299 */
300 public void remove(TrayIcon trayIcon) {
301 if (trayIcon == null) {
302 return;
303 }
304 TrayIcon[] oldArray = null, newArray = null;
305 synchronized (this) {
306 oldArray = systemTray.getTrayIcons();
307 Vector<TrayIcon> icons = (Vector<TrayIcon>)AppContext.getAppContext().get(TrayIcon.class);
308 // TrayIcon with no peer is not contained in the array.
309 if (icons == null || !icons.remove(trayIcon)) {
310 return;
311 }
312 trayIcon.removeNotify();
313 newArray = systemTray.getTrayIcons();
314 }
315 firePropertyChange("trayIcons", oldArray, newArray);
316 }
317
318 /**
319 * Returns an array of all icons added to the tray by this
320 * application. You can't access the icons added by another
321 * application. Some browsers partition applets in different
322 * code bases into separate contexts, and establish walls between
323 * these contexts. In such a scenario, only the tray icons added
324 * from this context will be returned.
325 *
326 * <p> The returned array is a copy of the actual array and may be
327 * modified in any way without affecting the system tray. To
328 * remove a <code>TrayIcon</code> from the
329 * <code>SystemTray</code>, use the {@link
330 * #remove(TrayIcon)} method.
331 *
332 * @return an array of all tray icons added to this tray, or an
333 * empty array if none has been added
334 * @see #add(TrayIcon)
335 * @see TrayIcon
336 */
337 public TrayIcon[] getTrayIcons() {
338 Vector<TrayIcon> icons = (Vector<TrayIcon>)AppContext.getAppContext().get(TrayIcon.class);
339 if (icons != null) {
340 return icons.toArray(new TrayIcon[icons.size()]);
341 }
342 return EMPTY_TRAY_ARRAY;
343 }
344
345 /**
346 * Returns the size, in pixels, of the space that a tray icon will
347 * occupy in the system tray. Developers may use this methods to
348 * acquire the preferred size for the image property of a tray icon
349 * before it is created. For convenience, there is a similar
350 * method {@link TrayIcon#getSize} in the <code>TrayIcon</code> class.
351 *
352 * @return the default size of a tray icon, in pixels
353 * @see TrayIcon#setImageAutoSize(boolean)
354 * @see java.awt.Image
355 * @see TrayIcon#getSize()
356 */
357 public Dimension getTrayIconSize() {
|
242 *
243 * @param trayIcon the <code>TrayIcon</code> to be added
244 * @throws NullPointerException if <code>trayIcon</code> is
245 * <code>null</code>
246 * @throws IllegalArgumentException if the same instance of
247 * a <code>TrayIcon</code> is added more than once
248 * @throws AWTException if the desktop system tray is missing
249 * @see #remove(TrayIcon)
250 * @see #getSystemTray
251 * @see TrayIcon
252 * @see java.awt.Image
253 */
254 public void add(TrayIcon trayIcon) throws AWTException {
255 if (trayIcon == null) {
256 throw new NullPointerException("adding null TrayIcon");
257 }
258 TrayIcon[] oldArray = null, newArray = null;
259 Vector<TrayIcon> icons = null;
260 synchronized (this) {
261 oldArray = systemTray.getTrayIcons();
262 @SuppressWarnings("unchecked")
263 Vector<TrayIcon> tmp = (Vector<TrayIcon>)AppContext.getAppContext().get(TrayIcon.class);
264 icons = tmp;
265 if (icons == null) {
266 icons = new Vector<TrayIcon>(3);
267 AppContext.getAppContext().put(TrayIcon.class, icons);
268
269 } else if (icons.contains(trayIcon)) {
270 throw new IllegalArgumentException("adding TrayIcon that is already added");
271 }
272 icons.add(trayIcon);
273 newArray = systemTray.getTrayIcons();
274
275 trayIcon.setID(++currentIconID);
276 }
277 try {
278 trayIcon.addNotify();
279 } catch (AWTException e) {
280 icons.remove(trayIcon);
281 throw e;
282 }
283 firePropertyChange("trayIcons", oldArray, newArray);
284 }
289 *
290 * <p> All icons added by the application are automatically
291 * removed from the <code>SystemTray</code> upon application exit
292 * and also when the desktop system tray becomes unavailable.
293 *
294 * <p> If <code>trayIcon</code> is <code>null</code> or was not
295 * added to the system tray, no exception is thrown and no action
296 * is performed.
297 *
298 * @param trayIcon the <code>TrayIcon</code> to be removed
299 * @see #add(TrayIcon)
300 * @see TrayIcon
301 */
302 public void remove(TrayIcon trayIcon) {
303 if (trayIcon == null) {
304 return;
305 }
306 TrayIcon[] oldArray = null, newArray = null;
307 synchronized (this) {
308 oldArray = systemTray.getTrayIcons();
309 @SuppressWarnings("unchecked")
310 Vector<TrayIcon> icons = (Vector<TrayIcon>)AppContext.getAppContext().get(TrayIcon.class);
311 // TrayIcon with no peer is not contained in the array.
312 if (icons == null || !icons.remove(trayIcon)) {
313 return;
314 }
315 trayIcon.removeNotify();
316 newArray = systemTray.getTrayIcons();
317 }
318 firePropertyChange("trayIcons", oldArray, newArray);
319 }
320
321 /**
322 * Returns an array of all icons added to the tray by this
323 * application. You can't access the icons added by another
324 * application. Some browsers partition applets in different
325 * code bases into separate contexts, and establish walls between
326 * these contexts. In such a scenario, only the tray icons added
327 * from this context will be returned.
328 *
329 * <p> The returned array is a copy of the actual array and may be
330 * modified in any way without affecting the system tray. To
331 * remove a <code>TrayIcon</code> from the
332 * <code>SystemTray</code>, use the {@link
333 * #remove(TrayIcon)} method.
334 *
335 * @return an array of all tray icons added to this tray, or an
336 * empty array if none has been added
337 * @see #add(TrayIcon)
338 * @see TrayIcon
339 */
340 public TrayIcon[] getTrayIcons() {
341 @SuppressWarnings("unchecked")
342 Vector<TrayIcon> icons = (Vector<TrayIcon>)AppContext.getAppContext().get(TrayIcon.class);
343 if (icons != null) {
344 return icons.toArray(new TrayIcon[icons.size()]);
345 }
346 return EMPTY_TRAY_ARRAY;
347 }
348
349 /**
350 * Returns the size, in pixels, of the space that a tray icon will
351 * occupy in the system tray. Developers may use this methods to
352 * acquire the preferred size for the image property of a tray icon
353 * before it is created. For convenience, there is a similar
354 * method {@link TrayIcon#getSize} in the <code>TrayIcon</code> class.
355 *
356 * @return the default size of a tray icon, in pixels
357 * @see TrayIcon#setImageAutoSize(boolean)
358 * @see java.awt.Image
359 * @see TrayIcon#getSize()
360 */
361 public Dimension getTrayIconSize() {
|