84cddff29d649cea4d6a87f83bd347ffcb1cee41
[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
12 import org.eclipse.core.runtime.CoreException;
13 import org.eclipse.core.runtime.IPath;
14 import org.eclipse.jface.dialogs.ErrorDialog;
15
16 /**
17  * <code>ObfuscatorIgnores</code> gives access to the available templates.
18  */
19 public class Templates extends TemplateSet {
20
21         private static final String DEFAULT_FILE= "default-templates.xml"; //$NON-NLS-1$
22         private static final String TEMPLATE_FILE= "templates.xml"; //$NON-NLS-1$
23
24         /** Singleton. */
25         private static Templates fgTemplates;
26
27         /**
28          * Returns an instance of templates.
29          */
30         public static Templates getInstance() {
31                 if (fgTemplates == null)
32                         fgTemplates= create();
33                 
34                 return fgTemplates;
35         }
36
37         private static Templates create() {
38                 Templates templates= new Templates();
39
40                 try {                   
41                         File templateFile= getTemplateFile();
42                         if (templateFile.exists()) {
43                                 templates.addFromFile(templateFile);
44                         } else {
45                                 templates.addFromStream(getDefaultsAsStream());
46                                 templates.saveToFile(templateFile);
47                         }
48
49                 } catch (CoreException e) {
50                         PHPeclipsePlugin.log(e);
51                         ErrorDialog.openError(null,
52                                 TemplateMessages.getString("Templates.error.title"), //$NON-NLS-1$
53                                 e.getMessage(), e.getStatus());
54
55                         templates.clear();
56                 }
57
58                 return templates;
59         }       
60         
61         /**
62          * Resets the template set.
63          */
64         public void reset() throws CoreException {
65                 clear();
66                 addFromFile(getTemplateFile());
67         }
68
69         /**
70          * Resets the template set with the default templates.
71          */
72         public void restoreDefaults() throws CoreException {
73                 clear();
74                 addFromStream(getDefaultsAsStream());
75         }
76
77         /**
78          * Saves the template set.
79          */
80         public void save() throws CoreException {                                       
81                 saveToFile(getTemplateFile());
82         }
83
84         private static InputStream getDefaultsAsStream() {
85                 return Templates.class.getResourceAsStream(DEFAULT_FILE);
86         }
87
88         private static File getTemplateFile() {
89                 IPath path= PHPeclipsePlugin.getDefault().getStateLocation();
90                 path= path.append(TEMPLATE_FILE);
91                 
92                 return path.toFile();
93         }
94 }
95