Geertjan is a DZone Zone Leader and has posted 468 posts at DZone. You can read more from them at their website. View Full User Profile

10 Brand New Code Templates for NetBeans Module Developers

03.28.2010
| 18685 views |
  • submit to reddit

Since writing Top 10 Interesting NetBeans IDE Java Shortcuts I Never Knew Existed, several more have been introduced in NetBeans IDE 6.9 builds (leaving me with the headache of how to fit things into the Keyboard Shortcuts Card, which is accessible from the Help menu in NetBeans IDE).

Specifically, a bunch of code templates are now available that are useful to those programming against NetBeans APIs, i.e., for creating NetBeans modules, i.e., for creating/extending NetBeans Platform applications.

Without further ado, here they are, type the abbreviation and then press the TAB key (or whatever your expansion key is set to) to see the expansions described below:

  1. Abbreviation: 2do

    Description: Convert FileObject to DataObject

    Expands to:

    try {
    ${dobType type="org.openide.loaders.DataObject" editable="false" default="DataObject"} ${dob newVarName default="dob"} = ${dobType}.find(${fo instanceof="org.openide.filesystems.FileObject" default="fo"});
    ${cursor}
    } catch (${etype type="org.openide.loaders.DataObjectNotFoundException" default="DataObjectNotFoundException" editable="false"} ${exName newVarName default="ex" editable="false"}) {
    ${exctype type="org.openide.util.Exceptions" editable="false" default=""}.printStackTrace(${exName});
    }
    Example:
    try {
    DataObject dataObject = DataObject.find(myFo);

    } catch (DataObjectNotFoundException dataObjectNotFoundException) {
    Exceptions.printStackTrace(dataObjectNotFoundException);
    }
  2. Abbreviation: 2f

    Description: Convert FileObject to java.io.File

    Expands to:

    ${fileType type="java.io.File" default="File" editable="false"} ${file newVarName default="f"} = ${FileUtilType type="org.openide.filesystems.FileUtil" editable="false")}.toFile(${fo instanceof="org.openide.filesystems.FileObject" default="fo"});
    ${cursor}
    Example:
    File file = FileUtil.toFile(myFo);
  3. Abbreviation: 2fo

    Description: Convert java.io.File to FileObject

    Expands to:

    ${fileType type="org.openide.filesystems.FileObject" default="FileObject" editable="false"} ${file newVarName default="f"} = ${FileUtilType type="org.openide.filesystems.FileUtil" editable="false")}.toFileObject(${FileUtilType}.normalizeFile(${f instanceof="java.io.File" default="f"}));
    ${cursor}
    Example:
    FileObject fileObject = FileUtil.toFileObject(FileUtil.normalizeFile(myFile));
  4. Abbreviation: Lka

    Description: Find all implementations of a certain type registered in META-INF/services.

    Expands to:

    ${coltype type="java.util.Collection" default="Collection" editable="false"}  ${obj newVarName default="obj"} = ${lkptype editable="false" default="Lookup" type="org.openide.util.Lookup"}.getDefault().lookupAll(${Type}.class);
    ${cursor}
    Example:
    Collection<? extends Type> collection = Lookup.getDefault().lookupAll(Type.class);

    Variation: lka (i.e., the first character is lowercase)

    Description: Find all implementations of a certain type from a local lookup, e.g., TopComponent, Node, or DataObject.

    Example:

    Collection<? extends Type> collection = myNode.lookupAll(Type.class);
  5. Abbreviation: Lkp

    Description: Find a single typed implementation registered in META-INF/services.

    Expands to:

    ${Type} ${obj newVarName default="obj"} = ${lkptype editable="false" default="Lookup" type="org.openide.util.Lookup"}.getDefault().lookup(${Type}.class);
    ${cursor}
    Example:
    Type type = Lookup.getDefault().lookup(Type.class);

    Variation: lkp (i.e., the first character is lowercase)

    Description: Find a single implementation of a certain type from a local lookup, e.g., TopComponent, Node, or DataObject.

    Example:

    Type type = myNode.lookup(Type.class);
  6. Abbreviation: Lkr

    Description: Assign a single typed instance from META-INF/services to a Result object, to which you can listen for changes.

    Expands to:

    ${coltype type="org.openide.util.Lookup.Result" default="Lookup.Result" editable="false"}  ${obj newVarName default="res"} = ${lkptype editable="false" default="Lookup" type="org.openide.util.Lookup"}.getDefault().lookupResult(${Type}.class);
    ${cursor}
    Example:
    Lookup.Result<? extends Type> res = Lookup.getDefault().lookupResult(Type.class);

    Variation: lkr (i.e., the first character is lowercase)

    Description: Assign a single typed instance from a local lookup to a Result object, to which you can listen for changes.

    Example:

    Result<? extends Type> all = myNode.lookupResult(Type.class);
  7. Abbreviation: lko

    Description: Create a lookup for a local object, e.g., TopComponent, Node, or DataObject.

    Expands to:

    ${Type} ${obj newVarName default="obj"} = ${prov instanceof="org.openide.util.Lookup.Provider"}.getLookup().lookup(${Type}.class);
    ${cursor}
    Example:
    Type type = myNode.getLookup().lookup(Type.class);
  8. Abbreviation: rp

    Description: Create code for using org.openide.util.RequestProcessor.

    Expands to:

    ${rp type="org.openide.util.RequestProcessor" default="RequestProcessor" editable="false"}.getDefault().post(${toRun instanceof="java.lang.Runnable" default="new Runnable() {
    public void run() {
    }
    }"});
    Example:
    RequestProcessor.getDefault().post(new Runnable() {

    public void run() {
    }
    });
  9. Abbreviation: stat

    Description: Create code for writing text obtained from a Bundle.properties file into the status bar.

    Expands to:

    ${coltype type="org.openide.awt.StatusDisplayer" default="StatusDisplayer" editable="false"}.getDefault().setStatusText(${bundletype type="org.openide.util.NbBundle" default="NbBundle" editable="false"}.getMessage(getClass(), "${KEY}"));
    ${cursor}
    Example:
    StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(getClass(), "KEY"));
  10. Abbreviation: nb

    Description: Get a text from a Bundle.properties file.

    Expands to:

    ${coltype type="org.openide.util.NbBundle" default="NbBundle" editable="false"}.getMessage(${classVar editable="false" currClassName default="getClass()"}.class, "${KEY}")
    Example:
    NbBundle.getMessage(DemoAction.class, "KEY")

    Variation: nbb (i.e., add an additional 'b' character)

    Description: Pass in parameters for formatting the text.

    Example:

    NbBundle.getMessage(DemoAction.class, "KEY", params)

 

