0

I'm working on an Eclipse 4 RCP application. I have a perspective with some parts and an editor. The editor's purpose is to open, edit and save a String.

  1. How can I open an editor that takes as input the string? Most of the IDE.openEditor(...) implementations take an IFile as input but I don't want to use a file as an intermediary.

  2. After editing the content of the editor, how can I save it into a string? when using a file the editor saves its content directly into the file.

3
  • You say 'Eclipse 4 RCP' - is this a pure e4 RCP (using an Application.e4xmi and only plug-ins in the the org.eclipse.e4.rcp feature)? Anything else (such as using IDE or IFile) is a 3.x compatability mode RCP. Commented Dec 13, 2018 at 8:02
  • For a 3.x compatability RCP you could use the variant of IDE.openEditor which takes an IEditorInput with a suitable IEditorInput implementation. I can't find a suitable existing editor input implementation so you would have to write one - which requires quite a lot of work researching what is needed. Commented Dec 13, 2018 at 8:14
  • Yes, it an e4 RCP using Application.e4xmi but we use the compatibility layer as some elements like the editor itself are from 3.x. Commented Dec 13, 2018 at 14:03

1 Answer 1

0

I figured it out. From Rüdiger Herrmann and greg-449 I was able to build this prototype.

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;
}

Note that setEditorText(String text) and getEditorText() are somehow connected to the buttons Open and Save in my RCP application.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.