Jump to: navigation, search

Difference between revisions of "FAQ How do I open an editor on a file outside the workspace?"

m
 
Line 1: Line 1:
 
Since 3.3 you can use the new EFS support to open an text editor on a file outside the workspace:
 
Since 3.3 you can use the new EFS support to open an text editor on a file outside the workspace:
<pre>
+
<source lang="java">
  String name= new FileDialog(aShell, SWT.OPEN).open();
+
String name= new FileDialog(aShell, SWT.OPEN).open();
  if (name == null)
+
if (name == null)
      return;
+
    return;
  IFileStore fileStore= EFS.getLocalFileSystem().getStore(new Path(filterPath));
+
IFileStore fileStore = EFS.getLocalFileSystem().getStore(new Path(filterPath));
  fileStore= fileStore.getChild(names[i]);
+
fileStore = fileStore.getChild(names[i]);
  if (!fileStore.fetchInfo().isDirectory() && fileStore.fetchInfo().exists()) {
+
if (!fileStore.fetchInfo().isDirectory() && fileStore.fetchInfo().exists()) {
    IWorkbenchPage page=  window.getActivePage();
+
    IWorkbenchPage page=  window.getActivePage();
    try {
+
    try {
      IDE.openEditorOnFileStore(page, fileStore);
+
        IDE.openEditorOnFileStore(page, fileStore);
    } catch (PartInitException e) {
+
    } catch (PartInitException e) {
      /* some code */
+
        /* some code */
    }
+
    }
</pre>
+
}
 +
</source>
  
 
Alternatively, you can create a <i>linked resource</i> in an existing project, which points to a file elsewhere in the file system.  This example snippet creates a project called &#147;External Files,&#148; and then prompts the user to select any file in the file system.  The code then creates a linked resource in the project to that external file,  allowing the platform to open the file in read/write mode in one of the standard editors:
 
Alternatively, you can create a <i>linked resource</i> in an existing project, which points to a file elsewhere in the file system.  This example snippet creates a project called &#147;External Files,&#148; and then prompts the user to select any file in the file system.  The code then creates a linked resource in the project to that external file,  allowing the platform to open the file in read/write mode in one of the standard editors:
  
<pre>
+
<source lang="java">
  IWorkspace ws = ResourcesPlugin.getWorkspace();
+
IWorkspace ws = ResourcesPlugin.getWorkspace();
  IProject project = ws.getRoot().getProject("External Files");
+
IProject project = ws.getRoot().getProject("External Files");
  if (!project.exists())
+
if (!project.exists())
      project.create(null);
+
    project.create(null);
  if (!project.isOpen())
+
if (!project.isOpen())
      project.open(null);
+
    project.open(null);
  Shell shell = window.getShell();
+
Shell shell = window.getShell();
  String name = new FileDialog(shell, SWT.OPEN).open();
+
String name = new FileDialog(shell, SWT.OPEN).open();
  if (name == null)
+
if (name == null)
      return;
+
    return;
  IPath location = new Path(name);
+
IPath location = new Path(name);
  IFile file = project.getFile(location.lastSegment());
+
IFile file = project.getFile(location.lastSegment());
  file.createLink(location, IResource.NONE, null);
+
file.createLink(location, IResource.NONE, null);
  IWorkbenchPage page = window.getActivePage();
+
IWorkbenchPage page = window.getActivePage();
  if (page != null)
+
if (page != null)
      page.openEditor(file);
+
    page.openEditor(file);
</pre>
+
</source>
  
 
== See Also: ==
 
== See Also: ==

Latest revision as of 09:44, 9 September 2008

Since 3.3 you can use the new EFS support to open an text editor on a file outside the workspace:

String name= new FileDialog(aShell, SWT.OPEN).open();
if (name == null)
    return;
IFileStore fileStore = EFS.getLocalFileSystem().getStore(new Path(filterPath));
fileStore = fileStore.getChild(names[i]);
if (!fileStore.fetchInfo().isDirectory() && fileStore.fetchInfo().exists()) {
    IWorkbenchPage page=  window.getActivePage();
    try {
        IDE.openEditorOnFileStore(page, fileStore);
    } catch (PartInitException e) {
        /* some code */
    }
}

Alternatively, you can create a linked resource in an existing project, which points to a file elsewhere in the file system. This example snippet creates a project called “External Files,” and then prompts the user to select any file in the file system. The code then creates a linked resource in the project to that external file, allowing the platform to open the file in read/write mode in one of the standard editors:

IWorkspace ws = ResourcesPlugin.getWorkspace();
IProject project = ws.getRoot().getProject("External Files");
if (!project.exists())
    project.create(null);
if (!project.isOpen())
    project.open(null);
Shell shell = window.getShell();
String name = new FileDialog(shell, SWT.OPEN).open();
if (name == null)
    return;
IPath location = new Path(name);
IFile file = project.getFile(location.lastSegment());
file.createLink(location, IResource.NONE, null);
IWorkbenchPage page = window.getActivePage();
if (page != null)
    page.openEditor(file);

See Also:


This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.