KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > ui > text > EditorOpener


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 package org.eclipse.search.internal.ui.text;
12
13 import org.eclipse.core.resources.IFile;
14
15 import org.eclipse.ui.IEditorDescriptor;
16 import org.eclipse.ui.IEditorInput;
17 import org.eclipse.ui.IEditorPart;
18 import org.eclipse.ui.IEditorReference;
19 import org.eclipse.ui.IEditorRegistry;
20 import org.eclipse.ui.IReusableEditor;
21 import org.eclipse.ui.IViewReference;
22 import org.eclipse.ui.IWorkbenchPage;
23 import org.eclipse.ui.IWorkbenchPartReference;
24 import org.eclipse.ui.PartInitException;
25 import org.eclipse.ui.ide.IDE;
26 import org.eclipse.ui.part.FileEditorInput;
27
28 import org.eclipse.search.ui.NewSearchUI;
29
30 import org.eclipse.search.internal.ui.SearchPlugin;
31
32 public class EditorOpener {
33
34     private IEditorReference fReusedEditor;
35     
36     public IEditorPart open(IFile file, boolean activate) throws PartInitException {
37         IWorkbenchPage wbPage= SearchPlugin.getActivePage();
38         if (NewSearchUI.reuseEditor())
39             return showWithReuse(file, wbPage, activate);
40         return showWithoutReuse(file, wbPage, activate);
41     }
42     
43     private IEditorPart showWithoutReuse(IFile file, IWorkbenchPage wbPage, boolean activate) throws PartInitException {
44         return IDE.openEditor(wbPage, file, activate);
45     }
46
47     private IEditorPart showWithReuse(IFile file, IWorkbenchPage wbPage, boolean activate) throws PartInitException {
48         String JavaDoc editorID= getEditorID(file);
49         return showInEditor(wbPage, file, editorID, activate);
50     }
51
52
53     private String JavaDoc getEditorID(IFile file) throws PartInitException {
54         IEditorDescriptor desc= IDE.getEditorDescriptor(file);
55         if (desc == null)
56             return SearchPlugin.getDefault().getWorkbench().getEditorRegistry().findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID).getId();
57         return desc.getId();
58     }
59     
60     private IEditorPart showInEditor(IWorkbenchPage page, IFile file, String JavaDoc editorId, boolean activate) throws PartInitException {
61         IEditorInput input= new FileEditorInput(file);
62         IEditorPart editor= page.findEditor(input);
63         if (editor != null) {
64             page.bringToTop(editor);
65             if (activate) {
66                 page.activate(editor);
67             }
68             return editor;
69         }
70         IEditorReference reusedEditorRef= fReusedEditor;
71         if (reusedEditorRef != null) {
72             boolean isOpen= reusedEditorRef.getEditor(false) != null;
73             boolean canBeReused= isOpen && !reusedEditorRef.isDirty() && !reusedEditorRef.isPinned();
74             if (canBeReused) {
75                 boolean showsSameInputType= reusedEditorRef.getId().equals(editorId);
76                 if (!showsSameInputType) {
77                     if (isOkToClose(page)) // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=188587
78
page.closeEditors(new IEditorReference[] { reusedEditorRef }, false);
79                     fReusedEditor= null;
80                 } else {
81                     editor= reusedEditorRef.getEditor(true);
82                     if (editor instanceof IReusableEditor) {
83                         ((IReusableEditor) editor).setInput(input);
84                         page.bringToTop(editor);
85                         if (activate) {
86                             page.activate(editor);
87                         }
88                         return editor;
89                     }
90                 }
91             }
92         }
93         editor= page.openEditor(input, editorId, activate);
94         if (editor instanceof IReusableEditor) {
95             IEditorReference reference= (IEditorReference) page.getReference(editor);
96             fReusedEditor= reference;
97         } else {
98             fReusedEditor= null;
99         }
100         return editor;
101     }
102
103     private boolean isOkToClose(IWorkbenchPage page) {
104         IWorkbenchPartReference searchViewRef= page.getActivePartReference();
105         return searchViewRef == null ||
106             !NewSearchUI.SEARCH_VIEW_ID.equals(searchViewRef.getId()) ||
107             !((IViewReference) searchViewRef).isFastView();
108     }
109     
110 }
111
Popular Tags