07.25.08

taT | Editor Windows Reactivated!

Posted in Java, NetBeans, Platform, Tips|Tricks at 2:14 pm by Varun

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.

Final thoughts

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

07.17.08

taT | Reload Debugging (debug-fix-nb)

Posted in NetBeans, Tips|Tricks, nb-61 at 11:18 pm by Varun

Just few days back, I talked about that its rather a good practice to debug, not to run! While following that, I discovered another thing, which was there for quite some time, but I didn’t notice it, as I didn’t debug :P

    <target name="debug-fix-nb" if="netbeans.home"
depends="init,-pre-debug-fix,compile-single">
        <nbjpdareload>
            <fileset includes="${fix.class}*.class"
dir="${build.classes.dir}"/>
        </nbjpdareload>
    </target>

You must be thinking, what’s “debug-fix-nb”, its actually an Ant target, ran while we apply Code fixes during Debugging! Shocking, Indeed! I felt the same way, actually while building a module to run it in Target Platform, I realized I could reload it as well, without building whole module again.

Goto Installation Directory, and look for harness/common.xml
It contains many Ant targets, including the above…

Similar functionality exists, when you want to reload while debugging, HOW? Here lies the trick, while I was debugging a Module in Target Platform, I realized an error just after a break-point, so thought of fixing code, just before error could be detected.

Then, I came across an Icon in Toolbar, whose tool-tip suggested its used for applying code fixes. So, I made changes wherever needed, and clicked on that Icon, it reloaded that class and the debugging continued, from same point!

Now, that’s what I call a utility. Amazing, never thought debugging could be fun ;)

enJoy Technology…

07.08.08

taT | Editor Fold Utility

Posted in NetBeans, Tips|Tricks at 12:44 pm by Varun

Hey,

Another tips and Tricks for today! Whenever you have created a JFrame in NetBeans, the auto-generated code which is in sync with Matisse Builder, has a method initComponents() initially, right?

When you make a switch to the Source view, you see a collapsed section of code, with a label on it- “Generated Code”. Ever thought, how it happens.

Here’s the trick

Write code between following comments;

        //<editor-fold defaultstate="$state" desc="$label">
        /* Place your code over here */
        //</editor-fold>

Replace $state with expanded/collapsed and $label with any name/phrase you choose. Also, replace the following;

        /* Place your code over here */

with your code, it could be either inside a method, or inside a Java class, wrapping various methods, variable declarations etc.

Tips to Remember

Keep in mind that, the editor-fold tags must be inside same block of code, either inside a Java class, or inside a method! You can’t opening tag inside a Java class, and closing tag inside a method OR vice-versa!

It works in all Java classes. Also, its basic use is to categorize the sections of code. Like, you may have an editor-fold for only variable declarations, and/or for user created methods, and/or for getter/setter methods!

eNjoy Technology…

07.07.08

taT | Play with XML Layer (Part 3)

Posted in NetBeans, Platform, Tips|Tricks, XML, nb-60 at 7:02 pm by Varun

Hey,

So, I am writing a third part, Wow ;) Really, I must admit, I just didn’t thought I would write so frequently, and that too, on NetBeans Platform. Its something, that needs a lot of time to understand its usability, functionality and extensibility… :) Anyways, in my previous entry- Part Two of this series, I wrote on how to transform an Action type from one form to another- CallableSystem TO Cookie! By the way, I have already done the reversal in form of a tutorial, and contributed to the Community Docs.

Here, I present to you tricks to add/remove entries into/from XML Layer, that would enable/disable context-aware actions.

Pre-requisites

Just have a look at this webpage- Tryst with Platform! Its basically an index of what all documentations (11), I have contributed to the NetBeans Community, be it blogs, tutorials, articles, tips and Tricks, Dzone entries, etc, in the last week of June.

Tips and Tricks

Following tips/tricks apply to CookieAction, not CallableSystemAction! Also, check out the references section (at the end of this entry), this part of blog series is inspired by those references!

Syntax of the MIME Type: type/subtype

Tricks for Context Menu Items

Trick #1 - If making the action, file type context-sensitive. Then, you have to add an entry in folder “Loaders” as follows into XML Layer.

    <folder name="Loaders">
        <folder name="type">
            <folder name="subtype">
                <folder name="Actions">
                    <file name="org-nvarun-tat-SayCheez.instance"/>
                </folder>
            </folder>
        </folder>
    </folder>

Trick #2 - If making the action, editor context-sensitive. Then, you have to add an entry in folder “Editors” as follows into XML Layer.

    <folder name="Editors">
        <folder name="type">
            <folder name="subtype">
                <folder name="Popup">
                    <file name="org-nvarun-tat-SayCheez.instance"/>
                </folder>
            </folder>
        </folder>
    </folder>

