Revision 1828

tmp/org.txm.chartsengine.rcp/src/org/txm/chartsengine/rcp/SWTChartsComponentsProvider.java (revision 1828)
87 87
	
88 88
	static	{
89 89
		
90
		// FIXME: SJ: test to fix the Swing focus problem on the 3 OS
90
		// FIXME: SJ: test to fix the Swing focus problems on the 3 OS
91 91
		IPartService service = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService();
92 92
		service.addPartListener(new IPartListener() {
93 93
			
tmp/org.txm.chartsengine.rcp/src/org/txm/chartsengine/rcp/swt/SwingChartComposite.java (revision 1828)
20 20
import java.awt.event.WindowListener;
21 21
import java.awt.image.BufferedImage;
22 22
import java.io.IOException;
23
import java.lang.reflect.InvocationTargetException;
23 24
import java.lang.reflect.Method;
24 25
import java.util.Stack;
25 26

  
26 27
import javax.swing.JApplet;
27 28
import javax.swing.JComponent;
28 29
import javax.swing.JPanel;
30
import javax.swing.SwingUtilities;
29 31
import javax.swing.border.LineBorder;
30 32

  
31 33
import org.eclipse.swt.awt.SWT_AWT;
......
54 56
	
55 57
	
56 58
	// For reducing resize flickering on Windows
57
	static	{
58
		try {
59
			System.setProperty("sun.awt.noerasebackground", "true"); //$NON-NLS-1$ //$NON-NLS-2$
60
		}
61
		catch (NoSuchMethodError error) {
62
		}
63
	}
59
//	static	{
60
//		try {
61
//			System.setProperty("sun.awt.noerasebackground", "true"); //$NON-NLS-1$ //$NON-NLS-2$
62
//		}
63
//		catch (NoSuchMethodError error) {
64
//		}
65
//	}
64 66

  
65 67

  
66 68
	
......
236 238
				// TODO Auto-generated method stub
237 239
				Log.finest("SwingChartComposite.SwingChartComposite(...).new WindowFocusListener() {...}.windowLostFocus()");
238 240
				
241
				// Force the keep the focus if the ChartEditor has the focus
242
				SwingChartComposite.this.getDisplay().asyncExec(new Runnable() {
243
					@Override
244
					public void run() {
245
						if (Display.getCurrent().getFocusControl() == SwingChartComposite.this) {
246
							requestFocusInChartComponent();
247
						}
248
					}
249
				});
250
				
251
				
252
				
239 253
				// FIXME: For Swing focus debug tests
240 254
				if(RCPPreferences.getInstance().getBoolean(TBXPreferences.EXPERT_USER))	{
241 255
					((JComponent) rootPanel).setBorder(javax.swing.BorderFactory.createEmptyBorder());
......
255 269
				
256 270
				// TODO Auto-generated method stub
257 271
				Log.finest("SwingChartComposite.SwingChartComposite(...).new WindowFocusListener() {...}.windowGainedFocus()");
272
				
273
				
274
				SwingChartComposite.this.getDisplay().asyncExec(new Runnable() {
275
					@Override
276
					public void run() {
277
						if (Display.getCurrent().getFocusControl() == SwingChartComposite.this) {
278
							Stack<Control> stack = new Stack<Control>();
279
							Control starter = SwingChartComposite.this;
280
							Shell shell = SwingChartComposite.this.getShell();
281
							while (starter != null && !(starter instanceof Shell)) {
282
								stack.push(starter.getParent());
283
								starter = starter.getParent();
284
							}
285

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

  
305
								m = shell.getClass().getDeclaredMethod("setActiveControl", Control.class);// $NON-NLS-1$
306
								m.setAccessible(true);
307
								while (!stack.isEmpty()) {
308
									m.invoke(shell, stack.pop());
309
								}
310
								
311
								// force the Swing component focus
312
								//setFocus();
313
								
314
							}
315
							catch (Exception e) {
316
								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$
317
								Log.printStackTrace(e);
318
							}
319
							finally {
320
								if (m != null) {
321
									m.setAccessible(false);
322
								}
323
							}
324
						}
325
					}
326
				});
327
				
258 328
			}
259 329
		});
260 330
		
......
262 332
		
263 333
	}
264 334

  
335
	
336
	
265 337
	@Override
338
	public boolean setFocus() {
339

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

  
407
	
408
	
409
	
410
	@Override
266 411
	public void loadChart()	{
267 412
		
268 413
		// loads the chart from the result
......
333 478
		Toolkit.getDefaultToolkit().getSystemClipboard().setContents(img, null);
334 479
		
335 480
	}
336
	
337
	@Override
338
	public boolean setFocus() {
339 481

  
340
		if (this.isDisposed()) {
341
			return false;
342
		}
343
		
344
		// Debug
345
		Log.finest("SwingChartComposite.setFocus(): trying to give focus to SWT composite and AWT component..."); //$NON-NLS-1$
346
		
347
		boolean focusState = super.setFocus();
348
		
349
		// Debug
350
		Log.finest(TXMCoreMessages.bind("SwingChartComposite.setFocus(): SWT composite focus given state = {0}.", this.isFocusControl())); //$NON-NLS-1$
351
		
352
		this.requestFocusInChartComponent();
353
		
354
		return focusState;
355
	}
356 482
	
357
	public void requestFocusInChartComponent()	{
358
		
359
		
360
		if(chartComponent != null) {
361
		
362
		EventQueue.invokeLater(new Runnable () {
363
			public void run () {
364
				if(!((Component) chartComponent).isFocusOwner())	{
365
					
366
					//boolean focusState = ((Component) chartComponent).requestFocusInWindow();
367
					//((Component) chartComponent).requestFocus();
368
					((JComponent) chartComponent).grabFocus();
369
					
370
					// Debug
371
					Log.finest(TXMCoreMessages.bind("SwingChartComposite.requestFocusInChartComponent(): AWT component focus given state = {0}.", ((Component) chartComponent).isFocusOwner())); //$NON-NLS-1$
372
					
373
//					// FIXME: For Swing focus debug tests
374
//					if(RCPPreferences.getInstance().getBoolean(TBXPreferences.EXPERT_USER))	{
375
//						if(((Component) chartComponent).isFocusOwner())	{
376
//							((JComponent) chartComponent).setBorder(new LineBorder(Color.red, 1));
377
//						}
378
//						else	{
379
//							((JComponent) chartComponent).setBorder(javax.swing.BorderFactory.createEmptyBorder());
380
//						}
381
//					}
382
					
383
					
384
				}
385
			}
386
		});
387
		
388
		}
389
	}
390

  
391 483
	
392
	
393 484
	@Override
394 485
	public EventCallBackHandler getMouseCallBackHandler() {
395 486
		return super.getMouseCallBackHandler((Component) this.chartComponent);

Also available in: Unified diff