| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2007 IBM Corporation and others. |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | ******************************************************************************/ |
| 11 | |
| 12 | package org.eclipse.ui.internal.tweaklets; |
| 13 | |
| 14 | import org.eclipse.jface.dialogs.IDialogConstants; |
| 15 | import org.eclipse.jface.dialogs.MessageDialog; |
| 16 | import org.eclipse.jface.dialogs.ProgressMonitorDialog; |
| 17 | import org.eclipse.osgi.util.NLS; |
| 18 | import org.eclipse.ui.IEditorInput; |
| 19 | import org.eclipse.ui.IEditorPart; |
| 20 | import org.eclipse.ui.IEditorReference; |
| 21 | import org.eclipse.ui.IReusableEditor; |
| 22 | import org.eclipse.ui.internal.EditorAreaHelper; |
| 23 | import org.eclipse.ui.internal.EditorManager; |
| 24 | import org.eclipse.ui.internal.EditorReference; |
| 25 | import org.eclipse.ui.internal.EditorSite; |
| 26 | import org.eclipse.ui.internal.IPreferenceConstants; |
| 27 | import org.eclipse.ui.internal.Workbench; |
| 28 | import org.eclipse.ui.internal.WorkbenchMessages; |
| 29 | import org.eclipse.ui.internal.WorkbenchPage; |
| 30 | import org.eclipse.ui.internal.WorkbenchPlugin; |
| 31 | import org.eclipse.ui.internal.progress.ProgressMonitorJobsDialog; |
| 32 | import org.eclipse.ui.internal.registry.EditorDescriptor; |
| 33 | |
| 34 | /** |
| 35 | * @since 3.3 |
| 36 | * |
| 37 | */ |
| 38 | public class TabBehaviourMRU extends TabBehaviour { |
| 39 | |
| 40 | public boolean alwaysShowPinAction() { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | public IEditorReference findReusableEditor(WorkbenchPage page) { |
| 45 | boolean reuse = WorkbenchPlugin.getDefault().getPreferenceStore() |
| 46 | .getBoolean(IPreferenceConstants.REUSE_EDITORS_BOOLEAN); |
| 47 | if (!reuse) { |
| 48 | return null; |
| 49 | } |
| 50 | |
| 51 | IEditorReference editors[] = page.getSortedEditors(); |
| 52 | if (editors.length < page.getEditorReuseThreshold()) { |
| 53 | return null; |
| 54 | } |
| 55 | |
| 56 | IEditorReference dirtyEditor = null; |
| 57 | |
| 58 | // Find a editor to be reused |
| 59 | for (int i = 0; i < editors.length; i++) { |
| 60 | IEditorReference editor = editors[i]; |
| 61 | // if(editor == activePart) |
| 62 | // continue; |
| 63 | if (editor.isPinned()) { |
| 64 | continue; |
| 65 | } |
| 66 | if (editor.isDirty()) { |
| 67 | if (dirtyEditor == null) { |
| 68 | dirtyEditor = editor; |
| 69 | } |
| 70 | continue; |
| 71 | } |
| 72 | return editor; |
| 73 | } |
| 74 | if (dirtyEditor == null) { |
| 75 | return null; |
| 76 | } |
| 77 | |
| 78 | /* fix for 11122 */ |
| 79 | boolean reuseDirty = WorkbenchPlugin.getDefault().getPreferenceStore() |
| 80 | .getBoolean(IPreferenceConstants.REUSE_DIRTY_EDITORS); |
| 81 | if (!reuseDirty) { |
| 82 | return null; |
| 83 | } |
| 84 | |
| 85 | MessageDialog dialog = new MessageDialog(page.getWorkbenchWindow() |
| 86 | .getShell(), |
| 87 | WorkbenchMessages.EditorManager_reuseEditorDialogTitle, |
| 88 | null, // accept the default window icon |
| 89 | NLS.bind(WorkbenchMessages.EditorManager_saveChangesQuestion, |
| 90 | dirtyEditor.getName()), MessageDialog.QUESTION, |
| 91 | new String[] { IDialogConstants.YES_LABEL, |
| 92 | IDialogConstants.NO_LABEL, |
| 93 | WorkbenchMessages.EditorManager_openNewEditorLabel }, 0); |
| 94 | int result = dialog.open(); |
| 95 | if (result == 0) { // YES |
| 96 | ProgressMonitorDialog pmd = new ProgressMonitorJobsDialog(dialog |
| 97 | .getShell()); |
| 98 | pmd.open(); |
| 99 | dirtyEditor.getEditor(true).doSave(pmd.getProgressMonitor()); |
| 100 | pmd.close(); |
| 101 | } else if ((result == 2) || (result == -1)) { |
| 102 | return null; |
| 103 | } |
| 104 | return dirtyEditor; |
| 105 | } |
| 106 | |
| 107 | public IEditorReference reuseInternalEditor(WorkbenchPage page, |
| 108 | EditorManager manager, EditorAreaHelper editorPresentation, |
| 109 | EditorDescriptor desc, IEditorInput input, |
| 110 | IEditorReference reusableEditorRef) { |
| 111 | IEditorPart reusableEditor = reusableEditorRef.getEditor(false); |
| 112 | if (reusableEditor == null) { |
| 113 | IEditorReference result = new EditorReference(manager, input, desc); |
| 114 | page.closeEditor(reusableEditorRef, false); |
| 115 | return result; |
| 116 | } |
| 117 | |
| 118 | EditorSite site = (EditorSite) reusableEditor.getEditorSite(); |
| 119 | EditorDescriptor oldDesc = site.getEditorDescriptor(); |
| 120 | if ((desc.getId().equals(oldDesc.getId())) |
| 121 | && (reusableEditor instanceof IReusableEditor)) { |
| 122 | Workbench wb = (Workbench) page.getWorkbenchWindow().getWorkbench(); |
| 123 | editorPresentation.moveEditor(reusableEditor, -1); |
| 124 | wb.getEditorHistory().add(reusableEditor.getEditorInput(), |
| 125 | site.getEditorDescriptor()); |
| 126 | page.reuseEditor((IReusableEditor) reusableEditor, input); |
| 127 | return reusableEditorRef; |
| 128 | } |
| 129 | // findReusableEditor(...) checks pinned and saves editor if |
| 130 | // necessary, so it's OK to close "reusableEditor" |
| 131 | IEditorReference ref = new EditorReference(manager, input, desc); |
| 132 | reusableEditor.getEditorSite().getPage().closeEditor(reusableEditor, |
| 133 | false); |
| 134 | return ref; |
| 135 | } |
| 136 | |
| 137 | } |