This tutorial lets you create a module which adds a new
template to the New wizard, including a custom wizard
and a custom internal executor. The template will be of a class
implementing Runnable
and the executor will run it.
The custom wizard will add a panel permitting the user to add
code to the run()
method in advance. The template
will also have a pleasant display name and icon.
Runnable
like so:
package Templates.Classes; /** A class which may be run. * @author __USER__ */ public class RunnableImpl implements Runnable { public RunnableImpl() { } public void run() { } }(Note: due to problems with source synchronization inside layers, you may need to write the
run()
first, then make the class
implement Runnable
later.)
Save the class. You should see it quoted inside the XML layer.
myProp
to be named count
and make it an int
, default value 1.
Correspondingly change the name of the
descriptor in RunRunnableExecutorBeanInfo
. Edit the bundle
and make sure the property is labelled Count, and the executor
Run Runnables.
RunRunnableExecutor
, change MyInterface
to
Runnable
in checkClass()
. In executeClass()
,
do the same and change the work code to:
for (int i = 0; i < count; i++) { thing.run(); }
RunRunnableExecutor
and
Paste beneath Service Types in the module JAR to add it to the
module manifest.
methodvalue
in the XML attribute; this is a shortcut.)
RunnableTemplateIterator
some arbitrary serialVersionUID
(just pick up a big long number at random).
createPanels()
, keep wiz.targetChooser() and also
add a second panel, new ImplementRunPanel().
createSteps()
, keep null for the standard second
panel and add a second step name,
NbBundle.getMessage(RunnableTemplateIterator.class, "LBL_step_run").
Make sure this step name exists in the bundle file, say Implement run().
ImplementRunPanel
, make sure TITLE_WizardPanel is
defined in the bundle, say Implement the run() method.
JEditorPane
named pane
and the initialization code should
read something like this:
pane = new JEditorPane(); pane.setContentType("text/x-java"); // This key in the bundle should say e.g.: // "Implement your run() method here:" label = new JLabel(NbBundle.getMessage (ImplementRunPanel.class, "LBL_implement_run_here")); add(label, BorderLayout.NORTH); scrollPane = new JScrollPane(pane); add(scrollPane, BorderLayout.CENTER);(If using the Form Editor, note that the I18N module has a setting which helps you internationalize text labels using
NbBundle
.)
ImplementRunPanel
, make sure the contents of the pane
are stored in the wizard, e.g.:
private static final String PROP = "runBody"; public void readSettings(Object o) { TemplateWizard wiz = (TemplateWizard)o; String s = (String)wiz.getProperty(PROP); if (s != null) pane.setText(s); } public void storeSettings(Object o) { TemplateWizard wiz = (TemplateWizard)o; wiz.putProperty(PROP, pane.getText()); }
RunnableTemplateIterator.instantiate()
, right before
the code using OpenCookie
, insert code like this:
try { SourceCookie source = (SourceCookie) result.getCookie(SourceCookie.class); ClassElement clazz = source.getSource().getClasses()[0]; MethodElement run = clazz.getMethod (Identifier.create("run"), new Type[0]); run.setBody ((String)wiz.getProperty("runBody")); SaveCookie save = (SaveCookie) result.getCookie(SaveCookie.class); save.save(); } catch (IOException ioe) { throw ioe; } catch (Exception e) { IOException ioe = new IOException(e.toString()); TopManager.getDefault().getErrorManager(). annotate(ioe, e); throw ioe; }
Class implementing <code>Runnable</code>. You will be prompted to fill in its <code>run</code> method. Hit <b>Execute</b> to run it.Click ... on the template's Description URL property and select this HTML file.
RunRunnableExecutorIcon
with an icon you like.
Click ... on the template's Custom Icon property
and select this icon.
System.out.println("Hi mom!");in the editor pane. Click Finish.
package pkg.you.chose; /** A class which may be run. * @author myself */ public class NameYouChose implements Runnable { public NameYouChose() { } public void run() { System.out.println("Hi mom!"); } }