/*
 * Created on 2004-12-28
 *
 * Part of the Symbian development plug-in for Eclipse
 * Copyright (C) Mikołaj Zalewski, 2004
 */
package pl.zalewski.mikolaj.symbdev.ui.mmpedit;

import java.util.Iterator;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.IAnnotationHover;
import org.eclipse.jface.text.source.ISourceViewer;

/**
 * Provides a mouse hover information for the annotations icons
 * on the left side of the editor
 * 
 * @author mikolaj
 */
public class MMPAnnotationHover implements IAnnotationHover {

    public MMPAnnotationHover() {
        super();
    }

    public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
        Iterator it=sourceViewer.getAnnotationModel().getAnnotationIterator();
        int count=0;
        String ret=null;
        while (it.hasNext())
        {
            Annotation a=(Annotation)it.next();
            if (a.getText()==null)
                return null;
            
            Position p=sourceViewer.getAnnotationModel().getPosition(a);
            if (p==null)
                return null;
            
            try {
                int line=sourceViewer.getDocument().getLineOfOffset(p.getOffset());
                if (line==lineNumber)
                {
                    if (count==0)
                    {
                        ret=a.getText();
                    } else if (count==1)
                    {
                        ret="Multiple markers for this line:\n  - "+ret+"\n  - "+a.getText();
                    } else ret=ret+"\n  - "+a.getText();
                    count++;
                }
            } catch (BadLocationException e) {
                // ignore
            }
        }
        return ret;
    }

}
