1 /*******************************************************************************
2 * Copyright (c) 2007 SAS Institute.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * SAS Institute - initial API and implementation
10 *******************************************************************************/
11 package org.simantics.utils.ui.internal.awt;
13 import java.awt.Component;
14 import java.awt.Container;
15 import java.awt.EventQueue;
17 import javax.swing.LayoutFocusTraversalPolicy;
20 public class EmbeddedChildFocusTraversalPolicy extends LayoutFocusTraversalPolicy {
22 private static final long serialVersionUID = -7708166698501335927L;
23 private final AwtFocusHandler awtHandler;
25 public EmbeddedChildFocusTraversalPolicy(AwtFocusHandler handler) {
26 assert handler != null;
30 public Component getComponentAfter(Container container, Component component) {
31 assert container != null;
32 assert component != null;
33 assert awtHandler != null;
34 assert EventQueue.isDispatchThread(); // On AWT event thread
36 if (component.equals(getLastComponent(container))) {
37 // Instead of cycling around to the first component, transfer to the next SWT component
38 awtHandler.transferFocusNext();
41 return super.getComponentAfter(container, component);
45 public Component getComponentBefore(Container container, Component component) {
46 assert container != null;
47 assert component != null;
48 assert awtHandler != null;
49 assert EventQueue.isDispatchThread(); // On AWT event thread
51 if (component.equals(getFirstComponent(container))) {
52 // Instead of cycling around to the last component, transfer to the previous SWT component
53 awtHandler.transferFocusPrevious();
56 return super.getComponentBefore(container, component);
60 public Component getDefaultComponent(Container container) {
61 assert container != null;
62 assert awtHandler != null;
63 assert EventQueue.isDispatchThread(); // On AWT event thread
65 // This is a hack which depends on knowledge of current JDK implementation to
66 // work. The implementation above of getComponentBefore/After
67 // properly returns null when transferring to SWT. However, the calling AWT container
68 // will then immediately try this method to find the next recipient of
69 // focus. But we don't want *any* AWT component to receive focus... it's just
70 // been transferred to SWT. So, this method must return null when AWT does
71 // not own the focus. When AWT *does* own the focus, behave normally.
72 if (awtHandler.awtHasFocus()) {
73 // System.out.println("getDefault: super");
74 return super.getDefaultComponent(container);
76 // System.out.println("getDefault: null");
81 public Component getCurrentComponent(Container container) {
82 assert container != null;
83 assert awtHandler != null;
84 assert EventQueue.isDispatchThread(); // On AWT event thread
86 Component currentAwtComponent = awtHandler.getCurrentComponent();
87 if ((currentAwtComponent != null) && container.isAncestorOf(currentAwtComponent)){
88 return currentAwtComponent;
90 return getDefaultComponent(container);