KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > JavaTemplatePreferencePage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.preferences;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.graphics.Font;
15 import org.eclipse.swt.graphics.Point;
16 import org.eclipse.swt.layout.GridData;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Control;
19
20 import org.eclipse.jface.preference.IPreferenceStore;
21 import org.eclipse.jface.resource.JFaceResources;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.window.Window;
24
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
32 import org.eclipse.ui.IWorkbenchPreferencePage;
33 import org.eclipse.ui.PlatformUI;
34 import org.eclipse.ui.texteditor.templates.TemplatePreferencePage;
35
36 import org.eclipse.jdt.ui.PreferenceConstants;
37 import org.eclipse.jdt.ui.text.IJavaPartitions;
38 import org.eclipse.jdt.ui.text.JavaTextTools;
39
40 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
41 import org.eclipse.jdt.internal.ui.JavaPlugin;
42 import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer;
43 import org.eclipse.jdt.internal.ui.text.SimpleJavaSourceViewerConfiguration;
44 import org.eclipse.jdt.internal.ui.text.template.preferences.TemplateVariableProcessor;
45
46 public class JavaTemplatePreferencePage extends TemplatePreferencePage implements IWorkbenchPreferencePage {
47
48     private TemplateVariableProcessor fTemplateProcessor;
49     
50     public JavaTemplatePreferencePage() {
51         setPreferenceStore(JavaPlugin.getDefault().getPreferenceStore());
52         setTemplateStore(JavaPlugin.getDefault().getTemplateStore());
53         setContextTypeRegistry(JavaPlugin.getDefault().getTemplateContextRegistry());
54         fTemplateProcessor= new TemplateVariableProcessor();
55     }
56     
57     /*
58      * @see PreferencePage#createControl(Composite)
59      */

60     public void createControl(Composite parent) {
61         super.createControl(parent);
62         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.TEMPLATE_PREFERENCE_PAGE);
63     }
64     
65     /*
66      * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#createContents(org.eclipse.swt.widgets.Composite)
67      * @since 3.3
68      */

69     protected Control createContents(Composite ancestor) {
70         ScrolledPageContent scrolled= new ScrolledPageContent(ancestor, SWT.H_SCROLL | SWT.V_SCROLL);
71         scrolled.setExpandHorizontal(true);
72         scrolled.setExpandVertical(true);
73         
74         Control control= super.createContents(scrolled);
75
76         scrolled.setContent(control);
77         final Point size= control.computeSize(SWT.DEFAULT, SWT.DEFAULT);
78         scrolled.setMinSize(size.x, size.y);
79         
80         return scrolled;
81     }
82
83
84     /*
85      * @see org.eclipse.jface.preference.IPreferencePage#performOk()
86      */

87     public boolean performOk() {
88         boolean ok= super.performOk();
89
90         JavaPlugin.getDefault().savePluginPreferences();
91         
92         return ok;
93     }
94     
95     /*
96      * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#getFormatterPreferenceKey()
97      */

98     protected String JavaDoc getFormatterPreferenceKey() {
99         return PreferenceConstants.TEMPLATES_USE_CODEFORMATTER;
100     }
101     
102     /*
103      * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#createTemplateEditDialog2(org.eclipse.jface.text.templates.Template, boolean, boolean)
104      */

105     protected Template editTemplate(Template template, boolean edit, boolean isNameModifiable) {
106         org.eclipse.jdt.internal.ui.preferences.EditTemplateDialog dialog= new org.eclipse.jdt.internal.ui.preferences.EditTemplateDialog(getShell(), template, edit, isNameModifiable, getContextTypeRegistry());
107         if (dialog.open() == Window.OK) {
108             return dialog.getTemplate();
109         }
110         return null;
111     }
112     
113     /*
114      * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#createViewer(org.eclipse.swt.widgets.Composite)
115      */

116     protected SourceViewer createViewer(Composite parent) {
117         IDocument document= new Document();
118         JavaTextTools tools= JavaPlugin.getDefault().getJavaTextTools();
119         tools.setupJavaDocumentPartitioner(document, IJavaPartitions.JAVA_PARTITIONING);
120         IPreferenceStore store= JavaPlugin.getDefault().getCombinedPreferenceStore();
121         SourceViewer viewer= new JavaSourceViewer(parent, null, null, false, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL, store);
122         SimpleJavaSourceViewerConfiguration configuration= new SimpleJavaSourceViewerConfiguration(tools.getColorManager(), store, null, IJavaPartitions.JAVA_PARTITIONING, false);
123         viewer.configure(configuration);
124         viewer.setEditable(false);
125         viewer.setDocument(document);
126     
127         Font font= JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT);
128         viewer.getTextWidget().setFont(font);
129         new JavaSourcePreviewerUpdater(viewer, configuration, store);
130         
131         Control control= viewer.getControl();
132         GridData data= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_VERTICAL);
133         control.setLayoutData(data);
134         
135         return viewer;
136     }
137     
138     
139     /*
140      * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#updateViewerInput()
141      */

142     protected void updateViewerInput() {
143         IStructuredSelection selection= (IStructuredSelection) getTableViewer().getSelection();
144         SourceViewer viewer= getViewer();
145         
146         if (selection.size() == 1 && selection.getFirstElement() instanceof TemplatePersistenceData) {
147             TemplatePersistenceData data= (TemplatePersistenceData) selection.getFirstElement();
148             Template template= data.getTemplate();
149             String JavaDoc contextId= template.getContextTypeId();
150             TemplateContextType type= JavaPlugin.getDefault().getTemplateContextRegistry().getContextType(contextId);
151             fTemplateProcessor.setContextType(type);
152             
153             IDocument doc= viewer.getDocument();
154             
155             String JavaDoc start= null;
156             if ("javadoc".equals(contextId)) { //$NON-NLS-1$
157
start= "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
158
} else
159                 start= ""; //$NON-NLS-1$
160

161             doc.set(start + template.getPattern());
162             int startLen= start.length();
163             viewer.setDocument(doc, startLen, doc.getLength() - startLen);
164
165         } else {
166             viewer.getDocument().set(""); //$NON-NLS-1$
167
}
168     }
169 }
170
Popular Tags