1 11 package org.eclipse.jdt.internal.ui.text; 12 13 14 import org.eclipse.jface.viewers.IStructuredSelection; 15 16 import org.eclipse.jface.text.IRegion; 17 import org.eclipse.jface.text.ITextViewer; 18 import org.eclipse.jface.text.Region; 19 import org.eclipse.jface.text.information.IInformationProvider; 20 import org.eclipse.jface.text.information.IInformationProviderExtension; 21 22 import org.eclipse.ui.IEditorPart; 23 24 import org.eclipse.jdt.core.IJavaElement; 25 import org.eclipse.jdt.core.JavaModelException; 26 27 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 28 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility; 29 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 30 31 34 public class JavaElementProvider implements IInformationProvider, IInformationProviderExtension { 35 36 private JavaEditor fEditor; 37 private boolean fUseCodeResolve; 38 39 public JavaElementProvider(IEditorPart editor) { 40 fUseCodeResolve= false; 41 if (editor instanceof JavaEditor) 42 fEditor= (JavaEditor)editor; 43 } 44 45 public JavaElementProvider(IEditorPart editor, boolean useCodeResolve) { 46 this(editor); 47 fUseCodeResolve= useCodeResolve; 48 } 49 50 53 public IRegion getSubject(ITextViewer textViewer, int offset) { 54 if (textViewer != null && fEditor != null) { 55 IRegion region= JavaWordFinder.findWord(textViewer.getDocument(), offset); 56 if (region != null) 57 return region; 58 else 59 return new Region(offset, 0); 60 } 61 return null; 62 } 63 64 67 public String getInformation(ITextViewer textViewer, IRegion subject) { 68 return getInformation2(textViewer, subject).toString(); 69 } 70 71 74 public Object getInformation2(ITextViewer textViewer, IRegion subject) { 75 if (fEditor == null) 76 return null; 77 78 try { 79 if (fUseCodeResolve) { 80 IStructuredSelection sel= SelectionConverter.getStructuredSelection(fEditor); 81 if (!sel.isEmpty()) 82 return sel.getFirstElement(); 83 } 84 IJavaElement element= SelectionConverter.getElementAtOffset(fEditor); 85 if (element != null) 86 return element; 87 88 return EditorUtility.getEditorInputJavaElement(fEditor, false); 89 } catch (JavaModelException e) { 90 return null; 91 } 92 } 93 } 94 | Popular Tags |