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;
18 import net.sourceforge.phpeclipse.ui.WebUI;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.jface.text.templates.Template;
25 * <code>CodeTemplates</code> gives access to the available code templates.
29 * {@link net.sourceforge.phpdt.internal.ui.JavaPlugin#getCodeTemplateStore()}
32 public class CodeTemplates extends
33 net.sourceforge.phpdt.internal.corext.template.php.TemplateSet {
35 private static final String DEFAULT_FILE = "default-codetemplates.xml"; //$NON-NLS-1$
37 private static final String TEMPLATE_FILE = "codetemplates.xml"; //$NON-NLS-1$
39 private static final ResourceBundle fgResourceBundle = ResourceBundle
40 .getBundle(JavaTemplateMessages.class.getName());
43 private static CodeTemplates fgTemplates;
45 public static Template getCodeTemplate(String name) {
46 return getInstance().getFirstTemplate(name);
50 * Returns an instance of templates.
52 public static CodeTemplates getInstance() {
53 if (fgTemplates == null)
54 fgTemplates = new CodeTemplates();
59 private CodeTemplates() {
61 "codetemplate", WebUI.getDefault().getCodeTemplateContextRegistry()); //$NON-NLS-1$
65 private void create() {
68 addFromStream(getDefaultsAsStream(), false, true, fgResourceBundle);
69 File templateFile = getTemplateFile();
70 if (templateFile.exists()) {
71 addFromFile(templateFile, false, fgResourceBundle);
73 saveToFile(templateFile);
75 } catch (CoreException e) {
76 PHPeclipsePlugin.log(e);
83 * Resets the template set.
85 public void reset() throws CoreException {
87 addFromFile(getTemplateFile(), false, fgResourceBundle);
91 * Resets the template set with the default templates.
93 public void restoreDefaults() throws CoreException {
95 addFromStream(getDefaultsAsStream(), false, true, fgResourceBundle);
99 * Saves the template set.
101 public void save() throws CoreException {
102 saveToFile(getTemplateFile());
105 private static InputStream getDefaultsAsStream() {
106 return CodeTemplates.class.getResourceAsStream(DEFAULT_FILE);
109 private static File getTemplateFile() {
110 IPath path = PHPeclipsePlugin.getDefault().getStateLocation();
111 path = path.append(TEMPLATE_FILE);
113 return path.toFile();