Forum Controls
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.

NetBeans 6: Matisse Updates

NetBeans 6 delivers great updates to the Matisse GUI builder. Spend a few minutes with Roman Strobl and get an expert briefing on what's new and what has changed.

Introduction to Groovy Part 3

In this, the third and final installation of Andres' Introduction to Groovy series, you learn about how Groovy handles variable numbers of arguments, named parameters, currying, and more about Groovy operators. Including, some new operators.

Easier Custom Components with Swing Fuse

Swing Fuse (actually just Fuse), is a framework designed to make it easier to create your own custom desktop components. In this article, Daniel Spiewak shows you how to get started and provides sample source code you can download.

Benchmark Analysis: Guice vs Spring

Willam Louth shows how he uses JXInsight Probes to investigate probable performance issues with code bases that he is not familiar with. He also highlights possible pitfalls in creating a benchmark, as well as in the analysis of results.
Replies: 1 - Pages: 1  
Threads: [ Previous | Next ]
  Click to reply to this thread Reply

Creating an QuickTag action in Netbeans 6 m10

At 4:55 PM on Aug 18, 2007, steven yi DeveloperZone Top 100 wrote:

Hi All,

I had a really neat experience just now in creating a quick tag action in Netbeans for HTML editing. I thought I'd share my experience to show how easy it was as well as to hear recommendations to improve what I did.

I've long found the "Wrap Tag" feature in Dreamweaver very handy where after selecting some text, hitting ctrl-T will open up a place to enter in a tagname. After hitting enter it closes the entry popup and then creates an open tag and close tag around the selected text. Also, if you type in something like:

span class="test"


it will correctly wrap the selected text like:

<span class="test">...</span>


So today when I started working with HTML in Netbeans and missing that feature I decided to see if I could add that.

Now I've been reading the Rich Client Programming book as well as tutorials online recently but this was all still beyond what I've come across yet. So with some basic knowledge I was able to achieve this task.

I began by creating a Module project in Netbeans called "QuickTag". After creating the module, I went to "File -> New File" then chose "Module Development" and then "Action". From here I chose "Conditionally Enabled" and then "EditorCookie" for the Cookie Class. On the next screen I chose "Edit" for category, deselected "Global Menu Item" and selected "Editor Context Menu Item" and then chose "text/html". For position I put it at the very end and checked "Separator Before". On the next screen I set the classname to "QuickTag" and the display name to "Quick Tag" and that was it.

After that, the files were all setup. I opened up the QuickTag.java file and modified it to be:

package com.kunstmusik.quicktag;
 
import javax.swing.JEditorPane;
import javax.swing.JOptionPane;
import javax.swing.text.BadLocationException;
import org.openide.cookies.EditorCookie;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.util.actions.CookieAction;
 
public final class QuickTag extends CookieAction {
 
    protected void performAction(Node[] activatedNodes) {
        EditorCookie editorCookie = (EditorCookie) activatedNodes[0].getLookup().lookup(EditorCookie.class);
 
        JEditorPane[] panes = editorCookie.getOpenedPanes();
 
        if (panes.length > 0) {
            for (JEditorPane pane : panes) {
                if (pane.getSelectedText() != null || pane.getSelectedText().length() > 0) {
                    try {
                        java.lang.Object retVal = javax.swing.JOptionPane.showInputDialog("Enter tag");
 
                        if (retVal == null) {
                            return;
                        }
 
                        int start = pane.getSelectionStart();
                        int end = pane.getSelectionEnd();
 
                        java.lang.String tag = (java.lang.String) retVal;
                        tag = tag.trim();
                        
                        String endTag = tag;
                        
                        if(endTag.indexOf(" ") >= 0) {
                            endTag = endTag.substring(0, endTag.indexOf(" "));
                        }
                        
                        editorCookie.getDocument().insertString(start, "<" + tag + ">", null);
                        editorCookie.getDocument().insertString(end + tag.length() + 2, "</" + endTag + ">", null);
                    } catch (BadLocationException ex) {
                        Exceptions.printStackTrace(ex);
                    }
                }
            }
        }
    }
 
    protected int mode() {
        return CookieAction.MODE_EXACTLY_ONE;
    }
 
    public String getName() {
        return NbBundle.getMessage(QuickTag.class, "CTL_QuickTag");
    }
 
    protected Class[] cookieClasses() {
        return new Class[]{EditorCookie.class};
    }
 
    protected void initialize() {
        super.initialize();
        // see org.openide.util.actions.SystemAction.iconResource() javadoc for more details
        putValue("noIconInMenu", Boolean.TRUE);
    }
 
    public HelpCtx getHelpCtx() {
        return HelpCtx.DEFAULT_HELP;
    }
 
    protected boolean asynchronous() {
        return false;
    }
}


The only thing here I really did was modify the performAction method. I made some guesses as to what the editorCookie did and after some fiddling found that the above worked. Being familiar with Swing, I found the rest of the code quite easy to handle.

For testing I right-clicked the "QuickTag" project in the projects window and chose the option "Install/Reload in Development IDE". This was *really* neat as it then installed live into the environment. I had an HTML file open and so I would select text, right click, choose Quick Tag, then give my module a try. When things didn't work, I'd edit, then reload into the current environment and with the HTML editor that was open just retry what I was doing and the action was reloaded. Very nice!

One thing that was hard for me to figure out was how to then add a shortcut for the action so I didn't have to use the context menu. I didn't find a simple answer anywhere so loooked at a Netbeans RCP-based project and looked at one of the layer.xml files. I ultimately modified mine by adding a shortcuts folder in the layer.xml file like so:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.1//EN" "http://www.netbeans.org/dtds/filesystem-1_1.dtd">
<filesystem>
    <folder name="Actions">
        <folder name="Edit">
            <file name="com-kunstmusik-quicktag-QuickTag.instance"/>
        </folder>
    </folder>
    <folder name="Editors">
        <folder name="text">
            <folder name="html">
                <folder name="Popup">
                    <attr name="com-kunstmusik-quicktag-separatorBefore.instance/com-kunstmusik-quicktag-QuickTag.shadow" boolvalue="true"/>
                    <attr name="dump-tokens/com-kunstmusik-quicktag-QuickTag.shadow" boolvalue="true"/>
                    <attr name="dump-tokens/com-kunstmusik-quicktag-separatorBefore.instance" boolvalue="true"/>
                    <file name="com-kunstmusik-quicktag-QuickTag.shadow">
                        <attr name="originalFile" stringvalue="Actions/Edit/com-kunstmusik-quicktag-QuickTag.instance"/>
                    </file>
                    <file name="com-kunstmusik-quicktag-separatorBefore.instance">
                        <attr name="instanceClass" stringvalue="javax.swing.JSeparator"/>
                    </file>
                </folder>
            </folder>
        </folder>
    </folder>
    <folder name="Shortcuts">
        <file name="C-T.shadow">
            <attr name="originalFile" stringvalue="Actions/Edit/com-kunstmusik-quicktag-QuickTag.instance"/>
        </file>
    </folder>
</filesystem>


and sure enough it worked!

All told it took maybe an hour to figure this all out, but I imagine in the future it would be quite quick to do. I had a great time in building this small but very useful-to-me feature and hope you enjoyed this small article. (Thanks to Netbeans team for making that possible and easy to do!)
1 . At 3:13 AM on Aug 31, 2007, Javanewcomer wrote:
  Click to reply to this thread Reply

Re: Creating an QuickTag action in Netbeans 6 m10

"Wrap Tag" very useful featrue.
Thanks for your article, and hope the netbean will provide such feature.
Java Tutorial
Java Software

thread.rss_message