问题
IResourceChangeListener
listens to changes in project workspace for example if the editor part file name has changed.
I want to know how to access that particular EditorPart
and change its title name accordingly (e.g. with .setPartName
), or maybe the refresh the editor so that it shows the new name automatically.
Ideal would be if the IResourceChangeListener
has a Rename event type but does not seem to be the case.
Reference question.
回答1:
The IResourceChangeListener
does fire for rename/move events using a combination of the REMOVED
kind and a MOVED_TO
flag). You can test for that in the IResourceDelta
with
@Override
public void resourceChanged(final IResourceChangeEvent event)
{
IResourceDelta delta = event.getDelta();
// Look for change to our file
delta = delta.findMember(IPath of file being edited);
if (delta == null)
return;
if delta.getKind() == IResourceDelta.REMOVED
{
if ((delta.getFlags() & IResourceDelta.MOVED_TO) != 0)
{
IPath newPath = delta.getMovedToPath();
... handle new path
}
}
}
The code to handle the new path might be something like:
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(newPath);
if (file != null)
{
setInput(new FileEditorInput(file));
setPartName(newPath.lastSegment());
... anything else required
}
来源:https://stackoverflow.com/questions/30170492/how-to-use-an-iresourcechangelistener-to-detect-a-file-rename-and-set-the-editor