开发者

Close editor if button inside this editor is pressed?? (RCP eclipse)

favorite 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. The following solution helped me:

// 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);

….but I still have some problems... As I can open many editors in the same time, and all of them have the same button "save", it happens that I press "save" in editor1 but close editor3... Actually, I save the last editor to be open (thanks to its "item")... this is the problem.. So I'm wondering if there is a way to identify the editor in which the button exists, so that I close it.. Thanks a lot I appreciate any help or hint (Sorry if my questions look easy and not worth being asked, but I'm still a beginner...)


if the Button is rendered in your IEditorPart implementation, you can close the editor directly in your EditorPart.

button.addListener(SWT.Selection, new Listener() {

    @Override
    public void handleEvent(Event event) {

        PlatformUI.getWorkbench().getActiveWorkbenchWindow()
                .getActivePage().closeEditor(this, true);

    }

});


Selected editor open or another editor can be close using RCP eclipse.

Multiple Editor open at time selected editor can be open or close using RCP eclipse.

    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
    IWorkbenchPage page = window.getActivePage();
    EmployeeEditorInput input = new EmployeeEditorInput();

    //List out all the editors open
    IEditorReference[] editors = page.getEditorReferences();

    for (int i=0; i<editors.length; i++) {

        //class : EmployeeEditor 
        //public static final String Id="rcp_demo.Editor.emp";  

          if (editors[i].getId().equals(EmployeeEditor.Id)) {
            page.activate(editors[i].getEditor(true));

            //or
            //page.closeEditor(page.getActiveEditor(),true);
            System.out.println("Employee Editor Exist");
            return null;
          } 
          else
          {
             page.closeEditor(page.getActiveEditor(), true);
             System.out.println("Close other Editor");  
          }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