1616import java .lang .reflect .Method ;
1717import org .eclipse .core .commands .ExecutionEvent ;
1818import org .eclipse .swt .SWT ;
19+ import org .eclipse .swt .custom .CTabFolder ;
20+ import org .eclipse .swt .custom .CTabItem ;
1921import org .eclipse .swt .widgets .Control ;
2022import org .eclipse .swt .widgets .Display ;
2123import org .eclipse .swt .widgets .Shell ;
@@ -38,9 +40,15 @@ public class TraversePageHandler extends WidgetMethodHandler {
3840 public final Object execute (final ExecutionEvent event ) {
3941 Control focusControl = Display .getCurrent ().getFocusControl ();
4042 if (focusControl != null ) {
41- int traversal = "next" .equals (methodName ) ? SWT .TRAVERSE_PAGE_NEXT : SWT .TRAVERSE_PAGE_PREVIOUS ; //$NON-NLS-1$
43+ boolean forward = "next" .equals (methodName ); //$NON-NLS-1$
44+ int traversal = getTraversalDirection (forward );
4245 Control control = focusControl ;
4346 do {
47+ if (control instanceof CTabFolder folder && isFinalItemInCTabFolder (folder , forward )) {
48+ loopToSecondToFirstItemInCTabFolder (folder , forward );
49+ traversal = getTraversalDirection (!forward ); // we are in the second-to-last item in the given
50+ // direction. Now, use the Traverse-event to move back by one
51+ }
4452 if (control .traverse (traversal ))
4553 return null ;
4654 if (control instanceof Shell )
@@ -52,6 +60,45 @@ public final Object execute(final ExecutionEvent event) {
5260 return null ;
5361 }
5462
63+ private int getTraversalDirection (boolean direction ) {
64+ return direction ? SWT .TRAVERSE_PAGE_NEXT : SWT .TRAVERSE_PAGE_PREVIOUS ;
65+ }
66+
67+ /**
68+ * Sets the current selection to the second-to-last item in the given direction.
69+ *
70+ * @param folder
71+ * @param forward
72+ */
73+ private void loopToSecondToFirstItemInCTabFolder (CTabFolder folder , boolean forward ) {
74+ if (forward ) {
75+ folder .showItem (folder .getItem (0 ));
76+ folder .setSelection (1 );
77+ } else {
78+ int itemCount = folder .getItemCount ();
79+ folder .setSelection (itemCount - 2 );
80+ }
81+ }
82+
83+ /**
84+ * {@return Returns whether the folder has currently selected the final item in
85+ * the given direction.}
86+ *
87+ * @param folder the CTabFolder which we want to inspect
88+ * @param forward whether we want to traverse forwards of backwards
89+ */
90+ private boolean isFinalItemInCTabFolder (CTabFolder folder , boolean forward ) {
91+ CTabItem currentFolder = folder .getSelection ();
92+ CTabItem lastFolder = null ;
93+ if (forward ) {
94+ int itemCount = folder .getItemCount ();
95+ lastFolder = folder .getItem (itemCount - 1 );
96+ } else {
97+ lastFolder = folder .getItem (0 );
98+ }
99+ return currentFolder .equals (lastFolder );
100+ }
101+
55102 /**
56103 * Looks up the traverse(int) method on the given focus control.
57104 *
0 commit comments