sandip chitale's blog    Sandip Chitale's blog (scblog)
NOTE: I have moved many of my modules to NetBeans Plugin Portal . Please check there for latest versions of modules described on this blog.
20061204 Monday December 04, 2006

UPDATED: Code Template Tools Module - Added import functionality and Expand Template action

I have updated the Code Template Tools Module on my NetBeans update center described (here). This version of the module has been tested with NB5.5 and 6.0dev.

What is new?

I have added two new actions.

  • Edit:Expand Template (Ctrl+J SPACE)- this action lets you expand a code template on demand. If a word preceding the caret matches a code template name, invoking this action expands the code template. For example, with the Java editor caret after the word re like below:
re|

invoking the Expand Template action results in:

return|

This is different than NetBeans editor's built in functionality in that, the code template expansion can be invoked on demand. In case of Netbeans editor you have to type r e SPACE in exact sequence to expand the template (abbreviation).

  • The Show Templates dialog now has an Import... button. When invoked the user is prompted to select the template file. The template file uses the same XML format as the
.../.netbeans/<nbversion>/config/Editors/text/<mime-type>/abbreviations.xml

file. There is no Export action required as you can simply define new templates and send the abbreviations.xml file from your .../.netbeans/<nbversion>/ via email (say!). This has an added advantage in that it only captures the templates added/customized by you. Now you can send your favorite code templates to your friends. I will be publishing some of my favorite code templates soon.

Sources

DISCLAIMER: This module is experimental. So no guarantees. Use the module at your own risk.


Posted by sandipchitale ( Dec 04 2006, 03:58:08 PM PST ) Permalink Comments [7]


Trackback URL: http://blogs.sun.com/scblog/entry/updated%3A_code_template_tools_module
Comments:

Thanks again. I can't say enough good things about your contributions to NetBeans!
Is there a way to select the template in your Show Templates dialog just by typing the first letters of the template? Something like what a JList provides in Java 5.0 and above? Being a keyboard-centric user, anything would help.

Thanks you,
Jay

Posted by Jay Logan on December 04, 2006 at 09:22 PM PST #

Jay, thanks for your suggestion. Done. Get it.

Posted by Sandip on December 04, 2006 at 11:41 PM PST #

Hello Sandip,

This is a great start, and I'll take it over what I had to do before.
I have one suggestion, though. Please allow me to explain further my original intention.

I have in my code templates:

  • sfo --> String.format(|)
  • souf --> System.out.format(|)
  • suil --> SwingUtilities.invokeLater(...);
  • (many templates later...)
  • uh --> public void ${method}(${args}) { ${cursor} } // easiest to type on my dvorak keyboard layout
I wish to invoke 'suil' template. So I bring up your awesome template screen (ALT+S+T, for me), and type the first few letters ('su'), However, it goes to the first template that starts with 's', and then immediately to the template that starts with 'u'. I would expect it to go to the first template that starts with 'su'.

A normal JList allows you to narrow the selection like this. No extra developer work needed. Is this possible with the NetBeans equivalent, or did they take this functionality away for some reason?

Posted by Jay Logan on December 05, 2006 at 07:48 AM PST #

Jay,

So basically you want the Quick Search like mechanism as available in Netbeans Navigator or Property sheet. I think it is doable. It will require a timer to clear the prefix if the user does not type anything for a second or so.

You mention that normal JList allows you to narrow the selection like this. You mean

javax.swing.JList
? I was not aware of that support. There is support for something I implemented in
javax.swing.JComboBox
in the form of
KeySelectionManager
, that I am aware of.

Posted by Sandip on December 05, 2006 at 09:12 AM PST #

Hi Sandip,

The code below was tested on Windows, using the Metal Look and Feel. It was run on java 1.5.0_09. I don't know about Mac, but I know it works on Windows and on UNIX.
Run it and see! :)

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.SwingUtilities;

/**
 *
 * @author jaylogan
 */
public class JListTest
{
    private static JList createJList()
    {
        Object [] items = new Object [] {
            "jlist",
            "narrow",
            "normal",
            "other",
            "selection",
            "sfo",
            "souf",
            "suil",
            "teepee",
            "telephone",
            "television",
            "uh",
            "west"
        };
        JList list = new JList(items);
        
        return list;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                JFrame frame = new JFrame("List Example");
                
                frame.add(createJList(), BorderLayout.CENTER);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
                
            }
        });
    }
}

Posted by Jay Logan on December 05, 2006 at 09:31 AM PST #

Jay,

Thanks for the example. Looks like they added the functionality in JDK 1.4. I need to keep up with Swing changes.

I have incorporated it in the module. Get it.

Posted by Sandip on December 05, 2006 at 12:53 PM PST #

Thanks a bunch!

Posted by Jay Logan on December 05, 2006 at 08:15 PM PST #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed








« July 2009
SunMonTueWedThuFriSat
   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
       
Today

Get NetBeans 5.5

Locations of visitors to this page

Today's Page Hits: 431


XML
All
/Creator
/General
/Hobby
/Java
/JavaScript
/Mozilla
/NetBeans
/Ubuntu
/VisualWeb
/VisualWebPack
/Web 2.0

XML
All
/Creator
/General
/Hobby
/Java
/JavaScript
/Mozilla
/NetBeans
/Ubuntu
/VisualWeb
/VisualWebPack
/Web 2.0

scblog
scblog