Platinum Partner
netbeans

10 Brand New Code Templates for NetBeans Module Developers

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.

 

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}