taT | Editor Windows Reactivated

Hey,

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 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] 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- http://wiki.netbeans.org/RevampedHyperlinkNavigation

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].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.

This blog has been contributed to NetBeans Community Docs Program and for latest updates on this content, refer to the contribution.

Final thoughts

This time all was said and done, rather than the saying “All is said, more than done!”.

One thought on “taT | Editor Windows Reactivated

  1. Pingback: 10 Tips and Tricks « N, Varun

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s