-
Notifications
You must be signed in to change notification settings - Fork 4
/
SwingControl.java
155 lines (132 loc) · 4.37 KB
/
SwingControl.java
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
/*******************************************************************************************************
*
* SwingControl.java, in ummisco.gama.java2d, is part of the source code of the GAMA modeling and simulation platform
* (v.1.8.2).
*
* (c) 2007-2022 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
********************************************************************************************************/
package ummisco.gama.java2d.swing;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JApplet;
import javax.swing.LayoutFocusTraversalPolicy;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import msi.gama.runtime.PlatformHelper;
import ummisco.gama.dev.utils.DEBUG;
import ummisco.gama.java2d.Java2DDisplaySurface;
import ummisco.gama.java2d.WorkaroundForIssue2476;
import ummisco.gama.ui.utils.WorkbenchHelper;
/**
* The Class SwingControl.
*/
public abstract class SwingControl extends Composite {
static {
DEBUG.ON();
}
/** The applet. */
JApplet applet;
/** The frame. */
Frame frame;
/** The populated. */
boolean populated = false;
/**
* Instantiates a new swing control.
*
* @param parent
* the parent
* @param style
* the style
*/
public SwingControl(final Composite parent, final int style) {
super(parent, style | ((style & SWT.BORDER) == 0 ? SWT.EMBEDDED : 0) | SWT.NO_BACKGROUND);
setLayout(new FillLayout());
addListener(SWT.Dispose, event -> EventQueue.invokeLater(() -> {
try {
frame.remove(applet);
} catch (final Exception e) {}
}));
}
@Override
public void checkWidget() {}
@Override
public boolean isFocusControl() {
boolean result = false;
try {
result = super.isFocusControl();
} catch (final Exception e) {
// Nothing. Eliminates annoying exceptions when closing Java2D displays.
}
return result;
}
/**
* Populate.
*/
protected void populate() {
if (isDisposed()) return;
if (!populated) {
populated = true;
// WorkbenchHelper.runInUI("Opening Java2D display", 400, m -> {
WorkbenchHelper.asyncRun(() -> {
frame = SWT_AWT.new_Frame(SwingControl.this);
frame.setAlwaysOnTop(false);
frame.setAutoRequestFocus(false);
// frame.setFocusable(false);
// frame.setFocusableWindowState(false);
applet = new JApplet();
// applet.setFocusable(false);
if (PlatformHelper.isWindows()) { applet.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy()); }
final Java2DDisplaySurface surface = createSwingComponent();
applet.getRootPane().getContentPane().add(surface);
WorkaroundForIssue2476.installOn(applet, surface);
frame.add(applet);
if (PlatformHelper.isMac()) {
MouseListener ml = new MouseAdapter() {
@Override
public void mouseExited(final MouseEvent e) {
if (surface.isFocusOwner() && !surface.contains(e.getPoint())) {
frame.setVisible(false);
frame.setVisible(true);
WorkbenchHelper.asyncRun(() -> getShell().forceActive());
}
}
};
applet.addMouseListener(ml);
surface.addMouseListener(ml);
}
});
// SwingControl.this.getParent().layout(true, true);
// EventQueue.invokeLater(() -> {
// WorkbenchHelper.asyncRun(() -> SwingControl.this.getParent().layout(true, true));
// });
}
}
/**
* Creates the embedded Swing component. This method is called from the AWT event thread.
*
* @return a non-null Swing component
*/
protected abstract Java2DDisplaySurface createSwingComponent();
@Override
public void setBounds(final Rectangle rect) {
setBounds(rect.x, rect.y, rect.width, rect.height);
}
/**
* Overridden to propagate the size to the embedded Swing component.
*/
@Override
public void setBounds(final int x, final int y, final int width, final int height) {
// DEBUG.OUT("-- Surface bounds set to " + x + " " + y + " | " + width + " " + height);
populate();
super.setBounds(x, y, width, height);
}
}