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