2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.corext.template;
8 import java.io.InputStream;
10 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
11 import org.eclipse.core.runtime.CoreException;
12 import org.eclipse.core.runtime.IPath;
13 import org.eclipse.jface.dialogs.ErrorDialog;
16 * <code>Templates</code> gives access to the available templates.
18 public class Templates extends TemplateSet {
20 private static final String DEFAULT_FILE= "default-templates.xml"; //$NON-NLS-1$
21 private static final String TEMPLATE_FILE= "templates.xml"; //$NON-NLS-1$
24 private static Templates fgTemplates;
27 * Returns an instance of templates.
29 public static Templates getInstance() {
30 if (fgTemplates == null)
31 fgTemplates= create();
36 private static Templates create() {
37 Templates templates= new Templates();
40 File templateFile= getTemplateFile();
41 if (templateFile.exists()) {
42 templates.addFromFile(templateFile);
44 templates.addFromStream(getDefaultsAsStream());
45 templates.saveToFile(templateFile);
48 } catch (CoreException e) {
49 PHPeclipsePlugin.log(e);
50 ErrorDialog.openError(null,
51 TemplateMessages.getString("Templates.error.title"), //$NON-NLS-1$
52 e.getMessage(), e.getStatus());
61 * Resets the template set.
63 public void reset() throws CoreException {
65 addFromFile(getTemplateFile());
69 * Resets the template set with the default templates.
71 public void restoreDefaults() throws CoreException {
73 addFromStream(getDefaultsAsStream());
77 * Saves the template set.
79 public void save() throws CoreException {
80 saveToFile(getTemplateFile());
83 private static InputStream getDefaultsAsStream() {
84 return Templates.class.getResourceAsStream(DEFAULT_FILE);
87 private static File getTemplateFile() {
88 IPath path= PHPeclipsePlugin.getDefault().getStateLocation();
89 path= path.append(TEMPLATE_FILE);