Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NextTab action: allow Cycling when reaching the end #1874

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.lang.reflect.Method;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
Expand All @@ -38,9 +40,15 @@ public class TraversePageHandler extends WidgetMethodHandler {
public final Object execute(final ExecutionEvent event) {
Control focusControl = Display.getCurrent().getFocusControl();
if (focusControl != null) {
int traversal = "next".equals(methodName) ? SWT.TRAVERSE_PAGE_NEXT : SWT.TRAVERSE_PAGE_PREVIOUS; //$NON-NLS-1$
boolean forward = "next".equals(methodName); //$NON-NLS-1$
int traversal = getTraversalDirection(forward);
Control control = focusControl;
do {
if (control instanceof CTabFolder folder && isFinalItemInCTabFolder(folder, forward)) {
loopToSecondToFirstItemInCTabFolder(folder, forward);
traversal = getTraversalDirection(!forward); // we are in the second-to-last item in the given
// direction. Now, use the Traverse-event to move back by one
}
if (control.traverse(traversal))
return null;
if (control instanceof Shell)
Expand All @@ -52,6 +60,45 @@ public final Object execute(final ExecutionEvent event) {
return null;
}

private int getTraversalDirection(boolean direction) {
return direction ? SWT.TRAVERSE_PAGE_NEXT : SWT.TRAVERSE_PAGE_PREVIOUS;
}

/**
* Sets the current selection to the second-to-last item in the given direction.
*
* @param folder
* @param forward
*/
private void loopToSecondToFirstItemInCTabFolder(CTabFolder folder, boolean forward) {
if (forward) {
folder.showItem(folder.getItem(0));
folder.setSelection(1);
} else {
int itemCount = folder.getItemCount();
folder.setSelection(itemCount - 2);
}
}

/**
* {@return Returns whether the folder has currently selected the final item in
* the given direction.}
*
* @param folder the CTabFolder which we want to inspect
* @param forward whether we want to traverse forwards of backwards
*/
private boolean isFinalItemInCTabFolder(CTabFolder folder, boolean forward) {
CTabItem currentFolder = folder.getSelection();
CTabItem lastFolder = null;
if (forward) {
int itemCount = folder.getItemCount();
lastFolder = folder.getItem(itemCount - 1);
} else {
lastFolder = folder.getItem(0);
}
return currentFolder.equals(lastFolder);
}

/**
* Looks up the traverse(int) method on the given focus control.
*
Expand Down
Loading