Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » Programatically instatiating an editor on a file in a project
Programatically instatiating an editor on a file in a project [message #170498] Mon, 15 December 2003 12:47 Go to next message
Eclipse UserFriend
Originally posted by: jmyron.ioconcepts.com

I've spent a few days trying to figure out the following. My editor is a
debugger so I need to be able to switch around between files while I'm
stepping through the code. I've succeeded in being able to switch between
files if a file is already open in an editor. However this puts a
requirement on the user to have to go open any files he is going to be
stepping through before actually stepping into them.

Additionally I'd like to be to search a project and create an editor
instance for each file that has the particular extension my editor handles.

And if you really have a thorough understanding of how the project
management portion of the eclipse plugin API works: Given a file with the
correct extension that my editor handles, being able to programatically
create a link to it in a project and then open it would really be cool to
know how to do as well.

A small snippet of code would be nice but just telling me a class, how I
instantiate that class from within a plugin class and the method I would use
to instantiate the editior is cool.. The enviroment I'm working from is a
plugin class erived from the AbstractUIPlugin class.

If you don't know how to search the project but do know how to
programatically create an instance of the editor from within a plugin class
that would be a big help just knowing how to do that.

Thanks for any help anyone can provide (I'm running out of time).
J Myron Smith
I/O Concepts Inc.
jmyron@ioconcepts.com
http://www.ioconcepts.com
http://www.consoleautomation.com
Re: Programatically instatiating an editor on a file in a project [message #174164 is a reply to message #170498] Sat, 27 December 2003 17:41 Go to previous message
Eclipse UserFriend
Originally posted by: ed.burnette.REMOVE.THIS.sas.com

I've requested some new APIs to make this easier but here's a code snippet I
use that opens a file and goes to a line number in M6. The most important
parts are the page.openEditor() and root.getFileForLocation() methods.

/**
* Open a file in the Workbench that may or may not exist in the
workspace. Must be run on the
* UI thread.
*
* @param filename
* @param line
* @throws CoreException
*/
public static void openFile(String filename, int line) throws
CoreException {

// reject directories
if (new File(filename).isDirectory())
return;

IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
IWorkbenchPage page = window.getActivePage();
File file = new File(filename);

// If the file exists in the workspace, open it
IEditorInput input = createEditorInput(file);
String editorId = getEditorId(file);
IEditorPart editor = page.openEditor(input, editorId);
ITextEditor textEditor = (ITextEditor)
editor.getAdapter(ITextEditor.class);

// If a line number was given, go to it
if (line > 0) {
try {
line--; // document is 0 based
IDocument document =

textEditor.getDocumentProvider().getDocument(textEditor.getE ditorInput());
textEditor.selectAndReveal(
document.getLineOffset(line),
document.getLineLength(line));

} catch (BadLocationException e) {
// invalid text position -> do nothing
}
}
}

private static String getEditorId(File file) {
IWorkbench workbench = PlatformUI.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 static IEditorInput createEditorInput(File file) {
IFile workspaceFile = getWorkspaceFile(file);
if (workspaceFile != null)
return new FileEditorInput(workspaceFile);
// support for external files you probably don't need: return new
ExternalFileEditorInput(file);
return null;
}

private static IFile getWorkspaceFile(File file) {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IPath location = new Path(file.getAbsolutePath());
return workspace.getRoot().getFileForLocation(location);
}



--
Ed Burnette, co-author, Eclipse in Action
www.eclipsepowered.org

"J Myron Smith" <jmyron@ioconcepts.com> wrote in message
news:brks3h$s1$1@eclipse.org...
> programatically create an instance of the editor from within a plugin
class
> that would be a big help just knowing how to do that.
Previous Topic:Keyboard on Mac
Next Topic:Which class implements "Open External File..." command?
Goto Forum:
  


Current Time: Thu Sep 18 13:59:04 EDT 2025

Powered by FUDForum. Page generated in 0.04196 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top