3

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);
Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68
maro
  • 53
  • 9

3 Answers3

3

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

Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68
Tom Seidel
  • 9,525
  • 1
  • 26
  • 38
0

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();
    }
//...................
Nazik
  • 8,696
  • 27
  • 77
  • 123
Frank
  • 66
  • 4
0

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.

Manuel Selva
  • 18,554
  • 22
  • 89
  • 134