KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdic > tray > TrayIcon


1 /*
2  * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is
3  * subject to license terms.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the Lesser GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  */

20
21 package org.jdesktop.jdic.tray;
22
23
24 import org.jdesktop.jdic.tray.internal.ServiceManager;
25 import org.jdesktop.jdic.tray.internal.TrayIconService;
26
27 import java.awt.Point JavaDoc;
28 import javax.swing.Icon JavaDoc;
29 import javax.swing.JPopupMenu JavaDoc;
30 import java.awt.event.ActionListener JavaDoc;
31
32
33 /**
34  * The <code>TrayIcon</code> class represents a tray Icon that can be added to
35  * a System tray. A TrayIcon can have a caption (text), an Icon and a menu
36  * associated with it. In addition a TrayIcon can also have a tooltip
37  * associated that is displayed when the mouse hovers over the TrayIcon.
38  * <p>
39  * TrayIcons will display the specified Menu when a right mouse click
40  * is performed on the TrayIcon. Left button clicking will trigger
41  * ActionEvents.
42  */

43
44
45 public class TrayIcon {
46
47     TrayIconService tis;
48
49     /**
50      * TrayIcon constructor - creates a TrayIcon with the specified Icon
51      *
52      */

53     public TrayIcon(Icon JavaDoc i) {
54         this(i, null, null);
55     }
56     
57     /**
58      * TrayIcon constructor - creates a TrayIcon with the specified Icon
59      * and the specified caption.
60      * @param icon Icon
61      * @param caption caption to use
62      */

63
64     public TrayIcon(Icon JavaDoc icon, String JavaDoc caption) {
65         this(icon, caption, null);
66     }
67
68     /**
69      * TrayIcon constructor - creates a TrayIcon with the specified Icon
70      * the specified caption and Popup menu.
71      * @param icon Icon
72      * @param caption caption to use
73      * @param popup popupMenu to use
74      */

75     public TrayIcon(Icon JavaDoc icon, String JavaDoc caption, JPopupMenu JavaDoc popup) {
76         tis = (TrayIconService)
77             ServiceManager.getService(ServiceManager.TRAY_ICON_SERVICE);
78         setIcon(icon);
79         setCaption(caption);
80         setPopupMenu(popup);
81     }
82
83     /**
84      * Sets the icon for this TrayIcon.
85      *
86      * @param icon icon to use
87      */

88
89     public void setIcon(Icon JavaDoc icon) {
90         if (tis != null) {
91             tis.setIcon(icon);
92         }
93     }
94
95     /**
96      * Sets the PopupMenu for this TrayIcon.
97      *
98      * @param popup Popup
99      */

100
101     public void setPopupMenu(JPopupMenu JavaDoc popup) {
102         if (tis != null) {
103             tis.setPopupMenu(popup);
104         }
105     }
106
107     /**
108      * Sets the ToolTip string for this TrayIcon.
109      *
110      * @param tooltip
111      */

112     public void setToolTip(String JavaDoc tooltip) {
113         setCaption(tooltip);
114     }
115
116     /**
117      * Adds the specified ActionListener to the list of ActionListeners
118      * for this component.
119      *
120      * @param listener
121      */

122     public void addActionListener(ActionListener JavaDoc listener) {
123         if (tis != null) {
124             tis.addActionListener(listener);
125         }
126     }
127
128     /**
129      * Removes the specified ActionListener from the list of ActionListeners
130      * for this component.
131      *
132      * @param listener
133      */

134     public void removeActionListener(ActionListener JavaDoc listener) {
135         if (tis != null) {
136             tis.removeActionListener(listener);
137         }
138     }
139
140     /**
141      * Sets the caption for this TrayIcon.
142      *
143      * @param caption
144      */

145     public void setCaption(String JavaDoc caption) {
146         if (tis != null) {
147             tis.setCaption(caption);
148         }
149     }
150
151     /**
152      *
153      * Sets the auto-size property.
154      * Auto-size determines whether the tray icon is automatically
155      * sized to fit the actual tray size.
156      *
157      */

158     public void setIconAutoSize(boolean autosize) {
159         if (tis != null) {
160             tis.setIconAutoSize(autosize);
161         }
162     }
163
164     TrayIconService getTrayIconService() {
165         return tis;
166     }
167
168     /**
169      * Gets the location on screen for this TrayIcon
170      * @return Point
171      */

172     public Point JavaDoc getLocationOnScreen() {
173         Point JavaDoc p = null;
174
175         if (tis != null) {
176             p = tis.getLocationOnScreen();
177         }
178
179         return p;
180     }
181
182     /**
183      * Balloon Message types. Used by <code>displayMessage(String, String, int)</code>
184      * to determine what icon to display.<p>
185      */

186     /** Used for information messages. */
187     public static final int INFO_MESSAGE_TYPE = 0;
188     /** Used for error messages. */
189     public static final int ERROR_MESSAGE_TYPE = 1;
190     /** Used for warning messages. */
191     public static final int WARNING_MESSAGE_TYPE = 2;
192     /** No icon is used. */
193     public static final int NONE_MESSAGE_TYPE = 3;
194
195     /**
196      * Displays a popup message near the tray icon.
197      * The message will disappear after a timeout or if user clicks on it.
198      * <p>
199      * Either the caption or the text may be null,
200      * but an NullPointerException is thrown if both are null.
201      * <p>
202      * <b>Note</b>: Some platforms may not support showing a message.
203      * <p>
204      * @param caption the caption displayed above the text, usually in bold; may be null
205      * @param text the text displayed for the particular message; may be null
206      * @param type the type of message to be displayed:
207      * <code>INFO_MESSAGE_TYPE</code>, <code>ERROR_MESSAGE_TYPE</code>,
208      * <code>WARNING_MESSAGE_TYPE</code>, <code>NONE_MESSAGE_TYPE</code>
209      *
210      * @throws NullPointerException If both caption and text are null
211      */

212     public void displayMessage(String JavaDoc caption, String JavaDoc text, int type)throws NullPointerException JavaDoc{
213         if(caption == null && text == null)
214             throw new NullPointerException JavaDoc("Both caption and text are null.");
215         if(tis != null)
216             tis.showBalloonMessage(caption, text, type);
217     }
218     
219     /**
220      * Adds the specified ActionListener to the list of ActionListeners
221      * for this trayicon's balloon message.
222      * @param listener
223      */

224     public void addBalloonActionListener(ActionListener JavaDoc listener) {
225         if (tis != null) {
226             tis.addBalloonActionListener(listener);
227         }
228     }
229     /**
230      * Removes the specified ActionListener from the list of ActionListeners
231      * for this trayicon's balloon message.
232      *
233      * @param listener
234      */

235     public void removeBalloonActionListener(ActionListener JavaDoc listener){
236         if (tis != null) {
237             tis.removeBalloonActionListener(listener);
238         }
239     }
240 }
241
Popular Tags