1 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 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 54 public TemplateVariableTextHover(TemplateVariableProcessor processor) { 55 fProcessor= processor; 56 } 57 58 61 public String 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))) { String varName= doc.get(offset, subject.getLength()); 67 TemplateContextType contextType= fProcessor.getContextType(); 68 if (contextType != null) { 69 Iterator 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 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 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 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 key, IColorManager manager) { 142 RGB rgb= PreferenceConverter.getColor(store, key); 143 return manager.getColor(rgb); 144 } 145 146 150 public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType, int stateMask) { 151 return new TemplateVariableTextHover(fProcessor); 152 } 153 154 } 155 | Popular Tags |