eclipse的JDT的hover是如何显示出来的

1.什么是JDT的hover。

见下图:

2. hover中的不同的字体,和颜色是如何显示出来的。

其实在对应的hover中,返回的是String类型,内容是html,,然后通过org.eclipse.swt.browser.Browser显示出来的。

3.工作原理:

3.1 add MouseTracker to current SourceViewer, when you open java file.org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor$AdaptedSourceViewer –> org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor$AdaptedSourceViewer

/*** After this method has been executed the caller knows that any installed text hover has been installed.*/private void ensureHoverControlManagerInstalled() {if (fTextHovers != null && !fTextHovers.isEmpty() && fHoverControlCreator != null && fTextHoverManager == null) {fTextHoverManager= new TextViewerHoverManager(this, fHoverControlCreator);fTextHoverManager.install(this.getTextWidget());fTextHoverManager.setSizeConstraints(TEXT_HOVER_WIDTH_CHARS, TEXT_HOVER_HEIGHT_CHARS, false, true);fTextHoverManager.setInformationControlReplacer(new StickyHoverManager(this));}} –> org.eclipse.jface.text.TextViewerHoverManager public void install(Control subjectControl) {if (fSubjectControl != null && !fSubjectControl.isDisposed() && fSubjectControlDisposeListener != null)fSubjectControl.removeDisposeListener(fSubjectControlDisposeListener);fSubjectControl= subjectControl;if (fSubjectControl != null)fSubjectControl.addDisposeListener(getSubjectControlDisposeListener());if (fInformationControlCloser != null)fInformationControlCloser.setSubjectControl(subjectControl);setEnabled(true);fDisposed= false;} –> org.eclipse.jface.text.TextViewerHoverManager public void setEnabled(boolean enabled) {boolean was= isEnabled();super.setEnabled(enabled);boolean is= isEnabled();if (was != is && fMouseTracker != null) {if (is)fMouseTracker.start(getSubjectControl());elsefMouseTracker.stop();}} –> org.eclipse.jface.text.AbstractHoverInformationControlManager$MouseTrackerpublic void start(Control subjectControl) {fSubjectControl= subjectControl;if (fSubjectControl != null && !fSubjectControl.isDisposed())fSubjectControl.addMouseTrackListener(this);fIsInRestartMode= false;fIsComputing= false;fMouseLostWhileComputing= false;fShellDeactivatedWhileComputing= false;}3.2 when you hover mouse on target SourceViewer, it will show hover org.eclipse.jface.text.AbstractHoverInformationControlManager$MouseTrackerpublic void mouseHover(MouseEvent event) {if (fIsComputing || fIsInRestartMode ||(fSubjectControl != null && !fSubjectControl.isDisposed() && fSubjectControl.getShell() != fSubjectControl.getShell().getDisplay().getActiveShell())) {if (DEBUG)System.out.println("AbstractHoverInformationControlManager…mouseHover: @ " + event.x + "/" + event.y + " : hover cancelled: fIsComputing= " + fIsComputing + ", fIsInRestartMode= " + fIsInRestartMode); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$return;}fIsInRestartMode= true;fIsComputing= true;fMouseLostWhileComputing= false;fShellDeactivatedWhileComputing= false;fHoverEventStateMask= event.stateMask;fHoverEvent= event;fHoverArea= new Rectangle(event.x – EPSILON, event.y – EPSILON, 2 * EPSILON, 2 * EPSILON );if (fHoverArea.x < 0)fHoverArea.x= 0;if (fHoverArea.y < 0)fHoverArea.y= 0;setSubjectArea(fHoverArea);if (fSubjectControl != null && !fSubjectControl.isDisposed()) {fSubjectControl.addMouseMoveListener(this);fSubjectControl.getShell().addShellListener(this);}doShowInformation();} –> org.eclipse.jface.text.AbstractInformationControlManager protected void doShowInformation() {fSubjectArea= null;fInformation= null;computeInformation();} –> org.eclipse.jface.text.TextViewerHoverManager protected void computeInformation() {//create a thread(Text Viewer Hover Presenter) to show hover} … –> org.eclipse.jface.text.AbstractInformationControlManager.internalShowInformationControl(Rectangle, Object) to create control to show the text.private void internalShowInformationControl(Rectangle subjectArea, Object information) {if (this instanceof InformationControlReplacer) {((InformationControlReplacer) this).showInformationControl(subjectArea, information);return;}IInformationControl informationControl= getInformationControl();if (informationControl != null) {Point sizeConstraints= computeSizeConstraints(fSubjectControl, fSubjectArea, informationControl);if (informationControl instanceof IInformationControlExtension3) {IInformationControlExtension3 iControl3= (IInformationControlExtension3) informationControl;Rectangle trim= iControl3.computeTrim();sizeConstraints.x += trim.width;sizeConstraints.y += trim.height;}informationControl.setSizeConstraints(sizeConstraints.x, sizeConstraints.y);if (informationControl instanceof IInformationControlExtension2)((IInformationControlExtension2)informationControl).setInput(information);elseinformationControl.setInformation(information.toString());if (informationControl instanceof IInformationControlExtension) {IInformationControlExtension extension= (IInformationControlExtension)informationControl;if (!extension.hasContents())return;}Point size= null;Point location= null;Rectangle bounds= restoreInformationControlBounds();if (bounds != null) {if (bounds.x > -1 && bounds.y > -1)location= Geometry.getLocation(bounds);if (bounds.width > -1 && bounds.height > -1)size= Geometry.getSize(bounds);}if (size == null)size= informationControl.computeSizeHint();if (fEnforceAsMinimalSize)size= Geometry.max(size, sizeConstraints);if (fEnforceAsMaximalSize)size= Geometry.min(size, sizeConstraints);if (location == null)location= computeInformationControlLocation(subjectArea, size);Rectangle controlBounds= Geometry.createRectangle(location, size);cropToClosestMonitor(controlBounds);location= Geometry.getLocation(controlBounds);size= Geometry.getSize(controlBounds);informationControl.setLocation(location);informationControl.setSize(size.x, size.y);showInformationControl(subjectArea);}} –> org.eclipse.jface.text.AbstractInformationControlManager protected void showInformationControl(Rectangle subjectArea) {fInformationControl.setVisible(true);if (fInformationControl == null)return; // could already be disposed if setVisible(..) runs the display loopif (fTakesFocusWhenVisible)fInformationControl.setFocus();if (fInformationControlCloser != null)fInformationControlCloser.start(subjectArea);}org.eclipse.jdt.internal.ui.text.java.hover.JavadocHover$HoverControlCreator public IInformationControl doCreateInformationControl(Shell parent) {String tooltipAffordanceString= fAdditionalInfoAffordance ? JavaPlugin.getAdditionalInfoAffordanceString() : EditorsUI.getTooltipAffordanceString();if (BrowserInformationControl.isAvailable(parent)) {String font= PreferenceConstants.APPEARANCE_JAVADOC_FONT;BrowserInformationControl iControl= new BrowserInformationControl(parent, font, tooltipAffordanceString) {/** @see org.eclipse.jface.text.IInformationControlExtension5#getInformationPresenterControlCreator()*/@Overridepublic IInformationControlCreator getInformationPresenterControlCreator() {return fInformationPresenterControlCreator;}};addLinkListener(iControl);return iControl;} else {return new DefaultInformationControl(parent, tooltipAffordanceString);}}抱最大的希望,为最大的努力,做最坏的打算

eclipse的JDT的hover是如何显示出来的

相关文章:

你感兴趣的文章:

标签云: