KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.graphics.Color;
16 import org.eclipse.swt.graphics.RGB;
17
18 import org.eclipse.jface.preference.IPreferenceStore;
19 import org.eclipse.jface.preference.PreferenceConverter;
20
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.IRegion;
24 import org.eclipse.jface.text.ITextHover;
25 import org.eclipse.jface.text.ITextViewer;
26 import org.eclipse.jface.text.contentassist.ContentAssistant;
27 import org.eclipse.jface.text.contentassist.IContentAssistant;
28 import org.eclipse.jface.text.source.ISourceViewer;
29 import org.eclipse.jface.text.templates.TemplateContextType;
30 import org.eclipse.jface.text.templates.TemplateVariableResolver;
31
32 import org.eclipse.ui.texteditor.ITextEditor;
33
34 import org.eclipse.jdt.ui.PreferenceConstants;
35 import org.eclipse.jdt.ui.text.IColorManager;
36 import org.eclipse.jdt.ui.text.IJavaPartitions;
37 import org.eclipse.jdt.ui.text.JavaTextTools;
38
39 import org.eclipse.jdt.internal.ui.JavaPlugin;
40 import org.eclipse.jdt.internal.ui.text.JavaWordFinder;
41 import org.eclipse.jdt.internal.ui.text.SimpleJavaSourceViewerConfiguration;
42 import org.eclipse.jdt.internal.ui.text.template.preferences.TemplateVariableProcessor;
43
44
45 public class TemplateEditorSourceViewerConfiguration extends SimpleJavaSourceViewerConfiguration {
46
47     private static class TemplateVariableTextHover implements ITextHover {
48
49         private TemplateVariableProcessor fProcessor;
50
51         /**
52          * @param processor the template variable processor
53          */

54         public TemplateVariableTextHover(TemplateVariableProcessor processor) {
55             fProcessor= processor;
56         }
57
58         /* (non-Javadoc)
59          * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
60          */

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

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

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

150     public ITextHover getTextHover(ISourceViewer sourceViewer, String JavaDoc contentType, int stateMask) {
151         return new TemplateVariableTextHover(fProcessor);
152     }
153
154 }
155
Popular Tags