It is for the first time that there are special code templates relevant to NetBeans module development, but there are many other new NetBeans code templates that I haven't mentioned yet. A separate article will deal with those, but you can already try some of them out yourself: 2al, 2ar, 2l, 2s, log, loge, logp.

Many more new ones besides those, but let's leave them for another day (or look in the Options window yourself, within the Editor | Code Templates tab, and you'll see all the code templates) when we'll look some more at new code templates in NetBeans IDE 6.9 builds.

 

Published at DZone with permission of its author, Geertjan Wielenga.

Comments

Jirka X1 replied on Mon, 2010/03/29 - 5:01am

Nice templates, but there are too many to memorize and as Geertjan says too many to put even on a Refcard.

I would suggest to create a "template completion list" invokable by some short cut. Currently, some templates are part of the regular code completion but not all (say I could not get sout -> System.out.println into the list). Also, adding them into a separate list would help us to learn them faster.

Peter Kirkham replied on Sat, 2010/04/03 - 3:27am

These look to be really useful additions to my mind. Whilst there may be a lot of them, no-one is forcing you to use them - my only issue is that it will deprecate some of the shortcuts I'd written myself! It took me a little while to really start to learn the usefulness of the existing shortcuts, but it's pretty clear that once you start using them they become second nature and really allow you to focus on writing the core part of your code rather than the boilerplate. Definitely looking forward to trying these out.

Matt Coleman replied on Tue, 2012/01/03 - 12:36pm in response to: Peter Kirkham

I agree with you Peter! website design buffalo

Carla Brian replied on Sat, 2012/04/14 - 9:55pm

They are really good. They are perfect for my future projects. Good work on this. - Instant Tax Solutions Reviews

Samad Khatri replied on Wed, 2014/10/08 - 4:04am

 type the abbreviation and then press the TAB key (or whatever your expansion key is set to) to see the expansions described below:

security systems

Samad Khatri replied on Wed, 2014/10/08 - 2:41pm

  useful to those programming against NetBeans APIs, i.e., for creating NetBeans modules, i.e., for creating/extending NetBeans Platform applications.

beauty food bible tracy patterson video review

Jack Jack13 replied on Wed, 2014/10/15 - 7:50am in response to: Carla Brian

 

I discovered this housing web page, <a href="http://ochousingnews.com/">OC Housing News</a>. I stumbled upon a post, <a href="http://ochousingnews.com/blog/mark-hanson-nobel-prize-winner-robert-shiller-warn-housing-bubble/ ">Mark Hanson and Nobel prize winner Robert Shiller warn of housing bubble</a>. This news post is better than most. The housing news and real estate analysis on the site is super, the cartoons and writing are entertaining and insightful, and the real estate educational materials are fantastic. I advise discovering this Internet site to tour what it has to offer.
<a href="http://ochousingnews.com/">Real Estate News</a>

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.