org.fife.ui.rsyntaxtextarea
Class CodeTemplateManager

java.lang.Object
  extended by org.fife.ui.rsyntaxtextarea.CodeTemplateManager

public class CodeTemplateManager
extends java.lang.Object

Manages "code templates."

All methods in this class are synchronized for thread safety, but as a best practice, you should probably only modify the templates known to a CodeTemplateManager on the EDT. Modifying a CodeTemplate retrieved from a CodeTemplateManager while not on the EDT could cause problems.

For more flexible boilerplate code insertion, consider using the TemplateCompletion class in the AutoComplete add-on library.

Version:
1.0
Author:
Robert Futrell

Constructor Summary
CodeTemplateManager()
          Constructor.
 
Method Summary
 void addTemplate(CodeTemplate template)
          Registers the specified template with this template manager.
 CodeTemplate getTemplate(RSyntaxTextArea textArea)
          Returns the template that should be inserted at the current caret position, assuming the trigger character was pressed.
 int getTemplateCount()
          Returns the number of templates this manager knows about.
 CodeTemplate[] getTemplates()
          Returns the templates currently available.
static boolean isValidChar(char ch)
          Returns whether the specified character is a valid character for a CodeTemplate id.
 boolean removeTemplate(CodeTemplate template)
          Returns the specified code template.
 CodeTemplate removeTemplate(java.lang.String id)
          Returns the code template with the specified id.
 void replaceTemplates(CodeTemplate[] newTemplates)
          Replaces the current set of available templates with the ones specified.
 boolean saveTemplates()
          Saves all templates as XML files in the current template directory.
 int setTemplateDirectory(java.io.File dir)
          Sets the directory in which to look for templates.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CodeTemplateManager

public CodeTemplateManager()
Constructor.

Method Detail

addTemplate

public void addTemplate(CodeTemplate template)
Registers the specified template with this template manager.

Parameters:
template - The template to register.
Throws:
java.lang.IllegalArgumentException - If template is null.
See Also:
removeTemplate(CodeTemplate), removeTemplate(String)

getTemplate

public CodeTemplate getTemplate(RSyntaxTextArea textArea)
Returns the template that should be inserted at the current caret position, assuming the trigger character was pressed.

Parameters:
textArea - The text area that's getting text inserted into it.
Returns:
A template that should be inserted, if appropriate, or null if no template should be inserted.

getTemplateCount

public int getTemplateCount()
Returns the number of templates this manager knows about.

Returns:
The template count.

getTemplates

public CodeTemplate[] getTemplates()
Returns the templates currently available.

Returns:
The templates available.

isValidChar

public static final boolean isValidChar(char ch)
Returns whether the specified character is a valid character for a CodeTemplate id.

Parameters:
ch - The character to check.
Returns:
Whether the character is a valid template character.

removeTemplate

public boolean removeTemplate(CodeTemplate template)
Returns the specified code template.

Parameters:
template - The template to remove.
Returns:
true if the template was removed, false if the template was not in this template manager.
Throws:
java.lang.IllegalArgumentException - If template is null.
See Also:
removeTemplate(String), addTemplate(CodeTemplate)

removeTemplate

public CodeTemplate removeTemplate(java.lang.String id)
Returns the code template with the specified id.

Parameters:
id - The id to check for.
Returns:
The code template that was removed, or null if there was no template with the specified ID.
Throws:
java.lang.IllegalArgumentException - If id is null.
See Also:
removeTemplate(CodeTemplate), addTemplate(CodeTemplate)

replaceTemplates

public void replaceTemplates(CodeTemplate[] newTemplates)
Replaces the current set of available templates with the ones specified.

Parameters:
newTemplates - The new set of templates. Note that we will be taking a shallow copy of these and sorting them.

saveTemplates

public boolean saveTemplates()
Saves all templates as XML files in the current template directory.

Returns:
Whether or not the save was successful.

setTemplateDirectory

public int setTemplateDirectory(java.io.File dir)
Sets the directory in which to look for templates. Calling this method adds any new templates found in the specified directory to the templates already registered.

Parameters:
dir - The new directory in which to look for templates.
Returns:
The new number of templates in this template manager, or -1 if the specified directory does not exist.