Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorangelozerr2017-12-21 14:31:28 +0000
committerMickael Istria2018-01-08 11:11:52 +0000
commitae1808bedf9b19c2eeb02600f58c48787041a1d3 (patch)
tree8d2af13daa207671fee4e5d00a002c901bbc09e8 /org.eclipse.jface.text.examples
parent71eeeb63bd19b1263f0378d09b7d92a5108e3b44 (diff)
downloadeclipse.platform.text-ae1808bedf9b19c2eeb02600f58c48787041a1d3.tar.gz
eclipse.platform.text-ae1808bedf9b19c2eeb02600f58c48787041a1d3.tar.xz
eclipse.platform.text-ae1808bedf9b19c2eeb02600f58c48787041a1d3.zip
Bug 529087 - [CodeMining] Improve draw of line content annotation.I20180110-0100I20180109-2000I20180108-2000
Change-Id: I4aabd08ea87b650722d3e50a77832689feb61521 Signed-off-by: angelozerr <angelo.zerr@gmail.com>
Diffstat (limited to 'org.eclipse.jface.text.examples')
-rw-r--r--org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/sources/inlined/ColorAnnotation.java20
-rw-r--r--org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/sources/inlined/InlinedAnnotationDemo.java55
2 files changed, 62 insertions, 13 deletions
diff --git a/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/sources/inlined/ColorAnnotation.java b/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/sources/inlined/ColorAnnotation.java
index a6e7a318768..3a169613ffd 100644
--- a/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/sources/inlined/ColorAnnotation.java
+++ b/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/sources/inlined/ColorAnnotation.java
@@ -30,19 +30,17 @@ public class ColorAnnotation extends LineContentAnnotation {
super(pos, viewer);
}
- @Override
- public int getWidth() {
- StyledText styledText = super.getTextWidget();
- return getSquareWidth(styledText);
- }
-
public void setColor(Color color) {
this.color = color;
}
@Override
- public void draw(GC gc, StyledText textWidget, int offset, int length, Color color, int x, int y) {
- int size = getSquareSize(gc.getFontMetrics());
+ protected int drawAndComputeWidth(GC gc, StyledText textWidget, int offset, int length, Color color, int x, int y) {
+ FontMetrics fontMetrics = gc.getFontMetrics();
+ int size = getSquareSize(fontMetrics);
+ x += fontMetrics.getLeading();
+ y += fontMetrics.getDescent();
+
Rectangle rect = new Rectangle(x, y, size, size);
// Fill square
@@ -52,6 +50,7 @@ public class ColorAnnotation extends LineContentAnnotation {
// Draw square box
gc.setForeground(textWidget.getForeground());
gc.drawRectangle(rect);
+ return getSquareWidth(gc.getFontMetrics());
}
/**
@@ -70,12 +69,9 @@ public class ColorAnnotation extends LineContentAnnotation {
* @param styledText
* @return the width of square
*/
- private static int getSquareWidth(StyledText styledText) {
- GC gc = new GC(styledText);
- FontMetrics fontMetrics = gc.getFontMetrics();
+ private static int getSquareWidth(FontMetrics fontMetrics) {
// width = 2 spaces + size width of square
int width = 2 * fontMetrics.getAverageCharWidth() + getSquareSize(fontMetrics);
- gc.dispose();
return width;
}
}
diff --git a/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/sources/inlined/InlinedAnnotationDemo.java b/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/sources/inlined/InlinedAnnotationDemo.java
index 3f3d81575a2..5043c0c0a8b 100644
--- a/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/sources/inlined/InlinedAnnotationDemo.java
+++ b/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/sources/inlined/InlinedAnnotationDemo.java
@@ -170,7 +170,7 @@ public class InlinedAnnotationDemo {
// Color annotation
if (color != null) {
- Position colorPos = new Position(pos.offset + index + "color:".length(), rgb.length());
+ Position colorPos = new Position(pos.offset + index + "color:".length(), 1);
ColorAnnotation colorAnnotation = support.findExistingAnnotation(colorPos);
if (colorAnnotation == null) {
colorAnnotation = new ColorAnnotation(colorPos, viewer);
@@ -179,6 +179,24 @@ public class InlinedAnnotationDemo {
annotations.add(colorAnnotation);
}
+ // rgb parameter names annotations
+ int rgbIndex = line.indexOf("rgb");
+ if (rgbIndex != -1) {
+ rgbIndex = rgbIndex + "rgb".length();
+ int startOffset = pos.offset + rgbIndex;
+ String rgbContent = line.substring(rgbIndex, line.length());
+ int startIndex = addRGBParamNameAnnotation("red:", rgbContent, 0, startOffset, viewer, support,
+ annotations);
+ if (startIndex != -1) {
+ startIndex = addRGBParamNameAnnotation("green:", rgbContent, startIndex, startOffset, viewer,
+ support, annotations);
+ if (startIndex != -1) {
+ startIndex = addRGBParamNameAnnotation("blue:", rgbContent, startIndex, startOffset,
+ viewer, support, annotations);
+ }
+ }
+ }
+
} catch (BadLocationException e) {
e.printStackTrace();
}
@@ -188,6 +206,41 @@ public class InlinedAnnotationDemo {
}
/**
+ * Add RGB parameter name annotation
+ *
+ * @param paramName
+ * @param rgbContent
+ * @param startIndex
+ * @param startOffset
+ * @param viewer
+ * @param support
+ * @param annotations
+ * @return the current parsed index
+ */
+ private static int addRGBParamNameAnnotation(String paramName, String rgbContent, int startIndex, int startOffset,
+ ISourceViewer viewer, InlinedAnnotationSupport support, Set<AbstractInlinedAnnotation> annotations) {
+ char startChar = startIndex == 0 ? '(' : ',';
+ char[] chars = rgbContent.toCharArray();
+ for (int i = startIndex; i < chars.length; i++) {
+ char c = chars[i];
+ if (c == startChar) {
+ if (i == chars.length - 1) {
+ return -1;
+ }
+ Position paramPos = new Position(startOffset + i + 1, 1);
+ LineContentAnnotation colorParamAnnotation = support.findExistingAnnotation(paramPos);
+ if (colorParamAnnotation == null) {
+ colorParamAnnotation = new LineContentAnnotation(paramPos, viewer);
+ }
+ colorParamAnnotation.setText(paramName);
+ annotations.add(colorParamAnnotation);
+ return i + 1;
+ }
+ }
+ return -1;
+ }
+
+ /**
* Parse the given input rgb color and returns an instance of SWT Color and null
* otherwise.
*

Back to the top