/plugins/Templates/tags/rel-3_3_0/docs/ug-quickstart.xml

# · XML · 319 lines · 276 code · 27 blank · 16 comment · 0 complexity · 534f890a544347701a06be6e04a4df8a MD5 · raw file

  1. <!-- Templates plugin user's guide: Quick Start chapter -->
  2. <!-- (C) 2002 Steve Jakob -->
  3. <!-- jEdit buffer-local properties: -->
  4. <!-- :indentSize=1:noTabs=true:folding=explicit: -->
  5. <!-- {{{ Chapter: Quick Start -->
  6. <chapter id="quick-start"><title>Quick Start</title>
  7. <para>
  8. This chapter will introduce the reader to the use of the Templates plugin
  9. by walking the user through the creation of several related Templates
  10. which exploit the dynamic capabilities of the Velocity engine embedded
  11. within the plugin. Since jEdit is an application developed in Java, this
  12. tutorial will describe the creation of a number of templates intended to
  13. facilitate the creation of Java classes.
  14. </para>
  15. <!-- {{{ Section: Starting Point -->
  16. <sect1><title>Our Starting Point</title>
  17. <para>
  18. Java Joe, our hypothetical programmer, writes Java code for a living.
  19. Like most programmers, he's always on the lookout for ways to increase
  20. his efficiency. He's been using jEdit as his development environment, and
  21. has created some static templates for use with the Templates plugin to
  22. provide a starting point for his Java classes. Although static files
  23. help to kickstart his projects, he finds himself wishing that the templates
  24. were more dynamic. He's heard that this sort of functionality has been
  25. added to the Templates plugin and decides to try to make use of this new
  26. functionality in his custom templates.
  27. </para>
  28. <para>
  29. The templates that Joe has created include:
  30. </para>
  31. <itemizedlist>
  32. <listitem><para>Java class skeleton</para></listitem>
  33. <listitem><para>Generic method template</para></listitem>
  34. <listitem><para>Accessor/Mutator (Getter/Setter) template</para></listitem>
  35. <listitem><para>Copyright/license notice</para></listitem>
  36. </itemizedlist>
  37. <para>
  38. The Java class skeleton contains the following code:
  39. </para>
  40. <programlisting>
  41. package com.javajoe.mypackage;
  42. import javax.swing.*;
  43. /**
  44. * Class description goes here
  45. */
  46. public class SomeClass extends Parent implements InterfaceList
  47. {
  48. // Insert class variables here
  49. public static void main(String[] args) {
  50. // Main method
  51. }
  52. //Constructors
  53. public SomeClass() {
  54. super();
  55. }
  56. // Accessors &amp; Mutators
  57. // Implementors
  58. }
  59. </programlisting>
  60. <para>
  61. The method template looks like this:
  62. </para>
  63. <programlisting>
  64. /**
  65. * Method comment goes here
  66. */
  67. public void doSomething(paramType paramName) {
  68. }
  69. </programlisting>
  70. <para>
  71. The accessor/mutator template looks like this:
  72. </para>
  73. <programlisting>
  74. /**
  75. * Accessor comment
  76. */
  77. public returnType getVariable() {
  78. return variable;
  79. }
  80. /**
  81. * Mutator comment
  82. */
  83. public void setVariable(paramType paramName) {
  84. this.variable = paramName;
  85. }
  86. </programlisting>
  87. <para>
  88. Finally, the license template contains the following:
  89. </para>
  90. <programlisting>
  91. /*
  92. * MyClass.java
  93. *
  94. * Copyright (c) 2002 Java Joe
  95. * Give my code a good home.
  96. */
  97. </programlisting>
  98. <para>
  99. Typically, Joe would perform the following steps during the creation of
  100. a new Java class:
  101. </para>
  102. <orderedlist>
  103. <listitem>
  104. <para>Call the Java class template to create the class skeleton.</para>
  105. </listitem>
  106. <listitem>
  107. <para>Modify the code by changing the package, adding required import
  108. statments, adding the class name, superclass (if required),
  109. interfaces (if required), and code for the main() method and
  110. constructor.</para>
  111. </listitem>
  112. <listitem>
  113. <para>Move the cursor to the top of the file and call the template for
  114. the copyright/license notice.</para>
  115. </listitem>
  116. <listitem>
  117. <para>Add class variables beneath "Insert class variables here".</para>
  118. </listitem>
  119. <listitem>
  120. <para>Run the accessor/mutator template as required to create
  121. getters/setters for the class variables (in the "Accessors &amp;
  122. Mutators" section.</para>
  123. </listitem>
  124. <listitem>
  125. <para>Add methods as required to the "Implementors" section using the
  126. generic method template.</para>
  127. </listitem>
  128. </orderedlist>
  129. </sect1>
  130. <!-- }}} -->
  131. <!-- {{{ Section: Automated Dates -->
  132. <sect1><title>Automated Dates</title>
  133. <para>
  134. The first thing that Joe notices he can do is to automate the date in the
  135. copyright notice (which he gets tired of updating each year). He changes
  136. the template as follows:
  137. </para>
  138. <programlisting>
  139. ## template=JavaJoe Copyright
  140. #today ( $year "yyyy" )
  141. /*
  142. * MyClass.java
  143. *
  144. * Copyright (c) ${year} Java Joe
  145. * Give my code a good home.
  146. */
  147. </programlisting>
  148. <para>
  149. By using the <function>#today</function> directive, Joe assigns the value
  150. of the current date, formatted as a four digit year, to the "year"
  151. variable. Then within the copyright notice, Joe references this variable
  152. to add the current year.
  153. </para>
  154. <para>
  155. Note also that Joe has added a template label to the top of the file, which
  156. will be displayed in the Templates menu, as well as the dockable template
  157. browser.
  158. </para>
  159. </sect1>
  160. <!-- }}} -->
  161. <!-- {{{ Section: Prompting For User Input -->
  162. <sect1><title>Prompting For User Input</title>
  163. <para>
  164. Next, Joe notices that by using the <function>#prompt</function> directive,
  165. he could accept input from the user while the template is being processed.
  166. To test this functionality, he modifies his method template:
  167. </para>
  168. <programlisting>
  169. #prompt ( "Method Description:" $methodDescription )
  170. #prompt ( "Return Type:" $returnType "void")
  171. #prompt ( "Method Name:" $methodName )
  172. #prompt ( "Parameters:" $parameters )
  173. /**
  174. * ${methodDescription}
  175. */
  176. public ${returnType} ${methodName}(${parameters}) {
  177. ${returnType} retVal = null;
  178. return retVal;
  179. }
  180. </programlisting>
  181. <para>
  182. Now when this template is processed by the Templates plugin, the user will
  183. be prompted for each of:
  184. </para>
  185. <itemizedlist>
  186. <listitem><para>a method description</para></listitem>
  187. <listitem><para>the type for the return value (default = "void")</para></listitem>
  188. <listitem><para>the name of the method</para></listitem>
  189. <listitem><para>any parameters to be passed to the method</para></listitem>
  190. </itemizedlist>
  191. <para>
  192. Each of these values is then "filled in" at the appropriate point in the
  193. generated code.
  194. </para>
  195. </sect1>
  196. <!-- }}} -->
  197. <!-- {{{ Section: Conditional Statements -->
  198. <sect1><title>Conditional Statements</title>
  199. <para>
  200. Joe runs the new method template and is pleased with the result. There is
  201. only one problem: if the return type is "void", Joe would rather not have
  202. the two lines where the return variable is initialized and returned. To
  203. fix this, Joe changes the template to the following:
  204. </para>
  205. <programlisting>
  206. #prompt ( "Method Description:" $methodDescription )
  207. #prompt ( "Return Type:" $returnType "void")
  208. #prompt ( "Method Name:" $methodName )
  209. #prompt ( "Parameters:" $parameters )
  210. /**
  211. * ${methodDescription}
  212. */
  213. public ${returnType} ${methodName}(${parameters}) {
  214. #if ( $returnType != "void" )
  215. ${returnType} retVal = null;
  216. return retVal;
  217. #end
  218. }
  219. </programlisting>
  220. <para>
  221. Note the addition of the <function>#if/#end</function> block around the
  222. two lines in question. Now when Joe runs the template, the method body
  223. will be empty if the return type is "void".
  224. </para>
  225. </sect1>
  226. <!-- }}} -->
  227. <!-- {{{ Section: Calling Other Templates -->
  228. <sect1><title>Calling Other Templates (#include/#parse)</title>
  229. <para>
  230. Joe is finding that he's spending a lot of time writing Java
  231. software. Since all of his software is released using the license
  232. statement shown earlier, he'd like to
  233. be able to automatically include his license statement in each Java
  234. class. At first glance, it appears that the #include directive should
  235. allow him to do just that. He changes his Java class file as follows:
  236. </para>
  237. <programlisting>
  238. package com.javajoe.mypackage;
  239. #include ( "gpl.vm" ); ## Joe's license file
  240. import javax.swing.*;
  241. /**
  242. * Class description goes here
  243. */
  244. public class SomeClass extends Parent implements InterfaceList
  245. {
  246. // Insert class variables here
  247. public static void main(String[] args) {
  248. // Main method
  249. }
  250. //Constructors
  251. public SomeClass() {
  252. super();
  253. }
  254. // Accessors &amp; Mutators
  255. // Implementors
  256. }
  257. </programlisting>
  258. <para>
  259. The <function>#include</function> directive takes as its parameter the
  260. name of the template file to be included. Upon running the new Java class
  261. template, the license statement is indeed included, but ends up looking
  262. like this:
  263. </para>
  264. <programlisting>
  265. #today ( $year "yyyy" )
  266. /*
  267. * MyClass.java
  268. *
  269. * Copyright (c) ${year} Java Joe
  270. * Give my code a good home.
  271. */
  272. </programlisting>
  273. <para>
  274. "What gives?", ponders Joe. "It was resolving the date before". Joe consults
  275. the documentation and realizes that the <function>#include</function>
  276. directive is used to import static text, while the <function>#parse</function>
  277. directive will process the imported file as a template. Joe changes the first
  278. section of his class template to look like this:
  279. </para>
  280. <programlisting>
  281. package com.javajoe.mypackage;
  282. #parse ( "gpl.vm" ); ## Joe's license file
  283. import javax.swing.*;
  284. ## ... etc.
  285. </programlisting>
  286. <para>
  287. Now the license statement file gets processed as expected.
  288. </para>
  289. </sect1>
  290. <!-- }}} -->
  291. </chapter>
  292. <!-- }}} -->