DevFaqGetOpenEditorWindows

Contents

How can I get a list of open editor windows?

To obtain a reference to the currently selected editor

Node[] arr = TopComponent.getRegistry().getCurrentNodes();
for (int i = 0; i < arr.length; i++) {
    EditorCookie ec = (EditorCookie) arr[i].getCookie(EditorCookie.class);
    if (ec != null) {
        JEditorPane[] panes = ec.getOpenedPanes();
        if (panes != null) {
            // USE panes
        }
    }
}

Note that EditorCookie.getOpenPanes() is deprecated (https://netbeans.org/bugzilla/show_bug.cgi?id=223383). Use org.openide.text.NbDocument.findRecentEditorPane(EditorCookie) instead.


To obtain references to all opened editors

Set<TopComponent> comps = TopComponent.getRegistry().getOpened();
for (TopComponent tc: comps) {
    Node[] arr = tc.getActivatedNodes();
    for (int j = 0; j < arr.length; j++) {
        EditorCookie ec = (EditorCookie) arr[j].getCookie(EditorCookie.class);
        if (ec != null) {
            JEditorPane[] panes = ec.getOpenedPanes();
            if (panes != null) {
                // USE panes
            }
        }
    }
}

Variants to get the Editor TopComponents

Variant A)
    private Collection<TopComponent> getCurrentOpenedEditors() {
        final ArrayList<TopComponent> result = new ArrayList<TopComponent>();
        final WindowManager wm = WindowManager.getDefault();
        for (Mode mode : wm.getModes()) {
            if (wm.isEditorMode(mode)) {
                //result.addAll(Arrays.asList(mode.getTopComponents())); OR even faster
                result.addAll(Arrays.asList(wm.getOpenedTopComponents(mode)));
            }
        }
        return result;
    }

Variant B)
    private TopComponent getCurrentEditor() {
	Set<? extends Mode> modes = WindowManager.getDefault().getModes();
	for (Mode mode : modes) {
	    if ("editor".equals(mode.getName())) {
		return mode.getSelectedTopComponent();
	    }
	}
	return null;
    }

    //or 
    private TopComponent getCurrentEditor() {
        Mode editor = WindowManager.getDefault().findMode("editor");
        return editor.getSelectedTopComponent();
    }

Reference- Editor Windows Reactivated


Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo