|
12 | 12 | package net.sf.j2s.ui.actions; |
13 | 13 |
|
14 | 14 | import java.io.File; |
15 | | -import java.io.FileNotFoundException; |
16 | | -import java.io.IOException; |
17 | | -import java.io.InputStream; |
18 | 15 | import java.util.ArrayList; |
19 | 16 |
|
20 | 17 | import net.sf.j2s.ui.Java2ScriptUIPlugin; |
21 | 18 |
|
22 | 19 | import org.eclipse.core.filesystem.EFS; |
23 | 20 | import org.eclipse.core.filesystem.IFileStore; |
24 | 21 | import org.eclipse.core.resources.IFile; |
25 | | -import org.eclipse.core.resources.IWorkspace; |
| 22 | +import org.eclipse.core.resources.IWorkspaceRoot; |
26 | 23 | import org.eclipse.core.resources.ResourcesPlugin; |
27 | | -import org.eclipse.core.runtime.CoreException; |
28 | 24 | import org.eclipse.core.runtime.IPath; |
29 | 25 | import org.eclipse.core.runtime.Path; |
30 | 26 | import org.eclipse.core.runtime.Platform; |
31 | | -import org.eclipse.core.runtime.content.IContentType; |
32 | 27 | import org.eclipse.jdt.core.ICompilationUnit; |
33 | 28 | import org.eclipse.jdt.core.IJavaElement; |
34 | 29 | import org.eclipse.jdt.core.IJavaModel; |
35 | 30 | import org.eclipse.jdt.core.IJavaProject; |
36 | 31 | import org.eclipse.jdt.core.JavaModelException; |
37 | 32 | import org.eclipse.jdt.internal.core.PackageFragmentRoot; |
38 | 33 | import org.eclipse.jface.dialogs.MessageDialog; |
| 34 | +import org.eclipse.osgi.util.NLS; |
39 | 35 | import org.eclipse.swt.widgets.Shell; |
40 | 36 | import org.eclipse.ui.IEditorDescriptor; |
41 | 37 | import org.eclipse.ui.IEditorInput; |
42 | 38 | import org.eclipse.ui.IEditorRegistry; |
43 | | -import org.eclipse.ui.IWorkbench; |
44 | 39 | import org.eclipse.ui.IWorkbenchPage; |
45 | 40 | import org.eclipse.ui.IWorkbenchWindow; |
46 | 41 | import org.eclipse.ui.PartInitException; |
47 | 42 | import org.eclipse.ui.editors.text.EditorsUI; |
48 | | -import org.eclipse.ui.internal.editors.text.EditorsPlugin; |
| 43 | +import org.eclipse.ui.ide.FileStoreEditorInput; |
| 44 | +import org.eclipse.ui.ide.IDE; |
| 45 | +import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; |
| 46 | +import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; |
49 | 47 | import org.eclipse.ui.part.FileEditorInput; |
50 | 48 |
|
51 | 49 | /** |
@@ -93,106 +91,90 @@ public static boolean openEditor( ICompilationUnit unit) { |
93 | 91 | IFileStore fileStore= EFS.getLocalFileSystem().getStore(new Path(file.getParent())); |
94 | 92 | fileStore= fileStore.getChild(file.getName()); |
95 | 93 | if (!fileStore.fetchInfo().isDirectory() && fileStore.fetchInfo().exists()) { |
96 | | - IEditorInput input= createEditorInput(fileStore); |
97 | | - if (input == null) { |
98 | | - return false; |
99 | | - } |
100 | | - IWorkbenchWindow fWindow = Java2ScriptUIPlugin.getDefault().getWorkbench() |
101 | | - .getActiveWorkbenchWindow(); |
102 | | - String editorId= getEditorId(fWindow, fileStore); |
103 | | - IWorkbenchPage page= fWindow.getActivePage(); |
| 94 | + IWorkbenchWindow fWindow = Java2ScriptUIPlugin.getDefault() |
| 95 | + .getWorkbench().getActiveWorkbenchWindow(); |
| 96 | + IWorkbenchPage page = fWindow.getActivePage(); |
104 | 97 | try { |
105 | | - page.openEditor(input, editorId); |
| 98 | + // Copy from IDE.openEditorOnFileStore |
| 99 | + IEditorInput input = getEditorInput(fileStore); |
| 100 | + String editorId = null; |
| 101 | + IEditorDescriptor descriptor; |
| 102 | + try { |
| 103 | + descriptor = IDE.getEditorDescriptor("java2script.txt"); // text editor |
| 104 | + if (descriptor != null) |
| 105 | + editorId = descriptor.getId(); |
| 106 | + } catch (PartInitException e) { |
| 107 | + } |
| 108 | + |
| 109 | + // open the editor on the file |
| 110 | + page.openEditor(input, editorId); |
106 | 111 | return true; |
107 | 112 | } catch (PartInitException e) { |
108 | | - EditorsPlugin.log(e.getStatus()); |
| 113 | + String msg = NLS.bind(IDEWorkbenchMessages.OpenLocalFileAction_message_errorOnOpen, fileStore.getName()); |
| 114 | + IDEWorkbenchPlugin.log(msg,e.getStatus()); |
| 115 | + MessageDialog.openError(fWindow.getShell(), IDEWorkbenchMessages.OpenLocalFileAction_title, msg); |
109 | 116 | } |
110 | 117 | } |
111 | 118 | } |
112 | 119 | return false; |
113 | 120 | } |
114 | | - /* |
115 | | - * XXX: Requested a helper to get the correct editor descriptor |
116 | | - * see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=110203 |
117 | | - */ |
118 | | - private static String getEditorId(IWorkbenchWindow fWindow, IFileStore file) { |
119 | | - IWorkbench workbench= fWindow.getWorkbench(); |
120 | | - IEditorRegistry editorRegistry= workbench.getEditorRegistry(); |
121 | | - IEditorDescriptor descriptor= editorRegistry.getDefaultEditor(file.getName(), getContentType(file)); |
122 | | - |
123 | | - // check the OS for in-place editor (OLE on Win32) |
124 | | - if (descriptor == null && editorRegistry.isSystemInPlaceEditorAvailable(file.getName())) |
125 | | - descriptor= editorRegistry.findEditor(IEditorRegistry.SYSTEM_INPLACE_EDITOR_ID); |
126 | | - |
127 | | -// // check the OS for external editor |
128 | | -// if (descriptor == null && editorRegistry.isSystemExternalEditorAvailable(file.getName())) |
129 | | -// descriptor= editorRegistry.findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID); |
130 | | - |
131 | | - if (descriptor != null) |
132 | | - return descriptor.getId(); |
133 | | - |
134 | | - return EditorsUI.DEFAULT_TEXT_EDITOR_ID; |
135 | | - } |
136 | | - |
137 | | - private static IContentType getContentType (IFileStore fileStore) { |
138 | | - if (fileStore == null) |
139 | | - return null; |
140 | | - |
141 | | - InputStream stream= null; |
142 | | - try { |
143 | | - stream= fileStore.openInputStream(EFS.NONE, null); |
144 | | - return Platform.getContentTypeManager().findContentTypeFor(stream, fileStore.getName()); |
145 | | - } catch (IOException x) { |
146 | | - EditorsPlugin.log(x); |
147 | | - return null; |
148 | | - } catch (CoreException x) { |
149 | | - // Do not log FileNotFoundException (no access) |
150 | | - if (!(x.getStatus().getException() instanceof FileNotFoundException)) |
151 | | - EditorsPlugin.log(x); |
152 | | - |
153 | | - return null; |
154 | | - } finally { |
155 | | - try { |
156 | | - if (stream != null) |
157 | | - stream.close(); |
158 | | - } catch (IOException x) { |
159 | | - EditorsPlugin.log(x); |
160 | | - } |
161 | | - } |
162 | | - } |
163 | 121 |
|
164 | | - private static IEditorInput createEditorInput(IFileStore fileStore) { |
165 | | - IFile workspaceFile= getWorkspaceFile(fileStore); |
| 122 | + /** |
| 123 | + * Create the Editor Input appropriate for the given <code>IFileStore</code>. |
| 124 | + * The result is a normal file editor input if the file exists in the |
| 125 | + * workspace and, if not, we create a wrapper capable of managing an |
| 126 | + * 'external' file using its <code>IFileStore</code>. |
| 127 | + * |
| 128 | + * @param fileStore |
| 129 | + * The file store to provide the editor input for |
| 130 | + * @return The editor input associated with the given file store |
| 131 | + */ |
| 132 | + private static IEditorInput getEditorInput(IFileStore fileStore) { |
| 133 | + IFile workspaceFile = getWorkspaceFile(fileStore); |
166 | 134 | if (workspaceFile != null) |
167 | 135 | return new FileEditorInput(workspaceFile); |
168 | | - else { |
169 | | - //return new FileEditorInput(new Path(fileStore.toURI().getPath()).) |
170 | | - return null; |
171 | | - } |
| 136 | + return new FileStoreEditorInput(fileStore); |
172 | 137 | } |
173 | 138 |
|
| 139 | + /** |
| 140 | + * Determine whether or not the <code>IFileStore</code> represents a file |
| 141 | + * currently in the workspace. |
| 142 | + * |
| 143 | + * @param fileStore |
| 144 | + * The <code>IFileStore</code> to test |
| 145 | + * @return The workspace's <code>IFile</code> if it exists or |
| 146 | + * <code>null</code> if not |
| 147 | + */ |
174 | 148 | private static IFile getWorkspaceFile(IFileStore fileStore) { |
175 | | - IWorkspace workspace= ResourcesPlugin.getWorkspace(); |
176 | | - IFile[] files= workspace.getRoot().findFilesForLocation(new Path(fileStore.toURI().getPath())); |
177 | | - files= filterNonExistentFiles(files); |
| 149 | + IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); |
| 150 | + IFile[] files = root.findFilesForLocationURI(fileStore.toURI()); |
| 151 | + files = filterNonExistentFiles(files); |
178 | 152 | if (files == null || files.length == 0) |
179 | 153 | return null; |
180 | | - if (files.length == 1) |
181 | | - return files[0]; |
182 | | - return null; |
| 154 | + |
| 155 | + // for now only return the first file |
| 156 | + return files[0]; |
183 | 157 | } |
184 | 158 |
|
185 | | - private static IFile[] filterNonExistentFiles(IFile[] files){ |
| 159 | + /** |
| 160 | + * Filter the incoming array of <code>IFile</code> elements by removing |
| 161 | + * any that do not currently exist in the workspace. |
| 162 | + * |
| 163 | + * @param files |
| 164 | + * The array of <code>IFile</code> elements |
| 165 | + * @return The filtered array |
| 166 | + */ |
| 167 | + private static IFile[] filterNonExistentFiles(IFile[] files) { |
186 | 168 | if (files == null) |
187 | 169 | return null; |
188 | 170 |
|
189 | | - int length= files.length; |
190 | | - ArrayList existentFiles= new ArrayList(length); |
191 | | - for (int i= 0; i < length; i++) { |
| 171 | + int length = files.length; |
| 172 | + ArrayList existentFiles = new ArrayList(length); |
| 173 | + for (int i = 0; i < length; i++) { |
192 | 174 | if (files[i].exists()) |
193 | 175 | existentFiles.add(files[i]); |
194 | 176 | } |
195 | | - return (IFile[])existentFiles.toArray(new IFile[existentFiles.size()]); |
| 177 | + return (IFile[]) existentFiles.toArray(new IFile[existentFiles.size()]); |
196 | 178 | } |
197 | 179 |
|
198 | 180 | protected static String getRelativeJSPath(ICompilationUnit unit) { |
|
0 commit comments