001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.internal.tweaklets;
011:
012: import org.eclipse.jface.dialogs.IDialogConstants;
013: import org.eclipse.jface.dialogs.MessageDialog;
014: import org.eclipse.jface.dialogs.ProgressMonitorDialog;
015: import org.eclipse.osgi.util.NLS;
016: import org.eclipse.ui.IEditorInput;
017: import org.eclipse.ui.IEditorPart;
018: import org.eclipse.ui.IEditorReference;
019: import org.eclipse.ui.IReusableEditor;
020: import org.eclipse.ui.internal.EditorAreaHelper;
021: import org.eclipse.ui.internal.EditorManager;
022: import org.eclipse.ui.internal.EditorReference;
023: import org.eclipse.ui.internal.EditorSite;
024: import org.eclipse.ui.internal.IPreferenceConstants;
025: import org.eclipse.ui.internal.Workbench;
026: import org.eclipse.ui.internal.WorkbenchMessages;
027: import org.eclipse.ui.internal.WorkbenchPage;
028: import org.eclipse.ui.internal.WorkbenchPlugin;
029: import org.eclipse.ui.internal.progress.ProgressMonitorJobsDialog;
030: import org.eclipse.ui.internal.registry.EditorDescriptor;
031:
032: /**
033: * @since 3.3
034: *
035: */
036: public class TabBehaviourMRU extends TabBehaviour {
037:
038: public boolean alwaysShowPinAction() {
039: return false;
040: }
041:
042: public IEditorReference findReusableEditor(WorkbenchPage page) {
043: boolean reuse = WorkbenchPlugin.getDefault()
044: .getPreferenceStore().getBoolean(
045: IPreferenceConstants.REUSE_EDITORS_BOOLEAN);
046: if (!reuse) {
047: return null;
048: }
049:
050: IEditorReference editors[] = page.getSortedEditors();
051: if (editors.length < page.getEditorReuseThreshold()) {
052: return null;
053: }
054:
055: IEditorReference dirtyEditor = null;
056:
057: // Find a editor to be reused
058: for (int i = 0; i < editors.length; i++) {
059: IEditorReference editor = editors[i];
060: // if(editor == activePart)
061: // continue;
062: if (editor.isPinned()) {
063: continue;
064: }
065: if (editor.isDirty()) {
066: if (dirtyEditor == null) {
067: dirtyEditor = editor;
068: }
069: continue;
070: }
071: return editor;
072: }
073: if (dirtyEditor == null) {
074: return null;
075: }
076:
077: /* fix for 11122 */
078: boolean reuseDirty = WorkbenchPlugin.getDefault()
079: .getPreferenceStore().getBoolean(
080: IPreferenceConstants.REUSE_DIRTY_EDITORS);
081: if (!reuseDirty) {
082: return null;
083: }
084:
085: MessageDialog dialog = new MessageDialog(
086: page.getWorkbenchWindow().getShell(),
087: WorkbenchMessages.EditorManager_reuseEditorDialogTitle,
088: null, // accept the default window icon
089: NLS
090: .bind(
091: WorkbenchMessages.EditorManager_saveChangesQuestion,
092: dirtyEditor.getName()),
093: MessageDialog.QUESTION,
094: new String[] {
095: IDialogConstants.YES_LABEL,
096: IDialogConstants.NO_LABEL,
097: WorkbenchMessages.EditorManager_openNewEditorLabel },
098: 0);
099: int result = dialog.open();
100: if (result == 0) { // YES
101: ProgressMonitorDialog pmd = new ProgressMonitorJobsDialog(
102: dialog.getShell());
103: pmd.open();
104: dirtyEditor.getEditor(true)
105: .doSave(pmd.getProgressMonitor());
106: pmd.close();
107: } else if ((result == 2) || (result == -1)) {
108: return null;
109: }
110: return dirtyEditor;
111: }
112:
113: public IEditorReference reuseInternalEditor(WorkbenchPage page,
114: EditorManager manager, EditorAreaHelper editorPresentation,
115: EditorDescriptor desc, IEditorInput input,
116: IEditorReference reusableEditorRef) {
117: IEditorPart reusableEditor = reusableEditorRef.getEditor(false);
118: if (reusableEditor == null) {
119: IEditorReference result = new EditorReference(manager,
120: input, desc);
121: page.closeEditor(reusableEditorRef, false);
122: return result;
123: }
124:
125: EditorSite site = (EditorSite) reusableEditor.getEditorSite();
126: EditorDescriptor oldDesc = site.getEditorDescriptor();
127: if ((desc.getId().equals(oldDesc.getId()))
128: && (reusableEditor instanceof IReusableEditor)) {
129: Workbench wb = (Workbench) page.getWorkbenchWindow()
130: .getWorkbench();
131: editorPresentation.moveEditor(reusableEditor, -1);
132: wb.getEditorHistory().add(reusableEditor.getEditorInput(),
133: site.getEditorDescriptor());
134: page.reuseEditor((IReusableEditor) reusableEditor, input);
135: return reusableEditorRef;
136: }
137: // findReusableEditor(...) checks pinned and saves editor if
138: // necessary, so it's OK to close "reusableEditor"
139: IEditorReference ref = new EditorReference(manager, input, desc);
140: reusableEditor.getEditorSite().getPage().closeEditor(
141: reusableEditor, false);
142: return ref;
143: }
144:
145: }
|