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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.corext.template.php;
14 import java.io.InputStream;
15 import java.util.ResourceBundle;
17 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.jface.text.templates.Template;
24 * <code>CodeTemplates</code> gives access to the available code templates.
26 * @deprecated use {@link org.eclipse.jdt.internal.ui.JavaPlugin#getCodeTemplateStore()} instead
28 public class CodeTemplates extends net.sourceforge.phpdt.internal.corext.template.php.TemplateSet {
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());
35 private static CodeTemplates fgTemplates;
37 public static Template getCodeTemplate(String name) {
38 return getInstance().getFirstTemplate(name);
42 * Returns an instance of templates.
44 public static CodeTemplates getInstance() {
45 if (fgTemplates == null)
46 fgTemplates= new CodeTemplates();
51 private CodeTemplates() {
52 super("codetemplate", PHPeclipsePlugin.getDefault().getCodeTemplateContextRegistry()); //$NON-NLS-1$
56 private void create() {
59 addFromStream(getDefaultsAsStream(), false, true, fgResourceBundle);
60 File templateFile= getTemplateFile();
61 if (templateFile.exists()) {
62 addFromFile(templateFile, false, fgResourceBundle);
64 saveToFile(templateFile);
66 } catch (CoreException e) {
67 PHPeclipsePlugin.log(e);
74 * Resets the template set.
76 public void reset() throws CoreException {
78 addFromFile(getTemplateFile(), false, fgResourceBundle);
82 * Resets the template set with the default templates.
84 public void restoreDefaults() throws CoreException {
86 addFromStream(getDefaultsAsStream(), false, true, fgResourceBundle);
90 * Saves the template set.
92 public void save() throws CoreException {
93 saveToFile(getTemplateFile());
96 private static InputStream getDefaultsAsStream() {
97 return CodeTemplates.class.getResourceAsStream(DEFAULT_FILE);
100 private static File getTemplateFile() {
101 IPath path= PHPeclipsePlugin.getDefault().getStateLocation();
102 path= path.append(TEMPLATE_FILE);
104 return path.toFile();