X

Geertjan's Blog

  • November 21, 2005

Never Type HTML Line Break Tags Again!

Geertjan Wielenga
Product Manager
So here's the absolutely no-frills simplest implementation of a new code snippet for NetBeans IDE 5.0. The end result in my Projects window is this (in the screenshot, I've selected the only two Java files I needed to create):

When I install my new module, the result is an additional code snippet in the HTML section of the Component Palette (which is displayed when I create an HTML file or JSP file):

When I double-click the code snippet, a <br> tag is created at the cursor. Alternatively, I can drag-and-drop the code snippet wherever I want in my HTML file or JSP file.

This is the content of the BR.java file (hyperlinks will take you to NetBeans API Javadoc):

package org.netbeans.modules.myfiletype.palette.items;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
import org.openide.text.ActiveEditorDrop;
public class BR implements ActiveEditorDrop {
public BR() {
}
private String createBody() {
String Br = "<br>";
return Br;
}
public boolean handleTransfer(JTextComponent targetComponent) {
String body = createBody();
try {
MyfiletypePaletteUtilities.insert(body, targetComponent);
} catch (BadLocationException ble) {
return false;
}
return true;
}
}

Note that, in the createBody() method above, the standard code snippets call a dialog box where you can pre-define the code snippet's values. However, I don't like this. I much prefer just dragging chunks of code into the HTML file or JSP file and then modifying them using code completion. (For me, a better solution than all those dialog boxes would be to implement them in the Options window instead. Then, I'd be able to set my desired values once and then tweak them if necessary, but I wouldn't have to set values every time I define a new code snippet.) And this is the content of the MyfiletypePaletteUtilities.java file:

package org.netbeans.modules.myfiletype.palette.items;
import javax.swing.text.BadLocationException;
import javax.swing.text.Caret;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import org.netbeans.editor.BaseDocument;
import org.netbeans.editor.Formatter;
public class MyfiletypePaletteUtilities {
public static void insert(String s, JTextComponent target)
throws BadLocationException {
insert(s, target, true);
}
public static void insert(String s, JTextComponent target, boolean reformat)
throws BadLocationException {
if (s == null)
s = "";
Document doc = target.getDocument();
if (doc == null)
return;
if (doc instanceof BaseDocument)
((BaseDocument)doc).atomicLock();
int start = insert(s, target, doc);
if (reformat && start >= 0 && doc instanceof BaseDocument) { // format the inserted text
int end = start + s.length();
Formatter f = ((BaseDocument)doc).getFormatter();
f.reformat((BaseDocument)doc, start, end);
}
if (doc instanceof BaseDocument)
((BaseDocument)doc).atomicUnlock();
}
private static int insert(String s, JTextComponent target, Document doc)
throws BadLocationException {
int start = -1;
try {
//at first, find selected text range
Caret caret = target.getCaret();
int p0 = Math.min(caret.getDot(), caret.getMark());
int p1 = Math.max(caret.getDot(), caret.getMark());
doc.remove(p0, p1 - p0);
//replace selected text by the inserted one
start = caret.getDot();
doc.insertString(start, s, null);
} catch (BadLocationException ble) {}
return start;
}
}

Next, you need a 16x16 image file and a 32x32 image file. (Because when you right-click on the Component Palette you can choose 'Show Big Icons'.) In the Resources package, I have a Bundle.properties file with this content (i.e., the label and the tooltip):

NAME_html-BR=Line Break
HINT_html-BR=\\
<html>\\
<br>
\\
</html>

And then a small XML file to reference all your resources, which then gets referenced in the layer.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE editor_palette_item PUBLIC "-//NetBeans//Editor Palette Item 1.0//EN"
"http://www.netbeans.org/dtds/editor-palette-item-1_0.dtd">
<editor_palette_item version="1.0">
<class name="org.netbeans.modules.myfiletype.palette.items.BR" />
<icon16 urlvalue="org/netbeans/modules/myfiletype/palette/items/resources/BR16.gif" />
<icon32 urlvalue="org/netbeans/modules/myfiletype/palette/items/resources/BR32.gif" />
<description localizing-bundle="org.netbeans.modules.myfiletype.palette.items.resources.Bundle"
display-name-key="NAME_html-BR"
tooltip-key="HINT_html-BR" />
</editor_palette_item>

Finally, the layer.xml file registers the new HTML code snippet like this:

<?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="HTMLPalette">
<folder name="HTML">
<file name="BR.xml" url="resources/BR.xml"/>
</folder>
</folder>
</filesystem>

That's the whole story of a very simple code snippet. There's really not very much code at all (especially when you realize that MyfiletypePaletteUtilities.java is shared between all the related code snippets). And, at the end of it, you can drag and drop the snippet and... then... you'll never need to type <br> again!

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.