Как отобразить / отредактировать / сохранить строку с помощью текстового редактора Eclipse в приложении RCP? - PullRequest
0 голосов
/ 13 декабря 2018

Я работаю над приложением Eclipse 4 RCP.У меня есть перспектива с некоторыми частями и редактором.Цель редактора - открыть, отредактировать и сохранить строку.

  1. Как открыть редактор, который принимает в качестве входной строки?Большинство IDE.openEditor(...) реализаций принимают IFile в качестве входных данных, но я не хочу использовать файл в качестве посредника.

  2. После редактирования содержимого редактора, как я могу сохранитьэто в строку?при использовании файла редактор сохраняет его содержимое непосредственно в файл.

1 Ответ

0 голосов
/ 13 декабря 2018

Я понял это.Из Rüdiger Herrmann и greg-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.

...