TaT ReactivatedEditorWindows

Reactivating Editor Windows

Contributed by; Varun Nischal


I have been blogging around for a while on hyperlinks in NetBeans IDE, what about the API’s, yeah the NetBeans API’s that helped me achieve a crucial milestone, i.e. activating the Opened panes! Also, the members of OpenIDE mailing lists, for giving accurate suggestions for making it possible.

Tricks


You must see numerous Developer FAQ’s based on Editor and Edited Files. Some of them referring to get access of relevant opened Window for editing. Still, something was missing. So, I would share what I experienced recently.

TopComponent[] comps = TopComponent.getRegistry().getOpened();
for (int i = 0; i < comps.length; i++) {
    Node[[ | ]] arr = comps[I].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
            }
        }
    }
}

This has one issue, it no longers returns an array of TopComponent’s in the 1st line, in 6.0 and 6.1, I saw it returned Set<TopComponent>, so outer loop also changes like this-

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
            }
        }
    }
}

What about My Usecase?

Oh, yeah! I wanted to tell you about re-activating opened panes. After interacting with Wade Chandler on OpenIDE mailing lists. I created this method- verifyHyperlinkStatus(), please have a look, you might have to scroll a bit. So, I have commented 2 lines there-

//final int index = k;
/**
 * After some coding...
 */
//tc[Index].requestActive();

tc[[[index | [index]] is a particular TopComponent instance obtained by iterating over an Array of TopComponent’s. Array? But, we have just discussed its Set<TopComponent> not an Array. You’re in for another surprise-

//To obtain an array, use this-
final TopComponent[] tc = TopComponent.getRegistry().
getOpened().toArray(new TopComponent[0]);
//As mentioned in the following reference tutorial.

Referenced Tutorial- Revamped Hyperlink Navigation

final, but why?

Actually, when you deal with methods that required to run within a AWT Thread, invoked by various means, then the variables used, within their implemented run() method, if defined outside should be made final so as to prevent it from any modification. In Java, final modifier means that an identifier would be constant within a block of code. In this case, within a method- verifyHyperlinkStatus()

So, index was modified as final, and was used within an AWT Thread inside a if-block, which invoked tc[[[index | [index]].requestActive(), i.e. to activate that window, if its already opened.

Why I am doing this?

As I have to re-activate a opened editor window which was already opened by clicking a hyperlink in HTML file. So, I was earlier using setCaretPositon(), that’s why I had to requestActive(), to show that editor window again.

Tips to Remember


Vita suggested to use NbEditorUtilities instead of a combination of setCaretPosition() and requestActive(). So, I did that by commenting the following statements present in one of the setPosition() methods.

NbEditorUtilities.getLine(d, index, true).show(Line.SHOW_GOTO);
//pane[Pos].setCaretPosition(index);

Here, index is found using indexOf(), and d is the document, whose contents were used to retrieve the index. This is indeed a great utility, why? Because, it opens/re-activates the opened document and places the cursor at the beginning of the line, that was searched.


Adapted from Blog Entry- http://nbguru.wordpress.com/2008/07/25/tat-editor-windows-reactivated-8/

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