|
Line 0
Link Here
|
|
|
1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2000, 2003 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Common Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/cpl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
|
| 11 |
|
| 12 |
*******************************************************************************/ |
| 13 |
package org.eclipse.linuxtools.rpm.ui.editor; |
| 14 |
|
| 15 |
import java.util.ArrayList; |
| 16 |
import java.util.Iterator; |
| 17 |
|
| 18 |
import org.eclipse.core.resources.IMarker; |
| 19 |
import org.eclipse.jface.text.BadLocationException; |
| 20 |
import org.eclipse.jface.text.IDocument; |
| 21 |
import org.eclipse.jface.text.Position; |
| 22 |
import org.eclipse.jface.text.source.IAnnotationHover; |
| 23 |
import org.eclipse.jface.text.source.IAnnotationModel; |
| 24 |
import org.eclipse.jface.text.source.ISourceViewer; |
| 25 |
import org.eclipse.ui.texteditor.MarkerAnnotation; |
| 26 |
|
| 27 |
|
| 28 |
/** |
| 29 |
* Class derived from org.eclipse.pde.internal.ui.editor.text.AnnotationHover |
| 30 |
* |
| 31 |
*/ |
| 32 |
public class AnnotationHover implements IAnnotationHover { |
| 33 |
|
| 34 |
|
| 35 |
public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) { |
| 36 |
String[] messages = getMessagesForLine(sourceViewer, lineNumber); |
| 37 |
|
| 38 |
|
| 39 |
if (messages.length == 0) |
| 40 |
return null; |
| 41 |
|
| 42 |
|
| 43 |
StringBuffer buffer = new StringBuffer(); |
| 44 |
for (int i = 0; i < messages.length; i++) { |
| 45 |
buffer.append(messages[i]); |
| 46 |
if (i < messages.length - 1) |
| 47 |
buffer.append(System.getProperty("line.separator")); //$NON-NLS-1$ |
| 48 |
} |
| 49 |
return buffer.toString(); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
private String[] getMessagesForLine(ISourceViewer viewer, int line) { |
| 54 |
IDocument document = viewer.getDocument(); |
| 55 |
IAnnotationModel model = viewer.getAnnotationModel(); |
| 56 |
|
| 57 |
|
| 58 |
if (model == null) |
| 59 |
return new String[0]; |
| 60 |
|
| 61 |
|
| 62 |
ArrayList messages = new ArrayList(); |
| 63 |
|
| 64 |
|
| 65 |
Iterator iter = model.getAnnotationIterator(); |
| 66 |
while (iter.hasNext()) { |
| 67 |
Object object = iter.next(); |
| 68 |
if (object instanceof MarkerAnnotation) { |
| 69 |
MarkerAnnotation annotation = (MarkerAnnotation) object; |
| 70 |
if (compareRulerLine(model.getPosition(annotation), |
| 71 |
document, |
| 72 |
line)) { |
| 73 |
IMarker marker = annotation.getMarker(); |
| 74 |
String message = |
| 75 |
marker.getAttribute(IMarker.MESSAGE, (String) null); |
| 76 |
if (message != null && message.trim().length() > 0) |
| 77 |
messages.add(message); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
return (String[]) messages.toArray(new String[messages.size()]); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
private boolean compareRulerLine( |
| 86 |
Position position, |
| 87 |
IDocument document, |
| 88 |
int line) { |
| 89 |
|
| 90 |
|
| 91 |
try { |
| 92 |
if (position.getOffset() > -1 && position.getLength() > -1) { |
| 93 |
return document.getLineOfOffset(position.getOffset()) == line; |
| 94 |
} |
| 95 |
} catch (BadLocationException e) { |
| 96 |
} |
| 97 |
return false; |
| 98 |
} |
| 99 |
} |