1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.preferences;
13 import java.util.Iterator;
15 import org.eclipse.swt.graphics.Color;
16 import org.eclipse.swt.graphics.RGB;
17 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.jface.preference.IPreferenceStore;
20 import org.eclipse.jface.preference.PreferenceConverter;
22 import org.eclipse.jface.text.BadLocationException;
23 import org.eclipse.jface.text.DefaultInformationControl;
24 import org.eclipse.jface.text.IDocument;
25 import org.eclipse.jface.text.IInformationControl;
26 import org.eclipse.jface.text.IInformationControlCreator;
27 import org.eclipse.jface.text.IRegion;
28 import org.eclipse.jface.text.ITextHover;
29 import org.eclipse.jface.text.ITextViewer;
30 import org.eclipse.jface.text.contentassist.ContentAssistant;
31 import org.eclipse.jface.text.contentassist.IContentAssistant;
32 import org.eclipse.jface.text.source.ISourceViewer;
33 import org.eclipse.jface.text.templates.TemplateContextType;
34 import org.eclipse.jface.text.templates.TemplateVariableResolver;
36 import org.eclipse.ui.texteditor.ITextEditor;
38 import org.eclipse.jdt.ui.PreferenceConstants;
39 import org.eclipse.jdt.ui.text.IColorManager;
40 import org.eclipse.jdt.ui.text.IJavaPartitions;
41 import org.eclipse.jdt.ui.text.JavaTextTools;
43 import org.eclipse.jdt.internal.ui.JavaPlugin;
44 import org.eclipse.jdt.internal.ui.text.JavaWordFinder;
45 import org.eclipse.jdt.internal.ui.text.SimpleJavaSourceViewerConfiguration;
46 import org.eclipse.jdt.internal.ui.text.template.preferences.TemplateVariableProcessor;
49 public class CodeTemplateSourceViewerConfiguration extends SimpleJavaSourceViewerConfiguration {
51 private static class TemplateVariableTextHover implements ITextHover {
53 private TemplateVariableProcessor fProcessor;
56 * @param processor the template variable processor
58 public TemplateVariableTextHover(TemplateVariableProcessor processor) {
59 fProcessor= processor;
63 * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
65 public String getHoverInfo(ITextViewer textViewer, IRegion subject) {
67 IDocument doc= textViewer.getDocument();
68 int offset= subject.getOffset();
69 if (offset >= 2 && "${".equals(doc.get(offset-2, 2))) { //$NON-NLS-1$
70 String varName= doc.get(offset, subject.getLength());
71 TemplateContextType contextType= fProcessor.getContextType();
72 if (contextType != null) {
73 Iterator<TemplateVariableResolver> iter= contextType.resolvers();
74 while (iter.hasNext()) {
75 TemplateVariableResolver var= iter.next();
76 if (varName.equals(var.getType())) {
77 return var.getDescription();
82 } catch (BadLocationException e) {
88 * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer, int)
90 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
91 if (textViewer != null) {
92 return JavaWordFinder.findWord(textViewer.getDocument(), offset);
99 private final TemplateVariableProcessor fProcessor;
101 public CodeTemplateSourceViewerConfiguration(IColorManager colorManager, IPreferenceStore store, ITextEditor editor, TemplateVariableProcessor processor) {
102 super(colorManager, store, editor, IJavaPartitions.JAVA_PARTITIONING, false);
103 fProcessor= processor;
107 * @see SourceViewerConfiguration#getContentAssistant(ISourceViewer)
110 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
112 IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
113 JavaTextTools textTools= JavaPlugin.getDefault().getJavaTextTools();
114 IColorManager manager= textTools.getColorManager();
117 ContentAssistant assistant= new ContentAssistant();
118 assistant.setContentAssistProcessor(fProcessor, IDocument.DEFAULT_CONTENT_TYPE);
119 // Register the same processor for strings and single line comments to get code completion at the start of those partitions.
120 assistant.setContentAssistProcessor(fProcessor, IJavaPartitions.JAVA_STRING);
121 assistant.setContentAssistProcessor(fProcessor, IJavaPartitions.JAVA_CHARACTER);
122 assistant.setContentAssistProcessor(fProcessor, IJavaPartitions.JAVA_SINGLE_LINE_COMMENT);
123 assistant.setContentAssistProcessor(fProcessor, IJavaPartitions.JAVA_MULTI_LINE_COMMENT);
124 assistant.setContentAssistProcessor(fProcessor, IJavaPartitions.JAVA_DOC);
126 assistant.enableAutoInsert(store.getBoolean(PreferenceConstants.CODEASSIST_AUTOINSERT));
127 assistant.enableAutoActivation(store.getBoolean(PreferenceConstants.CODEASSIST_AUTOACTIVATION));
128 assistant.setAutoActivationDelay(store.getInt(PreferenceConstants.CODEASSIST_AUTOACTIVATION_DELAY));
129 assistant.setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY);
130 assistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
131 assistant.setInformationControlCreator(new IInformationControlCreator() {
132 public IInformationControl createInformationControl(Shell parent) {
133 return new DefaultInformationControl(parent, JavaPlugin.getAdditionalInfoAffordanceString());
137 Color background= getColor(store, PreferenceConstants.CODEASSIST_PARAMETERS_BACKGROUND, manager);
138 assistant.setContextInformationPopupBackground(background);
139 assistant.setContextSelectorBackground(background);
141 Color foreground= getColor(store, PreferenceConstants.CODEASSIST_PARAMETERS_FOREGROUND, manager);
142 assistant.setContextInformationPopupForeground(foreground);
143 assistant.setContextSelectorForeground(foreground);
148 private Color getColor(IPreferenceStore store, String key, IColorManager manager) {
149 RGB rgb= PreferenceConverter.getColor(store, key);
150 return manager.getColor(rgb);
154 * @see SourceViewerConfiguration#getTextHover(ISourceViewer, String, int)
158 public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType, int stateMask) {
159 return new TemplateVariableTextHover(fProcessor);