Я понял это.Из 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.