KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > editors > text > OpenExternalFileAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.editors.text;
12
13 import java.io.FileNotFoundException JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.util.ArrayList JavaDoc;
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 /**
55  * @since 3.0
56  */

57 public class OpenExternalFileAction extends Action implements IWorkbenchWindowActionDelegate {
58
59     static class FileLabelProvider extends LabelProvider {
60         /*
61          * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
62          */

63         public String JavaDoc getText(Object JavaDoc element) {
64             if (element instanceof IFile) {
65                 IPath path= ((IFile) element).getFullPath();
66                 return path != null ? path.toString() : ""; //$NON-NLS-1$
67
}
68             return super.getText(element);
69         }
70     }
71
72
73     private IWorkbenchWindow fWindow;
74     private String JavaDoc fFilterPath;
75
76     public OpenExternalFileAction() {
77         setEnabled(true);
78     }
79
80     /*
81      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
82      */

83     public void dispose() {
84         fWindow= null;
85         fFilterPath= null;
86     }
87
88     /*
89      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
90      */

91     public void init(IWorkbenchWindow window) {
92         fWindow= window;
93         fFilterPath= System.getProperty("user.home"); //$NON-NLS-1$
94
}
95
96     /*
97      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
98      */

99     public void run(IAction action) {
100         run();
101     }
102
103     /*
104      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
105      */

106     public void selectionChanged(IAction action, ISelection selection) {
107     }
108
109     /*
110      * @see org.eclipse.jface.action.Action#run()
111      */

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 JavaDoc[] names= dialog.getFileNames();
118
119         if (names != null) {
120             fFilterPath= dialog.getFilterPath();
121
122             int numberOfFilesNotFound= 0;
123             StringBuffer JavaDoc notFound= new StringBuffer JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc msgFmt= numberOfFilesNotFound == 1 ? TextEditorMessages.OpenExternalFileAction_message_fileNotFound : TextEditorMessages.OpenExternalFileAction_message_filesNotFound;
147                 String JavaDoc msg= NLSUtility.format(msgFmt, notFound.toString());
148                 MessageDialog.openError(fWindow.getShell(), TextEditorMessages.OpenExternalFileAction_title, msg);
149             }
150         }
151     }
152
153     /*
154      * XXX: Requested a helper to get the correct editor descriptor
155      * see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=110203
156      */

157     private String JavaDoc getEditorId(IFileStore file) {
158         IWorkbench workbench= fWindow.getWorkbench();
159         IEditorRegistry editorRegistry= workbench.getEditorRegistry();
160         IEditorDescriptor descriptor= editorRegistry.getDefaultEditor(file.getName(), getContentType(file));
161
162         // check the OS for in-place editor (OLE on Win32)
163
if (descriptor == null && editorRegistry.isSystemInPlaceEditorAvailable(file.getName()))
164             descriptor= editorRegistry.findEditor(IEditorRegistry.SYSTEM_INPLACE_EDITOR_ID);
165         
166         // check the OS for external editor
167
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 JavaDoc stream= null;
181         try {
182             stream= fileStore.openInputStream(EFS.NONE, null);
183             return Platform.getContentTypeManager().findContentTypeFor(stream, fileStore.getName());
184         } catch (IOException JavaDoc x) {
185             EditorsPlugin.log(x);
186             return null;
187         } catch (CoreException x) {
188             // Do not log FileNotFoundException (no access)
189
if (!(x.getStatus().getException() instanceof FileNotFoundException JavaDoc))
190                 EditorsPlugin.log(x);
191             
192             return null;
193         } finally {
194             try {
195                 if (stream != null)
196                     stream.close();
197             } catch (IOException JavaDoc 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 JavaDoc existentFiles= new ArrayList JavaDoc(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