views:

224

answers:

3

I want to show my own text hover in eclipse for some specific words? Please provide me some examples

+1  A: 

You can start by looking at Koder examples.

E.g. this CEditorTextHoverDispatcher or this UCTextHover

package com.ut2003.uceditor;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextHover;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.swt.graphics.Point;

public class UCTextHover implements ITextHover
{

    /* (non-Javadoc)
     * Method declared on ITextHover
     */
    public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion)
    {
     if (hoverRegion != null)
     {
      try
      {
       if (hoverRegion.getLength() > -1)
        return textViewer.getDocument().get(hoverRegion.getOffset(), hoverRegion.getLength());
      }
      catch (BadLocationException x)
      {
      }
     }
     return "Empty Selection";
    }

    /* (non-Javadoc)
     * Method declared on ITextHover
     */
    public IRegion getHoverRegion(ITextViewer textViewer, int offset)
    {
     Point selection = textViewer.getSelectedRange();
     if (selection.x <= offset && offset < selection.x + selection.y)
      return new Region(selection.x, selection.y);
     return new Region(offset, 0);
    }
}


You would set a TextHover in a SourceViewerConfiguration like this GasSourceViewerConfiguration or this CalcSourceViewerConfiguration

package com.example.calc.ui.editors;

import org.eclipse.jface.text.ITextHover;
import org.eclipse.jface.text.reconciler.IReconciler;
import org.eclipse.jface.text.reconciler.MonoReconciler;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;

/**
 * @author cdaly
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class CalcSourceViewerConfiguration extends SourceViewerConfiguration {

    private CalcEditor _editor;

    public CalcSourceViewerConfiguration(CalcEditor editor){
     _editor = editor;
    }



    /* (non-Javadoc)
     * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getReconciler(org.eclipse.jface.text.source.ISourceViewer)
     */
    public IReconciler getReconciler(ISourceViewer sourceViewer) {
     return new MonoReconciler(_editor.getReconcilingStrategy(), false);
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getTextHover(org.eclipse.jface.text.source.ISourceViewer, java.lang.String)
     */
    public ITextHover getTextHover(
     ISourceViewer sourceViewer,
     String contentType) {
     ITextHover hover;
     if (_editor != null && _editor instanceof CalcEditor) {
      hover = new CalcTextHover((CalcEditor)_editor);
     } else {
      hover = null;
     }
     return hover;
    }

}


Beyond that, I have not much more information: the examples I have found are more programmatic than declarative (i.e. "plugin.xml"), so you may want to explore some more code.

Another good example: Eclipse: Rich Hovers Redux (it is for eclipse3.4 though, but the full example can give you another hint at how a custom ITextHover is added to the current editor)

VonC
Thanks a lot for the answers. Which wizard should i take in the eclipse plugin development environment to add this hover in JavaEditor? Which extension point should I add in plugin.xml to get this in JavaEditor
A: 

The best thing is to use the java editor plugin along with eclipse first. Take eclipse help ->welcome->Samples->Java Editor plugin

A: 

Blockquote

enter code here