Révision 1830

tmp/org.txm.chartsengine.rcp/src/org/txm/chartsengine/rcp/swt/SwingChartComposite.java (revision 1830)
11 11
import java.awt.datatransfer.DataFlavor;
12 12
import java.awt.datatransfer.Transferable;
13 13
import java.awt.datatransfer.UnsupportedFlavorException;
14
import java.awt.event.FocusEvent;
15
import java.awt.event.FocusListener;
14
import java.awt.event.WindowAdapter;
16 15
import java.awt.event.WindowEvent;
17
import java.awt.event.WindowFocusListener;
18
import java.awt.event.WindowListener;
19 16
import java.awt.image.BufferedImage;
20 17
import java.io.IOException;
21
import java.lang.reflect.Method;
22
import java.util.Stack;
23 18

  
24 19
import javax.swing.JApplet;
25 20
import javax.swing.JComponent;
26 21
import javax.swing.SwingUtilities;
27
import javax.swing.border.LineBorder;
28 22

  
29
import org.eclipse.swt.SWT;
30 23
import org.eclipse.swt.awt.SWT_AWT;
31 24
import org.eclipse.swt.widgets.Composite;
32
import org.eclipse.swt.widgets.Control;
33
import org.eclipse.swt.widgets.Display;
34
import org.eclipse.swt.widgets.Event;
35
import org.eclipse.swt.widgets.Shell;
36
import org.eclipse.ui.PlatformUI;
37 25
import org.txm.chartsengine.core.results.ChartResult;
38 26
import org.txm.chartsengine.rcp.IChartComponent;
39 27
import org.txm.chartsengine.rcp.editors.ChartEditor;
40 28
import org.txm.chartsengine.rcp.events.EventCallBackHandler;
41
import org.txm.core.messages.TXMCoreMessages;
42
import org.txm.core.preferences.TBXPreferences;
43
import org.txm.rcp.preferences.RCPPreferences;
44
import org.txm.utils.logger.Log;
45 29

  
46 30
/**
47 31
 * Base chart composite with Swing/AWT support.
......
55 39
	
56 40
	
57 41
	// For reducing resize flickering on Windows
58
//	static	{
59
//		try {
60
//			System.setProperty("sun.awt.noerasebackground", "true"); //$NON-NLS-1$ //$NON-NLS-2$
61
//		}
62
//		catch (NoSuchMethodError error) {
63
//		}
64
//	}
42
	static	{
43
		try {
44
			System.setProperty("sun.awt.noerasebackground", "true"); //$NON-NLS-1$ //$NON-NLS-2$
45
		}
46
		catch (NoSuchMethodError error) {
47
		}
48
	}
65 49

  
66 50

  
67 51
	
......
91 75
		//this.rootPanel = new JPanel(new BorderLayout());
92 76

  
93 77
		// FIXME: tests with Applet, see: https://www.eclipse.org/articles/article.php?file=Article-Swing-SWT-Integration/index.html
94
		JApplet applet = new JApplet(); 
78
		final JApplet applet = new JApplet(); 
95 79
		this.rootPanel = (JComponent) applet.getRootPane().getContentPane();
96 80
		
81
		
97 82
		this.frame.add(applet);
98 83
		
99 84
		//this.frame.pack();
100 85

  
101
		this.frame.setVisible(true);
86
		//this.frame.setVisible(true);
87
		//this.frame.setVisible(false);
102 88

  
103 89

  
104 90
		this.rootPanel.setBackground(Color.WHITE);
105 91
		
106 92
		
93
//		final sun.awt.EmbeddedFrame ef = (sun.awt.EmbeddedFrame)this.frame;
94
//		 		ef.addWindowListener(new WindowAdapter() {
95
//		 			public void windowActivated(WindowEvent e) {
96
//		 				ef.synthesizeWindowActivation(true);
97
//		 			}		
98
//		 		});
99
//		  		
100
//		  		SwingUtilities.invokeLater(new Runnable() {
101
//		  			public void run() {
102
//		  				applet.requestFocusInWindow();
103
//		  			}
104
//		  		});
107 105
		
108 106
		
109
		// SJ: Workaround to fix a bug with some Java version and some OS where the focus of an embedded Swing component does not delegate to SWT, eg. the Part is not activated.
110
		// see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=377104
111
		// see: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8203855
112
//		this.frame.addWindowListener(new java.awt.event.WindowAdapter() {
107
		
108
//		// SJ: Workaround to fix a bug with some Java version and some OS where the focus of an embedded Swing component does not delegate to SWT, eg. the Part is not activated.
109
//		// see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=377104
110
//		// see: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8203855
111
////		this.frame.addWindowListener(new java.awt.event.WindowAdapter() {
112
////			@Override
113
////			public void windowActivated(java.awt.event.WindowEvent e) {
114
////				SwingChartComposite.this.getDisplay().asyncExec(new Runnable() {
115
////					@Override
116
////					public void run() {
117
////						if (Display.getCurrent().getFocusControl() == SwingChartComposite.this) {
118
////							Stack<Control> stack = new Stack<Control>();
119
////							Control starter = SwingChartComposite.this;
120
////							Shell shell = SwingChartComposite.this.getShell();
121
////							while (starter != null && !(starter instanceof Shell)) {
122
////								stack.push(starter.getParent());
123
////								starter = starter.getParent();
124
////							}
125
////
126
////							Method m = null;
127
////							try {
128
////								// instead of calling the originally proposed
129
////								// workaround solution (below),
130
////								//
131
////								// Event event = new Event();
132
////								// event.display = Display.getCurrent();
133
////								// event.type = SWT.Activate;
134
////								// event.widget = stack.pop();
135
////								// event.widget.notifyListeners(SWT.Activate, event);
136
////								//
137
////								// which should but does NOT set the active
138
////								// widget/control on the shell, we had
139
////								// to call the setActiveControl method directly.
140
////								// Updating the active control on the shell is
141
////								// important so that the last active
142
////								// control, when selected again, get the proper
143
////								// activation events fired.
144
////
145
////								m = shell.getClass().getDeclaredMethod("setActiveControl", Control.class);// $NON-NLS-1$
146
////								m.setAccessible(true);
147
////								while (!stack.isEmpty()) {
148
////									m.invoke(shell, stack.pop());
149
////								}
150
////								
151
////								// force the Swing component focus
152
////								//setFocus();
153
////								
154
////							}
155
////							catch (Exception e) {
156
////								Log.severe("** Embedded part was not able to set active control on Shell. This will result in other workbench parts not getting activated."); //$NON-NLS-1$
157
////								Log.printStackTrace(e);
158
////							}
159
////							finally {
160
////								if (m != null) {
161
////									m.setAccessible(false);
162
////								}
163
////							}
164
////						}
165
////					}
166
////				});
167
////			}
168
////		});
169
//		
170
//		this.frame.addWindowListener(new WindowListener() {
171
//			
113 172
//			@Override
114
//			public void windowActivated(java.awt.event.WindowEvent e) {
173
//			public void windowOpened(WindowEvent e) {
174
//				// TODO Auto-generated method stub
175
//				
176
//			}
177
//			
178
//			@Override
179
//			public void windowIconified(WindowEvent e) {
180
//				// TODO Auto-generated method stub
181
//				
182
//			}
183
//			
184
//			@Override
185
//			public void windowDeiconified(WindowEvent e) {
186
//				// TODO Auto-generated method stub
187
//				
188
//			}
189
//			
190
//			@Override
191
//			public void windowDeactivated(WindowEvent e) {
192
//				// TODO Auto-generated method stub
193
//				Log.finest("SwingChartComposite.SwingChartComposite(...).new WindowListener() {...}.windowDeactivated()");
194
//			}
195
//			
196
//			@Override
197
//			public void windowClosing(WindowEvent e) {
198
//				// TODO Auto-generated method stub
199
//				
200
//			}
201
//			
202
//			@Override
203
//			public void windowClosed(WindowEvent e) {
204
//				// TODO Auto-generated method stub
205
//				
206
//			}
207
//			
208
//			@Override
209
//			public void windowActivated(WindowEvent e) {
210
//				// TODO Auto-generated method stub
211
//				Log.finest("SwingChartComposite.SwingChartComposite(...).new WindowListener() {...}.windowActivated()");
212
//			}
213
//		});
214
//		
215
//		
216
//		this.frame.addFocusListener(new FocusListener() {
217
//			
218
//			@Override
219
//			public void focusLost(FocusEvent e) {
220
//				// TODO Auto-generated method stub
221
//				Log.finest("SwingChartComposite.SwingChartComposite(...).new FocusListener() {...}.focusLost()");
222
//			}
223
//			
224
//			@Override
225
//			public void focusGained(FocusEvent e) {
226
//				// TODO Auto-generated method stub
227
//				Log.finest("SwingChartComposite.SwingChartComposite(...).new FocusListener() {...}.focusGained()");
228
//			}
229
//		});
230
//
231
//		
232
//		this.frame.addWindowFocusListener(new WindowFocusListener() {
233
//			
234
//			@Override
235
//			public void windowLostFocus(WindowEvent e) {
236
//				// TODO Auto-generated method stub
237
//				Log.finest("SwingChartComposite.SwingChartComposite(...).new WindowFocusListener() {...}.windowLostFocus()");
238
//				
239
//				// Force the keep the focus if the ChartEditor has the focus
240
//				if(!SwingChartComposite.this.isDisposed())	{
241
//					SwingChartComposite.this.getDisplay().asyncExec(new Runnable() {
242
//						@Override
243
//						public void run() {
244
//							if (Display.getCurrent().getFocusControl() == SwingChartComposite.this) {
245
//								Log.finest("SwingChartComposite.SwingChartComposite(...).new WindowFocusListener() {...}.windowLostFocus(): force focus in chart component.");
246
//								requestFocusInChartComponent();
247
//							}
248
//						}
249
//					});
250
//				}
251
//				
252
//				
253
//				
254
//				// FIXME: For Swing focus debug tests
255
//				if(RCPPreferences.getInstance().getBoolean(TBXPreferences.EXPERT_USER))	{
256
//					((JComponent) rootPanel).setBorder(javax.swing.BorderFactory.createEmptyBorder());
257
//				}
258
//
259
//				
260
//			}
261
//			
262
//			@Override
263
//			public void windowGainedFocus(WindowEvent e) {
264
//				
265
//				// FIXME: For Swing focus debug tests
266
//				if(RCPPreferences.getInstance().getBoolean(TBXPreferences.EXPERT_USER))	{
267
//					((JComponent) rootPanel).setBorder(new LineBorder(Color.red, 1));
268
//				}
269
//
270
//				
271
//				// TODO Auto-generated method stub
272
//				Log.finest("SwingChartComposite.SwingChartComposite(...).new WindowFocusListener() {...}.windowGainedFocus()");
273
//				
274
//				
115 275
//				SwingChartComposite.this.getDisplay().asyncExec(new Runnable() {
116 276
//					@Override
117 277
//					public void run() {
118
//						if (Display.getCurrent().getFocusControl() == SwingChartComposite.this) {
278
//						if (
279
//								Display.getCurrent().getFocusControl() == SwingChartComposite.this
280
//								//&&
281
//								//SwingChartComposite.this.chartEditor.getContextActivationToken() == null
282
////								SwingChartComposite.this.chartEditor != PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor()
283
//								) {
284
//							
285
//							Log.finest("SwingChartComposite.SwingChartComposite(...).new WindowFocusListener() {...}.windowGainedFocus(): activate the EditorPart.");
286
//							
119 287
//							Stack<Control> stack = new Stack<Control>();
120 288
//							Control starter = SwingChartComposite.this;
121 289
//							Shell shell = SwingChartComposite.this.getShell();
......
129 297
//								// instead of calling the originally proposed
130 298
//								// workaround solution (below),
131 299
//								//
132
//								// Event event = new Event();
133
//								// event.display = Display.getCurrent();
134
//								// event.type = SWT.Activate;
135
//								// event.widget = stack.pop();
136
//								// event.widget.notifyListeners(SWT.Activate, event);
300
////								 Event event = new Event();
301
////								 event.display = Display.getCurrent();
302
////								 event.type = SWT.Activate;
303
////								 event.widget = stack.pop();
304
////								 event.widget.notifyListeners(SWT.Activate, event);
137 305
//								//
138 306
//								// which should but does NOT set the active
139 307
//								// widget/control on the shell, we had
......
143 311
//								// control, when selected again, get the proper
144 312
//								// activation events fired.
145 313
//
314
//								//PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().activate(SwingChartComposite.this.chartEditor);
315
//								
146 316
//								m = shell.getClass().getDeclaredMethod("setActiveControl", Control.class);// $NON-NLS-1$
147 317
//								m.setAccessible(true);
148 318
//								while (!stack.isEmpty()) {
......
165 335
//						}
166 336
//					}
167 337
//				});
338
//				
168 339
//			}
169 340
//		});
170 341
		
171
		this.frame.addWindowListener(new WindowListener() {
172
			
173
			@Override
174
			public void windowOpened(WindowEvent e) {
175
				// TODO Auto-generated method stub
176
				
177
			}
178
			
179
			@Override
180
			public void windowIconified(WindowEvent e) {
181
				// TODO Auto-generated method stub
182
				
183
			}
184
			
185
			@Override
186
			public void windowDeiconified(WindowEvent e) {
187
				// TODO Auto-generated method stub
188
				
189
			}
190
			
191
			@Override
192
			public void windowDeactivated(WindowEvent e) {
193
				// TODO Auto-generated method stub
194
				Log.finest("SwingChartComposite.SwingChartComposite(...).new WindowListener() {...}.windowDeactivated()");
195
			}
196
			
197
			@Override
198
			public void windowClosing(WindowEvent e) {
199
				// TODO Auto-generated method stub
200
				
201
			}
202
			
203
			@Override
204
			public void windowClosed(WindowEvent e) {
205
				// TODO Auto-generated method stub
206
				
207
			}
208
			
209
			@Override
210
			public void windowActivated(WindowEvent e) {
211
				// TODO Auto-generated method stub
212
				Log.finest("SwingChartComposite.SwingChartComposite(...).new WindowListener() {...}.windowActivated()");
213
			}
214
		});
215 342
		
216 343
		
217
		this.frame.addFocusListener(new FocusListener() {
218
			
219
			@Override
220
			public void focusLost(FocusEvent e) {
221
				// TODO Auto-generated method stub
222
				Log.finest("SwingChartComposite.SwingChartComposite(...).new FocusListener() {...}.focusLost()");
223
			}
224
			
225
			@Override
226
			public void focusGained(FocusEvent e) {
227
				// TODO Auto-generated method stub
228
				Log.finest("SwingChartComposite.SwingChartComposite(...).new FocusListener() {...}.focusGained()");
229
			}
230
		});
231

  
232
		
233
		this.frame.addWindowFocusListener(new WindowFocusListener() {
234
			
235
			@Override
236
			public void windowLostFocus(WindowEvent e) {
237
				// TODO Auto-generated method stub
238
				Log.finest("SwingChartComposite.SwingChartComposite(...).new WindowFocusListener() {...}.windowLostFocus()");
239
				
240
				// Force the keep the focus if the ChartEditor has the focus
241
				if(!SwingChartComposite.this.isDisposed())	{
242
					SwingChartComposite.this.getDisplay().asyncExec(new Runnable() {
243
						@Override
244
						public void run() {
245
							if (Display.getCurrent().getFocusControl() == SwingChartComposite.this) {
246
								Log.finest("SwingChartComposite.SwingChartComposite(...).new WindowFocusListener() {...}.windowLostFocus(): force focus in chart component.");
247
								requestFocusInChartComponent();
248
							}
249
						}
250
					});
251
				}
252
				
253
				
254
				
255
				// FIXME: For Swing focus debug tests
256
				if(RCPPreferences.getInstance().getBoolean(TBXPreferences.EXPERT_USER))	{
257
					((JComponent) rootPanel).setBorder(javax.swing.BorderFactory.createEmptyBorder());
258
				}
259

  
260
				
261
			}
262
			
263
			@Override
264
			public void windowGainedFocus(WindowEvent e) {
265
				
266
				// FIXME: For Swing focus debug tests
267
				if(RCPPreferences.getInstance().getBoolean(TBXPreferences.EXPERT_USER))	{
268
					((JComponent) rootPanel).setBorder(new LineBorder(Color.red, 1));
269
				}
270

  
271
				
272
				// TODO Auto-generated method stub
273
				Log.finest("SwingChartComposite.SwingChartComposite(...).new WindowFocusListener() {...}.windowGainedFocus()");
274
				
275
				
276
				SwingChartComposite.this.getDisplay().asyncExec(new Runnable() {
277
					@Override
278
					public void run() {
279
						if (
280
								Display.getCurrent().getFocusControl() == SwingChartComposite.this
281
//								&&
282
//								SwingChartComposite.this.chartEditor != PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor()
283
								) {
284
							
285
							Log.finest("SwingChartComposite.SwingChartComposite(...).new WindowFocusListener() {...}.windowGainedFocus(): activate the EditorPart.");
286
							
287
							Stack<Control> stack = new Stack<Control>();
288
							Control starter = SwingChartComposite.this;
289
							Shell shell = SwingChartComposite.this.getShell();
290
							while (starter != null && !(starter instanceof Shell)) {
291
								stack.push(starter.getParent());
292
								starter = starter.getParent();
293
							}
294

  
295
							Method m = null;
296
							try {
297
								// instead of calling the originally proposed
298
								// workaround solution (below),
299
								//
300
//								 Event event = new Event();
301
//								 event.display = Display.getCurrent();
302
//								 event.type = SWT.Activate;
303
//								 event.widget = stack.pop();
304
//								 event.widget.notifyListeners(SWT.Activate, event);
305
								//
306
								// which should but does NOT set the active
307
								// widget/control on the shell, we had
308
								// to call the setActiveControl method directly.
309
								// Updating the active control on the shell is
310
								// important so that the last active
311
								// control, when selected again, get the proper
312
								// activation events fired.
313

  
314
								//PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().activate(SwingChartComposite.this.chartEditor);
315
								
316
								m = shell.getClass().getDeclaredMethod("setActiveControl", Control.class);// $NON-NLS-1$
317
								m.setAccessible(true);
318
								while (!stack.isEmpty()) {
319
									m.invoke(shell, stack.pop());
320
								}
321
								
322
								// force the Swing component focus
323
								//setFocus();
324
								
325
							}
326
							catch (Exception e) {
327
								Log.severe("** Embedded part was not able to set active control on Shell. This will result in other workbench parts not getting activated."); //$NON-NLS-1$
328
								Log.printStackTrace(e);
329
							}
330
							finally {
331
								if (m != null) {
332
									m.setAccessible(false);
333
								}
334
							}
335
						}
336
					}
337
				});
338
				
339
			}
340
		});
341
		
342
		
343
		
344 344
	}
345 345

  
346 346
	
347
	
348
	@Override
349
	public boolean setFocus() {
350

  
351
		if (this.isDisposed()) {
352
			return false;
353
		}
354
		
355
		// Debug
356
		Log.finest("SwingChartComposite.setFocus(): trying to give focus to SWT composite and AWT component..."); //$NON-NLS-1$
357
		
358
		boolean focusState = super.setFocus();
359
		
360
		// Debug
361
		Log.finest(TXMCoreMessages.bind("SwingChartComposite.setFocus(): SWT composite focus given state = {0}.", this.isFocusControl())); //$NON-NLS-1$
362
		
363
		this.requestFocusInChartComponent();
364
		
365
		return focusState;
366
	}
367
	
368
	
369
	
370
	/**
371
	 * Requests the focus in chart component.
372
	 */
