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:
- Abbreviation: 2do
Description: Convert FileObject to DataObject
Expands to:
try {Example:
${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});
}try {
DataObject dataObject = DataObject.find(myFo);
} catch (DataObjectNotFoundException dataObjectNotFoundException) {
Exceptions.printStackTrace(dataObjectNotFoundException);
} - 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"});Example:
${cursor}File file = FileUtil.toFile(myFo);
- 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"}));Example:
${cursor}FileObject fileObject = FileUtil.toFileObject(FileUtil.normalizeFile(myFile));
- 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);Example:
${cursor}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);
- 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);Example:
${cursor}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);
- 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);Example:
${cursor}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);
- 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);Example:
${cursor}Type type = myNode.getLookup().lookup(Type.class);
- 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() {Example:
public void run() {
}
}"});RequestProcessor.getDefault().post(new Runnable() {
public void run() {
}
}); - 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}"));Example:
${cursor}StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(getClass(), "KEY"));
- 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.





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
Matt Coleman replied on Tue, 2012/01/03 - 12:36pm
in response to:
Peter Kirkham
Carla Brian replied on Sat, 2012/04/14 - 9:55pm