Package org.eclipse.text.templates

# Class TemplateStoreCore

java.lang.Object
org.eclipse.text.templates.TemplateStoreCore

Direct Known Subclasses:
  `TemplateStore`

public class TemplateStoreCore extends Object
A collection of templates. Clients may instantiate this class. In order to load templates contributed using the `org.eclipse.ui.editors.templates` extension point, use a `ContributionTemplateStore`.

Since:
  3.7

## Constructor Summary

Constructors
Constructor
Description
`TemplateStoreCore(IEclipsePreferences store, String key)`
Creates a new template store.
`TemplateStoreCore(ContextTypeRegistry registry, IEclipsePreferences store, String key)`
Creates a new template store with a context type registry.

## Method Summary

Modifier and Type
Method
Description
`void`
`add(TemplatePersistenceData data)`
Adds a template encapsulated in its persistent form.
`void`
`delete(TemplatePersistenceData data)`
Removes a template from the store.
`Template`
`findTemplate(String name)`
Returns the first enabled template that matches the name.
`Template`
`findTemplate(String name, String contextTypeId)`
Returns the first enabled template that matches both name and context type id.
`Template`
`findTemplateById(String id)`
Returns the first enabled template that matches the given template id.
`protected final String`
`getKey()`
Return the key into the Preference Store whose value contains the custom templates encoded as XML.
`protected ContextTypeRegistry`
`getRegistry()`
Returns the registry.
`TemplatePersistenceData[]`
`getTemplateData(boolean includeDeleted)`
Returns all template data.
`TemplatePersistenceData`
`getTemplateData(String id)`
Returns the template data of the template with id `id` or `null` if no such template can be found.
`Template[]`
`getTemplates()`
Returns all enabled templates.
`Template[]`
`getTemplates(String contextTypeId)`
Returns all enabled templates for the given context type.
`protected void`
`handleException(IOException x)`
Handles an `IOException` thrown during reloading the preferences due to a preference store update.
`protected void`
`internalAdd(TemplatePersistenceData data)`
Adds a template to the internal store.
`protected final List<TemplatePersistenceData>`
`internalGetTemplates()`
Return the stored templates
`void`
`load()`
Loads the templates from contributions and preferences.
`protected void`
`loadContributedTemplates()`
Hook method to load contributed templates.
`void`
`restoreDefaults()`
Deletes all user-added templates and reverts all contributed templates.
`void`
`restoreDefaults(boolean doSave)`
Deletes all user-added templates and reverts all contributed templates.
`void`
`restoreDeleted()`
Restores all contributed templates that have been deleted.
`void`
`save()`
Saves the templates to the preferences.
`void`
`startListeningForPreferenceChanges()`
Starts listening for property changes on the preference store.
`void`
`stopListeningForPreferenceChanges()`
Stops the auto-updating behavior started by calling `startListeningForPreferenceChanges()`.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### TemplateStoreCore

public TemplateStoreCore(IEclipsePreferences store, String key)
Creates a new template store.

Parameters:
  `store` - the preference store in which to store custom templates under `key`
  `key` - the key into `store` where to store custom templates

### TemplateStoreCore

public TemplateStoreCore(ContextTypeRegistry registry, IEclipsePreferences store, String key)
Creates a new template store with a context type registry. Only templates that specify a context type contained in the registry will be loaded by this store if the registry is not `null`.

Parameters:
  `registry` - a context type registry, or `null` if all templates should be loaded
  `store` - the preference store in which to store custom templates under `key`
  `key` - the key into `store` where to store custom templates

## Method Details

### load

public void load() throws IOException
Loads the templates from contributions and preferences.

Throws:
  `IOException` - if loading fails.

### startListeningForPreferenceChanges

public void startListeningForPreferenceChanges()
Starts listening for property changes on the preference store. If the configured preference key changes, the template store is `reloaded`. Call `stopListeningForPreferenceChanges()` to remove any listener and stop the auto-updating behavior.

Since:
  3.2

### stopListeningForPreferenceChanges

public void stopListeningForPreferenceChanges()
Stops the auto-updating behavior started by calling `startListeningForPreferenceChanges()`.

Since:
  3.2

### handleException

protected void handleException(IOException x)
Handles an `IOException` thrown during reloading the preferences due to a preference store update. The default is to write to stderr.

Parameters:
  `x` - the exception
Since:
  3.2

### loadContributedTemplates

protected void loadContributedTemplates() throws IOException
Hook method to load contributed templates. Contributed templates are superseded by customized versions of user added templates stored in the preferences.

The default implementation does nothing.

Throws:
  `IOException` - if loading fails

### internalAdd

protected void internalAdd(TemplatePersistenceData data)
Adds a template to the internal store. The added templates must have a unique id.

Parameters:
  `data` - the template data to add

### save

public void save() throws IOException
Saves the templates to the preferences.

Throws:
  `IOException` - if the templates cannot be written

### add

public void add(TemplatePersistenceData data)
Adds a template encapsulated in its persistent form.

Parameters:
  `data` - the template to add

### delete

public void delete(TemplatePersistenceData data)
Removes a template from the store.

Parameters:
  `data` - the template to remove

### restoreDeleted

public void restoreDeleted()
Restores all contributed templates that have been deleted.

### restoreDefaults

public void restoreDefaults(boolean doSave)
Deletes all user-added templates and reverts all contributed templates.

Parameters:
  `doSave` - `true` if the store should be saved after restoring
Since:
  3.5

### restoreDefaults

public void restoreDefaults()
Deletes all user-added templates and reverts all contributed templates.

Note: the store will be saved after restoring.

### getTemplates

public Template[] getTemplates()
Returns all enabled templates.

Returns:
  all enabled templates

### getTemplates

public Template[] getTemplates(String contextTypeId)
Returns all enabled templates for the given context type.

Parameters:
  `contextTypeId` - the id of the context type of the requested templates, or `null` if all templates should be returned
Returns:
  all enabled templates for the given context type

### findTemplate

public Template findTemplate(String name)
Returns the first enabled template that matches the name.

Parameters:
  `name` - the name of the template searched for
Returns:
  the first enabled template that matches both name and context type id, or `null` if none is found

### findTemplate

public Template findTemplate(String name, String contextTypeId)
Returns the first enabled template that matches both name and context type id.

Parameters:
  `name` - the name of the template searched for
  `contextTypeId` - the context type id to clip unwanted templates, or `null` if any context type is OK
Returns:
  the first enabled template that matches both name and context type id, or `null` if none is found

### findTemplateById

public Template findTemplateById(String id)
Returns the first enabled template that matches the given template id.

Parameters:
  `id` - the id of the template searched for
Returns:
  the first enabled template that matches id, or `null` if none is found
Since:
  3.1

### getTemplateData

public TemplatePersistenceData[] getTemplateData(boolean includeDeleted)
Returns all template data.

Parameters:
  `includeDeleted` - whether to include deleted data
Returns:
  all template data, whether enabled or not

### getTemplateData

public TemplatePersistenceData getTemplateData(String id)
Returns the template data of the template with id `id` or `null` if no such template can be found.

Parameters:
  `id` - the id of the template data
Returns:
  the template data of the template with id `id` or `null`
Since:
  3.1

### getRegistry

protected ContextTypeRegistry getRegistry()
Returns the registry.

Returns:
  Returns the registry

### getKey

protected final String getKey()
Return the key into the Preference Store whose value contains the custom templates encoded as XML.

Returns:
  the key in the Preference Store

### internalGetTemplates

protected final List<TemplatePersistenceData> internalGetTemplates()
Return the stored templates

Returns:
  the stored templates