close editor when delete occurs [message #17245] |
Tue, 19 August 2008 10:14  |
Eclipse User |
|
|
|
We have a custom view that has a tree explorer of certain files. These can
be used to launch an editor. When someone deletes a file from this tree, we
want the editor to also close. How do I do that? Is it editor changes or
changes to this view?
|
|
|
Re: close editor when delete occurs [message #17296 is a reply to message #17245] |
Tue, 19 August 2008 18:29  |
Eclipse User |
|
|
|
Originally posted by: eclipse-news.rizzoweb.com
Jay Simpson wrote:
> We have a custom view that has a tree explorer of certain files. These can
> be used to launch an editor. When someone deletes a file from this tree, we
> want the editor to also close. How do I do that? Is it editor changes or
> changes to this view?
>
>
What I did was create a specialized IResourceChangeListener instance
that calls
ResourcesPlugin.getWorkspace().addResourceChangeListener(thi s,
IResourceChangeEvent.POST_CHANGE) when it is created. My editor holds
the reference to the listener and disposes it when the editor is disposed.
The listener's resourceChanged() implementation walks the event's delta
looking for removed files and if one of them matches the editor's input
file, the listener closes the editor. See code below.
It is a complex answer to a seemingly simple problem, but I looked
through the Java and plain text editor code and did not find anything
simpler.
Hope this helps,
Eric
public void resourceChanged(IResourceChangeEvent event) {
// Only listening to these.
// if (event.getType() == IResourceDelta.POST_CHANGE)
{
IResourceDelta delta = event.getDelta();
try {
IResourceDeltaVisitor visitor = new IResourceDeltaVisitor() {
public boolean visit(IResourceDelta delta) {
if (delta.getFlags() != IResourceDelta.MARKERS &&
delta.getResource().getType() == IResource.FILE) {
if (delta.getKind() == IResourceDelta.REMOVED) {
IResource resource = delta.getResource();
if (resource.equals(editor.getInputFile())) {
closeEditor();
}
}
// TODO: handle IResourceDelta.CHANGED events by offering
to reload the file
}
return true;
}
};
delta.accept(visitor);
}
catch (CoreException exception) {
SkywayCoreUIPlugin.getDefault().logError("Error tracking resource
change " + event, exception); //$NON-NLS-1$
}
}
}
|
|
|
Re: close editor when delete occurs [message #577098 is a reply to message #17245] |
Tue, 19 August 2008 18:29  |
Eclipse User |
|
|
|
Jay Simpson wrote:
> We have a custom view that has a tree explorer of certain files. These can
> be used to launch an editor. When someone deletes a file from this tree, we
> want the editor to also close. How do I do that? Is it editor changes or
> changes to this view?
>
>
What I did was create a specialized IResourceChangeListener instance
that calls
ResourcesPlugin.getWorkspace().addResourceChangeListener(thi s,
IResourceChangeEvent.POST_CHANGE) when it is created. My editor holds
the reference to the listener and disposes it when the editor is disposed.
The listener's resourceChanged() implementation walks the event's delta
looking for removed files and if one of them matches the editor's input
file, the listener closes the editor. See code below.
It is a complex answer to a seemingly simple problem, but I looked
through the Java and plain text editor code and did not find anything
simpler.
Hope this helps,
Eric
public void resourceChanged(IResourceChangeEvent event) {
// Only listening to these.
// if (event.getType() == IResourceDelta.POST_CHANGE)
{
IResourceDelta delta = event.getDelta();
try {
IResourceDeltaVisitor visitor = new IResourceDeltaVisitor() {
public boolean visit(IResourceDelta delta) {
if (delta.getFlags() != IResourceDelta.MARKERS &&
delta.getResource().getType() == IResource.FILE) {
if (delta.getKind() == IResourceDelta.REMOVED) {
IResource resource = delta.getResource();
if (resource.equals(editor.getInputFile())) {
closeEditor();
}
}
// TODO: handle IResourceDelta.CHANGED events by offering
to reload the file
}
return true;
}
};
delta.accept(visitor);
}
catch (CoreException exception) {
SkywayCoreUIPlugin.getDefault().logError("Error tracking resource
change " + event, exception); //$NON-NLS-1$
}
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.03224 seconds