Tips To Remember

In our case, I replaced type with text and subtype with x-java! What I did, I registered the Action into the Java Editor Context Menu, i.e. whenever you open a Java File, and do right-click, popup menu would show the above action. Also, when you expand relevant Package Node of a Project Node, to uncover Java File Nodes, right-click to see the Action in the popup menu!

However, it would be still be enabled, if you had earlier chosen EditorCookie, EditCookie, DataObject OR OpenCookie. It would be disabled, if you choose Project interface as Cookie class.

Now, what?

You can find out what all MIME Types are registered into the IDE, by clicking on Tools > Options, and click on button Advanced Options! There you expand nodes in this order- IDE Configuration —> System —> Object Types

Now, select any sub-node, and you could see its property sheet on right panel, where all possible MIME Types for that particular Object Type are listed. Try this on existing MIME Types, also you may do this for your own created languages (which you might like to integrate into the IDE).

References
[A] http://en.wikipedia.org/wiki/MIME#Content-Type
[B] Recognizing a File Type Tutorial
[C] http://wiki.netbeans.org/DevFaqActionAddEditorPopup
[D] http://wiki.netbeans.org/DevFaqActionAddFileMime

eNjoy Technology…

06.24.08

taT | Play with XML Layer (Part 2)

Posted in NetBeans, Platform, Tips|Tricks, XML, nb-60 at 5:15 pm by Varun

Wow!

I am really enjoying playing with XML Layer. I had mentioned an important note in my previous blog entry- Part One of this series! Its time to work with the CookieAction, i.e. make some Conditionally Enabled Actions and play with it.

By the way, I came to know, that CookieAction does support Shortcut Keys, however I used to think it doesn’t as it was always disabled, when I used Action Wizard to create it.
Its a bug, that’s present since 5.5 and I never realised- I have filed a bug report!

Pre-requisites

Read my previous blog entry- Part One of this series! You should be thorough with all that was explained previously. If not, get it clarified by me OR on Netbeans Community mailing lists.

Problem Description

Here, we are going to transform an Action from one type to another, i.e. from CallableSystemAction TO CookieAction, sounds cool! Though, never tried. Well, here I am sharing with you another trick to make it possible, without using New File Type Wizard. We will make use of SayCheez.java, which was made in previous entry.

Solution

So, to start with, remove the CallableSystemAction and let the class SayCheez extend CookieAction, like this;

public final class SayCheez extends CookieAction {

Now, don’t remove the already existing implementations of abstract methods of CallableSystemAction, just add following two to let the class SayCheez implement all the abstract methods defined by CookieAction.

@Override
protected Class<?>[] cookieClasses() {
return new Class[]{{$Interface}.class};
}

protected int mode() {
return CookieAction.{$mode};
}

Please note, that {$Interface} needs to be replaced by appropriate interface, i.e. it could be either of the following Cookie class(es), which are basically interfaces/abstract class(es);

  1. Project
  2. OpenCookie
  3. EditCookie
  4. EditorCookie
  5. DataObject (Abstract Class, NOT an Interface)

Also, {$mode} needs to be replaced with either MODE_EXACTLY_ONE[1] or MODE_ALL[2].

  1. User Selects One Node
  2. User May Select Multiple Nodes

MODE_EXACTLY_ONE -
Action will be enabled if there is exactly one selected node and it supports the given cookies.

MODE_ALL -
Action will be enabled if there are one or more selected nodes and all of them support the given cookies.

In our case, its EditorCookie!

Now, alter the performAction method like this,

protected void performAction(Node[] activatedNodes) {
{$Interface} ref = activatedNodes[0].getLookup().lookup({$Interface}.class);
// TODO use {$Interface}
}

Here, ref is basically the reference to either the interface/class being made, which is being assigned a subclass reference, it could be of great use. Now, add your code for DialogDisplayer used in previous blog entry, in place of TODO comment.

See, it was too easy! Anyways, the changes in XML Layer, which shall be done with respect to the change in the Action type, some of them are as follows and rest will be the mentioned in the next part of this series.

Tricks for Menu Item, Toolbar Button, Shortcut Keys

Trick #1 - If you had made a Global Menu Item OR Toolbar Button with CallableSystemAction, then no changes required.

Trick #2 - If you had made use of Shortcut Keys earlier, now you have to remove them, as CookieAction doesn’t support Shortcut Keys! Well, I don’t know why. So, remove following code from our XML Layer, formed earlier.

Thanks for following, enjoy and have fun! Surprise to be revealed soon…keep an eye on my blog!

Previously posted on NetBeans Zone -
Playing with NetBeans XML Layer Files (Part 1)

Add to Technorati Favorites

sTay Tuned for More…

« Older entries