Skip to content

Commit f16f503

Browse files
committed
Change classes with shortcut #2413
Tab change can now handle the chevron. And after reaching the last tab, it jumps to the first, if chevron doesn´t exist
1 parent 69f6e45 commit f16f503

File tree

2 files changed

+182
-7
lines changed

2 files changed

+182
-7
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2004, 2015 IBM Corporation and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* IBM Corporation - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.ui.internal.handlers;
15+
import java.lang.reflect.Method;
16+
import org.eclipse.core.commands.ExecutionEvent;
17+
import org.eclipse.swt.SWT;
18+
import org.eclipse.swt.custom.CTabFolder;
19+
import org.eclipse.swt.custom.CTabItem;
20+
import org.eclipse.swt.widgets.Control;
21+
import org.eclipse.swt.widgets.Display;
22+
import org.eclipse.swt.widgets.Shell;
23+
/**
24+
* This handler is an adaptation of the widget method handler that implements
25+
* page traversal via {@link SWT#TRAVERSE_PAGE_NEXT} and
26+
* {@link SWT#TRAVERSE_PAGE_PREVIOUS} events.
27+
*
28+
* @since 3.5
29+
*/
30+
public class TraversePageHandler extends WidgetMethodHandler {
31+
/**
32+
* The parameters for traverse(int).
33+
*/
34+
private static final Class<?>[] METHOD_PARAMETERS = { int.class };
35+
@Override
36+
public final Object execute(final ExecutionEvent event) {
37+
Control focusControl = Display.getCurrent().getFocusControl();
38+
if (focusControl != null) {
39+
boolean forward = "next".equals(methodName); //$NON-NLS-1$
40+
int traversal = getTraversalDirection(forward);
41+
Control control = focusControl;
42+
43+
do {
44+
if (control instanceof CTabFolder folder && isFinalItemInCTabFolder(folder, forward)
45+
&& !areHiddenItems(folder)) {
46+
loopToSecondToFirstItemInCTabFolder(folder, forward);
47+
traversal = getTraversalDirection(!forward); // we are in the second-to-last item in the given
48+
// direction. Now, use the Traverse-event to move back by one
49+
}
50+
if (control.traverse(traversal))
51+
return null;
52+
if (control instanceof Shell)
53+
return null;
54+
control = control.getParent();
55+
} while (control != null);
56+
}
57+
return null;
58+
}
59+
60+
private boolean areHiddenItems(CTabFolder folder) {
61+
CTabItem[] items = folder.getItems();
62+
for (CTabItem i : items) {
63+
if (!i.isShowing()) {
64+
return true;
65+
}
66+
}
67+
68+
return false;
69+
}
70+
private int getTraversalDirection(boolean direction) {
71+
return direction ? SWT.TRAVERSE_PAGE_NEXT : SWT.TRAVERSE_PAGE_PREVIOUS;
72+
}
73+
74+
/**
75+
* Sets the current selection to the second-to-last item in the given direction.
76+
*
77+
* @param folder the CTabFolder which we want to inspect
78+
* @param forward whether we want to traverse forwards of backwards
79+
*/
80+
private void loopToSecondToFirstItemInCTabFolder(CTabFolder folder, boolean forward) {
81+
if (forward) {
82+
folder.showItem(folder.getItem(0));
83+
folder.setSelection(1);
84+
} else {
85+
int itemCount = folder.getItemCount();
86+
folder.setSelection(itemCount - 2);
87+
}
88+
}
89+
90+
/**https://github.com/jannisCode/eclipse.jdt.ui.git
91+
* {@return Returns whether the folder has currently selected the final item in
92+
* the given direction.}
93+
*
94+
* @param folder the CTabFolder which we want to inspect
95+
* @param forward whether we want to traverse forwards of backwards
96+
*/
97+
private boolean isFinalItemInCTabFolder(CTabFolder folder, boolean forward) {
98+
CTabItem currentFolder = folder.getSelection();
99+
CTabItem lastFolder = null;
100+
if (forward) {
101+
int itemCount = folder.getItemCount();
102+
lastFolder = folder.getItem(itemCount - 1);
103+
} else {
104+
lastFolder = folder.getItem(0);
105+
}
106+
return currentFolder.equals(lastFolder);
107+
}
108+
109+
/**
110+
* Looks up the traverse(int) method on the given focus control.
111+
*
112+
* @return The method on the focus control; <code>null</code> if none.
113+
*/
114+
@Override
115+
protected Method getMethodToExecute() {
116+
final Control focusControl = Display.getCurrent().getFocusControl();
117+
if (focusControl != null) {
118+
try {
119+
return focusControl.getClass().getMethod("traverse", //$NON-NLS-1$
120+
METHOD_PARAMETERS);
121+
} catch (NoSuchMethodException e) {
122+
// Do nothing.
123+
}
124+
}
125+
return null;
126+
}
127+
}

bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/handlers/TraversePageHandler.java

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
* IBM Corporation - initial API and implementation
1313
*******************************************************************************/
1414
package org.eclipse.ui.internal.handlers;
15-
1615
import java.lang.reflect.Method;
16+
import java.util.Arrays;
1717
import org.eclipse.core.commands.ExecutionEvent;
1818
import org.eclipse.swt.SWT;
19+
import org.eclipse.swt.custom.CTabFolder;
20+
import org.eclipse.swt.custom.CTabItem;
1921
import org.eclipse.swt.widgets.Control;
2022
import org.eclipse.swt.widgets.Display;
2123
import org.eclipse.swt.widgets.Shell;
22-
2324
/**
2425
* This handler is an adaptation of the widget method handler that implements
2526
* page traversal via {@link SWT#TRAVERSE_PAGE_NEXT} and
@@ -28,30 +29,77 @@
2829
* @since 3.5
2930
*/
3031
public class TraversePageHandler extends WidgetMethodHandler {
31-
3232
/**
3333
* The parameters for traverse(int).
3434
*/
3535
private static final Class<?>[] METHOD_PARAMETERS = { int.class };
36-
3736
@Override
3837
public final Object execute(final ExecutionEvent event) {
3938
Control focusControl = Display.getCurrent().getFocusControl();
4039
if (focusControl != null) {
41-
int traversal = "next".equals(methodName) ? SWT.TRAVERSE_PAGE_NEXT : SWT.TRAVERSE_PAGE_PREVIOUS; //$NON-NLS-1$
40+
boolean forward = "next".equals(methodName); //$NON-NLS-1$
41+
int traversal = getTraversalDirection(forward);
4242
Control control = focusControl;
4343
do {
44+
if (control instanceof CTabFolder folder && isFinalItemInCTabFolder(folder, forward)
45+
&& !areHiddenItems(folder)) {
46+
loopToFirstOrLastItem(folder, forward);
47+
traversal = getTraversalDirection(!forward); // we are in the second-to-last item in the given
48+
// direction. Now, use the Traverse-event to move back by one
49+
}
4450
if (control.traverse(traversal))
4551
return null;
4652
if (control instanceof Shell)
4753
return null;
4854
control = control.getParent();
4955
} while (control != null);
5056
}
51-
5257
return null;
5358
}
5459

60+
private boolean areHiddenItems(CTabFolder folder) {
61+
return Arrays.stream(folder.getItems()).anyMatch(i -> !i.isShowing());
62+
}
63+
64+
private int getTraversalDirection(boolean direction) {
65+
return direction ? SWT.TRAVERSE_PAGE_NEXT : SWT.TRAVERSE_PAGE_PREVIOUS;
66+
}
67+
68+
/**
69+
* Sets the current selection to the first or last item the given direction.
70+
*
71+
* @param folder the CTabFolder which we want to inspect
72+
* @param forward whether we want to traverse forwards of backwards
73+
*/
74+
private void loopToFirstOrLastItem(CTabFolder folder, boolean forward) {
75+
if (forward) {
76+
folder.showItem(folder.getItem(0));
77+
folder.setSelection(1);
78+
} else {
79+
int itemCount = folder.getItemCount();
80+
folder.setSelection(itemCount - 2);
81+
}
82+
}
83+
84+
/**
85+
* {@return Returns whether the folder has currently selected the final item in
86+
* the given direction.}
87+
*
88+
* @param folder the CTabFolder which we want to inspect
89+
* @param forward whether we want to traverse forwards of backwards
90+
*/
91+
private boolean isFinalItemInCTabFolder(CTabFolder folder, boolean forward) {
92+
CTabItem currentFolder = folder.getSelection();
93+
CTabItem lastFolder = null;
94+
if (forward) {
95+
int itemCount = folder.getItemCount();
96+
lastFolder = folder.getItem(itemCount - 1);
97+
} else {
98+
lastFolder = folder.getItem(0);
99+
}
100+
return currentFolder.equals(lastFolder);
101+
}
102+
55103
/**
56104
* Looks up the traverse(int) method on the given focus control.
57105
*
@@ -71,4 +119,4 @@ protected Method getMethodToExecute() {
71119
return null;
72120
}
73121

74-
}
122+
}

0 commit comments

Comments
 (0)