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