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.swt.widgets.Label;
37 import org.eclipse.ui.IWorkbenchPreferencePage;
38 import org.eclipse.ui.help.WorkbenchHelp;
39 import org.eclipse.ui.texteditor.templates.TemplatePreferencePage;
41 public class JavaTemplatePreferencePage extends TemplatePreferencePage implements IWorkbenchPreferencePage {
43 private TemplateVariableProcessor fTemplateProcessor;
45 public JavaTemplatePreferencePage() {
46 setPreferenceStore(PHPeclipsePlugin.getDefault().getPreferenceStore());
47 setTemplateStore(PHPeclipsePlugin.getDefault().getTemplateStore());
48 setContextTypeRegistry(PHPeclipsePlugin.getDefault().getTemplateContextRegistry());
49 fTemplateProcessor= new TemplateVariableProcessor();
53 * @see PreferencePage#createControl(Composite)
55 public void createControl(Composite parent) {
56 super.createControl(parent);
57 WorkbenchHelp.setHelp(getControl(), IJavaHelpContextIds.JAVA_EDITOR_PREFERENCE_PAGE);
62 * @see org.eclipse.jface.preference.IPreferencePage#performOk()
64 public boolean performOk() {
65 boolean ok= super.performOk();
67 PHPeclipsePlugin.getDefault().savePluginPreferences();
73 * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#getFormatterPreferenceKey()
75 protected String getFormatterPreferenceKey() {
76 return PreferenceConstants.TEMPLATES_USE_CODEFORMATTER;
80 * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#createTemplateEditDialog(org.eclipse.jface.text.templates.Template, boolean, boolean)
82 protected Dialog createTemplateEditDialog(Template template, boolean edit, boolean isNameModifiable) {
83 return new EditTemplateDialog(getShell(), template, edit, isNameModifiable, getContextTypeRegistry());
88 * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#createViewer(org.eclipse.swt.widgets.Composite)
90 protected SourceViewer createViewer(Composite parent) {
91 Label label= new Label(parent, SWT.NONE);
92 label.setText(PreferencesMessages.getString("CodeTemplateBlock.preview")); //$NON-NLS-1$
93 GridData data= new GridData();
94 label.setLayoutData(data);
96 IDocument document= new Document();
97 JavaTextTools tools= PHPeclipsePlugin.getDefault().getJavaTextTools();
98 tools.setupJavaDocumentPartitioner(document, IPHPPartitions.PHP_PARTITIONING);
99 IPreferenceStore store= PHPeclipsePlugin.getDefault().getCombinedPreferenceStore();
100 SourceViewer viewer= new JavaSourceViewer(parent, null, null, false, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL, store);
101 TemplateEditorSourceViewerConfiguration configuration= new TemplateEditorSourceViewerConfiguration(tools.getColorManager(), store, null, fTemplateProcessor);
102 viewer.configure(configuration);
103 viewer.setEditable(false);
104 viewer.setDocument(document);
106 Font font= JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT);
107 viewer.getTextWidget().setFont(font);
108 new JavaSourcePreviewerUpdater(viewer, configuration, store);
110 Control control= viewer.getControl();
111 data= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_VERTICAL);
112 control.setLayoutData(data);
119 * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#updateViewerInput()
121 protected void updateViewerInput() {
122 IStructuredSelection selection= (IStructuredSelection) getTableViewer().getSelection();
123 SourceViewer viewer= getViewer();
125 if (selection.size() == 1 && selection.getFirstElement() instanceof TemplatePersistenceData) {
126 TemplatePersistenceData data= (TemplatePersistenceData) selection.getFirstElement();
127 Template template= data.getTemplate();
128 String contextId= template.getContextTypeId();
129 TemplateContextType type= PHPeclipsePlugin.getDefault().getTemplateContextRegistry().getContextType(contextId);
130 fTemplateProcessor.setContextType(type);
132 IDocument doc= viewer.getDocument();
135 if ("javadoc".equals(contextId)) { //$NON-NLS-1$
136 start= "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
138 start= ""; //$NON-NLS-1$
140 doc.set(start + template.getPattern());
141 int startLen= start.length();
142 viewer.setDocument(doc, startLen, doc.getLength() - startLen);
145 viewer.getDocument().set(""); //$NON-NLS-1$