Custom Code Templates with NetBeans

In the last weeks I’ve shown you some of the NetBeans Code Templates. The existing ones are nice, but what is even nicer is, that you can easily create your own Java templates. We can for example create the “2no” template I used to get the Node delegate from a DataObject as shown here:

Here’s how:

Go to “Settings“-> “Editor” -> “Code Templates” and select “Java” as the Language:

Now click the “New” Button, a dialog will ask for an abbreviation, enter “2no“. The abbreviation will be added to the table, and you can edit the “Expanded Text“. Add this:

${nodeType type="org.openide.nodes.Node" default="Node" editable="false"} ${node newVarName default="n"} = ${dob instanceof="org.openide.loaders.DataObject" default="dob"}.getNodeDelegate();

The type definition in the beginning will automatically add the import for org.openide.nodes.Node. The default defines, that we will get “Node” as the first String, and it’s not editable by the user. Then we define a new variable with a default name of  ”n“. If that name isn’t available in our scope NetBeans will check for the next free name by adding an int to the String (e.g. “n1“).  Next we declare, that we want to call a method called “getNodeDelegate()” on a “org.openide.datasystems.DataObject“, and if there is none in the scope use “dob” instead.

Dieser Eintrag wurde in netbeans geschrieben. Link merken.

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>