Eclipse 如何指示文件在使用IFile修改后已更改

Eclipse 如何指示文件在使用IFile修改后已更改,eclipse,eclipse-plugin,Eclipse,Eclipse Plugin,我已经能够通过我在右键单击文件时出现的源菜单下创建的弹出菜单扩展成功地更新文件的内容 我想指出文件已更改,需要保存。现在,文件内容会自动更改和保存。我认为IFile.touch方法会导致文件处于需要保存的状态,但我不认为会发生这种情况 这是我的密码 public Object execute(ExecutionEvent event) throws ExecutionException { IWorkbenchWindow window = HandlerUtil.getActiveWo

我已经能够通过我在右键单击文件时出现的源菜单下创建的弹出菜单扩展成功地更新文件的内容

我想指出文件已更改,需要保存。现在,文件内容会自动更改和保存。我认为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;
}

如果要将文件的内容标记为需要保存,则需要与要提示为需要保存的内存中表示进行交互—从中获取输入的编辑器。

基于nitind和本文的帮助,我可以更新文本编辑器,文件显示为已修改。关键是使用ITextEditor和IDocumentProvider,而不是使用文件编辑器

这是更新后的代码

public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
    IEditorPart editorPart = HandlerUtil.getActiveEditor(event);
    if (editorPart instanceof ITextEditor ) {
        ITextEditor editor = (ITextEditor)editorPart;
        IDocumentProvider prov = editor.getDocumentProvider();
        IDocument doc = prov.getDocument( editor.getEditorInput() );
        String className = getClassName(doc.get());
        ISelection sel = editor.getSelectionProvider().getSelection();
        if (sel instanceof TextSelection ) {
            TextSelection textSel = (TextSelection)sel;
            String newText = generateBuilderText(className, textSel.getText());
            try {
                doc.replace(textSel.getOffset(), textSel.getLength(), newText);
            } catch (Exception e) {
                MessageDialog.openError(
                    window.getShell(),
                    "Generate Builder Error",
                    "An Exception has been thrown when attempting to replace " 
                    + "text in editor " + editor.getTitle() +
                    ": " + e.getMessage());
            }
        }
    }
    return null;
}

好的-我是PDE开发的新手…我一直在摸索,似乎
IWorkbenchPage.setPartState
可能是我用于此的方法-听起来正确吗?嗯,不。如果它实际上与磁盘上的内容不同,则需要保存。它是?如果是这样的话,怎么做?也许我的做法是错误的…我最终只是尝试在编辑器中更新一些文本,然后用户可以保存或放弃更改,如果他们愿意的话…很难找到执行此操作的示例代码…如果您使用的是文本编辑器,它应该实现ITextEditor并具有IDocumentProvider,您可以使用ITextEditor.getDocumentProvider()获取它,然后使用它为编辑器的当前输入提供相应的IDocument。这就是你可以修改编辑内容的地方——它会将自己标记为肮脏,需要保存,就像你想要的一样。谢谢——那条评论让我找到了正确的地方;)-我将发布解决我问题的代码。。。