Prepared better HEREDOC support; see comment for bug #1319276
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / preferences / JavaTemplatePreferencePage.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.preferences;
12
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
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;
40
41 public class JavaTemplatePreferencePage extends TemplatePreferencePage implements IWorkbenchPreferencePage {
42
43         private TemplateVariableProcessor fTemplateProcessor;
44         
45         public JavaTemplatePreferencePage() {
46                 setPreferenceStore(PHPeclipsePlugin.getDefault().getPreferenceStore());
47                 setTemplateStore(PHPeclipsePlugin.getDefault().getTemplateStore());
48                 setContextTypeRegistry(PHPeclipsePlugin.getDefault().getTemplateContextRegistry());
49                 fTemplateProcessor= new TemplateVariableProcessor();
50         }
51         
52         /*
53          * @see PreferencePage#createControl(Composite)
54          */
55         public void createControl(Composite parent) {
56                 super.createControl(parent);
57                 WorkbenchHelp.setHelp(getControl(), IJavaHelpContextIds.JAVA_EDITOR_PREFERENCE_PAGE);
58         }
59
60
61         /*
62          * @see org.eclipse.jface.preference.IPreferencePage#performOk()
63          */
64         public boolean performOk() {
65                 boolean ok= super.performOk();
66
67                 PHPeclipsePlugin.getDefault().savePluginPreferences();
68                 
69                 return ok;
70         }
71         
72         /*
73          * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#getFormatterPreferenceKey()
74          */
75         protected String getFormatterPreferenceKey() {
76                 return PreferenceConstants.TEMPLATES_USE_CODEFORMATTER;
77         }
78         
79         /*
80          * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#createTemplateEditDialog(org.eclipse.jface.text.templates.Template, boolean, boolean)
81          */
82         protected Dialog createTemplateEditDialog(Template template, boolean edit, boolean isNameModifiable) {
83                 return new EditTemplateDialog(getShell(), template, edit, isNameModifiable, getContextTypeRegistry());
84         }
85         
86         
87         /*
88          * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#createViewer(org.eclipse.swt.widgets.Composite)
89          */
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);
95                 
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);
105         
106                 Font font= JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT);
107                 viewer.getTextWidget().setFont(font);
108                 new JavaSourcePreviewerUpdater(viewer, configuration, store);
109                 
110                 Control control= viewer.getControl();
111                 data= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_VERTICAL);
112                 control.setLayoutData(data);
113                 
114                 return viewer;
115         }
116         
117         
118         /*
119          * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#updateViewerInput()
120          */
121         protected void updateViewerInput() {
122                 IStructuredSelection selection= (IStructuredSelection) getTableViewer().getSelection();
123                 SourceViewer viewer= getViewer();
124                 
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);
131                         
132                         IDocument doc= viewer.getDocument();
133                         
134                         String start= null;
135                         if ("javadoc".equals(contextId)) { //$NON-NLS-1$
136                                 start= "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
137                         } else
138                                 start= ""; //$NON-NLS-1$
139                         
140                         doc.set(start + template.getPattern());
141                         int startLen= start.length();
142                         viewer.setDocument(doc, startLen, doc.getLength() - startLen);
143
144                 } else {
145                         viewer.getDocument().set(""); //$NON-NLS-1$
146                 }               
147         }
148 }