KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Iterator JavaDoc;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.graphics.Color;
17 import org.eclipse.swt.graphics.RGB;
18 import org.eclipse.swt.widgets.Shell;
19
20 import org.eclipse.jface.internal.text.html.HTMLTextPresenter;
21 import org.eclipse.jface.preference.IPreferenceStore;
22 import org.eclipse.jface.preference.PreferenceConverter;
23
24 import org.eclipse.jface.text.BadLocationException;
25 import org.eclipse.jface.text.DefaultInformationControl;
26 import org.eclipse.jface.text.IDocument;
27 import org.eclipse.jface.text.IInformationControl;
28 import org.eclipse.jface.text.IInformationControlCreator;
29 import org.eclipse.jface.text.IRegion;
30 import org.eclipse.jface.text.ITextHover;
31 import org.eclipse.jface.text.ITextViewer;
32 import org.eclipse.jface.text.contentassist.ContentAssistant;
33 import org.eclipse.jface.text.contentassist.IContentAssistant;
34 import org.eclipse.jface.text.source.ISourceViewer;
35 import org.eclipse.jface.text.templates.TemplateContextType;
36 import org.eclipse.jface.text.templates.TemplateVariableResolver;
37
38 import org.eclipse.ui.texteditor.ITextEditor;
39
40 import org.eclipse.jdt.ui.PreferenceConstants;
41 import org.eclipse.jdt.ui.text.IColorManager;
42 import org.eclipse.jdt.ui.text.IJavaPartitions;
43 import org.eclipse.jdt.ui.text.JavaTextTools;
44
45 import org.eclipse.jdt.internal.ui.JavaPlugin;
46 import org.eclipse.jdt.internal.ui.text.JavaWordFinder;
47 import org.eclipse.jdt.internal.ui.text.SimpleJavaSourceViewerConfiguration;
48 import org.eclipse.jdt.internal.ui.text.template.preferences.TemplateVariableProcessor;
49
50
51 public class CodeTemplateSourceViewerConfiguration extends SimpleJavaSourceViewerConfiguration {
52
53     private static class TemplateVariableTextHover implements ITextHover {
54
55         private TemplateVariableProcessor fProcessor;
56
57         /**
58          * @param processor the template variable processor
59          */

60         public TemplateVariableTextHover(TemplateVariableProcessor processor) {
61             fProcessor= processor;
62         }
63
64         /* (non-Javadoc)
65          * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
66          */

67         public String JavaDoc getHoverInfo(ITextViewer textViewer, IRegion subject) {
68             try {
69                 IDocument doc= textViewer.getDocument();
70                 int offset= subject.getOffset();
71                 if (offset >= 2 && "${".equals(doc.get(offset-2, 2))) { //$NON-NLS-1$
72
String JavaDoc varName= doc.get(offset, subject.getLength());
73                     TemplateContextType contextType= fProcessor.getContextType();
74                     if (contextType != null) {
75                         Iterator JavaDoc iter= contextType.resolvers();
76                         while (iter.hasNext()) {
77                             TemplateVariableResolver var= (TemplateVariableResolver) iter.next();
78                             if (varName.equals(var.getType())) {
79                                 return var.getDescription();
80                             }
81                         }
82                     }
83                 }
84             } catch (BadLocationException e) {
85             }
86             return null;
87         }
88
89         /* (non-Javadoc)
90          * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer, int)
91          */

92         public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
93             if (textViewer != null) {
94                 return JavaWordFinder.findWord(textViewer.getDocument(), offset);
95             }
96             return null;
97         }
98         
99     }
100     
101     private final TemplateVariableProcessor fProcessor;
102
103     public CodeTemplateSourceViewerConfiguration(IColorManager colorManager, IPreferenceStore store, ITextEditor editor, TemplateVariableProcessor processor) {
104         super(colorManager, store, editor, IJavaPartitions.JAVA_PARTITIONING, false);
105         fProcessor= processor;
106     }
107     
108     /*
109      * @see SourceViewerConfiguration#getContentAssistant(ISourceViewer)
110      */

111     public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
112
113         IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
114         JavaTextTools textTools= JavaPlugin.getDefault().getJavaTextTools();
115         IColorManager manager= textTools.getColorManager();
116         
117
118         ContentAssistant assistant= new ContentAssistant();
119         assistant.setContentAssistProcessor(fProcessor, IDocument.DEFAULT_CONTENT_TYPE);
120             // Register the same processor for strings and single line comments to get code completion at the start of those partitions.
121
assistant.setContentAssistProcessor(fProcessor, IJavaPartitions.JAVA_STRING);
122         assistant.setContentAssistProcessor(fProcessor, IJavaPartitions.JAVA_CHARACTER);
123         assistant.setContentAssistProcessor(fProcessor, IJavaPartitions.JAVA_SINGLE_LINE_COMMENT);
124         assistant.setContentAssistProcessor(fProcessor, IJavaPartitions.JAVA_MULTI_LINE_COMMENT);
125         assistant.setContentAssistProcessor(fProcessor, IJavaPartitions.JAVA_DOC);
126
127         assistant.enableAutoInsert(store.getBoolean(PreferenceConstants.CODEASSIST_AUTOINSERT));
128         assistant.enableAutoActivation(store.getBoolean(PreferenceConstants.CODEASSIST_AUTOACTIVATION));
129         assistant.setAutoActivationDelay(store.getInt(PreferenceConstants.CODEASSIST_AUTOACTIVATION_DELAY));
130         assistant.setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY);
131         assistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
132         assistant.setInformationControlCreator(new IInformationControlCreator() {
133             public IInformationControl createInformationControl(Shell parent) {
134                 return new DefaultInformationControl(parent, SWT.NONE, new HTMLTextPresenter(true));
135             }
136         });
137
138         Color background= getColor(store, PreferenceConstants.CODEASSIST_PROPOSALS_BACKGROUND, manager);
139         assistant.setContextInformationPopupBackground(background);
140         assistant.setContextSelectorBackground(background);
141         assistant.setProposalSelectorBackground(background);
142
143         Color foreground= getColor(store, PreferenceConstants.CODEASSIST_PROPOSALS_FOREGROUND, manager);
144         assistant.setContextInformationPopupForeground(foreground);
145         assistant.setContextSelectorForeground(foreground);
146         assistant.setProposalSelectorForeground(foreground);
147         
148         return assistant;
149     }
150
151     private Color getColor(IPreferenceStore store, String JavaDoc key, IColorManager manager) {
152         RGB rgb= PreferenceConverter.getColor(store, key);
153         return manager.getColor(rgb);
154     }
155     
156     /*
157      * @see SourceViewerConfiguration#getTextHover(ISourceViewer, String, int)
158      * @since 2.1
159      */

160     public ITextHover getTextHover(ISourceViewer sourceViewer, String JavaDoc contentType, int stateMask) {
161         return new TemplateVariableTextHover(fProcessor);
162     }
163
164 }
165
Popular Tags