1 /*******************************************************************************
2 * Copyright (c) 2002, 2010 QNX Software Systems 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
9 * QNX Software Systems - Initial API and implementation
10 * Wind River Systems, Inc. - Bug fixes
11 *******************************************************************************/
12 package org.eclipse.cdt.internal.ui.preferences;
14 import org.eclipse.jface.preference.IPreferenceStore;
15 import org.eclipse.jface.resource.JFaceResources;
16 import org.eclipse.jface.text.Document;
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.contentassist.ContentAssistant;
19 import org.eclipse.jface.text.contentassist.IContentAssistant;
20 import org.eclipse.jface.text.source.ISourceViewer;
21 import org.eclipse.jface.text.source.SourceViewer;
22 import org.eclipse.jface.text.templates.ContextTypeRegistry;
23 import org.eclipse.jface.text.templates.Template;
24 import org.eclipse.jface.window.Window;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.accessibility.AccessibleAdapter;
27 import org.eclipse.swt.accessibility.AccessibleEvent;
28 import org.eclipse.swt.graphics.Font;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Shell;
33 import org.eclipse.ui.PlatformUI;
34 import org.eclipse.ui.texteditor.templates.TemplatePreferencePage;
36 import org.eclipse.cdt.ui.CUIPlugin;
37 import org.eclipse.cdt.ui.PreferenceConstants;
38 import org.eclipse.cdt.ui.text.CSourceViewerConfiguration;
39 import org.eclipse.cdt.ui.text.ICPartitions;
41 import org.eclipse.cdt.internal.ui.ICHelpContextIds;
42 import org.eclipse.cdt.internal.ui.editor.CSourceViewer;
43 import org.eclipse.cdt.internal.ui.text.CTextTools;
46 * Template preference page for C/C++ editor templates.
48 public class CTemplatePreferencePage extends TemplatePreferencePage {
51 * A dialog to edit a template.
53 protected class CEditTemplateDialog extends EditTemplateDialog {
55 public CEditTemplateDialog(Shell shell, Template template,
56 boolean edit, boolean isNameModifiable,
57 ContextTypeRegistry contextTypeRegistry) {
58 super(shell, template, edit, isNameModifiable, contextTypeRegistry);
61 * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage.EditTemplateDialog#createViewer(org.eclipse.swt.widgets.Composite)
64 protected SourceViewer createViewer(Composite parent) {
65 IPreferenceStore store= CUIPlugin.getDefault().getCombinedPreferenceStore();
66 CSourceViewer viewer= new CSourceViewer(parent, null, null, false, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL, store);
67 CTextTools tools= CUIPlugin.getDefault().getTextTools();
68 CSourceViewerConfiguration configuration= new CSourceViewerConfiguration(tools.getColorManager(), store, null, tools.getDocumentPartitioning()) {
70 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
71 ContentAssistant assistant= new ContentAssistant();
72 assistant.enableAutoActivation(true);
73 assistant.enableAutoInsert(true);
74 assistant.setContentAssistProcessor(getTemplateProcessor(), IDocument.DEFAULT_CONTENT_TYPE);
75 assistant.setContentAssistProcessor(getTemplateProcessor(), ICPartitions.C_MULTI_LINE_COMMENT);
76 assistant.setContentAssistProcessor(getTemplateProcessor(), ICPartitions.C_SINGLE_LINE_COMMENT);
77 assistant.setContentAssistProcessor(getTemplateProcessor(), ICPartitions.C_PREPROCESSOR);
81 IDocument document = new Document();
82 tools.setupCDocument(document);
83 viewer.configure(configuration);
84 viewer.setEditable(true);
85 viewer.setDocument(document);
87 Font font= JFaceResources.getFontRegistry().get(PreferenceConstants.EDITOR_TEXT_FONT);
88 viewer.getTextWidget().setFont(font);
90 CSourcePreviewerUpdater.registerPreviewer(viewer, configuration, CUIPlugin.getDefault().getCombinedPreferenceStore());
95 public CTemplatePreferencePage() {
96 setPreferenceStore(CUIPlugin.getDefault().getPreferenceStore());
97 setTemplateStore(CUIPlugin.getDefault().getTemplateStore());
98 setContextTypeRegistry(CUIPlugin.getDefault().getTemplateContextRegistry());
102 * @see PreferencePage#createControl(Composite)
105 public void createControl(Composite parent) {
106 super.createControl(parent);
107 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), ICHelpContextIds.TEMPLATE_PREFERENCE_PAGE);
111 * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#getFormatterPreferenceKey()
114 protected String getFormatterPreferenceKey() {
115 return PreferenceConstants.TEMPLATES_USE_CODEFORMATTER;
119 * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#createTemplateEditDialog2(org.eclipse.jface.text.templates.Template, boolean, boolean)
122 protected Template editTemplate(Template template, boolean edit, boolean isNameModifiable) {
123 CEditTemplateDialog dialog= new CEditTemplateDialog(getShell(), template, edit, isNameModifiable, getContextTypeRegistry());
124 if (dialog.open() == Window.OK) {
125 return dialog.getTemplate();
131 * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#createViewer(org.eclipse.swt.widgets.Composite)
134 protected SourceViewer createViewer(Composite parent) {
135 IPreferenceStore store= CUIPlugin.getDefault().getCombinedPreferenceStore();
136 CSourceViewer viewer= new CSourceViewer(parent, null, null, false, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL, store);
137 CTextTools tools= CUIPlugin.getDefault().getTextTools();
138 CSourceViewerConfiguration configuration = new CSourceViewerConfiguration(tools.getColorManager(), store, null, tools.getDocumentPartitioning());
139 IDocument document = new Document();
140 tools.setupCDocument(document);
141 viewer.configure(configuration);
142 viewer.setEditable(false);
143 viewer.setDocument(document);
145 Font font= JFaceResources.getFontRegistry().get(PreferenceConstants.EDITOR_TEXT_FONT);
146 viewer.getTextWidget().setFont(font);
148 Control control= viewer.getControl();
149 GridData data= new GridData(GridData.FILL_BOTH);
150 data.heightHint= convertHeightInCharsToPixels(5);
151 control.setLayoutData(data);
153 control.getAccessible().addAccessibleListener(new AccessibleAdapter() {
155 public void getName(AccessibleEvent e) {
156 e.result = PreferencesMessages.TemplatePreferencePage_Viewer_preview;
159 CSourcePreviewerUpdater.registerPreviewer(viewer, configuration, CUIPlugin.getDefault().getCombinedPreferenceStore());