我已经能够通过我在右键单击文件时出现的源菜单下创建的弹出菜单扩展成功更新文件的内容。
我想指出该文件已更改并需要保存。现在,文件内容会自动更改并保存。我认为 IFile.touch 方法会导致文件处于需要保存的状态,但我没有看到这种情况发生。
这是我的代码...
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
IEditorPart editorPart = HandlerUtil.getActiveEditor(event);
IEditorInput input = editorPart.getEditorInput();
InputStream is = null;
if (input instanceof FileEditorInput) {
IFile file = ((FileEditorInput) input).getFile();
try {
is = file.getContents();
String originalContents = convertStreamToString(is);
String newContents = originalContents + "testing changing the contents...";
InputStream newInput = new ByteArrayInputStream(newContents.getBytes());
file.setContents(newInput, false, true, null);
file.touch(null);
} catch (CoreException e) {
MessageDialog.openError(
window.getShell(),
"Generate Builder Error",
"An Exception has been thrown when interacting with file " + file.getName() +
": " + e.getMessage());
}
}
return null;
}