簡體   English   中英

如何使用Eclipse文本編輯器在RCP應用程序中顯示/編輯/保存字符串?

[英]How to display / edit / save a string using an eclipse text editor into a RCP application?

我正在開發Eclipse 4 RCP應用程序。 我有一些部分和一個編輯器的觀點。 編輯器的目的是打開,編輯和保存字符串。

  1. 如何打開將字符串作為輸入的編輯器? 大多數IDE.openEditor(...)實現都將IFile作為輸入,但我不想將文件用作中介。

  2. 編輯編輯器的內容后,如何將其保存為字符串? 使用文件時,編輯器會將其內容直接保存到文件中。

我想到了。 通過RüdigerHerrmanngreg-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應用程序中的“打開”和“保存”按鈕。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM