1 11 package org.eclipse.jdt.internal.ui.text.java.hover; 12 13 14 import org.eclipse.swt.SWT; 15 import org.eclipse.swt.widgets.Shell; 16 17 import org.eclipse.jface.internal.text.html.BrowserInformationControl; 18 import org.eclipse.jface.internal.text.html.HTMLTextPresenter; 19 20 import org.eclipse.jface.text.AbstractReusableInformationControlCreator; 21 import org.eclipse.jface.text.DefaultInformationControl; 22 import org.eclipse.jface.text.IInformationControl; 23 import org.eclipse.jface.text.IInformationControlCreator; 24 import org.eclipse.jface.text.IRegion; 25 import org.eclipse.jface.text.ITextViewer; 26 import org.eclipse.jface.text.information.IInformationProvider; 27 import org.eclipse.jface.text.information.IInformationProviderExtension2; 28 29 import org.eclipse.ui.IEditorPart; 30 import org.eclipse.ui.IPartListener; 31 import org.eclipse.ui.IPerspectiveDescriptor; 32 import org.eclipse.ui.IWorkbenchPage; 33 import org.eclipse.ui.IWorkbenchPart; 34 import org.eclipse.ui.IWorkbenchWindow; 35 36 import org.eclipse.jdt.ui.text.java.hover.IJavaEditorTextHover; 37 38 import org.eclipse.jdt.internal.ui.text.JavaWordFinder; 39 40 41 public class JavaInformationProvider implements IInformationProvider, IInformationProviderExtension2 { 42 43 44 49 private static final class ControlCreator extends AbstractReusableInformationControlCreator { 50 53 public IInformationControl doCreateInformationControl(Shell parent) { 54 int shellStyle= SWT.RESIZE | SWT.TOOL; 55 int style= SWT.V_SCROLL | SWT.H_SCROLL; 56 if (BrowserInformationControl.isAvailable(parent)) 57 return new BrowserInformationControl(parent, shellStyle, style); 58 else 59 return new DefaultInformationControl(parent, shellStyle, style, new HTMLTextPresenter(false)); 60 } 61 } 62 63 64 class EditorWatcher implements IPartListener { 65 66 69 public void partOpened(IWorkbenchPart part) { 70 } 71 72 75 public void partDeactivated(IWorkbenchPart part) { 76 } 77 78 81 public void partClosed(IWorkbenchPart part) { 82 if (part == fEditor) { 83 fEditor.getSite().getWorkbenchWindow().getPartService().removePartListener(fPartListener); 84 fPartListener= null; 85 } 86 } 87 88 91 public void partActivated(IWorkbenchPart part) { 92 update(); 93 } 94 95 public void partBroughtToTop(IWorkbenchPart part) { 96 update(); 97 } 98 } 99 100 protected IEditorPart fEditor; 101 protected IPartListener fPartListener; 102 103 protected String fCurrentPerspective; 104 protected IJavaEditorTextHover fImplementation; 105 106 111 private IInformationControlCreator fPresenterControlCreator; 112 113 114 115 public JavaInformationProvider(IEditorPart editor) { 116 117 fEditor= editor; 118 119 if (fEditor != null) { 120 121 fPartListener= new EditorWatcher(); 122 IWorkbenchWindow window= fEditor.getSite().getWorkbenchWindow(); 123 window.getPartService().addPartListener(fPartListener); 124 125 update(); 126 } 127 } 128 129 protected void update() { 130 131 IWorkbenchWindow window= fEditor.getSite().getWorkbenchWindow(); 132 IWorkbenchPage page= window.getActivePage(); 133 if (page != null) { 134 135 IPerspectiveDescriptor perspective= page.getPerspective(); 136 if (perspective != null) { 137 String perspectiveId= perspective.getId(); 138 139 if (fCurrentPerspective == null || fCurrentPerspective != perspectiveId) { 140 fCurrentPerspective= perspectiveId; 141 142 fImplementation= new JavaTypeHover(); 143 fImplementation.setEditor(fEditor); 144 } 145 } 146 } 147 } 148 149 152 public IRegion getSubject(ITextViewer textViewer, int offset) { 153 154 if (textViewer != null) 155 return JavaWordFinder.findWord(textViewer.getDocument(), offset); 156 157 return null; 158 } 159 160 163 public String getInformation(ITextViewer textViewer, IRegion subject) { 164 if (fImplementation != null) { 165 String s= fImplementation.getHoverInfo(textViewer, subject); 166 if (s != null && s.trim().length() > 0) { 167 return s; 168 } 169 } 170 171 return null; 172 } 173 174 178 public IInformationControlCreator getInformationPresenterControlCreator() { 179 if (fPresenterControlCreator == null) 180 fPresenterControlCreator= new ControlCreator(); 181 return fPresenterControlCreator; 182 } 183 } 184 | Popular Tags |