1 11 package org.eclipse.ui.internal.editors.text; 12 13 import java.io.FileNotFoundException ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.util.ArrayList ; 17 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.widgets.FileDialog; 20 21 import org.eclipse.core.filesystem.EFS; 22 import org.eclipse.core.filesystem.IFileStore; 23 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.IPath; 26 import org.eclipse.core.runtime.Path; 27 import org.eclipse.core.runtime.Platform; 28 import org.eclipse.core.runtime.content.IContentType; 29 30 import org.eclipse.core.resources.IFile; 31 import org.eclipse.core.resources.IWorkspace; 32 import org.eclipse.core.resources.ResourcesPlugin; 33 34 import org.eclipse.jface.action.Action; 35 import org.eclipse.jface.action.IAction; 36 import org.eclipse.jface.dialogs.MessageDialog; 37 import org.eclipse.jface.viewers.ISelection; 38 import org.eclipse.jface.viewers.LabelProvider; 39 import org.eclipse.jface.window.Window; 40 41 import org.eclipse.ui.editors.text.EditorsUI; 42 43 import org.eclipse.ui.IEditorDescriptor; 44 import org.eclipse.ui.IEditorInput; 45 import org.eclipse.ui.IEditorRegistry; 46 import org.eclipse.ui.IWorkbench; 47 import org.eclipse.ui.IWorkbenchPage; 48 import org.eclipse.ui.IWorkbenchWindow; 49 import org.eclipse.ui.IWorkbenchWindowActionDelegate; 50 import org.eclipse.ui.PartInitException; 51 import org.eclipse.ui.dialogs.ElementListSelectionDialog; 52 import org.eclipse.ui.part.FileEditorInput; 53 54 57 public class OpenExternalFileAction extends Action implements IWorkbenchWindowActionDelegate { 58 59 static class FileLabelProvider extends LabelProvider { 60 63 public String getText(Object element) { 64 if (element instanceof IFile) { 65 IPath path= ((IFile) element).getFullPath(); 66 return path != null ? path.toString() : ""; } 68 return super.getText(element); 69 } 70 } 71 72 73 private IWorkbenchWindow fWindow; 74 private String fFilterPath; 75 76 public OpenExternalFileAction() { 77 setEnabled(true); 78 } 79 80 83 public void dispose() { 84 fWindow= null; 85 fFilterPath= null; 86 } 87 88 91 public void init(IWorkbenchWindow window) { 92 fWindow= window; 93 fFilterPath= System.getProperty("user.home"); } 95 96 99 public void run(IAction action) { 100 run(); 101 } 102 103 106 public void selectionChanged(IAction action, ISelection selection) { 107 } 108 109 112 public void run() { 113 FileDialog dialog= new FileDialog(fWindow.getShell(), SWT.OPEN | SWT.MULTI); 114 dialog.setText(TextEditorMessages.OpenExternalFileAction_title); 115 dialog.setFilterPath(fFilterPath); 116 dialog.open(); 117 String [] names= dialog.getFileNames(); 118 119 if (names != null) { 120 fFilterPath= dialog.getFilterPath(); 121 122 int numberOfFilesNotFound= 0; 123 StringBuffer notFound= new StringBuffer (); 124 for (int i= 0; i < names.length; i++) { 125 IFileStore fileStore= EFS.getLocalFileSystem().getStore(new Path(fFilterPath)); 126 fileStore= fileStore.getChild(names[i]); 127 if (!fileStore.fetchInfo().isDirectory() && fileStore.fetchInfo().exists()) { 128 IEditorInput input= createEditorInput(fileStore); 129 String editorId= getEditorId(fileStore); 130 IWorkbenchPage page= fWindow.getActivePage(); 131 try { 132 page.openEditor(input, editorId); 133 } catch (PartInitException e) { 134 EditorsPlugin.log(e.getStatus()); 135 String msg= NLSUtility.format(TextEditorMessages.OpenExternalFileAction_message_errorOnOpen, fileStore.getName()); 136 MessageDialog.openError(fWindow.getShell(), TextEditorMessages.OpenExternalFileAction_title, msg); 137 } 138 } else { 139 if (++numberOfFilesNotFound > 1) 140 notFound.append('\n'); 141 notFound.append(fileStore.getName()); 142 } 143 } 144 145 if (numberOfFilesNotFound > 0) { 146 String msgFmt= numberOfFilesNotFound == 1 ? TextEditorMessages.OpenExternalFileAction_message_fileNotFound : TextEditorMessages.OpenExternalFileAction_message_filesNotFound; 147 String msg= NLSUtility.format(msgFmt, notFound.toString()); 148 MessageDialog.openError(fWindow.getShell(), TextEditorMessages.OpenExternalFileAction_title, msg); 149 } 150 } 151 } 152 153 157 private String getEditorId(IFileStore file) { 158 IWorkbench workbench= fWindow.getWorkbench(); 159 IEditorRegistry editorRegistry= workbench.getEditorRegistry(); 160 IEditorDescriptor descriptor= editorRegistry.getDefaultEditor(file.getName(), getContentType(file)); 161 162 if (descriptor == null && editorRegistry.isSystemInPlaceEditorAvailable(file.getName())) 164 descriptor= editorRegistry.findEditor(IEditorRegistry.SYSTEM_INPLACE_EDITOR_ID); 165 166 if (descriptor == null && editorRegistry.isSystemExternalEditorAvailable(file.getName())) 168 descriptor= editorRegistry.findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID); 169 170 if (descriptor != null) 171 return descriptor.getId(); 172 173 return EditorsUI.DEFAULT_TEXT_EDITOR_ID; 174 } 175 176 private IContentType getContentType (IFileStore fileStore) { 177 if (fileStore == null) 178 return null; 179 180 InputStream stream= null; 181 try { 182 stream= fileStore.openInputStream(EFS.NONE, null); 183 return Platform.getContentTypeManager().findContentTypeFor(stream, fileStore.getName()); 184 } catch (IOException x) { 185 EditorsPlugin.log(x); 186 return null; 187 } catch (CoreException x) { 188 if (!(x.getStatus().getException() instanceof FileNotFoundException )) 190 EditorsPlugin.log(x); 191 192 return null; 193 } finally { 194 try { 195 if (stream != null) 196 stream.close(); 197 } catch (IOException x) { 198 EditorsPlugin.log(x); 199 } 200 } 201 } 202 203 private IEditorInput createEditorInput(IFileStore fileStore) { 204 IFile workspaceFile= getWorkspaceFile(fileStore); 205 if (workspaceFile != null) 206 return new FileEditorInput(workspaceFile); 207 return new JavaFileEditorInput(fileStore); 208 } 209 210 private IFile getWorkspaceFile(IFileStore fileStore) { 211 IWorkspace workspace= ResourcesPlugin.getWorkspace(); 212 IFile[] files= workspace.getRoot().findFilesForLocation(new Path(fileStore.toURI().getPath())); 213 files= filterNonExistentFiles(files); 214 if (files == null || files.length == 0) 215 return null; 216 if (files.length == 1) 217 return files[0]; 218 return selectWorkspaceFile(files); 219 } 220 221 private IFile[] filterNonExistentFiles(IFile[] files){ 222 if (files == null) 223 return null; 224 225 int length= files.length; 226 ArrayList existentFiles= new ArrayList (length); 227 for (int i= 0; i < length; i++) { 228 if (files[i].exists()) 229 existentFiles.add(files[i]); 230 } 231 return (IFile[])existentFiles.toArray(new IFile[existentFiles.size()]); 232 } 233 234 private IFile selectWorkspaceFile(IFile[] files) { 235 ElementListSelectionDialog dialog= new ElementListSelectionDialog(fWindow.getShell(), new FileLabelProvider()); 236 dialog.setElements(files); 237 dialog.setTitle(TextEditorMessages.OpenExternalFileAction_title_selectWorkspaceFile); 238 dialog.setMessage(TextEditorMessages.OpenExternalFileAction_message_fileLinkedToMultiple); 239 if (dialog.open() == Window.OK) 240 return (IFile) dialog.getFirstResult(); 241 return null; 242 } 243 } 244 | Popular Tags |