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.ui.preferences;
13 import net.sourceforge.phpdt.internal.ui.IJavaHelpContextIds;
14 import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions;
15 import net.sourceforge.phpdt.internal.ui.text.template.preferences.TemplateVariableProcessor;
16 import net.sourceforge.phpdt.ui.PreferenceConstants;
17 import net.sourceforge.phpdt.ui.text.JavaTextTools;
18 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import net.sourceforge.phpeclipse.phpeditor.JavaSourceViewer;
20 import net.sourceforge.phpeclipse.ui.WebUI;
22 import org.eclipse.jface.dialogs.Dialog;
23 import org.eclipse.jface.preference.IPreferenceStore;
24 import org.eclipse.jface.resource.JFaceResources;
25 import org.eclipse.jface.text.Document;
26 import org.eclipse.jface.text.IDocument;
27 import org.eclipse.jface.text.source.SourceViewer;
28 import org.eclipse.jface.text.templates.Template;
29 import org.eclipse.jface.text.templates.TemplateContextType;
30 import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.graphics.Font;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.ui.IWorkbenchPreferencePage;
38 import org.eclipse.ui.PlatformUI;
39 import org.eclipse.ui.texteditor.templates.TemplatePreferencePage;
41 public class JavaTemplatePreferencePage extends TemplatePreferencePage
42 implements IWorkbenchPreferencePage {
44 private TemplateVariableProcessor fTemplateProcessor;
46 public JavaTemplatePreferencePage() {
47 setPreferenceStore(WebUI.getDefault().getPreferenceStore());
48 setTemplateStore(WebUI.getDefault().getTemplateStore());
49 setContextTypeRegistry(WebUI.getDefault()
50 .getTemplateContextRegistry());
51 fTemplateProcessor = new TemplateVariableProcessor();
55 * @see PreferencePage#createControl(Composite)
57 public void createControl(Composite parent) {
58 super.createControl(parent);
59 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
60 IJavaHelpContextIds.JAVA_EDITOR_PREFERENCE_PAGE);
64 * @see org.eclipse.jface.preference.IPreferencePage#performOk()
66 public boolean performOk() {
67 boolean ok = super.performOk();
69 WebUI.getDefault().savePluginPreferences();
75 * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#getFormatterPreferenceKey()
77 protected String getFormatterPreferenceKey() {
78 return PreferenceConstants.TEMPLATES_USE_CODEFORMATTER;
82 * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#createTemplateEditDialog(org.eclipse.jface.text.templates.Template,
85 protected Dialog createTemplateEditDialog(Template template, boolean edit,
86 boolean isNameModifiable) {
87 return new EditTemplateDialog(getShell(), template, edit,
88 isNameModifiable, getContextTypeRegistry());
92 * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#createViewer(org.eclipse.swt.widgets.Composite)
94 protected SourceViewer createViewer(Composite parent) {
95 GridData data = new GridData();
96 IDocument document = new Document();
97 JavaTextTools tools = WebUI.getDefault().getJavaTextTools();
98 tools.setupJavaDocumentPartitioner(document,
99 IPHPPartitions.PHP_PARTITIONING);
100 IPreferenceStore store = WebUI.getDefault()
101 .getCombinedPreferenceStore();
102 SourceViewer viewer = new JavaSourceViewer(parent, null, null, false,
103 SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL, store);
104 TemplateEditorSourceViewerConfiguration configuration = new TemplateEditorSourceViewerConfiguration(
105 tools.getColorManager(), store, null, fTemplateProcessor);
106 viewer.configure(configuration);
107 viewer.setEditable(false);
108 viewer.setDocument(document);
110 Font font = JFaceResources
111 .getFont(PreferenceConstants.EDITOR_TEXT_FONT);
112 viewer.getTextWidget().setFont(font);
113 new JavaSourcePreviewerUpdater(viewer, configuration, store);
115 Control control = viewer.getControl();
116 data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
117 | GridData.FILL_VERTICAL);
118 control.setLayoutData(data);
124 * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#updateViewerInput()
126 protected void updateViewerInput() {
127 IStructuredSelection selection = (IStructuredSelection) getTableViewer()
129 SourceViewer viewer = getViewer();
131 if (selection.size() == 1
132 && selection.getFirstElement() instanceof TemplatePersistenceData) {
133 TemplatePersistenceData data = (TemplatePersistenceData) selection
135 Template template = data.getTemplate();
136 String contextId = template.getContextTypeId();
137 TemplateContextType type = WebUI.getDefault()
138 .getTemplateContextRegistry().getContextType(contextId);
139 fTemplateProcessor.setContextType(type);
141 IDocument doc = viewer.getDocument();
144 if ("javadoc".equals(contextId)) { //$NON-NLS-1$
145 start = "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
147 start = ""; //$NON-NLS-1$
149 doc.set(start + template.getPattern());
150 int startLen = start.length();
151 viewer.setDocument(doc, startLen, doc.getLength() - startLen);
154 viewer.getDocument().set(""); //$NON-NLS-1$