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;
21 import org.eclipse.jface.dialogs.Dialog;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.resource.JFaceResources;
24 import org.eclipse.jface.text.Document;
25 import org.eclipse.jface.text.IDocument;
26 import org.eclipse.jface.text.source.SourceViewer;
27 import org.eclipse.jface.text.templates.Template;
28 import org.eclipse.jface.text.templates.TemplateContextType;
29 import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.graphics.Font;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.ui.IWorkbenchPreferencePage;
37 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.ui.texteditor.templates.TemplatePreferencePage;
40 public class JavaTemplatePreferencePage extends TemplatePreferencePage
41 implements IWorkbenchPreferencePage {
43 private TemplateVariableProcessor fTemplateProcessor;
45 public JavaTemplatePreferencePage() {
46 setPreferenceStore(PHPeclipsePlugin.getDefault().getPreferenceStore());
47 setTemplateStore(PHPeclipsePlugin.getDefault().getTemplateStore());
48 setContextTypeRegistry(PHPeclipsePlugin.getDefault()
49 .getTemplateContextRegistry());
50 fTemplateProcessor = new TemplateVariableProcessor();
54 * @see PreferencePage#createControl(Composite)
56 public void createControl(Composite parent) {
57 super.createControl(parent);
58 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
59 IJavaHelpContextIds.JAVA_EDITOR_PREFERENCE_PAGE);
63 * @see org.eclipse.jface.preference.IPreferencePage#performOk()
65 public boolean performOk() {
66 boolean ok = super.performOk();
68 PHPeclipsePlugin.getDefault().savePluginPreferences();
74 * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#getFormatterPreferenceKey()
76 protected String getFormatterPreferenceKey() {
77 return PreferenceConstants.TEMPLATES_USE_CODEFORMATTER;
81 * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#createTemplateEditDialog(org.eclipse.jface.text.templates.Template,
84 protected Dialog createTemplateEditDialog(Template template, boolean edit,
85 boolean isNameModifiable) {
86 return new EditTemplateDialog(getShell(), template, edit,
87 isNameModifiable, getContextTypeRegistry());
91 * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#createViewer(org.eclipse.swt.widgets.Composite)
93 protected SourceViewer createViewer(Composite parent) {
94 GridData data = new GridData();
95 IDocument document = new Document();
96 JavaTextTools tools = PHPeclipsePlugin.getDefault().getJavaTextTools();
97 tools.setupJavaDocumentPartitioner(document,
98 IPHPPartitions.PHP_PARTITIONING);
99 IPreferenceStore store = PHPeclipsePlugin.getDefault()
100 .getCombinedPreferenceStore();
101 SourceViewer viewer = new JavaSourceViewer(parent, null, null, false,
102 SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL, store);
103 TemplateEditorSourceViewerConfiguration configuration = new TemplateEditorSourceViewerConfiguration(
104 tools.getColorManager(), store, null, fTemplateProcessor);
105 viewer.configure(configuration);
106 viewer.setEditable(false);
107 viewer.setDocument(document);
109 Font font = JFaceResources
110 .getFont(PreferenceConstants.EDITOR_TEXT_FONT);
111 viewer.getTextWidget().setFont(font);
112 new JavaSourcePreviewerUpdater(viewer, configuration, store);
114 Control control = viewer.getControl();
115 data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
116 | GridData.FILL_VERTICAL);
117 control.setLayoutData(data);
123 * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#updateViewerInput()
125 protected void updateViewerInput() {
126 IStructuredSelection selection = (IStructuredSelection) getTableViewer()
128 SourceViewer viewer = getViewer();
130 if (selection.size() == 1
131 && selection.getFirstElement() instanceof TemplatePersistenceData) {
132 TemplatePersistenceData data = (TemplatePersistenceData) selection
134 Template template = data.getTemplate();
135 String contextId = template.getContextTypeId();
136 TemplateContextType type = PHPeclipsePlugin.getDefault()
137 .getTemplateContextRegistry().getContextType(contextId);
138 fTemplateProcessor.setContextType(type);
140 IDocument doc = viewer.getDocument();
143 if ("javadoc".equals(contextId)) { //$NON-NLS-1$
144 start = "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
146 start = ""; //$NON-NLS-1$
148 doc.set(start + template.getPattern());
149 int startLen = start.length();
150 viewer.setDocument(doc, startLen, doc.getLength() - startLen);
153 viewer.getDocument().set(""); //$NON-NLS-1$