KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > actions > OpenLocalFileAction


1 /*******************************************************************************
2  * Copyright (c) 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.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 /**
38  * Standard action for opening an editor on local file(s).
39  * <p>
40  * This class may be instantiated; it is not intended to be subclassed.
41  * </p>
42  */

43 public class OpenLocalFileAction extends Action implements IWorkbenchWindowActionDelegate {
44
45     private IWorkbenchWindow window;
46     private String JavaDoc filterPath;
47
48     /**
49      * Creates a new action for opening a local file.
50      */

51     public OpenLocalFileAction() {
52         setEnabled(true);
53     }
54
55     /* (non-Javadoc)
56      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
57      */

58     public void dispose() {
59         window = null;
60         filterPath = null;
61     }
62
63     /* (non-Javadoc)
64      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
65      */

66     public void init(IWorkbenchWindow window) {
67         this.window = window;
68         filterPath = System.getProperty("user.home"); //$NON-NLS-1$
69
}
70
71     /* (non-Javadoc)
72      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
73      */

74     public void run(IAction action) {
75         run();
76     }
77
78     /* (non-Javadoc)
79      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
80      */

81     public void selectionChanged(IAction action, ISelection selection) {
82     }
83
84     /* (non-Javadoc)
85      * @see org.eclipse.jface.action.Action#run()
86      */

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 JavaDoc[] names = dialog.getFileNames();
93
94         if (names != null) {
95             filterPath = dialog.getFilterPath();
96
97             int numberOfFilesNotFound = 0;
98             StringBuffer JavaDoc notFound = new StringBuffer JavaDoc();
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 JavaDoc 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 JavaDoc msgFmt = numberOfFilesNotFound == 1 ? IDEWorkbenchMessages.OpenLocalFileAction_message_fileNotFound : IDEWorkbenchMessages.OpenLocalFileAction_message_filesNotFound;
120                 String JavaDoc msg = NLS.bind(msgFmt, notFound.toString());
121                 MessageDialog.openError(window.getShell(), IDEWorkbenchMessages.OpenLocalFileAction_title, msg);
122             }
123         }
124     }
125 }
126
Popular Tags