KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdic > tray > internal > impl > GnomeTrayAppletService


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.internal.impl;
22
23
24 import org.jdesktop.jdic.tray.internal.TrayAppletService;
25 import sun.awt.EmbeddedFrame;
26 import java.awt.Component JavaDoc;
27 import java.awt.Graphics JavaDoc;
28 import java.awt.peer.ComponentPeer;
29 import java.awt.Toolkit JavaDoc;
30 import java.awt.Frame JavaDoc;
31 import java.awt.Panel JavaDoc;
32 import java.awt.Dimension JavaDoc;
33 import java.lang.reflect.Method JavaDoc;
34 import java.lang.reflect.Constructor JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.Iterator JavaDoc;
37
38
39 /**
40  * The <code>GnomeTrayAppletService</code> interface is the contract for a
41  * Gnome TrayIcon implementation.
42  *
43  */

44
45
46 public class GnomeTrayAppletService implements TrayAppletService {
47
48     static HashMap JavaDoc winMap = new HashMap JavaDoc();
49     
50     EmbeddedFrame frame;
51     ComponentPeer peer;
52     Panel JavaDoc panel;
53
54     int x;
55     int y;
56     int width;
57     int height;
58     static {
59         System.loadLibrary("tray");
60         GnomeSystemTrayService.initNative(System.getProperty("java.home"));
61     }
62
63     public GnomeTrayAppletService() {
64         init();
65     }
66     
67     native long createAppletWindow();
68
69     native long getWidget(long window, int widht, int height, int x, int y);
70     native void adjustSizeHints (long window, int width, int height);
71
72     long window_id;
73    
74     EmbeddedFrame createEmbeddedFrame(long window) {
75         EmbeddedFrame ef = null;
76         String JavaDoc version = System.getProperty("java.version");
77         String JavaDoc os = System.getProperty("os.name");
78
79         // System.out.println("version = " + version);
80
// System.out.flush();
81

82         if ((version.indexOf("1.5") == -1) || (os.equals("SunOS"))) {
83             // 1.4.2 or older JVM, use MAWT !
84
long w = getWidget(window, 400, 400, 0, 0);
85             // System.out.println("Widget w = " + w);
86
Class JavaDoc clazz = null;
87
88             try {
89                 clazz = Class.forName("sun.awt.motif.MEmbeddedFrame");
90             } catch (Throwable JavaDoc e) {
91                 e.printStackTrace();
92             }
93             Constructor JavaDoc constructor = null;
94
95             try {
96                 constructor = clazz.getConstructor(new Class JavaDoc[] {int.class});
97             } catch (Throwable JavaDoc e1) {
98                 try {
99                     constructor = clazz.getConstructor(new Class JavaDoc[] {long.class});
100                 } catch (Throwable JavaDoc e2) {
101                     e1.printStackTrace();
102                 }
103             }
104             Object JavaDoc value = null;
105
106             try {
107                 value = constructor.newInstance(new Object JavaDoc[] {new Long JavaDoc(w)});
108             } catch (Throwable JavaDoc e) {
109                 e.printStackTrace();
110             }
111             ef = (EmbeddedFrame) value;
112         } else {
113             // 1.5 JVM decide on which EmbeddedFrame to use
114
Toolkit JavaDoc toolkit = Toolkit.getDefaultToolkit();
115
116             // System.out.println("toolkit = " + toolkit);
117
// System.out.flush();
118
if (toolkit instanceof sun.awt.motif.MToolkit) {
119                 Class JavaDoc clazz = null;
120
121                 try {
122                     clazz = Class.forName("sun.awt.motif.MEmbeddedFrame");
123                 } catch (Throwable JavaDoc e) {
124                     e.printStackTrace();
125                 }
126                 Constructor JavaDoc constructor = null;
127
128                 try {
129                     constructor = clazz.getConstructor(new Class JavaDoc[] {int.class});
130                 } catch (Throwable JavaDoc e1) {
131                     try {
132                         constructor = clazz.getConstructor(new Class JavaDoc[] {long.class});
133                     } catch (Throwable JavaDoc e2) {
134                         e1.printStackTrace();
135                     }
136                 }
137                 Object JavaDoc value = null;
138
139                 try {
140                     value = constructor.newInstance(new Object JavaDoc[] {new Long JavaDoc(window)});
141                 } catch (Throwable JavaDoc e) {
142                     e.printStackTrace();
143                 }
144
145                 ef = (EmbeddedFrame) value;
146
147             } else {
148                 Class JavaDoc clazz = null;
149
150                 try {
151                     clazz = Class.forName("sun.awt.X11.XEmbeddedFrame");
152                 } catch (Throwable JavaDoc e) {
153                     e.printStackTrace();
154                 }
155                 Constructor JavaDoc constructor = null;
156
157                 try {
158                     constructor = clazz.getConstructor(new Class JavaDoc[] {int.class});
159                 } catch (Throwable JavaDoc e1) {
160                     try {
161                         constructor = clazz.getConstructor(new Class JavaDoc[] {long.class});
162                     } catch (Throwable JavaDoc e2) {
163                         e1.printStackTrace();
164                     }
165                 }
166                 Object JavaDoc value = null;
167
168                 try {
169                     value = constructor.newInstance(new Object JavaDoc[] {new Long JavaDoc(window)});
170                 } catch (Throwable JavaDoc e) {
171                     e.printStackTrace();
172                 }
173
174                 ef = (EmbeddedFrame) value;
175             }
176         }
177         return ef;
178     }
179
180     void init() {
181         window_id = createAppletWindow();
182         // System.out.println("init: window " + window_id );
183

184         // Add a mapping in the window map.
185
synchronized (winMap) {
186             winMap.put(new Long JavaDoc(window_id), this);
187         }
188         frame = createEmbeddedFrame(window_id);
189         peer = frame.getPeer();
190         width = 40;
191         height = 46;
192         frame.setSize(width, height);
193         frame.setVisible(true);
194             
195     }
196
197     long getWindow() {
198         return window_id;
199     }
200
201     EmbeddedFrame getFrame() {
202         return frame;
203     }
204
205     public void add(Component JavaDoc a) {
206         frame.add(a);
207     }
208
209     public Graphics JavaDoc getGraphics() {
210         return frame.getGraphics();
211     }
212
213     public void setVisible(boolean b) {
214         frame.setVisible(b);
215     }
216
217     public void reshape(int x, int y, int width, int height) {
218         adjustSizeHints(getWindow(),width,height);
219         frame.reshape(x, y, width, height);
220     }
221
222     public ComponentPeer getPeer() {
223         return peer;
224     }
225
226     void configureWindow(int x, int y, int w, int h) {
227         this.x = x;
228         this.y = y;
229         this.width = w;
230         this.height = h;
231         frame.setSize(width, height);
232         frame.validate();
233      // System.out.println("configureWindow: frame = " + frame + " configure width = " + width + " height = " + height);
234
}
235
236     public Dimension JavaDoc getAppletSize() {
237         return new Dimension JavaDoc(width, height);
238     }
239
240     static void configureNotify(long window, int x, int y, int w, int h) {
241         GnomeTrayAppletService gas;
242
243       // System.out.println("configureNotify: window =" + window );
244
synchronized (winMap) {
245             gas = (GnomeTrayAppletService) winMap.get(new Long JavaDoc(window));
246         }
247         if (gas != null) {
248             gas.configureWindow(x, y, w, h);
249         }
250     }
251    
252     public void remove() {
253         // remove mapping in the window map.
254
synchronized (winMap) {
255             winMap.remove(new Long JavaDoc(getWindow()));
256         }
257         frame.dispose();
258         dispose(getWindow());
259     }
260     static {
261         Runtime.getRuntime().addShutdownHook(new Thread JavaDoc() {
262             public void run() {
263                 Iterator JavaDoc wins = winMap.keySet().iterator();
264                 while(wins.hasNext())
265                     dispose(((Long JavaDoc)wins.next()).longValue());
266             }
267         });
268     }
269  
270      static native void dispose(long window_id);
271 }
272
Popular Tags