author | dcherepanov |
Thu, 12 Feb 2009 18:24:35 +0300 | |
changeset 2453 | 35e5fab82613 |
parent 439 | 3488710b02f8 |
child 3938 | ef327bd847c0 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
2
diff
changeset
|
2 |
* Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.awt.X11; |
|
27 |
||
28 |
import java.awt.*; |
|
29 |
import java.awt.peer.SystemTrayPeer; |
|
30 |
import java.util.logging.Logger; |
|
31 |
import java.lang.reflect.Method; |
|
32 |
import java.lang.reflect.InvocationTargetException; |
|
33 |
import sun.awt.SunToolkit; |
|
34 |
import sun.awt.AppContext; |
|
35 |
||
36 |
public class XSystemTrayPeer implements SystemTrayPeer, XMSelectionListener { |
|
37 |
private static final Logger log = Logger.getLogger("sun.awt.X11.XSystemTrayPeer"); |
|
38 |
||
39 |
SystemTray target; |
|
40 |
static XSystemTrayPeer peerInstance; // there is only one SystemTray peer per application |
|
41 |
||
42 |
private volatile boolean available; |
|
43 |
private final XMSelection selection = new XMSelection("_NET_SYSTEM_TRAY"); |
|
44 |
||
45 |
private static final Method firePropertyChangeMethod = |
|
46 |
XToolkit.getMethod(SystemTray.class, "firePropertyChange", new Class[] {String.class, Object.class, Object.class}); |
|
47 |
private static final Method addNotifyMethod = XToolkit.getMethod(TrayIcon.class, "addNotify", null); |
|
48 |
private static final Method removeNotifyMethod = XToolkit.getMethod(TrayIcon.class, "removeNotify", null); |
|
49 |
||
50 |
private static final int SCREEN = 0; |
|
51 |
private static final String SYSTEM_TRAY_PROPERTY_NAME = "systemTray"; |
|
52 |
private static final XAtom _NET_SYSTEM_TRAY = XAtom.get("_NET_SYSTEM_TRAY_S" + SCREEN); |
|
53 |
private static final XAtom _XEMBED_INFO = XAtom.get("_XEMBED_INFO"); |
|
54 |
private static final XAtom _NET_SYSTEM_TRAY_OPCODE = XAtom.get("_NET_SYSTEM_TRAY_OPCODE"); |
|
55 |
private static final XAtom _NET_WM_ICON = XAtom.get("_NET_WM_ICON"); |
|
56 |
private static final long SYSTEM_TRAY_REQUEST_DOCK = 0; |
|
57 |
||
58 |
XSystemTrayPeer(SystemTray target) { |
|
59 |
this.target = target; |
|
60 |
peerInstance = this; |
|
61 |
||
62 |
selection.addSelectionListener(this); |
|
63 |
||
64 |
long selection_owner = selection.getOwner(SCREEN); |
|
65 |
available = (selection_owner != XConstants.None); |
|
66 |
||
67 |
log.fine(" check if system tray is available. selection owner: " + selection_owner); |
|
68 |
} |
|
69 |
||
70 |
public void ownerChanged(int screen, XMSelection sel, long newOwner, long data, long timestamp) { |
|
71 |
if (screen != SCREEN) { |
|
72 |
return; |
|
73 |
} |
|
74 |
if (!available) { |
|
75 |
available = true; |
|
76 |
firePropertyChange(SYSTEM_TRAY_PROPERTY_NAME, null, target); |
|
77 |
} else { |
|
78 |
removeTrayPeers(); |
|
79 |
} |
|
80 |
createTrayPeers(); |
|
81 |
} |
|
82 |
||
83 |
public void ownerDeath(int screen, XMSelection sel, long deadOwner) { |
|
84 |
if (screen != SCREEN) { |
|
85 |
return; |
|
86 |
} |
|
87 |
if (available) { |
|
88 |
available = false; |
|
89 |
firePropertyChange(SYSTEM_TRAY_PROPERTY_NAME, target, null); |
|
90 |
removeTrayPeers(); |
|
91 |
} |
|
92 |
} |
|
93 |
||
94 |
public void selectionChanged(int screen, XMSelection sel, long owner, XPropertyEvent event) { |
|
95 |
} |
|
96 |
||
97 |
public Dimension getTrayIconSize() { |
|
98 |
return new Dimension(XTrayIconPeer.TRAY_ICON_HEIGHT, XTrayIconPeer.TRAY_ICON_WIDTH); |
|
99 |
} |
|
100 |
||
101 |
boolean isAvailable() { |
|
102 |
return available; |
|
103 |
} |
|
104 |
||
105 |
void dispose() { |
|
106 |
selection.removeSelectionListener(this); |
|
107 |
} |
|
108 |
||
109 |
// *********************************************************************** |
|
110 |
// *********************************************************************** |
|
111 |
||
112 |
void addTrayIcon(XTrayIconPeer tiPeer) throws AWTException { |
|
113 |
long selection_owner = selection.getOwner(SCREEN); |
|
114 |
||
115 |
log.fine(" send SYSTEM_TRAY_REQUEST_DOCK message to owner: " + selection_owner); |
|
116 |
||
117 |
if (selection_owner == XConstants.None) { |
|
118 |
throw new AWTException("TrayIcon couldn't be displayed."); |
|
119 |
} |
|
120 |
||
121 |
long tray_window = tiPeer.getWindow(); |
|
122 |
long data[] = new long[] {XEmbedHelper.XEMBED_VERSION, XEmbedHelper.XEMBED_MAPPED}; |
|
123 |
long data_ptr = Native.card32ToData(data); |
|
124 |
||
125 |
_XEMBED_INFO.setAtomData(tray_window, data_ptr, data.length); |
|
126 |
||
127 |
sendMessage(selection_owner, SYSTEM_TRAY_REQUEST_DOCK, tray_window, 0, 0); |
|
128 |
} |
|
129 |
||
130 |
void sendMessage(long win, long msg, long data1, long data2, long data3) { |
|
131 |
XClientMessageEvent xev = new XClientMessageEvent(); |
|
132 |
||
133 |
try { |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
2
diff
changeset
|
134 |
xev.set_type(XConstants.ClientMessage); |
2 | 135 |
xev.set_window(win); |
136 |
xev.set_format(32); |
|
137 |
xev.set_message_type(_NET_SYSTEM_TRAY_OPCODE.getAtom()); |
|
138 |
xev.set_data(0, 0); |
|
139 |
xev.set_data(1, msg); |
|
140 |
xev.set_data(2, data1); |
|
141 |
xev.set_data(3, data2); |
|
142 |
xev.set_data(4, data3); |
|
143 |
||
144 |
XToolkit.awtLock(); |
|
145 |
try { |
|
146 |
XlibWrapper.XSendEvent(XToolkit.getDisplay(), win, false, |
|
439
3488710b02f8
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents:
2
diff
changeset
|
147 |
XConstants.NoEventMask, xev.pData); |
2 | 148 |
} finally { |
149 |
XToolkit.awtUnlock(); |
|
150 |
} |
|
151 |
} finally { |
|
152 |
xev.dispose(); |
|
153 |
} |
|
154 |
} |
|
155 |
||
156 |
static XSystemTrayPeer getPeerInstance() { |
|
157 |
return peerInstance; |
|
158 |
} |
|
159 |
||
160 |
private void firePropertyChange(final String propertyName, final Object oldValue, final Object newValue) { |
|
161 |
Runnable runnable = new Runnable() { |
|
162 |
public void run() { |
|
163 |
Object[] args = new Object[] {propertyName, oldValue, newValue}; |
|
164 |
invokeMethod(firePropertyChangeMethod, target, args); |
|
165 |
} |
|
166 |
}; |
|
167 |
invokeOnEachAppContext(runnable); |
|
168 |
} |
|
169 |
||
170 |
private void createTrayPeers() { |
|
171 |
invokeOnEachTrayIcon(addNotifyMethod); |
|
172 |
} |
|
173 |
||
174 |
private void removeTrayPeers() { |
|
175 |
invokeOnEachTrayIcon(removeNotifyMethod); |
|
176 |
} |
|
177 |
||
178 |
private void invokeOnEachTrayIcon(final Method method) { |
|
179 |
Runnable runnable = new Runnable() { |
|
180 |
public void run() { |
|
181 |
TrayIcon[] icons = target.getTrayIcons(); |
|
182 |
for (TrayIcon ti : icons) { |
|
183 |
invokeMethod(method, ti, (Object[]) null); |
|
184 |
} |
|
185 |
} |
|
186 |
}; |
|
187 |
invokeOnEachAppContext(runnable); |
|
188 |
} |
|
189 |
||
190 |
private void invokeMethod(Method method, Object obj, Object[] args) { |
|
191 |
try{ |
|
192 |
method.invoke(obj, args); |
|
193 |
} catch (InvocationTargetException e){ |
|
194 |
e.printStackTrace(); |
|
195 |
} catch (IllegalAccessException e) { |
|
196 |
e.printStackTrace(); |
|
197 |
} |
|
198 |
} |
|
199 |
||
200 |
private void invokeOnEachAppContext(Runnable runnable) { |
|
201 |
for (AppContext appContext : AppContext.getAppContexts()) { |
|
202 |
SunToolkit.invokeLaterOnAppContext(appContext, runnable); |
|
203 |
} |
|
204 |
} |
|
205 |
||
206 |
} |