1 /*******************************************************************************
2  * Copyright (c) 2007, 2015 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.ui.internal.ide.actions;
15 
16 import org.eclipse.osgi.util.NLS;
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.IFileInfo;
23 import org.eclipse.core.filesystem.IFileStore;
24 
25 import org.eclipse.core.runtime.Path;
26 
27 import org.eclipse.jface.action.Action;
28 import org.eclipse.jface.action.IAction;
29 import org.eclipse.jface.dialogs.MessageDialog;
30 import org.eclipse.jface.viewers.ISelection;
31 
32 import org.eclipse.ui.IWorkbenchPage;
33 import org.eclipse.ui.IWorkbenchWindow;
34 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
35 import org.eclipse.ui.PartInitException;
36 import org.eclipse.ui.ide.IDE;
37 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
38 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
39 
40 
41 /**
42  * Standard action for opening an editor on local file(s).
43  * <p>
44  * This class may be instantiated; it is not intended to be subclassed.
45  * </p>
46  */
47 public class OpenLocalFileAction extends Action implements IWorkbenchWindowActionDelegate {
48 
49 	private IWorkbenchWindow window;
50 	private String filterPath;
51 
52 	/**
53 	 * Creates a new action for opening a local file.
54 	 */
OpenLocalFileAction()55 	public OpenLocalFileAction() {
56 		setEnabled(true);
57 	}
58 
59 	@Override
dispose()60 	public void dispose() {
61 		window =  null;
62 		filterPath =  null;
63 	}
64 
65 	@Override
init(IWorkbenchWindow window)66 	public void init(IWorkbenchWindow window) {
67 		this.window =  window;
68 		filterPath =  System.getProperty("user.home"); //$NON-NLS-1$
69 	}
70 
71 	@Override
run(IAction action)72 	public void run(IAction action) {
73 		run();
74 	}
75 
76 	@Override
selectionChanged(IAction action, ISelection selection)77 	public void selectionChanged(IAction action, ISelection selection) {
78 	}
79 
80 	@Override
run()81 	public void run() {
82 		FileDialog dialog =  new FileDialog(window.getShell(), SWT.OPEN | SWT.MULTI | SWT.SHEET);
83 		dialog.setText(IDEWorkbenchMessages.OpenLocalFileAction_title);
84 		dialog.setFilterPath(filterPath);
85 		dialog.open();
86 		String[] names =  dialog.getFileNames();
87 
88 		if (names != null) {
89 			filterPath =  dialog.getFilterPath();
90 
91 			int numberOfFilesNotFound =  0;
92 			StringBuilder notFound =  new StringBuilder();
93 			for (String name : names) {
94 				IFileStore fileStore =  EFS.getLocalFileSystem().getStore(new Path(filterPath));
95 				fileStore =  fileStore.getChild(name);
96 				IFileInfo fetchInfo = fileStore.fetchInfo();
97 				if (!fetchInfo.isDirectory() && fetchInfo.exists()) {
98 					IWorkbenchPage page =  window.getActivePage();
99 					try {
100 						IDE.openEditorOnFileStore(page, fileStore);
101 					} catch (PartInitException e) {
102 						String msg =  NLS.bind(IDEWorkbenchMessages.OpenLocalFileAction_message_errorOnOpen, fileStore.getName());
103 						IDEWorkbenchPlugin.log(msg,e.getStatus());
104 						MessageDialog.open(MessageDialog.ERROR,window.getShell(), IDEWorkbenchMessages.OpenLocalFileAction_title, msg, SWT.SHEET);
105 					}
106 				} else {
107 					if (++numberOfFilesNotFound > 1)
108 						notFound.append('\n');
109 					notFound.append(fileStore.getName());
110 				}
111 			}
112 
113 			if (numberOfFilesNotFound > 0) {
114 				String msgFmt =  numberOfFilesNotFound == 1 ? IDEWorkbenchMessages.OpenLocalFileAction_message_fileNotFound : IDEWorkbenchMessages.OpenLocalFileAction_message_filesNotFound;
115 				String msg =  NLS.bind(msgFmt, notFound.toString());
116 				MessageDialog.open(MessageDialog.ERROR, window.getShell(), IDEWorkbenchMessages.OpenLocalFileAction_title, msg, SWT.SHEET);
117 			}
118 		}
119 	}
120 }
121