Fixed: 1736622 - "php marker occurrences" color - did`t have an effect
[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.ui.IWorkbenchPreferencePage;
37 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.ui.texteditor.templates.TemplatePreferencePage;
39
40 public class JavaTemplatePreferencePage extends TemplatePreferencePage
41                 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()
49                                 .getTemplateContextRegistry());
50                 fTemplateProcessor = new TemplateVariableProcessor();
51         }
52
53         /*
54          * @see PreferencePage#createControl(Composite)
55          */
56         public void createControl(Composite parent) {
57                 super.createControl(parent);
58                 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
59                                 IJavaHelpContextIds.JAVA_EDITOR_PREFERENCE_PAGE);
60         }
61
62         /*
63          * @see org.eclipse.jface.preference.IPreferencePage#performOk()
64          */
65         public boolean performOk() {
66                 boolean ok = super.performOk();
67
68                 PHPeclipsePlugin.getDefault().savePluginPreferences();
69
70                 return ok;
71         }
72
73         /*
74          * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#getFormatterPreferenceKey()
75          */
76         protected String getFormatterPreferenceKey() {
77                 return PreferenceConstants.TEMPLATES_USE_CODEFORMATTER;
78         }
79
80         /*
81          * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#createTemplateEditDialog(org.eclipse.jface.text.templates.Template,
82          *      boolean, boolean)
83          */
84         protected Dialog createTemplateEditDialog(Template template, boolean edit,
85                         boolean isNameModifiable) {
86                 return new EditTemplateDialog(getShell(), template, edit,
87                                 isNameModifiable, getContextTypeRegistry());
88         }
89
90         /*
91          * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#createViewer(org.eclipse.swt.widgets.Composite)
92          */
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);
108
109                 Font font = JFaceResources
110                                 .getFont(PreferenceConstants.EDITOR_TEXT_FONT);
111                 viewer.getTextWidget().setFont(font);
112                 new JavaSourcePreviewerUpdater(viewer, configuration, store);
113
114                 Control control = viewer.getControl();
115                 data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
116                                 | GridData.FILL_VERTICAL);
117                 control.setLayoutData(data);
118
119                 return viewer;
120         }
121
122         /*
123          * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#updateViewerInput()
124          */
125         protected void updateViewerInput() {
126                 IStructuredSelection selection = (IStructuredSelection) getTableViewer()
127                                 .getSelection();
128                 SourceViewer viewer = getViewer();
129
130                 if (selection.size() == 1
131                                 && selection.getFirstElement() instanceof TemplatePersistenceData) {
132                         TemplatePersistenceData data = (TemplatePersistenceData) selection
133                                         .getFirstElement();
134                         Template template = data.getTemplate();
135                         String contextId = template.getContextTypeId();
136                         TemplateContextType type = PHPeclipsePlugin.getDefault()
137                                         .getTemplateContextRegistry().getContextType(contextId);
138                         fTemplateProcessor.setContextType(type);
139
140                         IDocument doc = viewer.getDocument();
141
142                         String start = null;
143                         if ("javadoc".equals(contextId)) { //$NON-NLS-1$
144                                 start = "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
145                         } else
146                                 start = ""; //$NON-NLS-1$
147
148                         doc.set(start + template.getPattern());
149                         int startLen = start.length();
150                         viewer.setDocument(doc, startLen, doc.getLength() - startLen);
151
152                 } else {
153                         viewer.getDocument().set(""); //$NON-NLS-1$
154                 }
155         }
156 }