|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Vegard IT GmbH 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 | + * Sebastian Thomschke (Vegard IT GmbH) - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.ui.actions; |
| 15 | + |
| 16 | +import org.eclipse.core.resources.IFile; |
| 17 | +import org.eclipse.core.resources.IResource; |
| 18 | +import org.eclipse.jface.util.OpenStrategy; |
| 19 | +import org.eclipse.ui.IEditorDescriptor; |
| 20 | +import org.eclipse.ui.IEditorPart; |
| 21 | +import org.eclipse.ui.IEditorReference; |
| 22 | +import org.eclipse.ui.IEditorRegistry; |
| 23 | +import org.eclipse.ui.IPageLayout; |
| 24 | +import org.eclipse.ui.IReusableEditor; |
| 25 | +import org.eclipse.ui.IWorkbenchPage; |
| 26 | +import org.eclipse.ui.IWorkbenchPartReference; |
| 27 | +import org.eclipse.ui.PartInitException; |
| 28 | +import org.eclipse.ui.ide.IDE; |
| 29 | +import org.eclipse.ui.internal.IPreferenceConstants; |
| 30 | +import org.eclipse.ui.internal.ide.DialogUtil; |
| 31 | +import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; |
| 32 | +import org.eclipse.ui.internal.util.PrefUtil; |
| 33 | +import org.eclipse.ui.part.FileEditorInput; |
| 34 | + |
| 35 | +/** |
| 36 | + * Open action that optionally reuses a single editor for opens originating from |
| 37 | + * the Project Explorer, similar to the Search view behavior. When the feature |
| 38 | + * is disabled or outside Project Explorer, behavior falls back to |
| 39 | + * {@link OpenFileAction}. |
| 40 | + * |
| 41 | + * Scope is currently limited to Project Explorer via the creating part id |
| 42 | + * passed in the constructor. Reuse stops when the editor becomes dirty, is |
| 43 | + * pinned, closed, or when the resolved editor id differs from the currently |
| 44 | + * reused editor id. |
| 45 | + * |
| 46 | + * This class is intentionally package-public in org.eclipse.ui.actions to be a |
| 47 | + * drop-in replacement for {@link OpenFileAction} in CNF wiring. |
| 48 | + */ |
| 49 | +public class OpenFileWithReuseAction extends OpenFileAction { |
| 50 | + |
| 51 | + private final String hostPartId; |
| 52 | + private IEditorReference reusedRef; |
| 53 | + private boolean disableReuseForRun; |
| 54 | + |
| 55 | + /** |
| 56 | + * @param page workbench page |
| 57 | + * @param hostPartId id of the part creating this action (used to scope to |
| 58 | + * Project Explorer) |
| 59 | + */ |
| 60 | + public OpenFileWithReuseAction(final IWorkbenchPage page, final String hostPartId) { |
| 61 | + this(page, null, hostPartId); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @param page workbench page |
| 66 | + * @param descriptor editor descriptor to use (or null for default) |
| 67 | + * @param hostPartId id of the part creating this action (used to scope to |
| 68 | + * Project Explorer) |
| 69 | + */ |
| 70 | + public OpenFileWithReuseAction(final IWorkbenchPage page, final IEditorDescriptor descriptor, |
| 71 | + final String hostPartId) { |
| 72 | + super(page, descriptor); |
| 73 | + this.hostPartId = hostPartId; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void run() { |
| 78 | + // Disable reuse for multi-file selection to preserve baseline behavior |
| 79 | + int fileCount = 0; |
| 80 | + for (final IResource res : getSelectedResources()) { |
| 81 | + if (res instanceof IFile) { |
| 82 | + fileCount++; |
| 83 | + if (fileCount > 1) { |
| 84 | + break; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + disableReuseForRun = fileCount > 1; |
| 89 | + try { |
| 90 | + super.run(); |
| 91 | + } finally { |
| 92 | + disableReuseForRun = false; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + void openFile(final IFile file) { |
| 98 | + if (disableReuseForRun || !shouldApplyReuse()) { |
| 99 | + // Delegate to baseline behavior |
| 100 | + super.openFile(file); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + final IWorkbenchPage page = getWorkbenchPage(); |
| 105 | + final boolean activate = OpenStrategy.activateOnOpen(); |
| 106 | + |
| 107 | + // If already open, just bring to top/activate |
| 108 | + final IEditorPart existing = page.findEditor(new FileEditorInput(file)); |
| 109 | + if (existing != null) { |
| 110 | + if (activate) { |
| 111 | + page.activate(existing); |
| 112 | + } else { |
| 113 | + page.bringToTop(existing); |
| 114 | + } |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + // Determine target editor id (explicit), |
| 119 | + // falling back to external system editor id |
| 120 | + String editorId = IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID; |
| 121 | + IEditorDescriptor desc = null; |
| 122 | + try { |
| 123 | + desc = IDE.getEditorDescriptor(file, true, true); |
| 124 | + } catch (PartInitException e) { |
| 125 | + // ignore here; will fall back to system external editor id |
| 126 | + } |
| 127 | + if (desc != null) { |
| 128 | + editorId = desc.getId(); |
| 129 | + } |
| 130 | + |
| 131 | + // Do not attempt reuse for external editors |
| 132 | + if (IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID.equals(editorId)) { |
| 133 | + super.openFile(file); |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + // Try reuse if we have a valid reference |
| 138 | + if (reusedRef != null) { |
| 139 | + final boolean open = reusedRef.getEditor(false) != null; |
| 140 | + final boolean valid = open && !reusedRef.isDirty() && !reusedRef.isPinned(); |
| 141 | + if (valid) { |
| 142 | + if (!editorId.equals(reusedRef.getId())) { |
| 143 | + // Different editor type needed; close old reusable editor |
| 144 | + page.closeEditors(new IEditorReference[] { reusedRef }, false); |
| 145 | + reusedRef = null; |
| 146 | + } else { |
| 147 | + final IEditorPart part = reusedRef.getEditor(true); |
| 148 | + if (part instanceof IReusableEditor reusableEditor) { |
| 149 | + reusableEditor.setInput(new FileEditorInput(file)); |
| 150 | + if (activate) { |
| 151 | + page.activate(part); |
| 152 | + } else { |
| 153 | + page.bringToTop(part); |
| 154 | + } |
| 155 | + return; |
| 156 | + } |
| 157 | + // Not reusable after all |
| 158 | + reusedRef = null; |
| 159 | + } |
| 160 | + } else { |
| 161 | + // Reference no longer valid |
| 162 | + reusedRef = null; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + // Open a new editor and remember it if reusable |
| 167 | + try { |
| 168 | + final IEditorPart opened = IDE.openEditor(page, file, editorId, activate); |
| 169 | + if (opened instanceof IReusableEditor) { |
| 170 | + final IWorkbenchPartReference ref = page.getReference(opened); |
| 171 | + if (ref instanceof IEditorReference editorRef) { |
| 172 | + reusedRef = editorRef; |
| 173 | + } else { |
| 174 | + reusedRef = null; |
| 175 | + } |
| 176 | + } else { |
| 177 | + reusedRef = null; |
| 178 | + } |
| 179 | + } catch (final PartInitException ex) { |
| 180 | + DialogUtil.openError(page.getWorkbenchWindow().getShell(), |
| 181 | + IDEWorkbenchMessages.OpenFileAction_openFileShellTitle, ex.getMessage(), ex); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + private boolean shouldApplyReuse() { |
| 186 | + // Strictly scope to Project Explorer and opt-in preference |
| 187 | + if (hostPartId == null || !hostPartId.startsWith(IPageLayout.ID_PROJECT_EXPLORER)) { |
| 188 | + return false; |
| 189 | + } |
| 190 | + |
| 191 | + return PrefUtil.getInternalPreferenceStore().getBoolean(IPreferenceConstants.REUSE_LAST_OPENED_EDITOR); |
| 192 | + } |
| 193 | +} |
0 commit comments