Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorazerr2018-04-24 15:43:16 +0000
committerazerr2018-04-25 17:59:35 +0000
commit82a6d90a928017264f0f62c0b8c45626a2cec119 (patch)
tree394516908f80a4a9d4ecac4f8e2cf77e74ec5a47 /org.eclipse.jface.text/src/org/eclipse/jface/text
parent6e3500bbb912961be9724d506486bb4c65180477 (diff)
downloadeclipse.platform.text-82a6d90a928017264f0f62c0b8c45626a2cec119.tar.gz
eclipse.platform.text-82a6d90a928017264f0f62c0b8c45626a2cec119.tar.xz
eclipse.platform.text-82a6d90a928017264f0f62c0b8c45626a2cec119.zip
Bug 533811 - [code mining] Inlined content annotation cannot be clickedI20180426-2000I20180426-0500I20180425-2000
when it is drawn at the end of the line Change-Id: If744f7692021fd04ee9169bac430ec6559f2eb56 Signed-off-by: azerr <angelo.zerr@gmail.com>
Diffstat (limited to 'org.eclipse.jface.text/src/org/eclipse/jface/text')
-rw-r--r--org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/AbstractInlinedAnnotation.java51
-rw-r--r--org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationDrawingStrategy.java30
-rw-r--r--org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationSupport.java135
-rw-r--r--org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/LineContentAnnotation.java20
-rw-r--r--org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/LineHeaderAnnotation.java7
5 files changed, 96 insertions, 147 deletions
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/AbstractInlinedAnnotation.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/AbstractInlinedAnnotation.java
index bd0f1833249..731176378eb 100644
--- a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/AbstractInlinedAnnotation.java
+++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/AbstractInlinedAnnotation.java
@@ -52,11 +52,15 @@ public abstract class AbstractInlinedAnnotation extends Annotation {
*/
private InlinedAnnotationSupport support;
+ int fX;
+
+ int fY;
+
/**
* Inlined annotation constructor.
*
* @param position the position where the annotation must be drawn.
- * @param viewer the {@link ISourceViewer} where the annotation must be drawn.
+ * @param viewer the {@link ISourceViewer} where the annotation must be drawn.
*/
protected AbstractInlinedAnnotation(Position position, ISourceViewer viewer) {
super(TYPE, false, ""); //$NON-NLS-1$
@@ -112,13 +116,13 @@ public abstract class AbstractInlinedAnnotation extends Annotation {
* Draw the inlined annotation. By default it draw the text of the annotation with gray color.
* User can override this method to draw anything.
*
- * @param gc the graphics context
+ * @param gc the graphics context
* @param textWidget the text widget to draw on
- * @param offset the offset of the line
- * @param length the length of the line
- * @param color the color of the line
- * @param x the x position of the annotation
- * @param y the y position of the annotation
+ * @param offset the offset of the line
+ * @param length the length of the line
+ * @param color the color of the line
+ * @param x the x position of the annotation
+ * @param y the y position of the annotation
*/
public void draw(GC gc, StyledText textWidget, int offset, int length, Color color, int x, int y) {
gc.setForeground(color);
@@ -191,4 +195,37 @@ public abstract class AbstractInlinedAnnotation extends Annotation {
Font getFont(int style) {
return support.getFont(style);
}
+
+ /**
+ * Set the location where the annotation is drawn.
+ *
+ * @param x the x coordinate where draw of annotation starts.
+ * @param y the y coordinate where draw of annotation starts.
+ */
+ void setLocation(int x, int y) {
+ this.fX= x;
+ this.fY= y;
+ }
+
+ /**
+ * Returns <code>true</code> if the point specified by the arguments is inside the annotation
+ * specified by the receiver, and <code>false</code> otherwise.
+ *
+ * @param x the x coordinate of the point to test for containment
+ * @param y the y coordinate of the point to test for containment
+ * @return <code>true</code> if the annotation contains the point and <code>false</code>
+ * otherwise
+ */
+ boolean contains(int x, int y) {
+ StyledText styledText= getTextWidget();
+ GC gc= null;
+ try {
+ gc= new GC(styledText);
+ return x >= fX && y >= fY && y <= fY + styledText.getLineHeight(position.getOffset()) && x <= fX + gc.stringExtent(getText()).x + 2 * gc.getFontMetrics().getAverageCharacterWidth();
+ } finally {
+ if (gc != null) {
+ gc.dispose();
+ }
+ }
+ }
}
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationDrawingStrategy.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationDrawingStrategy.java
index 63db861bc3b..bda109202bd 100644
--- a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationDrawingStrategy.java
+++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationDrawingStrategy.java
@@ -46,11 +46,11 @@ class InlinedAnnotationDrawingStrategy implements IDrawingStrategy {
* Draw the inlined annotation.
*
* @param annotation the annotation to be drawn
- * @param gc the graphics context, <code>null</code> when in clearing mode
+ * @param gc the graphics context, <code>null</code> when in clearing mode
* @param textWidget the text widget to draw on
- * @param offset the offset of the line
- * @param length the length of the line
- * @param color the color of the line
+ * @param offset the offset of the line
+ * @param length the length of the line
+ * @param color the color of the line
*/
public static void draw(AbstractInlinedAnnotation annotation, GC gc, StyledText textWidget, int offset, int length,
Color color) {
@@ -65,11 +65,11 @@ class InlinedAnnotationDrawingStrategy implements IDrawingStrategy {
* Draw the line header annotation in the line spacing of the previous line.
*
* @param annotation the annotation to be drawn
- * @param gc the graphics context, <code>null</code> when in clearing mode
+ * @param gc the graphics context, <code>null</code> when in clearing mode
* @param textWidget the text widget to draw on
- * @param offset the offset of the line
- * @param length the length of the line
- * @param color the color of the line
+ * @param offset the offset of the line
+ * @param length the length of the line
+ * @param color the color of the line
*/
private static void draw(LineHeaderAnnotation annotation, GC gc, StyledText textWidget, int offset, int length,
Color color) {
@@ -99,6 +99,7 @@ class InlinedAnnotationDrawingStrategy implements IDrawingStrategy {
textWidget.drawBackground(gc, 0, y, client.width, annotation.getHeight());
// Draw the line header annotation
+ annotation.setLocation(x, y);
annotation.draw(gc, textWidget, offset, length, color, x, y);
int height= annotation.getHeight();
if (height != 0) {
@@ -162,7 +163,7 @@ class InlinedAnnotationDrawingStrategy implements IDrawingStrategy {
* Returns the style to apply with GlyphMetrics ascent only if needed.
*
* @param annotation the line header annotation
- * @param style the current style and null otherwise.
+ * @param style the current style and null otherwise.
* @return the style to apply with GlyphMetrics ascent only if needed.
*/
static StyleRange updateStyle(LineHeaderAnnotation annotation, StyleRange style) {
@@ -209,11 +210,11 @@ class InlinedAnnotationDrawingStrategy implements IDrawingStrategy {
* {@link GlyphMetrics}.
*
* @param annotation the annotation to be drawn
- * @param gc the graphics context, <code>null</code> when in clearing mode
+ * @param gc the graphics context, <code>null</code> when in clearing mode
* @param textWidget the text widget to draw on
- * @param offset the offset of the line
- * @param length the length of the line
- * @param color the color of the line
+ * @param offset the offset of the line
+ * @param length the length of the line
+ * @param color the color of the line
*/
private static void draw(LineContentAnnotation annotation, GC gc, StyledText textWidget, int offset, int length,
Color color) {
@@ -247,6 +248,7 @@ class InlinedAnnotationDrawingStrategy implements IDrawingStrategy {
y+= bounds.height - charHeight;
// Draw the line content annotation
+ annotation.setLocation(x, y);
annotation.draw(gc, textWidget, offset, length, color, x, y);
int width= annotation.getWidth();
if (width != 0) {
@@ -298,7 +300,7 @@ class InlinedAnnotationDrawingStrategy implements IDrawingStrategy {
* Returns the style to apply with GlyphMetrics width only if needed.
*
* @param annotation the line content annotation
- * @param style the current style and null otherwise.
+ * @param style the current style and null otherwise.
* @return the style to apply with GlyphMetrics width only if needed.
*/
static StyleRange updateStyle(LineContentAnnotation annotation, StyleRange style) {
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationSupport.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationSupport.java
index 2115c3132ae..4628c24b0e6 100644
--- a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationSupport.java
+++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationSupport.java
@@ -32,7 +32,6 @@ import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GlyphMetrics;
-import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.core.runtime.Assert;
@@ -45,7 +44,6 @@ import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ISynchronizable;
import org.eclipse.jface.text.ITextPresentationListener;
import org.eclipse.jface.text.ITextViewerExtension4;
-import org.eclipse.jface.text.ITextViewerExtension5;
import org.eclipse.jface.text.IViewportListener;
import org.eclipse.jface.text.JFaceTextUtil;
import org.eclipse.jface.text.Position;
@@ -243,7 +241,7 @@ public class InlinedAnnotationSupport {
private void update(MouseEvent e) {
fAnnotation= null;
fAction= null;
- AbstractInlinedAnnotation annotation= getInlinedAnnotationAtPoint(fViewer, new Point(e.x, e.y));
+ AbstractInlinedAnnotation annotation= getInlinedAnnotationAtPoint(fViewer, e.x, e.y);
if (annotation != null) {
Consumer<MouseEvent> action= annotation.getAction(e);
if (action != null) {
@@ -255,7 +253,16 @@ public class InlinedAnnotationSupport {
@Override
public void mouseHover(MouseEvent e) {
+ AbstractInlinedAnnotation oldAnnotation= fAnnotation;
update(e);
+ if (oldAnnotation != null) {
+ if (oldAnnotation.equals(fAnnotation)) {
+ // Same annotations which was hovered, do nothing.
+ return;
+ } else {
+ oldAnnotation.onMouseOut(e);
+ }
+ }
if (fAnnotation != null) {
fAnnotation.onMouseHover(e);
}
@@ -519,126 +526,18 @@ public class InlinedAnnotationSupport {
}
/**
- * Returns the {@link AbstractInlinedAnnotation} from the given line index and null otherwise.
- *
- * @param viewer the source viewer
- * @param lineIndex the line index.
- * @return the {@link AbstractInlinedAnnotation} from the given line index and null otherwise.
- */
- private static AbstractInlinedAnnotation getInlinedAnnotationAtLine(ISourceViewer viewer, int lineIndex) {
- if (viewer == null) {
- return null;
- }
- IAnnotationModel annotationModel= viewer.getAnnotationModel();
- if (annotationModel == null) {
- return null;
- }
- IDocument document= viewer.getDocument();
- int lineNumber= lineIndex;
- if (lineNumber > document.getNumberOfLines()) {
- return null;
- }
- try {
- if (viewer instanceof ITextViewerExtension5) {
- lineNumber= ((ITextViewerExtension5) viewer).widgetLine2ModelLine(lineNumber);
- }
- IRegion line= document.getLineInformation(lineNumber);
- return getInlinedAnnotationAtOffset(viewer, line.getOffset(), line.getLength());
- } catch (BadLocationException e) {
- return null;
- }
- }
-
- /**
* Returns the {@link AbstractInlinedAnnotation} from the given point and null otherwise.
*
* @param viewer the source viewer
- * @param point the origin of character bounding box relative to the origin of the widget
- * client area.
+ * @param x the x coordinate of the point
+ * @param y the y coordinate of the point
* @return the {@link AbstractInlinedAnnotation} from the given point and null otherwise.
*/
- private static AbstractInlinedAnnotation getInlinedAnnotationAtPoint(ISourceViewer viewer, Point point) {
- AbstractInlinedAnnotation annotation= getLineContentAnnotationAtPoint(viewer, point);
- if (annotation != null) {
- return annotation;
- }
- return getLineHeaderAnnotationAtPoint(viewer, point);
- }
-
- /**
- * Returns the {@link AbstractInlinedAnnotation} line content from the given point and null
- * otherwise.
- *
- * @param viewer the source viewer
- * @param point the origin of character bounding box relative to the origin of the widget
- * client area.
- * @return the {@link AbstractInlinedAnnotation} line content from the given point and null
- * otherwise.
- */
- private static AbstractInlinedAnnotation getLineContentAnnotationAtPoint(ISourceViewer viewer, Point point) {
- StyledText styledText= viewer.getTextWidget();
- int offset= styledText.getOffsetAtPoint(point);
- if (offset == -1) {
- return null;
- }
- if (viewer instanceof ITextViewerExtension5) {
- offset= ((ITextViewerExtension5) viewer).widgetOffset2ModelOffset(offset);
- }
- AbstractInlinedAnnotation annotation= getInlinedAnnotationAtOffset(viewer, offset, 1);
- if (annotation instanceof LineContentAnnotation) {
- return annotation;
- }
- return null;
- }
-
- /**
- * Returns the {@link AbstractInlinedAnnotation} line header from the given point and null
- * otherwise.
- *
- * @param viewer the source viewer
- * @param point the origin of character bounding box relative to the origin of the widget
- * client area.
- * @return the {@link AbstractInlinedAnnotation} line header from the given point and null
- * otherwise.
- */
- private static AbstractInlinedAnnotation getLineHeaderAnnotationAtPoint(ISourceViewer viewer, Point point) {
- StyledText styledText= viewer.getTextWidget();
- int lineIndex= styledText.getLineIndex(point.y);
- AbstractInlinedAnnotation annotation= getInlinedAnnotationAtLine(viewer, lineIndex);
- if (annotation instanceof LineHeaderAnnotation) {
- return annotation;
- }
- return null;
- }
-
- /**
- * Returns the {@link AbstractInlinedAnnotation} from the given offset and null otherwise.
- *
- * @param viewer the source viewer
- * @param offset the start position of the region, must be >= 0
- * @param length the length of the region, must be >= 0
- * @return the {@link AbstractInlinedAnnotation} from the given offset and null otherwise.
- */
- private static AbstractInlinedAnnotation getInlinedAnnotationAtOffset(ISourceViewer viewer, int offset, int length) {
- if (viewer == null) {
- return null;
- }
- IAnnotationModel annotationModel= viewer.getAnnotationModel();
- if (annotationModel == null) {
- return null;
- }
- Iterator<Annotation> iter= (annotationModel instanceof IAnnotationModelExtension2)
- ? ((IAnnotationModelExtension2) annotationModel).getAnnotationIterator(offset,
- length, true, true)
- : annotationModel.getAnnotationIterator();
- while (iter.hasNext()) {
- Annotation ann= iter.next();
- if (ann instanceof AbstractInlinedAnnotation) {
- Position p= annotationModel.getPosition(ann);
- if (p != null) {
- if (p.overlapsWith(offset, length)) {
- return (AbstractInlinedAnnotation) ann;
- }
+ private AbstractInlinedAnnotation getInlinedAnnotationAtPoint(ISourceViewer viewer, int x, int y) {
+ if (fInlinedAnnotations != null) {
+ for (AbstractInlinedAnnotation ann : fInlinedAnnotations) {
+ if (ann.contains(x, y)) {
+ return ann;
}
}
}
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/LineContentAnnotation.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/LineContentAnnotation.java
index dacc71b6e43..5042dc883ed 100644
--- a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/LineContentAnnotation.java
+++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/LineContentAnnotation.java
@@ -36,7 +36,7 @@ public class LineContentAnnotation extends AbstractInlinedAnnotation {
* Line content annotation constructor.
*
* @param position the position where the annotation must be drawn.
- * @param viewer the {@link ISourceViewer} where the annotation must be drawn.
+ * @param viewer the {@link ISourceViewer} where the annotation must be drawn.
*/
public LineContentAnnotation(Position position, ISourceViewer viewer) {
super(position, viewer);
@@ -66,13 +66,13 @@ public class LineContentAnnotation extends AbstractInlinedAnnotation {
* Draw the inlined annotation. By default it draws the text of the annotation with gray color.
* User can override this method to draw anything.
*
- * @param gc the graphics context
+ * @param gc the graphics context
* @param textWidget the text widget to draw on
- * @param offset the offset of the line
- * @param length the length of the line
- * @param color the color of the line
- * @param x the x position of the annotation
- * @param y the y position of the annotation
+ * @param offset the offset of the line
+ * @param length the length of the line
+ * @param color the color of the line
+ * @param x the x position of the annotation
+ * @param y the y position of the annotation
* @return the text width.
*/
protected int drawAndComputeWidth(GC gc, StyledText textWidget, int offset, int length, Color color, int x, int y) {
@@ -88,4 +88,10 @@ public class LineContentAnnotation extends AbstractInlinedAnnotation {
void setRedrawnCharacterWidth(int redrawnCharacterWidth) {
this.redrawnCharacterWidth= redrawnCharacterWidth;
}
+
+ @Override
+ boolean contains(int x, int y) {
+ return (x >= this.fX && x <= this.fX + width && y >= this.fY && y <= this.fY + getTextWidget().getLineHeight());
+ }
+
}
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/LineHeaderAnnotation.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/LineHeaderAnnotation.java
index c2819ef6048..8ba76690647 100644
--- a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/LineHeaderAnnotation.java
+++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/LineHeaderAnnotation.java
@@ -30,7 +30,7 @@ public class LineHeaderAnnotation extends AbstractInlinedAnnotation {
* Line header annotation constructor.
*
* @param position the position where the annotation must be drawn.
- * @param viewer the {@link ISourceViewer} where the annotation must be drawn.
+ * @param viewer the {@link ISourceViewer} where the annotation must be drawn.
*/
public LineHeaderAnnotation(Position position, ISourceViewer viewer) {
super(position, viewer);
@@ -61,4 +61,9 @@ public class LineHeaderAnnotation extends AbstractInlinedAnnotation {
void setRedrawnCharacterWidth(int redrawnCharacterWidth) {
this.redrawnCharacterWidth= redrawnCharacterWidth;
}
+
+ @Override
+ boolean contains(int x, int y) {
+ return (x >= this.fX && y >= this.fY && y <= this.fY + getHeight());
+ }
}

Back to the top