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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
/*******************************************************************************
* Copyright (c) 2000, 2018 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
* Bruno Haible haible@ilog.fr - bug 228890
*******************************************************************************/
package org.eclipse.ui.internal.handlers;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.internal.ExceptionHandler;
/**
* Handles the cut command in both dialogs and windows. This handler is enabled
* if the focus control supports the "cut", "copy" or "paste" methods.
*
* @since 3.0
*/
public class WidgetMethodHandler extends AbstractHandler implements
IExecutableExtension {
/**
* The parameters to pass to the method this handler invokes. This handler
* always passes no parameters.
*/
public WidgetMethodHandler() {
display = Display.getCurrent();
if (display != null) {
focusListener = event -> updateEnablement();
display.addFilter(SWT.FocusIn, focusListener);
}
}
void updateEnablement() {
boolean rc = isHandled();
if (rc != isEnabled()) {
setBaseEnabled(rc);
}
}
/**
* The name of the method to be invoked by this handler. This value should
* never be <code>null</code>.
*/
protected String methodName;
private Listener focusListener;
private Display display;
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
final Method methodToExecute = getMethodToExecute();
if (methodToExecute != null) {
try {
final Control focusControl = Display.getCurrent()
.getFocusControl();
if ((focusControl instanceof Composite)
&& ((((Composite) focusControl).getStyle() & SWT.EMBEDDED) != 0)) {
/*
* Okay. Have a seat. Relax a while. This is going to be a
* bumpy ride. If it is an embedded widget, then it *might*
* be a Swing widget. At the point where this handler is
* executing, the key event is already bound to be
* swallowed. If I don't do something, then the key will be
* gone for good. So, I will try to forward the event to the
* Swing widget. Unfortunately, we can't even count on the
* Swing libraries existing, so I need to use reflection
* everywhere. And, to top it off, I need to dispatch the
* event on the Swing event queue, which means that it will
* be carried out asynchronously to the SWT event queue.
*/
try {
final Object focusComponent = getFocusComponent();
if (focusComponent != null) {
Runnable methodRunnable = () -> {
try {
methodToExecute.invoke(focusComponent);
} catch (final IllegalAccessException e1) {
// The method is protected, so do
// nothing.
} catch (final InvocationTargetException e2) {
/*
* I would like to log this exception --
* and possibly show a dialog to the
* user -- but I have to go back to the
* SWT event loop to do this. So, back
* we go....
*/
focusControl.getDisplay().asyncExec(
() -> ExceptionHandler
.getInstance()
.handleException(
new ExecutionException(
"An exception occurred while executing " //$NON-NLS-1$
+ methodToExecute
.getName(),
e2
.getTargetException())));
}
};
swingInvokeLater(methodRunnable);
}
} catch (final ClassNotFoundException e) {
// There is no Swing support, so do nothing.
} catch (final NoSuchMethodException e) {
// The API has changed, which seems amazingly unlikely.
throw new Error("Something is seriously wrong here"); //$NON-NLS-1$
}
} else {
methodToExecute.invoke(focusControl);
}
} catch (IllegalAccessException e) {
// The method is protected, so do nothing.
} catch (InvocationTargetException e) {
throw new ExecutionException(
"An exception occurred while executing " //$NON-NLS-1$
+ methodToExecute.getName(), e
.getTargetException());
}
}
return null;
}
/**
* Invoke a runnable on the swing EDT.
*
* @param methodRunnable
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
protected void swingInvokeLater(Runnable methodRunnable)
throws ClassNotFoundException, NoSuchMethodException,
IllegalAccessException, InvocationTargetException {
final Class swingUtilitiesClass = Class.forName("javax.swing.SwingUtilities"); //$NON-NLS-1$
final Method swingUtilitiesInvokeLaterMethod = swingUtilitiesClass.getMethod("invokeLater", //$NON-NLS-1$
Runnable.class);
swingUtilitiesInvokeLaterMethod.invoke(swingUtilitiesClass, methodRunnable);
}
/**
* Find the swing focus component, if it is available.
*
* @return Hopefully, the swing focus component, but it can return
* <code>null</code>.
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
protected Object getFocusComponent() throws ClassNotFoundException,
NoSuchMethodException, IllegalAccessException,
InvocationTargetException {
/*
* Before JRE 1.4, one has to use
* javax.swing.FocusManager.getCurrentManager().getFocusOwner(). Since
* JRE 1.4, one has to use
* java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager
* ().getFocusOwner(); the use of the older API would install a
* LegacyGlueFocusTraversalPolicy which causes endless recursions in
* some situations.
*/
Class keyboardFocusManagerClass = null;
try {
keyboardFocusManagerClass = Class
.forName("java.awt.KeyboardFocusManager"); //$NON-NLS-1$
} catch (ClassNotFoundException e) {
// switch to the old guy
}
if (keyboardFocusManagerClass != null) {
// Use JRE 1.4 API
final Method keyboardFocusManagerGetCurrentKeyboardFocusManagerMethod = keyboardFocusManagerClass
.getMethod("getCurrentKeyboardFocusManager"); //$NON-NLS-1$
final Object keyboardFocusManager = keyboardFocusManagerGetCurrentKeyboardFocusManagerMethod
.invoke(keyboardFocusManagerClass);
final Method keyboardFocusManagerGetFocusOwner = keyboardFocusManagerClass
.getMethod("getFocusOwner"); //$NON-NLS-1$
final Object focusComponent = keyboardFocusManagerGetFocusOwner
.invoke(keyboardFocusManager);
return focusComponent;
}
// Use JRE 1.3 API
final Class focusManagerClass = Class
.forName("javax.swing.FocusManager"); //$NON-NLS-1$
final Method focusManagerGetCurrentManagerMethod = focusManagerClass
.getMethod("getCurrentManager"); //$NON-NLS-1$
final Object focusManager = focusManagerGetCurrentManagerMethod
.invoke(focusManagerClass);
final Method focusManagerGetFocusOwner = focusManagerClass
.getMethod("getFocusOwner"); //$NON-NLS-1$
final Object focusComponent = focusManagerGetFocusOwner
.invoke(focusManager);
return focusComponent;
}
@Override
public final boolean isHandled() {
return getMethodToExecute() != null;
}
/**
* Looks up the method on the focus control.
*
* @return The method on the focus control; <code>null</code> if none.
*/
protected Method getMethodToExecute() {
Display display = Display.getCurrent();
if (display == null)
return null;
final Control focusControl = display.getFocusControl();
Method method = null;
if (focusControl != null) {
final Class clazz = focusControl.getClass();
try {
method = clazz.getMethod(methodName);
} catch (NoSuchMethodException e) {
// Fall through...
}
}
if ((method == null)
&& (focusControl instanceof Composite)
&& ((((Composite) focusControl).getStyle() & SWT.EMBEDDED) != 0)) {
/*
* We couldn't find the appropriate method on the current focus
* control. It is possible that the current focus control is an
* embedded SWT composite, which could be containing some Swing
* components. If this is the case, then we should try to pass
* through to the underlying Swing component hierarchy. Insha'allah,
* this will work.
*/
try {
final Object focusComponent = getFocusComponent();
if (focusComponent != null) {
final Class clazz = focusComponent.getClass();
try {
method = clazz.getMethod(methodName);
} catch (NoSuchMethodException e) {
// Do nothing.
}
}
} catch (final ClassNotFoundException e) {
// There is no Swing support, so do nothing.
} catch (final NoSuchMethodException e) {
// The API has changed, which seems amazingly unlikely.
throw new Error("Something is seriously wrong here"); //$NON-NLS-1$
} catch (IllegalAccessException e) {
// The API has changed, which seems amazingly unlikely.
throw new Error("Something is seriously wrong here"); //$NON-NLS-1$
} catch (InvocationTargetException e) {
// The API has changed, which seems amazingly unlikely.
throw new Error("Something is seriously wrong here"); //$NON-NLS-1$
}
}
return method;
}
@Override
public void setInitializationData(IConfigurationElement config,
String propertyName, Object data) {
// The data is really just a string (i.e., the method name).
methodName = data.toString();
}
@Override
public void dispose() {
if (display!=null && !display.isDisposed()) {
display.removeFilter(SWT.FocusIn, focusListener);
}
display = null;
focusListener = null;
}
}
|