2c00L
2c00L

Reputation: 514

How to use an IResourceChangeListener to detect a file rename and set the EditorPart name dynamically?

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.

Upvotes: 1

Views: 1581

Answers (1)

greg-449
greg-449

Reputation: 111217

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 
 }

Upvotes: 4

Related Questions