<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Out Of What Box? &#187; plug-in development</title>
	<atom:link href="http://www.outofwhatbox.com/blog/tag/plug-in-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.outofwhatbox.com/blog</link>
	<description>Ruminations on software and other impossible things</description>
	<lastBuildDate>Thu, 08 Sep 2011 15:57:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Eclipse: Rich Hovers Redux</title>
		<link>http://www.outofwhatbox.com/blog/2009/05/eclipse-rich-hovers-redux/</link>
		<comments>http://www.outofwhatbox.com/blog/2009/05/eclipse-rich-hovers-redux/#comments</comments>
		<pubDate>Tue, 12 May 2009 21:10:09 +0000</pubDate>
		<dc:creator>Dan Breslau</dc:creator>
				<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[plug-in development]]></category>
		<category><![CDATA[Rich Text Hovers]]></category>

		<guid isPermaLink="false">http://www.outofwhatbox.com/blog/?p=310</guid>
		<description><![CDATA[In my last post, I misstated how to use the new <code>ITextHoverExtension2#getHoverInfo2</code> interface. Herein, I attempt to set things right(er).]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.outofwhatbox.com/blog/2009/04/using-the-new-rich-text-hovers-in-eclipse-34/">Using the new Rich Text Hovers in Eclipse 3.4</a>, I, uh, suggested that perhaps, um, <em>maybe</em>, an implementation of <a href="http://help.eclipse.org/stable/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/jface/text/ITextHoverExtension2.html#getHoverInfo2(org.eclipse.jface.text.ITextViewer,%20org.eclipse.jface.text.IRegion)"><code>ITextHoverExtension2#getHoverInfo2</code></a> (from the package <code>org.eclipse.jface.text</code>) could return either a String or &#8220;An implementation of <code>IInformationControlExtension2</code>&#8220;.</p>
<p>That was a m- mi- mis- <a href="http://www.outofwhatbox.com/blog/2009/04/how_to_make_mistakes/"><em>mistake</em></a>. What I <a href="http://help.eclipse.org/stable/nftopic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/jface/text/IInformationControlExtension.html">meant to say</a> was:</p>
<blockquote><p>
 [It] is the responsibility of the implementer of <a href="http://help.eclipse.org/stable/nftopic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/jface/text/IInformationControl.html"><code>IInformationControl</code></a> and <a href="http://help.eclipse.org/stable/nftopic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/jface/text/IInformationControlExtension2.html"><code>IInformationControlExtension2</code></a> to specify the concrete nature of the information control&#8217;s input&#8230;</p></blockquote>
<p>In plain(er) English: If you write the hover control, you get to specify the hover information. The implementation of <code>getHoverInfo2</code> must be aware of the hover control&#8217;s implementation, and must conform to its specification.</p>
<p>So as a plug-in author, how can you create hovers with rich display formats?  I&#8217;ve found two options. The first of these doesn&#8217;t even require any of the extensions that I&#8217;ve already touched upon.</p>
<h2>Using the <code>DefaultHoverControl</code></h2>
<p>Straight out of the box (as it were), the JavaEditor example plug-in is configured to use the default version of the <code>org.eclipse.jface.text.DefaultHoverControl</code> class. In this configuration, the <code>DefaultHoverControl</code> treats its text input as raw text (without markup), as shown in the screen shot below.</p>
<p><em>(Note: The example Java editor plug-in simply copies the current text selection into the hover. In these screen shots, the selection is a Java comment that contains HTML markup.)</em></p>
<p><img src="http://www.outofwhatbox.com/images/RichTextRedux/StandardPoorHover.jpg" alt="Screen shot of Java editor plug-in example, showing HTML markup passed through as text." /></p>
<p>Some of the constructors for <code>DefaultHoverControl</code> enable the control to process a subset of HTML. To use one of these, we need to add the following method to <code>JavaSourceViewerConfiguration</code> in the sample editor; this overrides the base class implementation in <code>org.eclipse.jface.text.source.SourceViewerConfiguration</code> :</p>
<pre class="brush: java;">
/**
 * Returns the information control creator. The creator is a
 * factory creating information controls for the given source
 * viewer. This implementation always returns a creator for
 * &lt;code&gt;JavaInformationControl&lt;/code&gt; instances.
 *
 * @param sourceViewer the source viewer to be configured
 *        by this configuration
 * @return the information control creator, or &lt;code&gt;null&lt;/code&gt;
 *         if no information support should be installed
 * @since 2.0
 */
public IInformationControlCreator
getInformationControlCreator(ISourceViewer sourceViewer) {
   return new IInformationControlCreator() {
      public IInformationControl createInformationControl(Shell parent) {
          return new JavaHoverInformationControl(parent);
      }
   };
}
</pre>
<p>Unlike the base class implementation, this version calls the two-argument constructor <code>DefaultInformationControl(parent, false)</code>. Internally, this constructor will create an <code>HTMLTextPresenter</code> object, which supports some simple HTML markup.</p>
<p>The emphasis here is on <em>simple</em> HTML. Unfortunately, while <code>HTMLTextPresenter</code> happily <strong>reads</strong> the HTML markup, it doesn&#8217;t <strong>implement</strong> very much of it. As this screenshot illustrates, both <code>&lt;code></code> and <code>&lt;em></code> markup are ignored. (There&#8217;s no example hyperlink here, but I can confirm that they&#8217;re ignored as well.)</p>
<p><img src="http://www.outofwhatbox.com/images/RichTextRedux/UsingHTMLTextPresenter.jpg" alt="Another Java editor plug-in example, showing partially supported HTML markup." /></p>
<h2>Creating a new hover control</h2>
<p><img src="http://www.outofwhatbox.com/images/RichTextRedux/UFO.jpg" alt="Hovering" class="oowbleft"/>If you need to support richer content than <code>DefaultInformationControl</code> can handle, then it&#8217;s likely that you&#8217;ll need to create your own hover control. This may require some experimentation, as the definitive guide to writing hover controls hasn&#8217;t been written yet (or if it has been, its visibility flag is set to <code>false</code>.)</p>
<p>I&#8217;ve put together a demo of a hover control that uses the SWT Browser widget. The <code>JavaHoverInformationControl</code> class implementation requires only about 150 lines of code, plus a few changes in two other files in the JavaEditor demo. It&#8217;s still rough around the edges; you may not be replacing your desktop browser with it anytime soon, but it should help to illustrate how a plug-in author can take control over hover controls.</p>
<p>In this screenshot, you can see that the example comment is now displayed in all of its HTML glory in the hover:</p>
<p><img src="http://www.outofwhatbox.com/images/RichTextRedux/JavaHoverControl.jpg" alt="The JavaHoverInformationControl, showing HTML displayed in a small Browser widget." /></p>
<p>For additional demo value—and to show how this all ties in with <code>ITextHoverExtension2#getHoverInfo2</code>—the <code>JavaHoverInformationControl</code> also displays something useful when no text is currently selected:</p>
<p><img src="http://www.outofwhatbox.com/images/RichTextRedux/JavaHoverControlNoSelection.jpg" alt="It isn't all that useful, really." /><br />
<em>(Well, it&#8217;s useful to </em>me<em>, anyway!)</em></p>
<p>A few aspects of the demo are worth describing here. The tie-in to <code>ITextHoverExtension2#getHoverInfo2</code> is through an interface nested within the new <code>JavaHoverInformationControl</code> class:</p>
<pre class="brush: java;">
public interface IHTMLHoverInfo {
    /**
     * @return true if the String returned by getHTMLString()
     *  represents a URL;  false if the String contains marked-up text.
     */
    public boolean isURL();

    /**
     * @return The input string to be displayed in the Browser widget
     *  (either as marked-up text, or as a URL.)
     */
    public String getHTMLString();
}
</pre>
<p>This is reflected in the <code>JavaTextHover</code> class, which I&#8217;ve modified to implement<code>ITextHoverExtension2#getHoverInfo2</code>. This implementation returns an <code>Object</code> that conforms to the <code>IHTMLHoverInfo</code> interface:</p>
<pre class="brush: java;">
public class JavaTextHover
                implements ITextHover, ITextHoverExtension2 {
//... other methods are not shown here...
public Object getHoverInfo2(ITextViewer textViewer,
                                      IRegion hoverRegion) {

    // Start with the string returned by the older getHoverInfo()
    final String selection = getHoverInfo(textViewer, hoverRegion);

   // If text is selected in the editor window, it's returned as the
   // hover string. If no text is selected, then the returned hover is
   // a URL pointing to www.outofwhatbox.com/blog.
    return new JavaHoverInformationControl.IHTMLHoverInfo() {
        public boolean isURL() {return selection.length() == 0;}
        public String getHTMLString() {
            if (isURL()){
                return &quot;http://www.outofwhatbox.com/blog&quot;;
            }
            return selection;
        }
    };
}
</pre>
<p>Similarly, the <code>JavaHoverInformationControl#setObject</code> method will invoke this object&#8217;s <code>isURL</code> method, indicating whether to handle the input as marked-up text or as a URL:</p>
<pre class="brush: java;">
    public void setInput(Object input) {
        // Assume that the input is marked-up text, not a URL
        fIsURL = false;
        final String inputString;

        if (input instanceof IHTMLHoverInfo) {
            // Get the input string, then see whether it's a URL
            IHTMLHoverInfo inputInfo = (IHTMLHoverInfo) input;
            inputString = inputInfo.getHTMLString();
            fIsURL= inputInfo.isURL();

//... rest of the code not shown here...
</pre>
<p>I&#8217;ve packaged the <code>JavaHoverInformationControl</code> implementation with the modified source from the sample <code>org.eclipse.ui.examples.javaeditor</code> package. It&#8217;s available for download as a <a href="http://www.outofwhatbox.com/downloads/RichTextRedux/javaeditor.zip">ZIP file</a> file and as a <a href="http://www.outofwhatbox.com/downloads/RichTextRedux/javaeditor.tgz">gzip&#8217;d tar file</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.outofwhatbox.com/blog/2009/05/eclipse-rich-hovers-redux/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

