]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/preferences/CodeTemplateSourceViewerConfiguration.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / preferences / CodeTemplateSourceViewerConfiguration.java
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
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;
14
15 import org.eclipse.swt.graphics.Color;
16 import org.eclipse.swt.graphics.RGB;
17 import org.eclipse.swt.widgets.Shell;
18
19 import org.eclipse.jface.preference.IPreferenceStore;
20 import org.eclipse.jface.preference.PreferenceConverter;
21
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;
35
36 import org.eclipse.ui.texteditor.ITextEditor;
37
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;
42
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;
47
48
49 public class CodeTemplateSourceViewerConfiguration extends SimpleJavaSourceViewerConfiguration {
50
51         private static class TemplateVariableTextHover implements ITextHover {
52
53                 private TemplateVariableProcessor fProcessor;
54
55                 /**
56                  * @param processor the template variable processor
57                  */
58                 public TemplateVariableTextHover(TemplateVariableProcessor processor) {
59                         fProcessor= processor;
60                 }
61
62                 /* (non-Javadoc)
63                  * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
64                  */
65                 public String getHoverInfo(ITextViewer textViewer, IRegion subject) {
66                         try {
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();
78                                                         }
79                                                 }
80                                         }
81                                 }
82                         } catch (BadLocationException e) {
83                         }
84                         return null;
85                 }
86
87                 /* (non-Javadoc)
88                  * @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer, int)
89                  */
90                 public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
91                         if (textViewer != null) {
92                                 return JavaWordFinder.findWord(textViewer.getDocument(), offset);
93                         }
94                         return null;
95                 }
96
97         }
98
99         private final TemplateVariableProcessor fProcessor;
100
101         public CodeTemplateSourceViewerConfiguration(IColorManager colorManager, IPreferenceStore store, ITextEditor editor, TemplateVariableProcessor processor) {
102                 super(colorManager, store, editor, IJavaPartitions.JAVA_PARTITIONING, false);
103                 fProcessor= processor;
104         }
105
106         /*
107          * @see SourceViewerConfiguration#getContentAssistant(ISourceViewer)
108          */
109         @Override
110         public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
111
112                 IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
113                 JavaTextTools textTools= JavaPlugin.getDefault().getJavaTextTools();
114                 IColorManager manager= textTools.getColorManager();
115
116
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);
125
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());
134                         }
135                 });
136
137                 Color background= getColor(store, PreferenceConstants.CODEASSIST_PARAMETERS_BACKGROUND, manager);
138                 assistant.setContextInformationPopupBackground(background);
139                 assistant.setContextSelectorBackground(background);
140
141                 Color foreground= getColor(store, PreferenceConstants.CODEASSIST_PARAMETERS_FOREGROUND, manager);
142                 assistant.setContextInformationPopupForeground(foreground);
143                 assistant.setContextSelectorForeground(foreground);
144
145                 return assistant;
146         }
147
148         private Color getColor(IPreferenceStore store, String key, IColorManager manager) {
149                 RGB rgb= PreferenceConverter.getColor(store, key);
150                 return manager.getColor(rgb);
151         }
152
153         /*
154          * @see SourceViewerConfiguration#getTextHover(ISourceViewer, String, int)
155          * @since 2.1
156          */
157         @Override
158         public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType, int stateMask) {
159                 return new TemplateVariableTextHover(fProcessor);
160         }
161
162 }