|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2009 Tasktop Technologies and others. |
| 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 |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* Tasktop Technologies - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.mylyn.internal.discovery.ui.util; |
| 13 |
|
| 14 |
import java.util.ArrayList; |
| 15 |
import java.util.HashSet; |
| 16 |
import java.util.List; |
| 17 |
import java.util.Set; |
| 18 |
import java.util.Stack; |
| 19 |
|
| 20 |
import org.eclipse.swt.SWT; |
| 21 |
import org.eclipse.swt.custom.ScrolledComposite; |
| 22 |
import org.eclipse.swt.events.DisposeEvent; |
| 23 |
import org.eclipse.swt.events.DisposeListener; |
| 24 |
import org.eclipse.swt.events.TraverseEvent; |
| 25 |
import org.eclipse.swt.events.TraverseListener; |
| 26 |
import org.eclipse.swt.widgets.Button; |
| 27 |
import org.eclipse.swt.widgets.Composite; |
| 28 |
import org.eclipse.swt.widgets.Control; |
| 29 |
import org.eclipse.swt.widgets.Display; |
| 30 |
import org.eclipse.swt.widgets.Link; |
| 31 |
import org.eclipse.swt.widgets.Shell; |
| 32 |
import org.eclipse.swt.widgets.Text; |
| 33 |
|
| 34 |
/** |
| 35 |
* @author David Green |
| 36 |
*/ |
| 37 |
public class TraverseManager implements TraverseListener, DisposeListener { |
| 38 |
|
| 39 |
public static final String KEY_ACCEPT_FOCUS = TraverseManager.class.getName() + "#acceptFocus"; //$NON-NLS-1$ |
| 40 |
|
| 41 |
private boolean handleEvent = true; |
| 42 |
|
| 43 |
private final Composite control; |
| 44 |
|
| 45 |
private final Set<Control> managedControls = new HashSet<Control>(); |
| 46 |
|
| 47 |
public TraverseManager(Composite control) { |
| 48 |
this.control = control; |
| 49 |
} |
| 50 |
|
| 51 |
public void widgetDisposed(DisposeEvent e) { |
| 52 |
managedControls.remove(e.widget); |
| 53 |
} |
| 54 |
|
| 55 |
public void manage(Control control) { |
| 56 |
if (managedControls.add(control)) { |
| 57 |
control.addTraverseListener(this); |
| 58 |
control.addDisposeListener(this); |
| 59 |
} |
| 60 |
List<Control> focusCandidates = getFocusCandidates(null); |
| 61 |
for (Control c : focusCandidates) { |
| 62 |
if (managedControls.add(c)) { |
| 63 |
c.addTraverseListener(this); |
| 64 |
c.addDisposeListener(this); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
public void keyTraversed(TraverseEvent event) { |
| 70 |
if (!handleEvent) { |
| 71 |
return; |
| 72 |
} |
| 73 |
Control focusControl = Display.getCurrent().getFocusControl(); |
| 74 |
switch (event.detail) { |
| 75 |
case SWT.TRAVERSE_TAB_PREVIOUS: |
| 76 |
case SWT.TRAVERSE_ARROW_PREVIOUS: { |
| 77 |
List<Control> focusCandidates = getFocusCandidates(focusControl); |
| 78 |
if (focusCandidates.size() > 0) { |
| 79 |
Control nextFocus = focusCandidates.get(focusCandidates.size() - 1); |
| 80 |
if (!nextFocus.forceFocus()) { |
| 81 |
nextFocus.setFocus(); |
| 82 |
} |
| 83 |
} |
| 84 |
break; |
| 85 |
} |
| 86 |
case SWT.TRAVERSE_TAB_NEXT: |
| 87 |
case SWT.TRAVERSE_ARROW_NEXT: { |
| 88 |
List<Control> focusCandidates = getFocusCandidates(null); |
| 89 |
int i = focusControl == null ? -1 : focusCandidates.indexOf(focusControl); |
| 90 |
if (i >= 0 && (i < (focusCandidates.size() - 1))) { |
| 91 |
Control nextFocus = focusCandidates.get(i + 1); |
| 92 |
if (!nextFocus.forceFocus()) { |
| 93 |
nextFocus.setFocus(); |
| 94 |
} |
| 95 |
} |
| 96 |
break; |
| 97 |
} |
| 98 |
default: |
| 99 |
handleEvent = false; |
| 100 |
event.doit = true; |
| 101 |
Control c = control; |
| 102 |
Shell shell = c.getShell(); |
| 103 |
while (c != null) { |
| 104 |
if (c.traverse(event.detail)) { |
| 105 |
break; |
| 106 |
} |
| 107 |
if (!event.doit || control == shell) { |
| 108 |
break; |
| 109 |
} |
| 110 |
c = c.getParent(); |
| 111 |
} |
| 112 |
handleEvent = true; |
| 113 |
break; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
private List<Control> getFocusCandidates(Control stop) { |
| 118 |
List<Control> candidates = new ArrayList<Control>(20); |
| 119 |
// document order traversal (depth-first) |
| 120 |
Stack<Control> controls = new Stack<Control>(); |
| 121 |
controls.push(control); |
| 122 |
while (!controls.isEmpty()) { |
| 123 |
Control c = controls.pop(); |
| 124 |
if (c == stop) { |
| 125 |
break; |
| 126 |
} |
| 127 |
if (accept(c)) { |
| 128 |
if (c.isEnabled()) { |
| 129 |
candidates.add(c); |
| 130 |
} |
| 131 |
} else { |
| 132 |
if (c instanceof ScrolledComposite) { |
| 133 |
controls.push(((ScrolledComposite) c).getContent()); |
| 134 |
} else if (c instanceof Composite) { |
| 135 |
Control[] children = ((Composite) c).getChildren(); |
| 136 |
for (int x = children.length - 1; x >= 0; --x) { |
| 137 |
controls.push(children[x]); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
return candidates; |
| 143 |
} |
| 144 |
|
| 145 |
protected boolean accept(Control c) { |
| 146 |
Class<? extends Control> classOfControl = c.getClass(); |
| 147 |
if (classOfControl == Button.class || classOfControl == Link.class || classOfControl == Text.class) { |
| 148 |
return true; |
| 149 |
} |
| 150 |
if (c.getData(KEY_ACCEPT_FOCUS) != null) { |
| 151 |
return true; |
| 152 |
} |
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
} |