RSyntaxTextArea - Code Templates Example
Back to Example IndexDownload as Eclipse project
Back to RSyntaxTextArea Home
Description
A "code template" is a kind of macro for commonly-typed code. It associates
a short identifier with a longer code snippet. When code templates are
enabled and a short identifier is typed, followed by Ctrl+Shift+Space, it is replaced
with the corresponding longer code snippet.
Code templates are shared among all RSyntaxTextAreas in an application. Adding and
removing of code templates is done through the installed
CodeTemplateManager.
RSyntaxTextArea comes with a
StaticCodeTemplate concrete implementation that
inserts static text before and/or after the current caret position. You can also create
and use your own custom
CodeTemplate implementations.
The Example Sourceimport java.awt.*; import javax.swing.*; import org.fife.ui.rtextarea.*; import org.fife.ui.rsyntaxtextarea.*; import org.fife.ui.rsyntaxtextarea.templates.*; /** * A simple example showing how to add and use Code Templates to an * RSyntaxTextArea.<p> * * A "code template" is a kind of macro for commonly-typed code. It associates a * short identifier with a longer code snippet. When code templates are enabled * and a short identifier is typed, it is replaced with the corresponding longer * code snippet. Type Ctrl+Shift+Space to insert the template.<p> * * This example uses RSyntaxTextArea 2.0.1.<p> * * Project Home: http://fifesoft.com/rsyntaxtextarea<br> * Downloads: https://sourceforge.net/projects/rsyntaxtextarea */ public class CodeTemplateDemo extends JFrame { private static final long serialVersionUID = 1L; public CodeTemplateDemo() { JPanel cp = new JPanel(new BorderLayout()); RSyntaxTextArea textArea = new RSyntaxTextArea(20, 60); textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA); textArea.setCodeFoldingEnabled(true); textArea.setAntiAliasingEnabled(true); RTextScrollPane sp = new RTextScrollPane(textArea); sp.setFoldIndicatorEnabled(true); cp.add(sp); // Whether templates are enabled is a global property affecting all // RSyntaxTextAreas, so this method is static. RSyntaxTextArea.setTemplatesEnabled(true); // Code templates are shared among all RSyntaxTextAreas. You add and // remove templates through the shared CodeTemplateManager instance. CodeTemplateManager ctm = RSyntaxTextArea.getCodeTemplateManager(); // StaticCodeTemplates are templates that insert static text before and // after the current caret position. This template is basically shorthand // for "System.out.println(". CodeTemplate ct = new StaticCodeTemplate("sout", "System.out.println(", null); ctm.addTemplate(ct); // This template is for a for-loop. The caret is placed at the upper // bound of the loop. ct = new StaticCodeTemplate("fb", "for (int i=0; i<", "; i++) {\n\t\n}\n"); ctm.addTemplate(ct); setContentPane(cp); setTitle("CodeTemplate Demo"); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); } public static void main(String[] args) { // Start all Swing applications on the EDT. SwingUtilities.invokeLater(new Runnable() { public void run() { new CodeTemplateDemo().setVisible(true); } }); } }Save this file as CodeTemplateDemo.java.
Compiling the Example
For simplicity, we will just use javac on the command line to compile. Bring up a command
prompt or shell, make sure javac is on your PATH, and run the following command:
Windows: javac -classpath <path-to-jar>\rsyntaxtextarea.jar CodeTemplateDemo.java Unix: javac -classpath <path-to-jar>/rsyntaxtextarea.jar CodeTemplateDemo.javawhere <path-to-jar> is the path to the rsyntaxtextarea.jar file. This should yield no errors or warnings, and on completion there should see a file named CodeTemplateDemo.class in your current directory.
Running the Example
Running the example is just as simple as compiling it:
Windows: java -classpath <path-to-jar>\rsyntaxtextarea.jar;. CodeTemplateDemo Unix: java -classpath <path-to-jar>/rsyntaxtextarea.jar:. CodeTemplateDemoA window should pop up, containing a text editor. When you type in the editor, the text will be highilghted as Java code.
If you type "sout", then press Ctrl+Shift+space, "sout" will be replaced
by "System.out.println(". Similarly, typing "fb" followed by
Ctrl+Shift+space will change "fb" into a basic for-loop structure.