KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > custom > JTaskTrayItem


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9
10    $Log: JTaskTrayItem.java,v $
11    Revision 1.2 2004/04/19 14:34:01 bobintetley
12    Cleaned up docs and methods for JTaskTrayItem
13
14    Revision 1.1 2004/04/19 12:49:33 bobintetley
15    JTaskTray implementation (and demo), along with Frame repaint fix
16
17
18 */

19
20 package swingwtx.custom;
21
22 import swingwtx.swing.*;
23
24 import org.eclipse.swt.*;
25 import org.eclipse.swt.widgets.*;
26
27 /**
28  * Wraps up an item on the system task tray.
29  * For Win32 - the task tray, for GTK2/XFCE4 the
30  * GNOME notification area.
31  *
32  * Additionally, allows a JPopupMenu to be assigned
33  * to popup when the user right clicks on the item.
34  *
35  * An ActionEvent is fired for a left click on the
36  * tray item.
37  *
38  * @author Robin Rawson-Tetley
39  */

40 public class JTaskTrayItem extends JComponent {
41     
42     protected TrayItem ppeer = null;
43     protected Icon pIcon = null;
44     protected JPopupMenu popup = null;
45     
46     public JTaskTrayItem() { this(null); }
47     public JTaskTrayItem(Icon icon) { this(icon, ""); }
48     public JTaskTrayItem(Icon icon, String JavaDoc tooltiptext) { this(icon, tooltiptext, null); }
49     public JTaskTrayItem(Icon icon, String JavaDoc tooltiptext, JPopupMenu menu) {
50          super();
51          pIcon = icon;
52          pToolTipText = tooltiptext;
53          popup = menu;
54     }
55     
56     /**
57      * Sets the image used for the tray item
58      * @param icon The icon to display in the tray
59      */

60     public void setIcon(Icon icon) {
61         pIcon = icon;
62         final JTaskTrayItem pthis = this;
63         // Not a widget, so can't use SwingWTUtils.isSWTControlAvailable()
64
if (ppeer != null)
65             SwingUtilities.invokeSync(new Runnable JavaDoc() {
66                 public void run() {
67                     ppeer.setImage( SwingWTUtils.getSWTImageFromSwingIcon(pthis, pIcon) );
68                 }
69             });
70     }
71     
72     
73     /**
74      * Sets the tooltip used on the tray icon
75      * @param text The tooltip text
76      */

77     public void setToolTipText(String JavaDoc text) {
78         pToolTipText = text;
79         // Not a widget, so can't use SwingWTUtils.isSWTControlAvailable()
80
if (ppeer != null)
81             SwingUtilities.invokeSync(new Runnable JavaDoc() {
82                 public void run() {
83                     ppeer.setToolTipText(pToolTipText);
84                 }
85             });
86     }
87     
88     /**
89      * Returns the text used by the tray icon
90      * @return The tray icon tooltip text
91      */

92     public String JavaDoc getToolTipText() {
93         return pToolTipText;
94     }
95     
96     /**
97      * Sets the popup menu used by the tray icon.
98      * @param menu The popup
99      */

100     public void setPopupMenu(JPopupMenu menu) {
101         popup = menu;
102     }
103     
104     /**
105      * Returns the popup menu
106      * @return The popup menu used by the tray icon
107      */

108     public JPopupMenu getPopupMenu() {
109         return popup;
110     }
111     
112     /**
113      * Determines whether the tray item is visible.
114      * @param b Whether to show the tray item or not
115      */

116     public void setVisible(final boolean b) {
117         
118         final JTaskTrayItem pthis = this;
119         SwingUtilities.invokeSync(new Runnable JavaDoc() {
120             public void run() {
121
122                 if (b) {
123                     if (ppeer != null)
124                         ppeer.dispose();
125                     ppeer = new TrayItem(SwingWTUtils.getDisplay().getSystemTray(), SWT.NONE);
126                     if (pIcon != null) ppeer.setImage( SwingWTUtils.getSWTImageFromSwingIcon(pthis, pIcon) );
127                     if (!pToolTipText.equals("")) ppeer.setToolTipText(pToolTipText);
128                     registerEvents();
129                 }
130                 else
131                 {
132                     if (ppeer != null) {
133                         ppeer.dispose();
134                         ppeer = null;
135                     }
136                 }
137                 
138             }
139         });
140         
141     }
142     
143     public void registerEvents() {
144         
145         // Mapped to action listener
146
ppeer.addListener (SWT.Selection, new Listener () {
147             public void handleEvent (Event event) {
148                 processActionEvent(0);
149             }
150     });
151
152         // Used to display any popup menu we have assigned
153
ppeer.addListener (SWT.MenuDetect, new Listener () {
154             public void handleEvent (Event event) {
155                 
156                 // Work out whether we need to create the popup
157
boolean needToCreate = popup == null;
158                 if (popup.getSWTMenu() == null)
159                     needToCreate = true;
160                 if (needToCreate)
161                     // Create the popup (use the dummy shared frame for
162
// dialogs as the parent).
163
popup.setSwingWTParent( JOptionPane.getSharedFrame() );
164                 
165                 // Show the menu
166
popup.getSWTMenu().setVisible(true);
167             }
168     });
169         
170     }
171     
172     /** Display the tray item */
173     public void show() { setVisible(true); }
174     /** Hide the tray item */
175     public void hide() { setVisible(false); }
176     
177 }
178
Popular Tags