-/**\r
-* Copyright (C) 2007 EDIT\r
-* European Distributed Institute of Taxonomy \r
-* http://www.e-taxonomy.eu\r
-* \r
-* The contents of this file are subject to the Mozilla Public License Version 1.1\r
-* See LICENSE.TXT at the top of this package for the full license terms.\r
-*/\r
-\r
-package eu.etaxonomy.taxeditor.editor.name;\r
-\r
-import org.eclipse.jface.text.source.Annotation;\r
-import org.eclipse.jface.text.source.AnnotationPainter.SquigglesStrategy;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.custom.StyledText;\r
-import org.eclipse.swt.graphics.Color;\r
-import org.eclipse.swt.graphics.GC;\r
-import org.eclipse.swt.graphics.Point;\r
-\r
-/**\r
- * Adds ability to draw multiline squiggles when a <code>StyledText</code>\r
- * contains line-wrapping.\r
- * \r
- * @author p.ciardelli\r
- * @created 21.11.2008\r
- * @version 1.0\r
- */\r
-public class LineWrapSquigglesStrategy extends SquigglesStrategy {\r
- \r
- public static final String ID = "linewrap_squigglesstrategy";\r
-\r
- private GC gc;\r
-\r
- private Color color;\r
-\r
- private int lineHeight;\r
-\r
- private int baseline;\r
-\r
- private StyledText textWidget;\r
-\r
- private int offset;\r
- \r
- /* (non-Javadoc)\r
- * @see org.eclipse.jface.text.source.AnnotationPainter$SquigglesStrategy#draw(org.eclipse.jface.text.source.Annotation, org.eclipse.swt.graphics.GC, org.eclipse.swt.custom.StyledText, int, int, org.eclipse.swt.graphics.Color)\r
- */\r
- public void draw(Annotation annotation, GC gc, StyledText textWidget, int offset, int length, Color color) {\r
- \r
- this.gc = gc;\r
- this.color = color;\r
- this.textWidget = textWidget;\r
- this.offset = offset;\r
- \r
- if (gc != null) {\r
-\r
- if (length < 1)\r
- return;\r
- \r
- baseline = textWidget.getBaseline(offset);\r
- lineHeight = textWidget.getLineHeight(offset);\r
- \r
- Point right = null;\r
- int offsetNewline = offset;\r
- int end = offset + length;\r
- \r
- // Go through the length one character at a time\r
- for (int i = offset; i <= end; i++) {\r
- \r
- // If the y of the current offset is different from that of the last offset,\r
- // we are on a new line\r
- if (right != null && textWidget.getLocationAtOffset(i).y > right.y) {\r
- \r
- // Draw a line of squigglies\r
- drawPolyline(offsetNewline, right);\r
-\r
- // Save offset of line break\r
- offsetNewline = i;\r
- }\r
- \r
- // Get x,y position in case the next char is on a new line\r
- right = textWidget.getLocationAtOffset(i); \r
- \r
- }\r
- \r
- // Draw the last line of squigglies \r
- drawPolyline(offsetNewline, right);\r
-\r
- } else {\r
- textWidget.redrawRange(offset, length, true);\r
- }\r
- }\r
- \r
- /**\r
- * Draws a squiggly line from the offset <code>offsetNewline</code> to\r
- * the x,y coordinates at <code>right</code>.\r
- * \r
- * @param offsetNewline\r
- * @param right\r
- */\r
- private void drawPolyline(int offsetNewline, Point right) {\r
-\r
- // Get offset at last line break\r
- Point left = textWidget.getLocationAtOffset(offsetNewline);\r
- \r
- // Prevent solitary red dot from appearing at EOL\r
- if (left.equals(right)) {\r
- return;\r
- }\r
- \r
- // Only start drawing from 0 if not on the first line\r
- if (offsetNewline != offset) {\r
- \r
- // Offset.x is at the end of the first letter of the new line, not at 0\r
- left.x = 0;\r
- }\r
- \r
- int[] polyline= computePolyline(left, right, baseline, lineHeight);\r
-\r
- gc.setLineWidth(0); // NOTE: 0 means width is 1 but with optimized performance\r
- gc.setLineStyle(SWT.LINE_SOLID);\r
- gc.setForeground(color);\r
- gc.drawPolyline(polyline);\r
- }\r
-\r
- /**\r
- * Copied verbatim from {@link org.eclipse.jface.text.source.AnnotationPainter$SquigglesStrategy} \r
- * \r
- * @see org.eclipse.jface.text.source.AnnotationPainter$SquigglesStrategy\r
- * \r
- * @param left\r
- * @param right\r
- * @param baseline\r
- * @param lineHeight\r
- * @return\r
- */\r
- private int[] computePolyline(Point left, Point right, int baseline, int lineHeight) {\r
-\r
- final int WIDTH= 4; // must be even\r
- final int HEIGHT= 2; // can be any number\r
-\r
- int peaks= (right.x - left.x) / WIDTH;\r
- if (peaks == 0 && right.x - left.x > 2)\r
- peaks= 1;\r
-\r
- int leftX= left.x;\r
-\r
- // compute (number of point) * 2\r
- int length= ((2 * peaks) + 1) * 2;\r
- if (length < 0)\r
- return new int[0];\r
-\r
- int[] coordinates= new int[length];\r
-\r
- // cache peeks' y-coordinates\r
- int top= left.y + Math.min(baseline + 1, lineHeight - HEIGHT - 1);\r
- int bottom= top + HEIGHT;\r
-\r
- // populate array with peek coordinates\r
- for (int i= 0; i < peaks; i++) {\r
- int index= 4 * i;\r
- coordinates[index]= leftX + (WIDTH * i);\r
- coordinates[index+1]= bottom;\r
- coordinates[index+2]= coordinates[index] + WIDTH/2;\r
- coordinates[index+3]= top;\r
- }\r
-\r
- // the last down flank is missing\r
- coordinates[length-2]= Math.min(Math.max(0, right.x - 1), left.x + (WIDTH * peaks));\r
- coordinates[length-1]= bottom;\r
-\r
- return coordinates;\r
- }\r
-}\r
+/**
+* Copyright (C) 2007 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+
+package eu.etaxonomy.taxeditor.editor.name;
+
+import org.eclipse.jface.text.source.Annotation;
+import org.eclipse.jface.text.source.AnnotationPainter.SquigglesStrategy;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.StyledText;
+import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.graphics.GC;
+import org.eclipse.swt.graphics.Point;
+
+/**
+ * Adds ability to draw multiline squiggles when a <code>StyledText</code>
+ * contains line-wrapping.
+ *
+ * @author p.ciardelli
+ * @created 21.11.2008
+ * @version 1.0
+ */
+public class LineWrapSquigglesStrategy extends SquigglesStrategy {
+
+ /** Constant <code>ID="linewrap_squigglesstrategy"</code> */
+ public static final String ID = "linewrap_squigglesstrategy";
+
+ private GC gc;
+
+ private Color color;
+
+ private int lineHeight;
+
+ private int baseline;
+
+ private StyledText textWidget;
+
+ private int offset;
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.text.source.AnnotationPainter$SquigglesStrategy#draw(org.eclipse.jface.text.source.Annotation, org.eclipse.swt.graphics.GC, org.eclipse.swt.custom.StyledText, int, int, org.eclipse.swt.graphics.Color)
+ */
+ /** {@inheritDoc} */
+ public void draw(Annotation annotation, GC gc, StyledText textWidget, int offset, int length, Color color) {
+
+ this.gc = gc;
+ this.color = color;
+ this.textWidget = textWidget;
+ this.offset = offset;
+
+ if (gc != null) {
+
+ if (length < 1)
+ return;
+
+ baseline = textWidget.getBaseline(offset);
+ lineHeight = textWidget.getLineHeight(offset);
+
+ Point right = null;
+ int offsetNewline = offset;
+ int end = offset + length;
+
+ // Go through the length one character at a time
+ for (int i = offset; i <= end; i++) {
+
+ // If the y of the current offset is different from that of the last offset,
+ // we are on a new line
+ if (right != null && textWidget.getLocationAtOffset(i).y > right.y) {
+
+ // Draw a line of squigglies
+ drawPolyline(offsetNewline, right);
+
+ // Save offset of line break
+ offsetNewline = i;
+ }
+
+ // Get x,y position in case the next char is on a new line
+ right = textWidget.getLocationAtOffset(i);
+
+ }
+
+ // Draw the last line of squigglies
+ drawPolyline(offsetNewline, right);
+
+ } else {
+ textWidget.redrawRange(offset, length, true);
+ }
+ }
+
+ /**
+ * Draws a squiggly line from the offset <code>offsetNewline</code> to
+ * the x,y coordinates at <code>right</code>.
+ *
+ * @param offsetNewline
+ * @param right
+ */
+ private void drawPolyline(int offsetNewline, Point right) {
+
+ // Get offset at last line break
+ Point left = textWidget.getLocationAtOffset(offsetNewline);
+
+ // Prevent solitary red dot from appearing at EOL
+ if (left.equals(right)) {
+ return;
+ }
+
+ // Only start drawing from 0 if not on the first line
+ if (offsetNewline != offset) {
+
+ // Offset.x is at the end of the first letter of the new line, not at 0
+ left.x = 0;
+ }
+
+ int[] polyline= computePolyline(left, right, baseline, lineHeight);
+
+ gc.setLineWidth(0); // NOTE: 0 means width is 1 but with optimized performance
+ gc.setLineStyle(SWT.LINE_SOLID);
+ gc.setForeground(color);
+ gc.drawPolyline(polyline);
+ }
+
+ /**
+ * Copied verbatim from {@link org.eclipse.jface.text.source.AnnotationPainter$SquigglesStrategy}
+ *
+ * @see org.eclipse.jface.text.source.AnnotationPainter$SquigglesStrategy
+ *
+ * @param left
+ * @param right
+ * @param baseline
+ * @param lineHeight
+ * @return
+ */
+ private int[] computePolyline(Point left, Point right, int baseline, int lineHeight) {
+
+ final int WIDTH= 4; // must be even
+ final int HEIGHT= 2; // can be any number
+
+ int peaks= (right.x - left.x) / WIDTH;
+ if (peaks == 0 && right.x - left.x > 2)
+ peaks= 1;
+
+ int leftX= left.x;
+
+ // compute (number of point) * 2
+ int length= ((2 * peaks) + 1) * 2;
+ if (length < 0)
+ return new int[0];
+
+ int[] coordinates= new int[length];
+
+ // cache peeks' y-coordinates
+ int top= left.y + Math.min(baseline + 1, lineHeight - HEIGHT - 1);
+ int bottom= top + HEIGHT;
+
+ // populate array with peek coordinates
+ for (int i= 0; i < peaks; i++) {
+ int index= 4 * i;
+ coordinates[index]= leftX + (WIDTH * i);
+ coordinates[index+1]= bottom;
+ coordinates[index+2]= coordinates[index] + WIDTH/2;
+ coordinates[index+3]= top;
+ }
+
+ // the last down flank is missing
+ coordinates[length-2]= Math.min(Math.max(0, right.x - 1), left.x + (WIDTH * peaks));
+ coordinates[length-1]= bottom;
+
+ return coordinates;
+ }
+}