1 11 package org.eclipse.swt.awt; 12 13 import java.lang.reflect.Constructor ; 14 import java.lang.reflect.Field ; 15 import java.lang.reflect.Method ; 16 17 18 import org.eclipse.swt.*; 19 import org.eclipse.swt.graphics.Rectangle; 20 import org.eclipse.swt.widgets.Shell; 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Display; 23 import org.eclipse.swt.widgets.Listener; 24 import org.eclipse.swt.widgets.Event; 25 import org.eclipse.swt.internal.*; 26 27 28 import java.awt.EventQueue ; 29 import java.awt.Canvas ; 30 import java.awt.Frame ; 31 import java.awt.Dimension ; 32 import java.awt.Toolkit ; 33 import java.awt.event.ComponentAdapter ; 34 import java.awt.event.ComponentEvent ; 35 import java.awt.event.WindowEvent ; 36 import java.awt.event.FocusEvent ; 37 38 44 public class SWT_AWT { 45 46 50 public static String embeddedFrameClass; 51 52 56 static String EMBEDDED_FRAME_KEY = "org.eclipse.swt.awt.SWT_AWT.embeddedFrame"; 57 58 static boolean loaded, swingInitialized; 59 60 static native final int getAWTHandle (Canvas canvas); 61 62 static synchronized void loadLibrary () { 63 if (loaded) return; 64 loaded = true; 65 Toolkit.getDefaultToolkit(); 66 72 try { 73 System.loadLibrary("jawt"); 74 } catch (Throwable e) {} 75 Library.loadLibrary("swt-awt"); 76 } 77 78 static synchronized void initializeSwing() { 79 if (swingInitialized) return; 80 swingInitialized = true; 81 try { 82 83 Class [] emptyClass = new Class [0]; 84 Object [] emptyObject = new Object [0]; 85 Class clazz = Class.forName("javax.swing.UIManager"); 86 Method method = clazz.getMethod("getDefaults", emptyClass); 87 if (method != null) method.invoke(clazz, emptyObject); 88 } catch (Throwable e) {} 89 } 90 91 104 public static Frame getFrame (Composite parent) { 105 if (parent == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 106 if ((parent.getStyle () & SWT.EMBEDDED) == 0) return null; 107 return (Frame )parent.getData(EMBEDDED_FRAME_KEY); 108 } 109 110 133 public static Frame new_Frame (final Composite parent) { 134 if (parent == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 135 if ((parent.getStyle () & SWT.EMBEDDED) == 0) { 136 SWT.error (SWT.ERROR_INVALID_ARGUMENT); 137 } 138 int handle = parent.handle; 139 144 Class clazz = null; 145 try { 146 String className = embeddedFrameClass != null ? embeddedFrameClass : "sun.awt.windows.WEmbeddedFrame"; 147 clazz = Class.forName(className); 148 } catch (Throwable e) { 149 SWT.error (SWT.ERROR_NOT_IMPLEMENTED, e); 150 } 151 Constructor constructor = null; 152 try { 153 constructor = clazz.getConstructor (new Class [] {int.class}); 154 } catch (Throwable e1) { 155 try { 156 constructor = clazz.getConstructor (new Class [] {long.class}); 157 } catch (Throwable e2) { 158 SWT.error (SWT.ERROR_NOT_IMPLEMENTED, e2); 159 } 160 } 161 initializeSwing (); 162 Object value = null; 163 try { 164 value = constructor.newInstance (new Object [] {new Integer (handle)}); 165 } catch (Throwable e) { 166 SWT.error (SWT.ERROR_NOT_IMPLEMENTED, e); 167 } 168 final Frame frame = (Frame ) value; 169 174 frame.addNotify(); 175 176 parent.setData(EMBEDDED_FRAME_KEY, frame); 177 178 179 final Listener shellListener = new Listener () { 180 public void handleEvent (Event e) { 181 switch (e.type) { 182 case SWT.Deiconify: 183 EventQueue.invokeLater(new Runnable () { 184 public void run () { 185 frame.dispatchEvent (new WindowEvent (frame, WindowEvent.WINDOW_DEICONIFIED)); 186 } 187 }); 188 break; 189 case SWT.Iconify: 190 EventQueue.invokeLater(new Runnable () { 191 public void run () { 192 frame.dispatchEvent (new WindowEvent (frame, WindowEvent.WINDOW_ICONIFIED)); 193 } 194 }); 195 break; 196 } 197 } 198 }; 199 Shell shell = parent.getShell (); 200 shell.addListener (SWT.Deiconify, shellListener); 201 shell.addListener (SWT.Iconify, shellListener); 202 203 208 Listener listener = new Listener () { 209 public void handleEvent (Event e) { 210 switch (e.type) { 211 case SWT.Dispose: 212 Shell shell = parent.getShell (); 213 shell.removeListener (SWT.Deiconify, shellListener); 214 shell.removeListener (SWT.Iconify, shellListener); 215 parent.setVisible(false); 216 EventQueue.invokeLater(new Runnable () { 217 public void run () { 218 frame.dispose (); 219 } 220 }); 221 break; 222 case SWT.Activate: 223 EventQueue.invokeLater(new Runnable () { 224 public void run () { 225 if (Library.JAVA_VERSION < Library.JAVA_VERSION(1, 4, 0)) { 226 frame.dispatchEvent (new WindowEvent (frame, WindowEvent.WINDOW_ACTIVATED)); 227 frame.dispatchEvent (new FocusEvent (frame, FocusEvent.FOCUS_GAINED)); 228 } else { 229 frame.dispatchEvent (new WindowEvent (frame, WindowEvent.WINDOW_ACTIVATED)); 230 frame.dispatchEvent (new WindowEvent (frame, 207 )); 231 } 232 } 233 }); 234 break; 235 case SWT.Deactivate: 236 EventQueue.invokeLater(new Runnable () { 237 public void run () { 238 if (Library.JAVA_VERSION < Library.JAVA_VERSION(1, 4, 0)) { 239 frame.dispatchEvent (new WindowEvent (frame, WindowEvent.WINDOW_DEACTIVATED)); 240 frame.dispatchEvent (new FocusEvent (frame, FocusEvent.FOCUS_LOST)); 241 } else { 242 frame.dispatchEvent (new WindowEvent (frame, 208 )); 243 frame.dispatchEvent (new WindowEvent (frame, WindowEvent.WINDOW_DEACTIVATED)); 244 } 245 } 246 }); 247 break; 248 } 249 } 250 }; 251 parent.addListener (SWT.Activate, listener); 252 parent.addListener (SWT.Deactivate, listener); 253 parent.addListener (SWT.Dispose, listener); 254 255 parent.getDisplay().asyncExec(new Runnable () { 256 public void run () { 257 if (parent.isDisposed()) return; 258 final Rectangle clientArea = parent.getClientArea(); 259 EventQueue.invokeLater(new Runnable () { 260 public void run () { 261 frame.setSize (clientArea.width, clientArea.height); 262 frame.validate (); 263 } 264 }); 265 } 266 }); 267 274 EventQueue.invokeLater(new Runnable () { 275 public void run () { 276 try { 277 Class clazz = Class.forName("sun.awt.windows.WComponentPeer"); 278 Field field = clazz.getDeclaredField("winGraphicsConfig"); 279 field.setAccessible(true); 280 field.set(frame.getPeer(), frame.getGraphicsConfiguration()); 281 } catch (Throwable e) {} 282 } 283 }); 284 return frame; 285 } 286 287 303 public static Shell new_Shell (final Display display, final Canvas parent) { 304 if (display == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 305 if (parent == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 306 int handle = 0; 307 try { 308 loadLibrary (); 309 handle = getAWTHandle (parent); 310 } catch (Throwable e) { 311 SWT.error (SWT.ERROR_NOT_IMPLEMENTED, e); 312 } 313 if (handle == 0) SWT.error (SWT.ERROR_INVALID_ARGUMENT, null, " [peer not created]"); 314 315 final Shell shell = Shell.win32_new (display, handle); 316 parent.addComponentListener(new ComponentAdapter () { 317 public void componentResized (ComponentEvent e) { 318 display.syncExec (new Runnable () { 319 public void run () { 320 Dimension dim = parent.getSize (); 321 shell.setSize (dim.width, dim.height); 322 } 323 }); 324 } 325 }); 326 shell.setVisible (true); 327 return shell; 328 } 329 } 330 | Popular Tags |