Eclipse RCP - 自定义重命名参与者

时间:2017-10-16 19:40:21

标签: java eclipse eclipse-rcp renaming automated-refactoring

我创建了一个自定义RenamePartipant,如下所示:

<extension point="org.eclipse.ltk.core.refactoring.renameParticipants">
  <renameParticipant class="com.project.explorer.RenameParticipantAction"
        id="com.project.explorer.renameParticipant1"
        name="Rename">
     <enablement>
        <with variable="affectedNatures">
           <iterate operator="or">
              <equals  value="data.nature">
              </equals>
           </iterate>
        </with>
        <with variable="element">
           <instanceof  value="org.eclipse.core.resources.IResource">
           </instanceof>
        </with>
     </enablement>
  </renameParticipant>
</extension>

,自定义重命名参与者createChange方法如下:

@Override
public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {

    pm.beginTask("Rename task ", 1);
    CompositeChange change = new CompositeChange("Renaming " + changedResource.getName());

        if (isDataFile(changedResource.getName())) {
            return new DataResourceChange(changedResource.getFullPath(), getArguments().getNewName());
        }
    }

    pm.done();
    return change;
}

和自定义Change对象如下:

public class DataResourceChange extends ResourceChange {

private IPath resourcePath;
private String resourceNewName;
private long stampToRestore;

public DataResourceChange(IPath resourcePath, String resourceNewName) {
    this(resourcePath, resourceNewName, IResource.NULL_STAMP);
}

protected DataResourceChange(IPath resourcePath, String newName, long 
stampToRestore) {
    this.resourcePath = resourcePath;
    this.resourceNewName = newName;
    this.stampToRestore = stampToRestore;
}

@Override
protected IResource getModifiedResource() {
    return 
ResourcesPlugin.getWorkspace().getRoot().findMember(resourcePath);
}

@Override
public Change perform(IProgressMonitor pm) throws CoreException {
    pm.beginTask("Rename resource", 1);

    // IResource resource= getModifiedResource();
}

}

每当我重命名文件时,首先调用RenameResourceChange.perform()来修改资源。

之后,每当我在自定义更改类中调用getModifiedResource()方法时,它都会为null(因为资源已被移动)。

如何跳过默认的RenameResourceChange.createChange()方法?

0 个答案:

没有答案