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