1 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 ; 27 import java.awt.Graphics ; 28 import java.awt.peer.ComponentPeer; 29 import java.awt.Toolkit ; 30 import java.awt.Frame ; 31 import java.awt.Panel ; 32 import java.awt.Dimension ; 33 import java.lang.reflect.Method ; 34 import java.lang.reflect.Constructor ; 35 import java.util.HashMap ; 36 import java.util.Iterator ; 37 38 39 44 45 46 public class GnomeTrayAppletService implements TrayAppletService { 47 48 static HashMap winMap = new HashMap (); 49 50 EmbeddedFrame frame; 51 ComponentPeer peer; 52 Panel 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 version = System.getProperty("java.version"); 77 String os = System.getProperty("os.name"); 78 79 82 if ((version.indexOf("1.5") == -1) || (os.equals("SunOS"))) { 83 long w = getWidget(window, 400, 400, 0, 0); 85 Class clazz = null; 87 88 try { 89 clazz = Class.forName("sun.awt.motif.MEmbeddedFrame"); 90 } catch (Throwable e) { 91 e.printStackTrace(); 92 } 93 Constructor constructor = null; 94 95 try { 96 constructor = clazz.getConstructor(new Class [] {int.class}); 97 } catch (Throwable e1) { 98 try { 99 constructor = clazz.getConstructor(new Class [] {long.class}); 100 } catch (Throwable e2) { 101 e1.printStackTrace(); 102 } 103 } 104 Object value = null; 105 106 try { 107 value = constructor.newInstance(new Object [] {new Long (w)}); 108 } catch (Throwable e) { 109 e.printStackTrace(); 110 } 111 ef = (EmbeddedFrame) value; 112 } else { 113 Toolkit toolkit = Toolkit.getDefaultToolkit(); 115 116 if (toolkit instanceof sun.awt.motif.MToolkit) { 119 Class clazz = null; 120 121 try { 122 clazz = Class.forName("sun.awt.motif.MEmbeddedFrame"); 123 } catch (Throwable e) { 124 e.printStackTrace(); 125 } 126 Constructor constructor = null; 127 128 try { 129 constructor = clazz.getConstructor(new Class [] {int.class}); 130 } catch (Throwable e1) { 131 try { 132 constructor = clazz.getConstructor(new Class [] {long.class}); 133 } catch (Throwable e2) { 134 e1.printStackTrace(); 135 } 136 } 137 Object value = null; 138 139 try { 140 value = constructor.newInstance(new Object [] {new Long (window)}); 141 } catch (Throwable e) { 142 e.printStackTrace(); 143 } 144 145 ef = (EmbeddedFrame) value; 146 147 } else { 148 Class clazz = null; 149 150 try { 151 clazz = Class.forName("sun.awt.X11.XEmbeddedFrame"); 152 } catch (Throwable e) { 153 e.printStackTrace(); 154 } 155 Constructor constructor = null; 156 157 try { 158 constructor = clazz.getConstructor(new Class [] {int.class}); 159 } catch (Throwable e1) { 160 try { 161 constructor = clazz.getConstructor(new Class [] {long.class}); 162 } catch (Throwable e2) { 163 e1.printStackTrace(); 164 } 165 } 166 Object value = null; 167 168 try { 169 value = constructor.newInstance(new Object [] {new Long (window)}); 170 } catch (Throwable 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 184 synchronized (winMap) { 186 winMap.put(new Long (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 a) { 206 frame.add(a); 207 } 208 209 public Graphics 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 } 235 236 public Dimension getAppletSize() { 237 return new Dimension (width, height); 238 } 239 240 static void configureNotify(long window, int x, int y, int w, int h) { 241 GnomeTrayAppletService gas; 242 243 synchronized (winMap) { 245 gas = (GnomeTrayAppletService) winMap.get(new Long (window)); 246 } 247 if (gas != null) { 248 gas.configureWindow(x, y, w, h); 249 } 250 } 251 252 public void remove() { 253 synchronized (winMap) { 255 winMap.remove(new Long (getWindow())); 256 } 257 frame.dispose(); 258 dispose(getWindow()); 259 } 260 static { 261 Runtime.getRuntime().addShutdownHook(new Thread () { 262 public void run() { 263 Iterator wins = winMap.keySet().iterator(); 264 while(wins.hasNext()) 265 dispose(((Long )wins.next()).longValue()); 266 } 267 }); 268 } 269 270 static native void dispose(long window_id); 271 } 272 | Popular Tags |