1 11 package org.eclipse.jdt.internal.ui.text.java.hover; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.jface.text.IInformationControlCreator; 18 import org.eclipse.jface.text.IRegion; 19 import org.eclipse.jface.text.ITextHover; 20 import org.eclipse.jface.text.ITextHoverExtension; 21 import org.eclipse.jface.text.ITextViewer; 22 import org.eclipse.jface.text.information.IInformationProviderExtension2; 23 24 import org.eclipse.ui.IEditorPart; 25 26 import org.eclipse.jdt.ui.PreferenceConstants; 27 import org.eclipse.jdt.ui.text.java.hover.IJavaEditorTextHover; 28 29 import org.eclipse.jdt.internal.ui.JavaPlugin; 30 31 34 public class BestMatchHover extends AbstractJavaEditorTextHover implements ITextHoverExtension, IInformationProviderExtension2 { 35 36 private List fTextHoverSpecifications; 37 private List fInstantiatedTextHovers; 38 private ITextHover fBestHover; 39 40 public BestMatchHover() { 41 installTextHovers(); 42 } 43 44 public BestMatchHover(IEditorPart editor) { 45 this(); 46 setEditor(editor); 47 } 48 49 52 private void installTextHovers() { 53 54 fTextHoverSpecifications= new ArrayList (2); 56 fInstantiatedTextHovers= new ArrayList (2); 57 58 JavaEditorTextHoverDescriptor[] hoverDescs= JavaPlugin.getDefault().getJavaEditorTextHoverDescriptors(); 60 for (int i= 0; i < hoverDescs.length; i++) { 61 if (!PreferenceConstants.ID_BESTMATCH_HOVER.equals(hoverDescs[i].getId())) 63 fTextHoverSpecifications.add(hoverDescs[i]); 64 } 65 } 66 67 private void checkTextHovers() { 68 if (fTextHoverSpecifications.size() == 0) 69 return; 70 71 for (Iterator iterator= new ArrayList (fTextHoverSpecifications).iterator(); iterator.hasNext(); ) { 72 JavaEditorTextHoverDescriptor spec= (JavaEditorTextHoverDescriptor) iterator.next(); 73 74 IJavaEditorTextHover hover= spec.createTextHover(); 75 if (hover != null) { 76 hover.setEditor(getEditor()); 77 addTextHover(hover); 78 fTextHoverSpecifications.remove(spec); 79 } 80 } 81 } 82 83 protected void addTextHover(ITextHover hover) { 84 if (!fInstantiatedTextHovers.contains(hover)) 85 fInstantiatedTextHovers.add(hover); 86 } 87 88 91 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) { 92 93 checkTextHovers(); 94 fBestHover= null; 95 96 if (fInstantiatedTextHovers == null) 97 return null; 98 99 for (Iterator iterator= fInstantiatedTextHovers.iterator(); iterator.hasNext(); ) { 100 ITextHover hover= (ITextHover)iterator.next(); 101 102 String s= hover.getHoverInfo(textViewer, hoverRegion); 103 if (s != null && s.trim().length() > 0) { 104 fBestHover= hover; 105 return s; 106 } 107 } 108 109 return null; 110 } 111 112 116 public IInformationControlCreator getHoverControlCreator() { 117 if (fBestHover instanceof ITextHoverExtension) 118 return ((ITextHoverExtension)fBestHover).getHoverControlCreator(); 119 120 return null; 121 } 122 123 127 public IInformationControlCreator getInformationPresenterControlCreator() { 128 if (fBestHover instanceof IInformationProviderExtension2) 129 return ((IInformationProviderExtension2)fBestHover).getInformationPresenterControlCreator(); 130 131 return null; 132 } 133 } 134 | Popular Tags |