upload tizen1.0 source
[sdk/ide/product.git] / org.eclipse.cdt.ui / src / org / eclipse / cdt / internal / ui / preferences / CTemplatePreferencePage.java
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
7  *
8  * Contributors:
9  * QNX Software Systems - Initial API and implementation
10  * Wind River Systems, Inc. - Bug fixes
11  *******************************************************************************/
12 package org.eclipse.cdt.internal.ui.preferences;
13
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;
35
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;
40
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;
44
45 /**
46  * Template preference page for C/C++ editor templates.
47  */
48 public class CTemplatePreferencePage extends TemplatePreferencePage {
49
50         /**
51          * A dialog to edit a template.
52          */
53         protected class CEditTemplateDialog extends EditTemplateDialog {
54
55                 public CEditTemplateDialog(Shell shell, Template template,
56                                 boolean edit, boolean isNameModifiable,
57                                 ContextTypeRegistry contextTypeRegistry) {
58                         super(shell, template, edit, isNameModifiable, contextTypeRegistry);
59                 }
60                 /*
61                  * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage.EditTemplateDialog#createViewer(org.eclipse.swt.widgets.Composite)
62                  */
63                 @Override
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()) {
69                                 @Override
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);
78                                         return assistant;
79                                 }
80                         };
81                         IDocument document = new Document();
82                         tools.setupCDocument(document);
83                         viewer.configure(configuration);
84                         viewer.setEditable(true);
85                         viewer.setDocument(document);
86                 
87                         Font font= JFaceResources.getFontRegistry().get(PreferenceConstants.EDITOR_TEXT_FONT);
88                         viewer.getTextWidget().setFont(font);
89                 
90                         CSourcePreviewerUpdater.registerPreviewer(viewer, configuration, CUIPlugin.getDefault().getCombinedPreferenceStore());
91                         return viewer;
92                 }
93         }
94
95         public CTemplatePreferencePage() {
96                 setPreferenceStore(CUIPlugin.getDefault().getPreferenceStore());
97                 setTemplateStore(CUIPlugin.getDefault().getTemplateStore());
98                 setContextTypeRegistry(CUIPlugin.getDefault().getTemplateContextRegistry());
99         }
100         
101         /*
102          * @see PreferencePage#createControl(Composite)
103          */
104         @Override
105         public void createControl(Composite parent) {
106                 super.createControl(parent);
107                 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), ICHelpContextIds.TEMPLATE_PREFERENCE_PAGE);
108         }
109
110         /*
111          * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#getFormatterPreferenceKey()
112          */
113         @Override
114         protected String getFormatterPreferenceKey() {
115                 return PreferenceConstants.TEMPLATES_USE_CODEFORMATTER;
116         }
117
118         /*
119          * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#createTemplateEditDialog2(org.eclipse.jface.text.templates.Template, boolean, boolean)
120          */
121         @Override
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();
126                 }
127                 return null;
128         }
129
130         /*
131          * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#createViewer(org.eclipse.swt.widgets.Composite)
132          */
133         @Override
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);
144         
145                 Font font= JFaceResources.getFontRegistry().get(PreferenceConstants.EDITOR_TEXT_FONT);
146                 viewer.getTextWidget().setFont(font);
147                 
148                 Control control= viewer.getControl();
149                 GridData data= new GridData(GridData.FILL_BOTH);
150                 data.heightHint= convertHeightInCharsToPixels(5);
151                 control.setLayoutData(data);
152         
153                 control.getAccessible().addAccessibleListener(new AccessibleAdapter() {                 
154                         @Override
155                         public void getName(AccessibleEvent e) {
156                                 e.result = PreferencesMessages.TemplatePreferencePage_Viewer_preview; 
157                 }});
158                 
159                 CSourcePreviewerUpdater.registerPreviewer(viewer, configuration, CUIPlugin.getDefault().getCombinedPreferenceStore());
160                 return viewer;
161         }
162
163 }