71c4c9d66edceb97963136d09af01c19121cd408
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / corext / template / Templates.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.corext.template;
6
7 import java.io.File;
8 import java.io.InputStream;
9
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;
14
15 /**
16  * <code>Templates</code> gives access to the available templates.
17  */
18 public class Templates extends TemplateSet {
19
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$
22
23         /** Singleton. */
24         private static Templates fgTemplates;
25
26         /**
27          * Returns an instance of templates.
28          */
29         public static Templates getInstance() {
30                 if (fgTemplates == null)
31                         fgTemplates= create();
32                 
33                 return fgTemplates;
34         }
35
36         private static Templates create() {
37                 Templates templates= new Templates();
38
39                 try {                   
40                         File templateFile= getTemplateFile();
41                         if (templateFile.exists()) {
42                                 templates.addFromFile(templateFile);
43                         } else {
44                                 templates.addFromStream(getDefaultsAsStream());
45                                 templates.saveToFile(templateFile);
46                         }
47
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());
53
54                         templates.clear();
55                 }
56
57                 return templates;
58         }       
59         
60         /**
61          * Resets the template set.
62          */
63         public void reset() throws CoreException {
64                 clear();
65                 addFromFile(getTemplateFile());
66         }
67
68         /**
69          * Resets the template set with the default templates.
70          */
71         public void restoreDefaults() throws CoreException {
72                 clear();
73                 addFromStream(getDefaultsAsStream());
74         }
75
76         /**
77          * Saves the template set.
78          */
79         public void save() throws CoreException {                                       
80                 saveToFile(getTemplateFile());
81         }
82
83         private static InputStream getDefaultsAsStream() {
84                 return Templates.class.getResourceAsStream(DEFAULT_FILE);
85         }
86
87         private static File getTemplateFile() {
88                 IPath path= PHPeclipsePlugin.getDefault().getStateLocation();
89                 path= path.append(TEMPLATE_FILE);
90                 
91                 return path.toFile();
92         }
93 }
94