1 11 package org.eclipse.jdt.internal.ui.preferences; 12 13 import java.util.Iterator ; 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 60 public TemplateVariableTextHover(TemplateVariableProcessor processor) { 61 fProcessor= processor; 62 } 63 64 67 public String 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))) { String varName= doc.get(offset, subject.getLength()); 73 TemplateContextType contextType= fProcessor.getContextType(); 74 if (contextType != null) { 75 Iterator 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 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 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 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 key, IColorManager manager) { 152 RGB rgb= PreferenceConverter.getColor(store, key); 153 return manager.getColor(rgb); 154 } 155 156 160 public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType, int stateMask) { 161 return new TemplateVariableTextHover(fProcessor); 162 } 163 164 } 165 | Popular Tags |