Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Open an editor and specify the line of the document
Open an editor and specify the line of the document [message #464927] Tue, 20 March 2007 15:43 Go to next message
Sebastien Pennec is currently offline Sebastien PennecFriend
Messages: 31
Registered: July 2009
Member
Hello,

I'm using the following line to open a file in an editor:

IDE.openEditor(page, file);

But I'd like to open the editor at a specific line... how can I do that?

Thanks for your help :)

Sébastien
Re: Open an editor and specify the line of the document [message #465022 is a reply to message #464927] Thu, 22 March 2007 00:04 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
Sebastien Pennec wrote:

> Hello,
>
> I'm using the following line to open a file in an editor:
>
> IDE.openEditor(page, file);
>
> But I'd like to open the editor at a specific line... how can I do that?

Check whether it's an ITextEditor and then exploit its API to select the
line or text range.

Dani

>
> Thanks for your help :)
>
> Sébastien
Re: Open an editor and specify the line of the document [message #465034 is a reply to message #465022] Thu, 22 March 2007 07:27 Go to previous messageGo to next message
Sebastien Pennec is currently offline Sebastien PennecFriend
Messages: 31
Registered: July 2009
Member
Hello Daniel,

I finally made it like this:

IWorkbenchPage page =
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(IMarker.LINE_NUMBER, new Integer(lineNumber));
map.put(IDE.EDITOR_ID_ATTR, "org.eclipse.jdt.internal.ui.javaeditor.JavaEditor");
try {
IMarker marker = file.createMarker(IMarker.TEXT);
marker.setAttributes(map);
IDE.openEditor(page, marker);
marker.delete();
} catch (PartInitException e) {
e.printStackTrace();
} catch (CoreException e) {
e.printStackTrace();
}

Is that a good practice?

Also, to obtain the IFile based on the name of the Java class I want to open, I had
to use org.eclipse.jdt.internal.core.JavaProject and
org.eclipse.jdt.internal.corext.util.JavaModelUtil, which are internal classes. Is
there a way to do that task without calling these classes?

Thanks,

Sébastien

Daniel Megert wrote:
> Sebastien Pennec wrote:
>
>> Hello,
>>
>> I'm using the following line to open a file in an editor:
>>
>> IDE.openEditor(page, file);
>>
>> But I'd like to open the editor at a specific line... how can I do that?
>
> Check whether it's an ITextEditor and then exploit its API to select the
> line or text range.
>
> Dani
>
>>
>> Thanks for your help :)
>>
>> Sébastien
Re: Open an editor and specify the line of the document [message #465048 is a reply to message #465034] Thu, 22 March 2007 11:16 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
Sebastien Pennec wrote:

> Hello Daniel,
>
> I finally made it like this:
>
> IWorkbenchPage page =
> PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage();
> HashMap<String, Object> map = new HashMap<String, Object>();
> map.put(IMarker.LINE_NUMBER, new Integer(lineNumber));
> map.put(IDE.EDITOR_ID_ATTR,
> "org.eclipse.jdt.internal.ui.javaeditor.JavaEditor");
> try {
> IMarker marker = file.createMarker(IMarker.TEXT);
> marker.setAttributes(map);
> IDE.openEditor(page, marker);
> marker.delete();
> } catch (PartInitException e) {
> e.printStackTrace();
> } catch (CoreException e) {
> e.printStackTrace();
> }
>
> Is that a good practice?

It is OK if the editor isn't a text editor. In your case it is and hence
you should use:

if (editor instanceof ITextEditor) {
((ITextEditor)editor).selectAndReveal(offset, length);
return;
}

>
> Also, to obtain the IFile based on the name of the Java class I want
> to open,

What do you know from that class? Just the name? The file's location?

Dani

> I had to use org.eclipse.jdt.internal.core.JavaProject and
> org.eclipse.jdt.internal.corext.util.JavaModelUtil, which are internal
> classes. Is there a way to do that task without calling these classes?
>
> Thanks,
>
> Sébastien
>
> Daniel Megert wrote:
>
>> Sebastien Pennec wrote:
>>
>>> Hello,
>>>
>>> I'm using the following line to open a file in an editor:
>>>
>>> IDE.openEditor(page, file);
>>>
>>> But I'd like to open the editor at a specific line... how can I do
>>> that?
>>
>>
>> Check whether it's an ITextEditor and then exploit its API to select
>> the line or text range.
>>
>> Dani
>>
>>>
>>> Thanks for your help :)
>>>
>>> Sébastien
>>
Re: Open an editor and specify the line of the document [message #465059 is a reply to message #465048] Thu, 22 March 2007 12:44 Go to previous messageGo to next message
Sebastien Pennec is currently offline Sebastien PennecFriend
Messages: 31
Registered: July 2009
Member
Daniel Megert wrote:

> It is OK if the editor isn't a text editor. In your case it is and hence
> you should use:
>
> if (editor instanceof ITextEditor) {
> ((ITextEditor)editor).selectAndReveal(offset, length);
> return;
> }
>

Ok I'll change my code to use that, thanks for the tip :)

>>
>> Also, to obtain the IFile based on the name of the Java class I want
>> to open,
>
> What do you know from that class? Just the name? The file's location?
>

I know the fully qualified class name and line number.

Sébastien
Re: Open an editor and specify the line of the document [message #465072 is a reply to message #465059] Thu, 22 March 2007 13:46 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
Sebastien Pennec wrote:

> Daniel Megert wrote:
>
>> It is OK if the editor isn't a text editor. In your case it is and
>> hence you should use:
>>
>> if (editor instanceof ITextEditor) {
>> ((ITextEditor)editor).selectAndReveal(offset, length);
>> return;
>> }
>>
>
> Ok I'll change my code to use that, thanks for the tip :)
>
>>>
>>> Also, to obtain the IFile based on the name of the Java class I want
>>> to open,
>>
>>
>> What do you know from that class? Just the name? The file's location?
>>
>
> I know the fully qualified class name and line number.

If you know the project you can create the Java project using
JavaCore.create(project). Then you need to copy the code from
JavaModelUtil which searches the type inside that Java project.

Dani

>
> Sébastien
Re: Open an editor and specify the line of the document [message #465074 is a reply to message #465072] Thu, 22 March 2007 14:16 Go to previous message
Sebastien Pennec is currently offline Sebastien PennecFriend
Messages: 31
Registered: July 2009
Member
Daniel Megert wrote:

>>> What do you know from that class? Just the name? The file's location?
>>>
>>
>> I know the fully qualified class name and line number.
>
> If you know the project you can create the Java project using
> JavaCore.create(project). Then you need to copy the code from
> JavaModelUtil which searches the type inside that Java project.
>

Ok, it works perfectly now, thanks! :)

I have another question, since we're on the subject of discouraged dependencies:

I want to get or create a JavaStackTraceConsole, set its content and show it to the
user. Up to now, I had to create one (hence the discouraged dependency) and add it to
the console manager, since when I call the console manager's getConsoles() method no
such console is returned.

Do you know of a best-practice way to create or retrieve such a console, set its
content and display it?

Thanks for your help,

Sébastien
Previous Topic:About dialog - Hide buttons
Next Topic:Making Views as Drop Target
Goto Forum:
  


Current Time: Wed Sep 11 21:08:41 GMT 2024

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

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

Back to the top