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