1 11 12 package org.eclipse.jdt.internal.ui.text.java.hover; 13 14 import java.util.Iterator ; 15 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IPath; 18 19 import org.eclipse.core.filebuffers.FileBuffers; 20 import org.eclipse.core.filebuffers.ITextFileBufferManager; 21 import org.eclipse.core.filebuffers.LocationKind; 22 23 import org.eclipse.jface.internal.text.html.HTMLPrinter; 24 import org.eclipse.jface.preference.IPreferenceStore; 25 26 import org.eclipse.jface.text.IRegion; 27 import org.eclipse.jface.text.ITextViewer; 28 import org.eclipse.jface.text.Position; 29 import org.eclipse.jface.text.source.Annotation; 30 import org.eclipse.jface.text.source.IAnnotationModel; 31 import org.eclipse.jface.text.source.ISourceViewer; 32 33 import org.eclipse.ui.editors.text.EditorsUI; 34 35 import org.eclipse.ui.IEditorInput; 36 import org.eclipse.ui.IStorageEditorInput; 37 import org.eclipse.ui.texteditor.AnnotationPreference; 38 import org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess; 39 40 import org.eclipse.jdt.internal.ui.JavaPlugin; 41 import org.eclipse.jdt.internal.ui.javaeditor.JavaAnnotationIterator; 42 43 44 49 public abstract class AbstractAnnotationHover extends AbstractJavaEditorTextHover { 50 51 private IPreferenceStore fStore= JavaPlugin.getDefault().getCombinedPreferenceStore(); 52 private DefaultMarkerAnnotationAccess fAnnotationAccess= new DefaultMarkerAnnotationAccess(); 53 private boolean fAllAnnotations; 54 55 56 public AbstractAnnotationHover(boolean allAnnotations) { 57 fAllAnnotations= allAnnotations; 58 } 59 60 63 private String formatMessage(String message) { 64 StringBuffer buffer= new StringBuffer (); 65 HTMLPrinter.insertPageProlog(buffer, 0, getStyleSheet()); 66 buffer.append(HTMLPrinter.convertToHTMLContent(message)); 67 HTMLPrinter.addPageEpilog(buffer); 68 return buffer.toString(); 69 } 70 71 74 public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) { 75 IPath path; 76 IAnnotationModel model; 77 if (textViewer instanceof ISourceViewer) { 78 path= null; 79 model= ((ISourceViewer)textViewer).getAnnotationModel(); 80 } else { 81 path= getEditorInputPath(); 83 model= getAnnotationModel(path); 84 } 85 if (model == null) 86 return null; 87 88 try { 89 Iterator e= new JavaAnnotationIterator(model, true, fAllAnnotations); 90 int layer= -1; 91 String message= null; 92 while (e.hasNext()) { 93 Annotation a= (Annotation) e.next(); 94 95 AnnotationPreference preference= getAnnotationPreference(a); 96 if (preference == null || !(preference.getTextPreferenceKey() != null && fStore.getBoolean(preference.getTextPreferenceKey()) || (preference.getHighlightPreferenceKey() != null && fStore.getBoolean(preference.getHighlightPreferenceKey())))) 97 continue; 98 99 Position p= model.getPosition(a); 100 101 int l= fAnnotationAccess.getLayer(a); 102 103 if (l > layer && p != null && p.overlapsWith(hoverRegion.getOffset(), hoverRegion.getLength())) { 104 String msg= a.getText(); 105 if (msg != null && msg.trim().length() > 0) { 106 message= msg; 107 layer= l; 108 } 109 } 110 } 111 if (layer > -1) 112 return formatMessage(message); 113 114 } finally { 115 try { 116 if (path != null) { 117 ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager(); 118 manager.disconnect(path, LocationKind.NORMALIZE, null); 119 } 120 } catch (CoreException ex) { 121 JavaPlugin.log(ex.getStatus()); 122 } 123 } 124 125 return null; 126 } 127 128 private IPath getEditorInputPath() { 129 if (getEditor() == null) 130 return null; 131 132 IEditorInput input= getEditor().getEditorInput(); 133 if (input instanceof IStorageEditorInput) { 134 try { 135 return ((IStorageEditorInput)input).getStorage().getFullPath(); 136 } catch (CoreException ex) { 137 JavaPlugin.log(ex.getStatus()); 138 } 139 } 140 return null; 141 } 142 143 private IAnnotationModel getAnnotationModel(IPath path) { 144 if (path == null) 145 return null; 146 147 ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager(); 148 try { 149 manager.connect(path, LocationKind.NORMALIZE, null); 150 } catch (CoreException ex) { 151 JavaPlugin.log(ex.getStatus()); 152 return null; 153 } 154 155 IAnnotationModel model= null; 156 try { 157 model= manager.getTextFileBuffer(path, LocationKind.NORMALIZE).getAnnotationModel(); 158 return model; 159 } finally { 160 if (model == null) { 161 try { 162 manager.disconnect(path, LocationKind.NORMALIZE, null); 163 } catch (CoreException ex) { 164 JavaPlugin.log(ex.getStatus()); 165 } 166 } 167 } 168 } 169 170 176 private AnnotationPreference getAnnotationPreference(Annotation annotation) { 177 178 if (annotation.isMarkedDeleted()) 179 return null; 180 return EditorsUI.getAnnotationPreferenceLookup().getAnnotationPreference(annotation); 181 } 182 } 183 | Popular Tags |