How to use IResourceChangeListener to detect file rename and dynamically change EditorPart name?

IResourceChangeListener listens for changes in the project workspace, for example, if the file name of the editor part has been changed.

I want to know how to access this particular EditorPart and change its name accordingly (for example, using .setPartName ), or maybe update the editor so that it automatically displays the new name.

An ideal would be if the IResourceChangeListener is of the Rename event type but does not look.

Link question .

+1
source share
1 answer

IResourceChangeListener fires rename / move events using a combination of the form REMOVED and a MOVED_TO ). You can check this in 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 for handling the new path might look something like this:

 IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(newPath); if (file != null) { setInput(new FileEditorInput(file)); setPartName(newPath.lastSegment()); ... anything else required } 
+4
source

Source: https://habr.com/ru/post/976359/


All Articles