From b29f4bf00b5e18ce8bec5a92b7a37d5b7e39d11d Mon Sep 17 00:00:00 2001 From: Deepika Udayagiri Date: Wed, 7 Oct 2026 12:40:22 +0530 Subject: [PATCH] Show pinned editors first in chevron and Ctrl+E when enabled --- .../renderers/swt/BasicPartList.java | 15 +- .../renderers/swt/PinnedEditorsOrder.java | 80 ++++++++++ .../renderers/swt/CTabRendering.java | 13 +- .../ide/dialogs/IDEEditorsPreferencePage.java | 3 +- .../ui/internal/WorkbenchMessages.java | 4 +- .../ui/internal/WorkbookEditorsHandler.java | 11 +- .../dialogs/EditorsPreferencePage.java | 30 +++- .../eclipse/ui/internal/messages.properties | 4 +- .../internal/WorkbookEditorsHandlerTest.java | 140 ++++++++++++++++-- 9 files changed, 280 insertions(+), 20 deletions(-) create mode 100644 bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/internal/workbench/renderers/swt/PinnedEditorsOrder.java diff --git a/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/internal/workbench/renderers/swt/BasicPartList.java b/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/internal/workbench/renderers/swt/BasicPartList.java index 63f57378ec1..fec6560a543 100644 --- a/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/internal/workbench/renderers/swt/BasicPartList.java +++ b/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/internal/workbench/renderers/swt/BasicPartList.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2015 IBM Corporation and others. + * Copyright (c) 2000, 2026 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -122,10 +122,15 @@ public BasicPartList(Shell parent, int shellStyle, int treeStyler, this.renderer = renderer; // this.saveHandler = saveHandler; if (mru && getTableViewer() != null) { + boolean pinnedFirst = shouldShowPinnedEditorsFirst(); getTableViewer().setComparator(new ViewerComparator() { @Override public int category(Object element) { if (element instanceof MPart part) { + if (pinnedFirst && PinnedEditorsOrder.isPinned(part)) { + // pinned editors form one block above all others + return -2; + } CTabItem item = BasicPartList.this.renderer.findItemForPart(part); if (item != null && !item.isShowing()) { return -1; @@ -170,9 +175,17 @@ private List getInput() { list.add(element); } } + if (PartStackUtil.isEditorStack(input)) { + return PinnedEditorsOrder.pinnedFirst(list, + element -> element instanceof MPart part && PinnedEditorsOrder.isPinned(part)); + } return list; } + private boolean shouldShowPinnedEditorsFirst() { + return PartStackUtil.isEditorStack(input) && PinnedEditorsOrder.isEnabled(); + } + public void setInput() { getTableViewer().setInput(getInput()); selectFirstMatch(); diff --git a/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/internal/workbench/renderers/swt/PinnedEditorsOrder.java b/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/internal/workbench/renderers/swt/PinnedEditorsOrder.java new file mode 100644 index 00000000000..f4b58eb5e9b --- /dev/null +++ b/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/internal/workbench/renderers/swt/PinnedEditorsOrder.java @@ -0,0 +1,80 @@ +/******************************************************************************* + * Copyright (c) 2026 IBM Corporation and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ + +package org.eclipse.e4.ui.internal.workbench.renderers.swt; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Predicate; +import org.eclipse.core.runtime.Platform; +import org.eclipse.e4.ui.model.application.ui.basic.MPart; +import org.eclipse.e4.ui.workbench.IPresentationEngine; +import org.eclipse.e4.ui.workbench.renderers.swt.CTabRendering; + +/** + * Single place for the "show pinned editors first" behavior of the editor + * drop-down lists (chevron list and Ctrl+E). The preference only affects the + * order of these lists, tabs are never moved. + */ +public final class PinnedEditorsOrder { + + private PinnedEditorsOrder() { + // utility class + } + + /** + * @return whether pinned editors should be listed before the other editors + */ + public static boolean isEnabled() { + return Platform.getPreferencesService().getBoolean( + CTabRendering.PREF_QUALIFIER_ECLIPSE_E4_UI_WORKBENCH_RENDERERS_SWT, + CTabRendering.SHOW_PINNED_EDITORS_FIRST, + CTabRendering.SHOW_PINNED_EDITORS_FIRST_DEFAULT, + null); + } + + /** + * @return whether the given part is pinned + */ + public static boolean isPinned(MPart part) { + return part.getTags().contains(IPresentationEngine.ADORNMENT_PIN); + } + + /** + * Returns the given elements with the pinned ones first. The relative order + * inside the pinned and inside the other elements is preserved. If the + * preference is disabled, the elements are returned unchanged. + * + * @param elements the elements in their regular order + * @param isPinned tells whether an element is pinned + * @return a list with the pinned elements first + */ + public static List pinnedFirst(List elements, Predicate isPinned) { + if (!isEnabled()) { + return elements; + } + List result = new ArrayList<>(elements.size()); + for (T element : elements) { + if (isPinned.test(element)) { + result.add(element); + } + } + for (T element : elements) { + if (!isPinned.test(element)) { + result.add(element); + } + } + return result; + } +} diff --git a/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/CTabRendering.java b/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/CTabRendering.java index d38097ded1d..ff0e204d711 100644 --- a/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/CTabRendering.java +++ b/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/CTabRendering.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010, 2020 IBM Corporation and others. + * Copyright (c) 2010, 2026 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -91,6 +91,17 @@ public class CTabRendering extends CTabFolderRenderer implements ICTabRendering, */ public static final boolean SHOW_DIRTY_INDICATOR_ON_TABS_DEFAULT = true; + /** + * A named preference for whether pinned editors are listed first in the editor + * chevron list and in the editor quick switch list (Ctrl+E). + */ + public static final String SHOW_PINNED_EDITORS_FIRST = "SHOW_PINNED_EDITORS_FIRST"; //$NON-NLS-1$ + + /** + * Default value for "show pinned editors first" preference + */ + public static final boolean SHOW_PINNED_EDITORS_FIRST_DEFAULT = false; + private static int MIN_VIEW_CHARS = 1; private static int MAX_VIEW_CHARS = Integer.MAX_VALUE; diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/dialogs/IDEEditorsPreferencePage.java b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/dialogs/IDEEditorsPreferencePage.java index 13d631e8276..d5f7525f3f9 100644 --- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/dialogs/IDEEditorsPreferencePage.java +++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/dialogs/IDEEditorsPreferencePage.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2015 IBM Corporation and others. + * Copyright (c) 2000, 2026 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -61,6 +61,7 @@ protected Control createContents(Composite parent) { createAllowInplaceEditorPref(composite); createUseIPersistablePref(composite); createPromptWhenStillOpenPref(composite); + createShowPinnedEditorsFirstPref(composite); createEditorReuseGroup(composite); createAlignMultiPageEditorTabs(composite); diff --git a/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/WorkbenchMessages.java b/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/WorkbenchMessages.java index e92931653d2..bd8006a2fbd 100644 --- a/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/WorkbenchMessages.java +++ b/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/WorkbenchMessages.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005, 2020 IBM Corporation and others. + * Copyright (c) 2005, 2026 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -472,6 +472,8 @@ public class WorkbenchMessages extends NLS { public static String WorkbenchPreference_allowInplaceEditingButton; public static String WorkbenchPreference_useIPersistableEditorButton; public static String WorkbenchPreference_promptWhenStillOpenButton; + public static String WorkbenchPreference_showPinnedEditorsFirstButton; + public static String WorkbenchPreference_showPinnedEditorsFirstStoreError; public static String WorkbenchPreference_stickyCycleButton; public static String WorkbenchPreference_RunInBackgroundButton; public static String WorkbenchPreference_RunInBackgroundToolTip; diff --git a/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/WorkbookEditorsHandler.java b/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/WorkbookEditorsHandler.java index dbb4d2f7bb3..08705d89273 100644 --- a/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/WorkbookEditorsHandler.java +++ b/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/WorkbookEditorsHandler.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2019 IBM Corporation and others. + * Copyright (c) 2007, 2026 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -33,6 +33,7 @@ import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.core.runtime.preferences.InstanceScope; import org.eclipse.e4.ui.internal.workbench.renderers.swt.DirtyIndicatorPainter; +import org.eclipse.e4.ui.internal.workbench.renderers.swt.PinnedEditorsOrder; import org.eclipse.e4.ui.model.application.ui.basic.MPart; import org.eclipse.e4.ui.workbench.renderers.swt.CTabRendering; import org.eclipse.e4.ui.workbench.renderers.swt.StackRenderer; @@ -117,7 +118,7 @@ private List getParts(WorkbenchPage page) { refs.add((EditorReference) ier); } } - return refs; + return PinnedEditorsOrder.pinnedFirst(refs, EditorReference::isPinned); } /** @@ -483,12 +484,12 @@ protected ParameterizedCommand getForwardCommand() { @Override protected int getCurrentItemIndex() { - if (isMruEnabled()) { + if (isMruEnabled() && !PinnedEditorsOrder.isEnabled()) { return 0; } // We need to find previously selected part and return the index of the - // part before this part in our list, which ordered not by use but by - // position + // part before this part in our list, which is not ordered purely by use + // (positional order, or pinned editors moved to the top) WorkbenchPage page = (WorkbenchPage) window.getActivePage(); List sortedByUse = page.getSortedEditorReferences(); diff --git a/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/dialogs/EditorsPreferencePage.java b/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/dialogs/EditorsPreferencePage.java index 1aca9a93217..197f8b4b404 100644 --- a/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/dialogs/EditorsPreferencePage.java +++ b/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/dialogs/EditorsPreferencePage.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2016 IBM Corporation and others. + * Copyright (c) 2000, 2026 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -18,6 +18,11 @@ import static org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter; +import org.eclipse.core.runtime.preferences.DefaultScope; +import org.eclipse.core.runtime.preferences.IEclipsePreferences; +import org.eclipse.core.runtime.preferences.InstanceScope; +import org.eclipse.e4.ui.internal.workbench.renderers.swt.PinnedEditorsOrder; +import org.eclipse.e4.ui.workbench.renderers.swt.CTabRendering; import org.eclipse.jface.action.Action; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; @@ -46,6 +51,7 @@ import org.eclipse.ui.internal.WorkbenchMessages; import org.eclipse.ui.internal.WorkbenchPlugin; import org.eclipse.ui.internal.util.PrefUtil; +import org.osgi.service.prefs.BackingStoreException; /** * The Editors preference page of the workbench. @@ -74,6 +80,8 @@ public class EditorsPreferencePage extends PreferencePage implements IWorkbenchP private Button promptWhenStillOpenEditor; + private Button showPinnedEditorsFirst; + private Button allowInplaceEditor; private ComboFieldEditor multiPageEditorTabPositionComboField; @@ -88,6 +96,7 @@ protected Control createContents(Composite parent) { createAllowInplaceEditorPref(composite); createUseIPersistablePref(composite); createPromptWhenStillOpenPref(composite); + createShowPinnedEditorsFirstPref(composite); createEditorReuseGroup(composite); // ((TabBehaviour)Tweaklets.get(TabBehaviour.KEY)).setPreferenceVisibility(editorReuseGroup, // showMultipleEditorTabs); @@ -136,6 +145,13 @@ protected void createPromptWhenStillOpenPref(Composite composite) { setButtonLayoutData(promptWhenStillOpenEditor); } + protected void createShowPinnedEditorsFirstPref(Composite composite) { + showPinnedEditorsFirst = new Button(composite, SWT.CHECK); + showPinnedEditorsFirst.setText(WorkbenchMessages.WorkbenchPreference_showPinnedEditorsFirstButton); + showPinnedEditorsFirst.setSelection(PinnedEditorsOrder.isEnabled()); + setButtonLayoutData(showPinnedEditorsFirst); + } + protected void createAlignMultiPageEditorTabs(Composite parent) { Composite comboComposite = new Composite(parent, SWT.NONE); comboComposite.setLayout(GridLayoutFactory.fillDefaults().numColumns(2).create()); @@ -176,6 +192,9 @@ protected void performDefaults() { useIPersistableEditor.setSelection(store.getDefaultBoolean(IPreferenceConstants.USE_IPERSISTABLE_EDITORS)); promptWhenStillOpenEditor.setSelection(getAPIPreferenceStore() .getDefaultBoolean(IWorkbenchPreferenceConstants.PROMPT_WHEN_SAVEABLE_STILL_OPEN)); + showPinnedEditorsFirst.setSelection(DefaultScope.INSTANCE + .getNode(CTabRendering.PREF_QUALIFIER_ECLIPSE_E4_UI_WORKBENCH_RENDERERS_SWT) + .getBoolean(CTabRendering.SHOW_PINNED_EDITORS_FIRST, CTabRendering.SHOW_PINNED_EDITORS_FIRST_DEFAULT)); reuseEditors.setSelection(store.getDefaultBoolean(IPreferenceConstants.REUSE_EDITORS_BOOLEAN)); reuseEditorsThreshold.loadDefault(); reuseEditorsThreshold.getLabelControl(editorReuseThresholdGroup).setEnabled(reuseEditors.getSelection()); @@ -194,6 +213,15 @@ public boolean performOk() { getAPIPreferenceStore().setValue(IWorkbenchPreferenceConstants.PROMPT_WHEN_SAVEABLE_STILL_OPEN, promptWhenStillOpenEditor.getSelection()); + IEclipsePreferences renderersPrefs = InstanceScope.INSTANCE + .getNode(CTabRendering.PREF_QUALIFIER_ECLIPSE_E4_UI_WORKBENCH_RENDERERS_SWT); + renderersPrefs.putBoolean(CTabRendering.SHOW_PINNED_EDITORS_FIRST, showPinnedEditorsFirst.getSelection()); + try { + renderersPrefs.flush(); + } catch (BackingStoreException e) { + WorkbenchPlugin.log(WorkbenchMessages.WorkbenchPreference_showPinnedEditorsFirstStoreError, e); + } + // store the reuse editors setting store.setValue(IPreferenceConstants.REUSE_EDITORS_BOOLEAN, reuseEditors.getSelection()); reuseEditorsThreshold.store(); diff --git a/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/messages.properties b/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/messages.properties index c1c3b2bdc7c..2fa44773e17 100644 --- a/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/messages.properties +++ b/bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/messages.properties @@ -1,5 +1,5 @@ ############################################################################### -# Copyright (c) 2000, 2020 IBM Corporation and others. +# Copyright (c) 2000, 2026 IBM Corporation and others. # # This program and the accompanying materials # are made available under the terms of the Eclipse Public License 2.0 @@ -414,6 +414,8 @@ PreferenceExportWarning_applyAndContinue = &Apply and Continue WorkbenchPreference_allowInplaceEditingButton = Allow in-place &system editors WorkbenchPreference_useIPersistableEditorButton = Restore &editor state on startup WorkbenchPreference_promptWhenStillOpenButton = Pr&ompt to save on close even if still open elsewhere +WorkbenchPreference_showPinnedEditorsFirstButton = Show &pinned editors first in editor lists (chevron and Ctrl+E) +WorkbenchPreference_showPinnedEditorsFirstStoreError = Failed to store the preference to show pinned editors first. WorkbenchPreference_stickyCycleButton = Keep &next/previous editor, view and perspectives dialog open WorkbenchPreference_RunInBackgroundButton=Always r&un in background WorkbenchPreference_RunInBackgroundToolTip=Run long operations in the background where possible diff --git a/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/internal/WorkbookEditorsHandlerTest.java b/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/internal/WorkbookEditorsHandlerTest.java index 22bad7811ad..608d9f58d52 100644 --- a/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/internal/WorkbookEditorsHandlerTest.java +++ b/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/internal/WorkbookEditorsHandlerTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2022 vogella GmbH. + * Copyright (c) 2022, 2026 vogella GmbH. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -10,6 +10,7 @@ * * Contributors: * Fabian Pfaff (vogella GmbH) - initial API and implementation + * IBM Corporation - pinned editors first in chevron list and Ctrl+E ******************************************************************************/ package org.eclipse.ui.tests.internal; @@ -26,16 +27,23 @@ import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.preferences.IEclipsePreferences; +import org.eclipse.core.runtime.preferences.InstanceScope; +import org.eclipse.e4.ui.workbench.renderers.swt.CTabRendering; +import org.eclipse.e4.ui.workbench.renderers.swt.StackRenderer; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableItem; +import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.commands.ICommandService; import org.eclipse.ui.handlers.IHandlerService; import org.eclipse.ui.ide.IDE; +import org.eclipse.ui.internal.WorkbenchPage; +import org.eclipse.ui.internal.WorkbenchPartReference; import org.eclipse.ui.internal.WorkbookEditorsHandler; import org.eclipse.ui.part.FileEditorInput; import org.eclipse.ui.tests.harness.util.CloseTestWindowsRule; @@ -68,15 +76,25 @@ public final void setUp() throws CoreException { @After public final void tearDown() throws Exception { - if (project1 != null) { - project1.delete(true, true, null); - project1 = null; - } - if (project2 != null) { - project2.delete(true, true, null); - project2 = null; + try { + if (project1 != null) { + project1.delete(true, true, null); + project1 = null; + } + if (project2 != null) { + project2.delete(true, true, null); + project2 = null; + } + if (activePage != null) { + activePage.closeAllEditors(false); + } + } finally { + IEclipsePreferences renderersPrefs = InstanceScope.INSTANCE + .getNode(CTabRendering.PREF_QUALIFIER_ECLIPSE_E4_UI_WORKBENCH_RENDERERS_SWT); + renderersPrefs.remove(CTabRendering.SHOW_PINNED_EDITORS_FIRST); + renderersPrefs.remove(StackRenderer.MRU_KEY); + renderersPrefs.flush(); } - activePage.closeAllEditors(false); } @Test @@ -372,6 +390,110 @@ public String getName() { assertEquals("Display text should match name of editor input", editorInputName, handler.tableItemTexts.get(1)); } + @Test + public void testShowPinnedEditorsFirstPreference() throws Exception { + String file1Name = "file1.txt"; + String file2Name = "file2.txt"; + String file3Name = "file3.txt"; + String file4Name = "file4.txt"; + IDE.openEditor(activePage, FileUtil.createFile(file1Name, project1), true); + IEditorPart editor2 = IDE.openEditor(activePage, FileUtil.createFile(file2Name, project1), true); + IDE.openEditor(activePage, FileUtil.createFile(file3Name, project1), true); + IEditorPart editor4 = IDE.openEditor(activePage, FileUtil.createFile(file4Name, project1), true); + + // Pin editors 2 and 4 + WorkbenchPartReference ref2 = (WorkbenchPartReference) ((WorkbenchPage) activePage).getReference(editor2); + ref2.setPinned(true); + WorkbenchPartReference ref4 = (WorkbenchPartReference) ((WorkbenchPage) activePage).getReference(editor4); + ref4.setPinned(true); + + ICommandService cmdService = PlatformUI.getWorkbench().getService(ICommandService.class); + final Command cmd = cmdService.getCommand("org.eclipse.ui.window.openEditorDropDown"); + IHandlerService handlerService = PlatformUI.getWorkbench().getService(IHandlerService.class); + + IEclipsePreferences renderersPrefs = InstanceScope.INSTANCE + .getNode(CTabRendering.PREF_QUALIFIER_ECLIPSE_E4_UI_WORKBENCH_RENDERERS_SWT); + + // 1. In default MRU mode when preference is disabled (false): + // Expects standard MRU activation history order: [file4.txt, file3.txt, file2.txt, file1.txt] + renderersPrefs.putBoolean(StackRenderer.MRU_KEY, true); + renderersPrefs.putBoolean(CTabRendering.SHOW_PINNED_EDITORS_FIRST, false); + WorkbookEditorsHandlerTestable handlerMruDisabled = new WorkbookEditorsHandlerTestable(); + cmd.setHandler(handlerMruDisabled); + handlerMruDisabled.execute(handlerService.createExecutionEvent(cmd, null)); + assertEquals(List.of(file4Name, file3Name, file2Name, file1Name), handlerMruDisabled.tableItemTexts); + + // When preference is enabled (true) in MRU mode: + // Pinned editors first in MRU order (file4, file2), followed by unpinned in MRU order (file3, file1) + renderersPrefs.putBoolean(CTabRendering.SHOW_PINNED_EDITORS_FIRST, true); + WorkbookEditorsHandlerTestable handlerMruEnabled = new WorkbookEditorsHandlerTestable(); + cmd.setHandler(handlerMruEnabled); + handlerMruEnabled.execute(handlerService.createExecutionEvent(cmd, null)); + assertEquals(List.of(file4Name, file2Name, file3Name, file1Name), handlerMruEnabled.tableItemTexts); + + // 2. In non-MRU (tab index) mode when preference is disabled (false): + // Expects standard tab index order: [file1.txt, file2.txt, file3.txt, file4.txt] + renderersPrefs.putBoolean(StackRenderer.MRU_KEY, false); + renderersPrefs.putBoolean(CTabRendering.SHOW_PINNED_EDITORS_FIRST, false); + WorkbookEditorsHandlerTestable handlerNonMruDisabled = new WorkbookEditorsHandlerTestable(); + cmd.setHandler(handlerNonMruDisabled); + handlerNonMruDisabled.execute(handlerService.createExecutionEvent(cmd, null)); + assertEquals(List.of(file1Name, file2Name, file3Name, file4Name), handlerNonMruDisabled.tableItemTexts); + + // When preference is enabled (true) in non-MRU mode: + // Pinned editors first in screen tab order (file2, file4), followed by unpinned in screen tab order (file1, file3) + renderersPrefs.putBoolean(CTabRendering.SHOW_PINNED_EDITORS_FIRST, true); + WorkbookEditorsHandlerTestable handlerNonMruEnabled = new WorkbookEditorsHandlerTestable(); + cmd.setHandler(handlerNonMruEnabled); + handlerNonMruEnabled.execute(handlerService.createExecutionEvent(cmd, null)); + assertEquals(List.of(file2Name, file4Name, file1Name, file3Name), handlerNonMruEnabled.tableItemTexts); + } + + @Test + public void testShowPinnedEditorsFirstPreselectsPreviouslyUsedEditorInMruMode() throws Exception { + String file1Name = "file1.txt"; + String file2Name = "file2.txt"; + String file3Name = "file3.txt"; + String file4Name = "file4.txt"; + IDE.openEditor(activePage, FileUtil.createFile(file1Name, project1), true); + IEditorPart editor2 = IDE.openEditor(activePage, FileUtil.createFile(file2Name, project1), true); + IEditorPart editor3 = IDE.openEditor(activePage, FileUtil.createFile(file3Name, project1), true); + IEditorPart editor4 = IDE.openEditor(activePage, FileUtil.createFile(file4Name, project1), true); + + WorkbenchPartReference ref2 = (WorkbenchPartReference) ((WorkbenchPage) activePage).getReference(editor2); + WorkbenchPartReference ref3 = (WorkbenchPartReference) ((WorkbenchPage) activePage).getReference(editor3); + WorkbenchPartReference ref4 = (WorkbenchPartReference) ((WorkbenchPage) activePage).getReference(editor4); + + ICommandService cmdService = PlatformUI.getWorkbench().getService(ICommandService.class); + final Command cmd = cmdService.getCommand("org.eclipse.ui.window.openEditorDropDown"); + IHandlerService handlerService = PlatformUI.getWorkbench().getService(IHandlerService.class); + + IEclipsePreferences renderersPrefs = InstanceScope.INSTANCE + .getNode(CTabRendering.PREF_QUALIFIER_ECLIPSE_E4_UI_WORKBENCH_RENDERERS_SWT); + renderersPrefs.putBoolean(StackRenderer.MRU_KEY, true); + renderersPrefs.putBoolean(CTabRendering.SHOW_PINNED_EDITORS_FIRST, true); + + // MRU order is file4, file3, file2, file1. With file2 and file4 pinned the list + // is file4, file2, file3, file1. The previously used editor (file3) is no + // longer at index 1 and must still be the preselected one. + ref2.setPinned(true); + ref4.setPinned(true); + WorkbookEditorsHandlerTestable handler = new WorkbookEditorsHandlerTestable(); + cmd.setHandler(handler); + handler.execute(handlerService.createExecutionEvent(cmd, null)); + assertEquals(List.of(file4Name, file2Name, file3Name, file1Name), handler.tableItemTexts); + assertEquals(List.of(file3Name), handler.selectionTexts); + + // Previously used editor (file3) is pinned and therefore the first entry. + ref4.setPinned(false); + ref3.setPinned(true); + WorkbookEditorsHandlerTestable handlerFirstEntry = new WorkbookEditorsHandlerTestable(); + cmd.setHandler(handlerFirstEntry); + handlerFirstEntry.execute(handlerService.createExecutionEvent(cmd, null)); + assertEquals(List.of(file3Name, file2Name, file4Name, file1Name), handlerFirstEntry.tableItemTexts); + assertEquals(List.of(file3Name), handlerFirstEntry.selectionTexts); + } + class WorkbookEditorsHandlerTestable extends WorkbookEditorsHandler { List selectionTexts; List tableItemTexts;