Syntax highlighting is changeable.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / corext / template / php / Templates.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.corext.template.php;
12
13 import java.io.File;
14 import java.io.InputStream;
15 import java.util.ResourceBundle;
16
17 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21
22
23 /**
24  * <code>Templates</code> gives access to the available templates.
25  * 
26  * @deprecated As of 3.0, replaced by {@link org.eclipse.jface.text.templates.persistence.TemplateStore}
27  */
28 public class Templates extends net.sourceforge.phpdt.internal.corext.template.php.TemplateSet {
29
30         private static final String DEFAULT_FILE= "default-templates.xml"; //$NON-NLS-1$
31         private static final String TEMPLATE_FILE= "templates.xml"; //$NON-NLS-1$
32         private static final ResourceBundle fgResourceBundle= ResourceBundle.getBundle(JavaTemplateMessages.class.getName());
33
34         /** Singleton. */
35         private static Templates fgTemplates;
36
37         /**
38          * Returns an instance of templates.
39          * 
40          * @deprecated As of 3.0, replaced by {@link net.sourceforge.phpdt.internal.ui.JavaPlugin#getTemplateStore()}
41          */
42         public static Templates getInstance() {
43                 if (fgTemplates == null)
44                         fgTemplates= new Templates();
45                 
46                 return fgTemplates;
47         }
48         
49         public Templates() {
50                 super("template", PHPeclipsePlugin.getDefault().getTemplateContextRegistry()); //$NON-NLS-1$
51                 create();
52         }
53         
54
55         private void create() {
56
57                 try {
58                         File templateFile= getTemplateFile();
59                         if (templateFile.exists()) {
60                                 addFromFile(templateFile, true, fgResourceBundle);
61                         }
62
63                 } catch (CoreException e) {
64                   PHPeclipsePlugin.log(e);
65                         clear();
66                 }
67
68         }       
69         
70         /**
71          * Resets the template set.
72          */
73         public void reset() throws CoreException {
74                 clear();
75                 addFromFile(getTemplateFile(), true, fgResourceBundle);
76         }
77
78         /**
79          * Resets the template set with the default templates.
80          */
81         public void restoreDefaults() throws CoreException {
82                 clear();
83                 addFromStream(getDefaultsAsStream(), true, true, fgResourceBundle);
84         }
85
86         /**
87          * Saves the template set.
88          */
89         public void save() throws CoreException {                                       
90                 saveToFile(getTemplateFile());
91         }
92
93         private static InputStream getDefaultsAsStream() {
94                 return Templates.class.getResourceAsStream(DEFAULT_FILE);
95         }
96
97         private static File getTemplateFile() {
98                 IPath path= PHPeclipsePlugin.getDefault().getStateLocation();
99                 path= path.append(TEMPLATE_FILE);
100                 
101                 return path.toFile();
102         }
103 }
104