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