How does one get the filename of a document when it's loaded? [message #214084] |
Sat, 20 March 2004 19:12  |
Eclipse User |
|
|
|
Originally posted by: otupman-news.dts-workshop.com
Hi there,
I'm attempting to obtain the filename of the document that has just been
loaded (createDocument() in my document provider).
Some how I'd like to get the filename of the IDocument that's been created.
I've found the following code referenced in various places, but cannot
get it to work.
((FileEditorInput)getEditorInput()).getFile().getLocation();
When I use it the editor causes this error:
!MESSAGE Plug-in "com.rohanclan.cfml" was unable to instantiate class
"com.rohanclan.cfml.editors.CFMLEditor".
!STACK 0
java.lang.NullPointerException
at com.rohanclan.cfml.editors.CFMLEditor.<init>(CFMLEditor.java:80)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Nativ e Method)
I'm calling the line of code from the editor constructor. Is this the
corect place? Should I somehow call it from the document provider?
All I want to do is obtain the location of a document so I can insert
tasks into the task window.
Hope someone can help.
Cheers,
Oliver Tupman.
|
|
|
|
Re: How does one get the filename of a document when it's loaded? [message #214203 is a reply to message #214092] |
Sun, 21 March 2004 14:07   |
Eclipse User |
|
|
|
Originally posted by: otupman-news.dts-workshop.com
Cheers Dan,
Unfortunately I can't get the code to work :(
I have to admit at this stage that I'm a beginner Java programmer and
I'm still experiencing difficulties crossing over from C++.
My problem was that the 'theInstance' variable in the
MyDocumentProvider() method could not be found by the compiler. I
suspect that it's something amazingly simple that I've overlooked, but
where do I get theInstance from??
Though saying that, I have managed to get the filename out... but to say
it's a bodge is an understatement (a change listener, coupled with a
delta visitor triggered by saving the doc in createDocument). But at
least I can move on somewhat while I keep searching for solutions.
So I'm still open to suggestions!
Cheers,
Oliver.
nobody@nowhere.com wrote:
> I too wanted to get from IDocument to IFile.
>
> The solution I went for was the following:
> 1. Make sure there is only one instance of my DocumentProvider.
> Did this by registering my DocumentProvider as
> a org.eclipse.ui.editors.documentProviders extension and
> making sure that my document provider was only accessed
> through the DocumentProviderRegistry.
>
> While I was at it I added a static method 'getInstance'
> to my document provider so I wouldn't have to cast
> everywhere. This of course gets the instance through
> the DocumentProviderRegistry.
>
> 2. In MyDocumentProvider add a getFile( IDocument ) method
> that would find the IFile for an IDocument.
>
> See code below. A couple of notes:
> o getFile won't work until after the input element is 'connected'
>
> In my case this meant that MyDocumentProvider couldn't attach
> my DocumentPartitioner, which needs getFile to work, in
> createDocument. My workaround was to just attach the
> partitioner in getDocument. ( Of course checking to make
> sure the partitioner hadn't already been attached. )
> o In my case the file extension was created just so
> I could find my document provider. I don't actually
> create files with the extension. Menttion this on
> off chance you have a similar circumstance.
>
> -- Regards, Dan S
>
> public static MYDocumentProvider instance()
> {
> if ( theInstance == null)
> {
> final DocumentProviderRegistry dr
> = DocumentProviderRegistry.getDefault();
> // Ugh. Coding by side affect... Should cause framework to
> // instantiate our doc provider which will have the side
> affect
> // of initializing theInstance.
> dr.getDocumentProvider( Constants.DOCUMENT_EXTENSION_ID );
> if ( theInstance == null )
> {
> final String msg = "MYDocumentProvider not found provider
> for "
> + Constants.DOCUMENT_EXTENSION_ID;
> throw new IllegalState( msg );
> }
> }
> return theInstance;
> }
>
> public IFile getFile( final IDocument d )
> {
> IFile ret = null;
> for ( final java.util.Iterator i = getConnectedElements();
> i.hasNext() && ret == null; )
> {
> final Object o = i.next();
> if ( o instanceof IFileEditorInput )
> {
> final ElementInfo ei = getElementInfo( o );
> if ( d == ei.fDocument )
> {
> ret = ( ( IFileEditorInput )o ).getFile();
> }
> }
> }
> return ret;
> }
>
>
>>>>>>>>>>>>>>>>>>>Original Message <<<<<<<<<<<<<<<<<<
>
>
> On 3/20/2004, 4:12:39 PM, Oliver Tupman <otupman-news@dts-workshop.com>
> wrote regarding How does one get the filename of a document when it's
> loaded?:
>
>
>
>>Hi there,
>
>
>>I'm attempting to obtain the filename of the document that has just been
>>loaded (createDocument() in my document provider).
>
>
>>Some how I'd like to get the filename of the IDocument that's been
>
> created.
>
>
>>I've found the following code referenced in various places, but cannot
>>get it to work.
>
>
>> ((FileEditorInput)getEditorInput()).getFile().getLocation();
>
>
>>When I use it the editor causes this error:
>
>
>>!MESSAGE Plug-in "com.rohanclan.cfml" was unable to instantiate class
>>"com.rohanclan.cfml.editors.CFMLEditor".
>>!STACK 0
>>java.lang.NullPointerException
>> at com.rohanclan.cfml.editors.CFMLEditor.<init>(CFMLEditor.java:80)
>> at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Nativ e
>
> Method)
>
>
>>I'm calling the line of code from the editor constructor. Is this the
>>corect place? Should I somehow call it from the document provider?
>
>
>>All I want to do is obtain the location of a document so I can insert
>>tasks into the task window.
>
>
>>Hope someone can help.
>
>
>>Cheers,
>
>
>> Oliver Tupman.
>
>
|
|
|
Re: How does one get the filename of a document when it's loaded? [message #214256 is a reply to message #214203] |
Sun, 21 March 2004 22:06  |
Eclipse User |
|
|
|
>>>>>>>>>>>>>>>>>> Original Message <<<<<<<<<<<<<<<<<<
On 3/21/2004, 11:07:56 AM, Oliver Tupman <otupman-news@dts-workshop.com>
wrote regarding Re: How does one get the filename of a document when it's
loaded?:
> I have to admit at this stage that I'm a beginner Java programmer and
> I'm still experiencing difficulties crossing over from C++.
You have my sympathy. I code in Java cause have to and not
because I want to. If I had my way I'd be coding in C++...
( If Java's so great cause there are no pointers why do I keep
hearing about NullPointerExceptions? )
> My problem was that the 'theInstance' variable in the
> MyDocumentProvider() method could not be found by the compiler. I
> suspect that it's something amazingly simple that I've overlooked, but
> where do I get theInstance from??
Sorry, that's my fault. theInsance is a static member of
MyDocumentProvider.
private static MyDocumentProvider theInstance;
Also here's some other code that manages theInstance.
public MyDocumentProvider()
{
assert theInstance == null;
theInstance = this;
}
> Though saying that, I have managed to get the filename out... but to say
> it's a bodge is an understatement (a change listener, coupled with a
> delta visitor triggered by saving the doc in createDocument). But at
> least I can move on somewhat while I keep searching for solutions.
> So I'm still open to suggestions!
Well hope the snippets I provided above help some. That said my opinion
is that this stuff is kinda of a mess.
-- Regards, Dan S
|
|
|
Powered by
FUDForum. Page generated in 0.08317 seconds