001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.examples.templateeditor.editors;
011:
012: import java.io.IOException;
013:
014: import org.eclipse.core.runtime.IStatus;
015: import org.eclipse.core.runtime.Status;
016:
017: import org.eclipse.jface.preference.IPreferenceStore;
018: import org.eclipse.jface.resource.ImageDescriptor;
019: import org.eclipse.jface.resource.ImageRegistry;
020:
021: import org.eclipse.jface.text.templates.ContextTypeRegistry;
022: import org.eclipse.jface.text.templates.persistence.TemplateStore;
023:
024: import org.eclipse.ui.editors.text.templates.ContributionTemplateStore;
025: import org.eclipse.ui.editors.text.templates.ContributionContextTypeRegistry;
026: import org.eclipse.ui.examples.javaeditor.JavaEditorExamplePlugin;
027: import org.eclipse.ui.examples.templateeditor.template.XMLContextType;
028: import org.eclipse.ui.plugin.AbstractUIPlugin;
029:
030: /**
031: * The main plugin class to be used in the desktop.
032: */
033: public class TemplateEditorUI {
034: /** Key to store custom templates. */
035: private static final String CUSTOM_TEMPLATES_KEY = "org.eclipse.ui.examples.templateeditor.customtemplates"; //$NON-NLS-1$
036:
037: /** The shared instance. */
038: private static TemplateEditorUI fInstance;
039:
040: /** The template store. */
041: private TemplateStore fStore;
042: /** The context type registry. */
043: private ContributionContextTypeRegistry fRegistry;
044:
045: private TemplateEditorUI() {
046: }
047:
048: /**
049: * Returns the shared instance.
050: *
051: * @return the shared instance
052: */
053: public static TemplateEditorUI getDefault() {
054: if (fInstance == null)
055: fInstance = new TemplateEditorUI();
056: return fInstance;
057: }
058:
059: /**
060: * Returns this plug-in's template store.
061: *
062: * @return the template store of this plug-in instance
063: */
064: public TemplateStore getTemplateStore() {
065: if (fStore == null) {
066: fStore = new ContributionTemplateStore(
067: getContextTypeRegistry(), JavaEditorExamplePlugin
068: .getDefault().getPreferenceStore(),
069: CUSTOM_TEMPLATES_KEY);
070: try {
071: fStore.load();
072: } catch (IOException e) {
073: JavaEditorExamplePlugin
074: .getDefault()
075: .getLog()
076: .log(
077: new Status(
078: IStatus.ERROR,
079: "org.eclipse.ui.examples.javaeditor", IStatus.OK, "", e)); //$NON-NLS-1$ //$NON-NLS-2$
080: }
081: }
082: return fStore;
083: }
084:
085: /**
086: * Returns this plug-in's context type registry.
087: *
088: * @return the context type registry for this plug-in instance
089: */
090: public ContextTypeRegistry getContextTypeRegistry() {
091: if (fRegistry == null) {
092: // create an configure the contexts available in the template editor
093: fRegistry = new ContributionContextTypeRegistry();
094: fRegistry.addContextType(XMLContextType.XML_CONTEXT_TYPE);
095: }
096: return fRegistry;
097: }
098:
099: /* Forward plug-in methods to javaeditor example plugin default instance */
100: public ImageRegistry getImageRegistry() {
101: return JavaEditorExamplePlugin.getDefault().getImageRegistry();
102: }
103:
104: public static ImageDescriptor imageDescriptorFromPlugin(
105: String string, String default_image) {
106: return AbstractUIPlugin.imageDescriptorFromPlugin(string,
107: default_image);
108: }
109:
110: public IPreferenceStore getPreferenceStore() {
111: return JavaEditorExamplePlugin.getDefault()
112: .getPreferenceStore();
113: }
114:
115: public void savePluginPreferences() {
116: JavaEditorExamplePlugin.getDefault().savePluginPreferences();
117: }
118:
119: }
|