| /********************************************************************** |
| Copyright (c) 2000, 2003 IBM Corp. and others. |
| All rights reserved. This program and the accompanying materials |
| are made available under the terms of the Common Public License v1.0 |
| which accompanies this distribution, and is available at |
| http://www.eclipse.org/legal/cpl-v10.html |
| |
| Contributors: |
| IBM Corporation - Initial implementation |
| **********************************************************************/ |
| package org.eclipse.ui.internal.editors.text; |
| |
| import java.io.File; |
| |
| import org.eclipse.core.resources.IFile; |
| import org.eclipse.core.resources.IWorkspace; |
| import org.eclipse.core.resources.ResourcesPlugin; |
| import org.eclipse.core.runtime.IPath; |
| import org.eclipse.core.runtime.Path; |
| |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.widgets.FileDialog; |
| |
| import org.eclipse.jface.action.Action; |
| import org.eclipse.jface.action.IAction; |
| import org.eclipse.jface.viewers.ISelection; |
| |
| import org.eclipse.ui.IEditorDescriptor; |
| import org.eclipse.ui.IEditorInput; |
| import org.eclipse.ui.IEditorRegistry; |
| import org.eclipse.ui.IWorkbench; |
| import org.eclipse.ui.IWorkbenchPage; |
| import org.eclipse.ui.IWorkbenchWindow; |
| import org.eclipse.ui.IWorkbenchWindowActionDelegate; |
| import org.eclipse.ui.PartInitException; |
| import org.eclipse.ui.part.FileEditorInput; |
| |
| /** |
| * @since 3.0 |
| */ |
| public class OpenExternalFileAction extends Action implements IWorkbenchWindowActionDelegate { |
| |
| private IWorkbenchWindow fWindow; |
| |
| public OpenExternalFileAction() { |
| setEnabled(true); |
| } |
| |
| /* |
| * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose() |
| */ |
| public void dispose() { |
| fWindow= null; |
| } |
| |
| /* |
| * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow) |
| */ |
| public void init(IWorkbenchWindow window) { |
| fWindow= window; |
| } |
| |
| /* |
| * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction) |
| */ |
| public void run(IAction action) { |
| run(); |
| } |
| |
| /* |
| * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection) |
| */ |
| public void selectionChanged(IAction action, ISelection selection) { |
| } |
| |
| private File queryFile() { |
| FileDialog dialog= new FileDialog(fWindow.getShell(), SWT.OPEN); |
| dialog.setText(TextEditorMessages.getString("OpenExternalFileAction.dialog.text")); //$NON-NLS-1$ |
| String path= dialog.open(); |
| if (path != null) |
| return new File(path); |
| return null; |
| } |
| |
| /* |
| * @see org.eclipse.jface.action.Action#run() |
| */ |
| public void run() { |
| File file= queryFile(); |
| if (file != null && file.exists()) { |
| IEditorInput input= createEditorInput(file); |
| String editorId= getEditorId(file); |
| IWorkbenchPage page= fWindow.getActivePage(); |
| try { |
| page.openEditor(input, editorId); |
| } catch (PartInitException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| |
| private String getEditorId(File file) { |
| IWorkbench workbench= fWindow.getWorkbench(); |
| IEditorRegistry editorRegistry= workbench.getEditorRegistry(); |
| IEditorDescriptor descriptor= editorRegistry.getDefaultEditor(file.getName()); |
| if (descriptor != null) |
| return descriptor.getId(); |
| return "org.eclipse.ui.DefaultTextEditor"; //$NON-NLS-1$ |
| } |
| |
| private IEditorInput createEditorInput(File file) { |
| IFile workspaceFile= getWorkspaceFile(file); |
| if (workspaceFile != null) |
| return new FileEditorInput(workspaceFile); |
| return new JavaFileEditorInput(file); |
| } |
| |
| private IFile getWorkspaceFile(File file) { |
| IWorkspace workspace= ResourcesPlugin.getWorkspace(); |
| IPath location= new Path(file.getAbsolutePath()); |
| return workspace.getRoot().getFileForLocation(location); |
| } |
| } |