当前位置: 首页 > 知识库问答 >
问题:

如何使用eclipse文本编辑器将字符串显示/编辑/保存到RCP应用程序中?

罗梓
2023-03-14

我正在开发一个Eclipse4 RCP应用程序。我对某些部分和一个编辑有一个观点。编辑器的目的是打开、编辑和保存一个字符串。

编辑完编辑器的内容后,如何将其保存成字符串?当使用文件时,编辑器将其内容直接保存到文件中。

共有1个答案

松和璧
2023-03-14

我想出来了。从吕迪格,赫尔曼和格雷格-449我能够建立这个原型。

class StringEditorInput implements IStorageEditorInput {

    private IStorage storage;

    public StringEditorInput(IStorage storage) {

        this.storage = Objects.requireNonNull(storage, "Storage object cannot be null.");
    }

    @Override
    public boolean exists() {
        return true;
    }

    @Override
    public IStorage getStorage() throws CoreException {
        return storage;
    }

   /* Uninteresting methods left out for brevity */
}

class StringStorage implements IStorage {

    private String content;

    public StringStorage(String content) {

        this.content = Objects.requireNonNull(content, "The new content string cannot be null.");
    }

    @Override
    public InputStream getContents() throws CoreException {
        return new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
    }

    /* Uninteresting methods left out for brevity */
}

/**
 * Set the text in the PDDL editor.
 *
 * @param text
 *            PDDL code to show in the editor.
 */
public void setEditorText(String text) {

    String editorId = "pl.poznan.put.cs.gui4pddl.PDDLEditor";
    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();

    try {
        editor = IDE.openEditor(page, new StringEditorInput(new StringStorage(text)), editorId);
    } catch (PartInitException e) {
        e.printStackTrace();
    }
}

/**
 * Get the text currently displayed in the PDDL editor.
 *
 * @return PDDL code.
 */
public String getEditorText() {

    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    page.saveEditor(editor, false);

    String editorText = "";

    if (editor instanceof ITextEditor) {
        ITextEditor textEditor = (ITextEditor) editor;

        IDocumentProvider provider = textEditor.getDocumentProvider();

        IEditorInput input = editor.getEditorInput();

        IDocument document = provider.getDocument(input);

        editorText = document.get();
    }

    return editorText;
}

请注意,SetEditorText(String text)GetEditorText()以某种方式连接到我的RCP应用程序中的“打开”和“保存”按钮。

 类似资料:
  • 我正在开发一个RCP应用程序,它使用Groovy作为脚本语言。我包含了Groovy Eclipse插件,让应用程序的用户使用Groovy编辑器在应用程序中编写Groovy代码。 Groovy脚本必须使用应用程序中的一些Java类,我如何在Groovy编辑器中为这些类添加可见性,以尽可能透明地为用户提供代码完成等功能。 要执行Groovy脚本,我使用GroovyShell,并将一个属性添加到传递给G

  • 问题内容: 我正在开发一个插件。 单击按钮时,我想调用Eclipse的save方法或调用Eclipse工具栏上的save按钮。 怎么做呢? 问题答案: 应该可以。 如果要保存活动的编辑器,请尝试 请注意,导航路径中的元素可以为null。

  • Android Studio中我的应用程序的可以编辑,但编辑完文本后,退出窗口时将不会保存。怎么办?

  • Eclipse 4 RCP应用程序支持浮动编辑器窗口。 使用兼容性层,我将一组RCP应用程序从Eclipse 3.8移植到Eclipse 4.4。这些应用程序不是为浮动编辑器设计的。我想把重新设计的费用推迟到以后。 在注释40中,浮动编辑器窗口的Eclipse增强请求提到了控制编辑器窗口策略的可取性: 在e4中,理想情况下,我们能够在布局的任何地方“托管”当前的编辑器或视图。将其位置限制在编辑器区

  • 在Python的交互式命令行写程序,好处是一下就能得到结果,坏处是没法保存,下次还想运行的时候,还得再敲一遍。 所以,实际开发的时候,我们总是使用一个文本编辑器来写代码,写完了,保存为一个文件,这样,程序就可以反复运行了。 现在,我们就把上次的'hello, world'程序用文本编辑器写出来,保存下来。 那么问题来了:文本编辑器到底哪家强? 推荐两款文本编辑器: 一个是Sublime Text,

  • 在Python的交互式命令行写程序,好处是一下就能得到结果,坏处是没法保存,下次还想运行的时候,还得再敲一遍。 所以,实际开发的时候,我们总是使用一个文本编辑器来写代码,写完了,保存为一个文件,这样,程序就可以反复运行了。 现在,我们就把上次的'hello, world'程序用文本编辑器写出来,保存下来。 那么问题来了:文本编辑器到底哪家强? Visual Studio Code! 我们推荐微软出