Skip to main content
summaryrefslogtreecommitdiffstats
blob: 58ed177f81997708bfbb19cc82bdc2d7daa7c3e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package org.eclipse.swt.internal.awt.win32;

/*
 * Copyright (c) 2000, 2002 IBM Corp.  All rights reserved.
 * This file is made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 */
 
import java.lang.reflect.Method;
import java.lang.reflect.Constructor;

/* Win32, SUN AWT */
import sun.awt.windows.WEmbeddedFrame;
//import sun.awt.DrawingSurface;
//import sun.awt.windows.WDrawingSurfaceInfo;

/* SWT Imports */
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.graphics.Rectangle;

/* AWT Imports */
import java.awt.Canvas;
import java.awt.Panel;
import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowEvent;
import java.awt.event.FocusEvent;

public class SWT_AWT {

public static Panel new_Panel (final Composite parent) {
	int handle = parent.handle;
	/*
	 * Some JREs have implemented the WEmbeddedFrame constructor to take an integer
	 * value for the HWND parameter and other JREs take a long for the HWND parameter.
	 * To handle this binary incompatability, we use reflection to perform the equivalent of
	 * the following line of code:
	 * 
	 * final WEmbeddedFrame frame = new WEmbeddedFrame(handle);
	 */
	Constructor constructor = null;
	try {
		constructor = WEmbeddedFrame.class.getConstructor (new Class [] {int.class});
	} catch (Exception e1) {
		try {
			constructor = WEmbeddedFrame.class.getConstructor (new Class [] {long.class});
		} catch (Exception e2) {
			SWT.error (SWT.ERROR_NOT_IMPLEMENTED, e2);
		}
	}
	 WEmbeddedFrame value = null;
	try {
		value = (WEmbeddedFrame) constructor.newInstance (new Object [] {new Integer (handle)});
	} catch (Exception e) {
		SWT.error (SWT.ERROR_NOT_IMPLEMENTED, e);
	}
	final WEmbeddedFrame frame = value;
	
	Panel panel = new Panel ();
	frame.add (panel);
	parent.addListener (SWT.Activate, new Listener () {
		public void handleEvent (Event e) {
			frame.dispatchEvent (new WindowEvent (frame, WindowEvent.WINDOW_ACTIVATED));
			frame.dispatchEvent (new FocusEvent (frame, FocusEvent.FOCUS_GAINED));
		}
	});
	parent.addListener (SWT.Deactivate, new Listener () {
		public void handleEvent (Event e) {
			frame.dispatchEvent (new WindowEvent (frame, WindowEvent.WINDOW_DEACTIVATED));
			frame.dispatchEvent (new FocusEvent (frame, FocusEvent.FOCUS_LOST));
		}
	});
	parent.getShell ().addListener (SWT.Move, new Listener () {
		public void handleEvent (Event e) {
			final Rectangle rect = parent.getClientArea ();
			frame.getToolkit ().getSystemEventQueue ().invokeLater(new Runnable () {
				public void run () {
					frame.dispatchEvent (new ComponentEvent (frame, ComponentEvent.COMPONENT_MOVED));
				}
			});
		}
	});
	parent.addListener (SWT.Resize, new Listener () {
		public void handleEvent (Event e) {
			final Rectangle rect = parent.getClientArea ();
			frame.getToolkit ().getSystemEventQueue ().invokeLater(new Runnable () {
				public void run () {
					frame.setSize (rect.width, rect.height);
					frame.validate ();
				}
			});
		}
	});
	parent.addListener (SWT.Dispose, new Listener () {
		public void handleEvent (Event event) {
			parent.setVisible(false);
			frame.dispose ();
		}
	});
	return panel;
}

public static Shell new_Shell (Display display, final Canvas parent) {
	/*
	* As of JDK 1.4, the DrawingSurface and WDrawingSurfaceInfo no longer exist
	* so that code that references these classes no longer compiles.  The fix is
	* to use refection to invoke equivalent code that is commented below.  There
	* is no fix at this time for the missing WDrawingSurfaceInfo functionality.
	*/
//	DrawingSurface ds = (DrawingSurface)parent.getPeer();
//	WDrawingSurfaceInfo wds = (WDrawingSurfaceInfo)ds.getDrawingSurfaceInfo();
//	wds.lock ();
//	int handle = (int) wds.getHWnd ();
//	wds.unlock ();
	Integer hwnd = null;
	try {
		Object ds = parent.getPeer ();
		Class drawingSurfaceClass = Class.forName ("sun.awt.DrawingSurface");
		Method method = drawingSurfaceClass.getDeclaredMethod ("getDrawingSurfaceInfo", null);
		Object wds = method.invoke (ds, null);
		Class wDrawingSurfaceClass = Class.forName ("sun.awt.windows.WDrawingSurfaceInfo");
		method = wDrawingSurfaceClass.getMethod ("lock", null);
		method.invoke (wds, null);
		method = wDrawingSurfaceClass.getMethod ("getHWnd", null);
		hwnd = (Integer) method.invoke (wds, null);
		method = wDrawingSurfaceClass.getMethod ("unlock", null);
		method.invoke (wds, null);
	} catch (Exception e) {
		SWT.error (SWT.ERROR_NOT_IMPLEMENTED, e);
	}
	int handle = hwnd.intValue();

	final Shell shell = Shell.win32_new (display, handle);
	final Display newDisplay = shell.getDisplay ();
	parent.addComponentListener(new ComponentAdapter () {
		public void componentResized (ComponentEvent e) {
			newDisplay.syncExec (new Runnable () {
				public void run () {
					Dimension dim = parent.getSize ();
					shell.setSize (dim.width, dim.height);
				}
			});
		}
	});
	shell.setVisible (true);
	return shell;
}
}

Back to the top