【问题标题】:How to display / edit / save a string using an eclipse text editor into a RCP application?如何使用 Eclipse 文本编辑器将字符串显示/编辑/保存到 RCP 应用程序中?
【发布时间】:2018-12-13 02:27:27
【问题描述】:

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

  1. 如何打开将字符串作为输入的编辑器?大多数IDE.openEditor(...) 实现都将 IFile 作为输入,但我不想使用文件作为中介。

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

【问题讨论】:

  • 您说“Eclipse 4 RCP”——这是纯 e4 RCP(使用 Application.e4xmi 并且仅使用 org.eclipse.e4.rcp 功能中的插件)吗?其他任何东西(例如使用IDEIFile)都是3.x 兼容模式RCP。
  • 对于 3.x 兼容性 RCP,您可以使用 IDE.openEditor 的变体,它采用 IEditorInput 和合适的 IEditorInput 实现。我找不到合适的现有编辑器输入实现,因此您必须编写一个 - 这需要大量工作来研究所需内容。
  • 是的,它是一个使用 Application.e4xmi 的 e4 RCP,但我们使用兼容层,因为编辑器本身等一些元素来自 3.x。

标签: java eclipse rcp


【解决方案1】:

我想通了。通过Rüdiger Herrmanngreg-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 应用程序中的“打开”和“保存”按钮。

【讨论】:

    猜你喜欢
    • 2014-04-10
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-06
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多