1 11 package org.eclipse.ui.internal.ide.actions; 12 13 import org.eclipse.osgi.util.NLS; 14 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.widgets.FileDialog; 17 18 import org.eclipse.core.filesystem.EFS; 19 import org.eclipse.core.filesystem.IFileStore; 20 21 import org.eclipse.core.runtime.Path; 22 23 import org.eclipse.jface.action.Action; 24 import org.eclipse.jface.action.IAction; 25 import org.eclipse.jface.dialogs.MessageDialog; 26 import org.eclipse.jface.viewers.ISelection; 27 28 import org.eclipse.ui.IWorkbenchPage; 29 import org.eclipse.ui.IWorkbenchWindow; 30 import org.eclipse.ui.IWorkbenchWindowActionDelegate; 31 import org.eclipse.ui.PartInitException; 32 import org.eclipse.ui.ide.IDE; 33 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 34 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 35 36 37 43 public class OpenLocalFileAction extends Action implements IWorkbenchWindowActionDelegate { 44 45 private IWorkbenchWindow window; 46 private String filterPath; 47 48 51 public OpenLocalFileAction() { 52 setEnabled(true); 53 } 54 55 58 public void dispose() { 59 window = null; 60 filterPath = null; 61 } 62 63 66 public void init(IWorkbenchWindow window) { 67 this.window = window; 68 filterPath = System.getProperty("user.home"); } 70 71 74 public void run(IAction action) { 75 run(); 76 } 77 78 81 public void selectionChanged(IAction action, ISelection selection) { 82 } 83 84 87 public void run() { 88 FileDialog dialog = new FileDialog(window.getShell(), SWT.OPEN | SWT.MULTI); 89 dialog.setText(IDEWorkbenchMessages.OpenLocalFileAction_title); 90 dialog.setFilterPath(filterPath); 91 dialog.open(); 92 String [] names = dialog.getFileNames(); 93 94 if (names != null) { 95 filterPath = dialog.getFilterPath(); 96 97 int numberOfFilesNotFound = 0; 98 StringBuffer notFound = new StringBuffer (); 99 for (int i = 0; i < names.length; i++) { 100 IFileStore fileStore = EFS.getLocalFileSystem().getStore(new Path(filterPath)); 101 fileStore = fileStore.getChild(names[i]); 102 if (!fileStore.fetchInfo().isDirectory() && fileStore.fetchInfo().exists()) { 103 IWorkbenchPage page = window.getActivePage(); 104 try { 105 IDE.openEditorOnFileStore(page, fileStore); 106 } catch (PartInitException e) { 107 String msg = NLS.bind(IDEWorkbenchMessages.OpenLocalFileAction_message_errorOnOpen, fileStore.getName()); 108 IDEWorkbenchPlugin.log(msg,e.getStatus()); 109 MessageDialog.openError(window.getShell(), IDEWorkbenchMessages.OpenLocalFileAction_title, msg); 110 } 111 } else { 112 if (++numberOfFilesNotFound > 1) 113 notFound.append('\n'); 114 notFound.append(fileStore.getName()); 115 } 116 } 117 118 if (numberOfFilesNotFound > 0) { 119 String msgFmt = numberOfFilesNotFound == 1 ? IDEWorkbenchMessages.OpenLocalFileAction_message_fileNotFound : IDEWorkbenchMessages.OpenLocalFileAction_message_filesNotFound; 120 String msg = NLS.bind(msgFmt, notFound.toString()); 121 MessageDialog.openError(window.getShell(), IDEWorkbenchMessages.OpenLocalFileAction_title, msg); 122 } 123 } 124 } 125 } 126 | Popular Tags |