b52098564dcc4956d6f9adafca62d5f5a5272142
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / 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 import net.sourceforge.phpeclipse.ui.WebUI;
19
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IPath;
22
23 /**
24  * <code>Templates</code> gives access to the available templates.
25  * 
26  * @deprecated As of 3.0, replaced by
27  *             {@link org.eclipse.jface.text.templates.persistence.TemplateStore}
28  */
29 public class Templates extends
30                 net.sourceforge.phpdt.internal.corext.template.php.TemplateSet {
31
32         private static final String DEFAULT_FILE = "default-templates.xml"; //$NON-NLS-1$
33
34         private static final String TEMPLATE_FILE = "templates.xml"; //$NON-NLS-1$
35
36         private static final ResourceBundle fgResourceBundle = ResourceBundle
37                         .getBundle(JavaTemplateMessages.class.getName());
38
39         /** Singleton. */
40         private static Templates fgTemplates;
41
42         /**
43          * Returns an instance of templates.
44          * 
45          * @deprecated As of 3.0, replaced by
46          *             {@link net.sourceforge.phpdt.internal.ui.JavaPlugin#getTemplateStore()}
47          */
48         public static Templates getInstance() {
49                 if (fgTemplates == null)
50                         fgTemplates = new Templates();
51
52                 return fgTemplates;
53         }
54
55         public Templates() {
56                 super(
57                                 "template", WebUI.getDefault().getTemplateContextRegistry()); //$NON-NLS-1$
58                 create();
59         }
60
61         private void create() {
62
63                 try {
64                         File templateFile = getTemplateFile();
65                         if (templateFile.exists()) {
66                                 addFromFile(templateFile, true, fgResourceBundle);
67                         }
68
69                 } catch (CoreException e) {
70                         PHPeclipsePlugin.log(e);
71                         clear();
72                 }
73
74         }
75
76         /**
77          * Resets the template set.
78          */
79         public void reset() throws CoreException {
80                 clear();
81                 addFromFile(getTemplateFile(), true, fgResourceBundle);
82         }
83
84         /**
85          * Resets the template set with the default templates.
86          */
87         public void restoreDefaults() throws CoreException {
88                 clear();
89                 addFromStream(getDefaultsAsStream(), true, true, fgResourceBundle);
90         }
91
92         /**
93          * Saves the template set.
94          */
95         public void save() throws CoreException {
96                 saveToFile(getTemplateFile());
97         }
98
99         private static InputStream getDefaultsAsStream() {
100                 return Templates.class.getResourceAsStream(DEFAULT_FILE);
101         }
102
103         private static File getTemplateFile() {
104                 IPath path = PHPeclipsePlugin.getDefault().getStateLocation();
105                 path = path.append(TEMPLATE_FILE);
106
107                 return path.toFile();
108         }
109 }