373
	public void requestFocusInChartComponent()	{
374
		
375
		
376
		if(chartComponent != null) {
377
		
378
//		try {
379
			SwingUtilities.invokeLater(new Runnable () {
380
				public void run () {
381
//				if(!((Component) chartComponent).isFocusOwner())	{
382
						
383
						//((Component) chartComponent).requestFocus();
384
						((JComponent) chartComponent).grabFocus();
385
						boolean focusState = ((Component) chartComponent).requestFocusInWindow();
386
						
387
						// Debug
388
						//Log.finest(TXMCoreMessages.bind("SwingChartComposite.requestFocusInChartComponent(): AWT component focus given state = {0}.", ((Component) chartComponent).isFocusOwner())); //$NON-NLS-1$
389
						Log.finest(TXMCoreMessages.bind("SwingChartComposite.requestFocusInChartComponent(): AWT component focus given state = {0}.", focusState)); //$NON-NLS-1$
390
						
391
//					// FIXME: For Swing focus debug tests
392
//					if(RCPPreferences.getInstance().getBoolean(TBXPreferences.EXPERT_USER))	{
393
//						if(((Component) chartComponent).isFocusOwner())	{
394
//							((JComponent) chartComponent).setBorder(new LineBorder(Color.red, 1));
395
//						}
396
//						else	{
397
//							((JComponent) chartComponent).setBorder(javax.swing.BorderFactory.createEmptyBorder());
398
//						}
399
//					}
400
						
401
						
402
//			}
403
				}
404
			});
405
	//	}
406
//		catch (InvocationTargetException e) {
407
//			// TODO Auto-generated catch block
408
//			e.printStackTrace();
347
//	
348
//	@Override
349
//	public boolean setFocus() {
350
//
351
//		if (this.isDisposed()) {
352
//			return false;
409 353
//		}
410
//		catch (InterruptedException e) {
411
//			// TODO Auto-generated catch block
412
//			e.printStackTrace();
354
//		
355
//		// Debug
356
//		Log.finest("SwingChartComposite.setFocus(): trying to give focus to SWT composite and AWT component..."); //$NON-NLS-1$
357
//		
358
//		boolean focusState = super.setFocus();
359
//		
360
//		// Debug
361
//		Log.finest(TXMCoreMessages.bind("SwingChartComposite.setFocus(): SWT composite focus given state = {0}.", this.isFocusControl())); //$NON-NLS-1$
362
//		
363
//		this.requestFocusInChartComponent();
364
//		
365
//		return focusState;
366
//	}
367
//	
368
//	
369
//	
370
//	/**
371
//	 * Requests the focus in chart component.
372
//	 */
373
//	public void requestFocusInChartComponent()	{
374
//		
375
//		
376
//		if(chartComponent != null) {
377
//		
378
////		try {
379
//			SwingUtilities.invokeLater(new Runnable () {
380
//				public void run () {
381
////				if(!((Component) chartComponent).isFocusOwner())	{
382
//						
383
//						//((Component) chartComponent).requestFocus();
384
//						((JComponent) chartComponent).grabFocus();
385
//						boolean focusState = ((Component) chartComponent).requestFocusInWindow();
386
//						
387
//						// Debug
388
//						//Log.finest(TXMCoreMessages.bind("SwingChartComposite.requestFocusInChartComponent(): AWT component focus given state = {0}.", ((Component) chartComponent).isFocusOwner())); //$NON-NLS-1$
389
//						Log.finest(TXMCoreMessages.bind("SwingChartComposite.requestFocusInChartComponent(): AWT component focus given state = {0}.", focusState)); //$NON-NLS-1$
390
//						
391
////					// FIXME: For Swing focus debug tests
392
////					if(RCPPreferences.getInstance().getBoolean(TBXPreferences.EXPERT_USER))	{
393
////						if(((Component) chartComponent).isFocusOwner())	{
394
////							((JComponent) chartComponent).setBorder(new LineBorder(Color.red, 1));
395
////						}
396
////						else	{
397
////							((JComponent) chartComponent).setBorder(javax.swing.BorderFactory.createEmptyBorder());
398
////						}
399
////					}
400
//						
401
//						
402
////			}
403
//				}
404
//			});
405
//	//	}
406
////		catch (InvocationTargetException e) {
407
////			// TODO Auto-generated catch block
408
////			e.printStackTrace();
409
////		}
410
////		catch (InterruptedException e) {
411
////			// TODO Auto-generated catch block
412
////			e.printStackTrace();
413
////		}
414
//		
413 415
//		}
414
		
415
		}
416
	}
416
//	}
417 417

  
418 418
	
419 419
	

Formats disponibles : Unified diff