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

情到浓时终转凉″ 提交于 2019-12-31 04:00:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!