1. What is the hover of JDT.
See figure below:
2. The different fonts in the hover, and how the colors are displayed.
In fact, in the corresponding hover, the string type is returned, the content is HTML, and then displayed through the Org.eclipse.swt.browser.Browser.
3. Working principle:
3.1 Add Mousetracker to the 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-any installed text hover has been installed.
* *
private void ensurehovercontrolmanagerinstalled () {
if (ftexthovers!= null &&!ftexthovers.isempty () && fhovercontrolcreator!= null && Ftexthovermanager = null) {
ftexthovermanager= new Textviewerh Overmanager (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 () & amp;& 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!= are && fmousetracker!= null) {
if (IS)
Fmousetracker.start (Getsubjectcontrol ());
else
fmousetracker.stop ();
}
}
--> Org.eclipse.jface.text.abstracthoverinformationcontrolmanager$mousetracker
public 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 your hover mouse on target sourceviewer, it'll show hover
Org.eclipse.jface.text.abstracthoverinformationcontrolmanager$mousetracker
public void MouseHover (MouseEvent event) {if (fiscomputing | | fisinrestartmode | | (Fsubjectcontrol!= null &&!fsubjectcontrol.isdisposed () && Fsubjectcontrol.getshell ()!= Fsubjectcontrol.getshell (). Getdisplay (). Getactiveshell ()) {if (DEBUG) System.out.pri Ntln ("Abstracthoverinformationcontrolmanager...mousehover: @" + event.x + "/" + Event.y + ": hover Cancelled:fiscomputi Ng= "+ 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.addmousemovelisten
ER (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 informatio
Ncontrolreplacer) {(informationcontrolreplacer) this). Showinformationcontrol (Subjectarea, information);
Return
} Iinformationcontrol informationcontrol= Getinformationcontrol (); if (Informationcontrol!= null) {point sizeconstraints= computesizeconstraints (Fsubjectcontrol, Fsubjectarea, Informa
Tioncontrol); if (Informationcontrol instanceof IInformationControlExtension3) {IInformationControlExtension3 icontrol3= (Iinforma
TionControlExtension3) 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); else informationcontrOl.setinformation (Information.tostring ()); if (Informationcontrol instanceof iinformationcontrolextension) {iinformationcontrolextension extension= (IInformati
oncontrolextension) 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 is disposed if setvisible (..) runs the display loop
if ( 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= fAdd Itionalinfoaffordance?
Javaplugin.getadditionalinfoaffordancestring (): editorsui.gettooltipaffordancestring (); if (browserinformationcontrol.isavailable (parent)) {String Preferenceconstants.appearance_javadoc_f
ONT;
Browserinformationcontrol icontrol= New Browserinformationcontrol (parent, font, tooltipaffordancestring) { * * @see Org.eclipse.jface.text.iinformationcontrolextension5#getinformationpresentercontrolcre Ator () */@Override public iinformationcontrolcreator GetInfo
Rmationpresentercontrolcreator () {return finformationpresentercontrolcreator;
}
};
Addlinklistener (icontrol);
return icontrol;
} else { return new Defaultinformationcontrol (parent, tooltipaffordancestring); }
}
4. Below is the System.out.println HTML content and the effect shown under Firefox.
Display effect: