Reputation: 53
I have a UI in which when I select an item (in a tree) and then press a button "add", I get a new editor. With each item I can get an editor. (but all have the same ID)
My purpose is to close only the editor of item1, for example, when I press "save".
I'm able to close all the editors with:
getSite().getWorkbenchWindow().getActivePage().closeAllEditors(true);
But not only the one that I need to close.
I think, this problem might be solved using the IEditorreferences but don't know exactly how to do it! :( please help :)
List<IEditorReference> editors = new ArrayList<IEditorReference>();
for (IWorkbenchWindow window : PlatformUI.getWorkbench().getWorkbenchWindows()) {
for (IWorkbenchPage page : window.getPages()) {
for (IEditorReference editor : page.getEditorReferences()) {
editors.add(editor);
}
}
}
getSite().getWorkbenchWindow().getActivePage().closeEditor(editors.get(index)????,true);
Upvotes: 3
Views: 2103
Reputation: 9535
Editor can be tracked with the editor-input. The object representing item1 must be part of your editor-input...
Something like:
// Creating and opening
MyObject item1 = ... //create item1
// open editor
myInput = new MyEditorInput(item1)
IDE.openEditor(workbenchPage, myInput, MY_EDITOR_ID);
// Closing
tmpInput = new MyEditorInput(item1)
IEditorReference[] editorReferences = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage()
.getEditorReferences();
List<IEditorReference> relevantEditors = new ArrayList<IEditorReference>();
for (IEditorReference iEditorReference : editorReferences) {
if (iEditorReference.getEditorInput().equals(tmpInput)) {
relevantEditors.add(iEditorReference);
}
}
PlatformUI
.getWorkbench()
.getActiveWorkbenchWindow()
.getActivePage()
.closeEditors(
(IEditorReference[]) relevantEditors.toArray(new IEditorReference[relevantEditors
.size()]), true);
Make sure that you have overriden the equals and hashCode of your EditorInput to check the equality of the wrapped MyObject
-instance
Upvotes: 3
Reputation: 96
thanks to Tom, your answer helps a lot.
As each IEditorInput could have its name that can be set, we also can use like followings:
// String str =.....
// str, could be an editor's property
if (iEditorReference.getEditorInput().getName().equals(str))
Besides, it shall throw PartInitException like this:
//....................
try {
for (IEditorReference iEditorReference : editorReferences) {
if (iEditorReference.getEditorInput().getName().equals(str)) {
relevantEditors.add(iEditorReference);
}
}
} catch (PartInitException e) {
e.printStackTrace();
}
//...................
Upvotes: 0
Reputation: 19050
When opening your editor you have to track the mapping between your items and the associated opened IEditorReference
This can be done for example using a simple HashMap
object.
Upvotes: 0