Revision 8481f952
Added by Andreas Müller almost 4 years ago
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/name/container/LineWrapSquigglesStrategy.java | ||
---|---|---|
1 | 1 |
/** |
2 | 2 |
* Copyright (C) 2007 EDIT |
3 |
* European Distributed Institute of Taxonomy
|
|
3 |
* European Distributed Institute of Taxonomy |
|
4 | 4 |
* http://www.e-taxonomy.eu |
5 |
*
|
|
5 |
* |
|
6 | 6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
7 | 7 |
* See LICENSE.TXT at the top of this package for the full license terms. |
8 | 8 |
*/ |
... | ... | |
23 | 23 |
* |
24 | 24 |
* @author p.ciardelli |
25 | 25 |
* @created 21.11.2008 |
26 |
* @version 1.0 |
|
27 | 26 |
*/ |
28 | 27 |
public class LineWrapSquigglesStrategy extends SquigglesStrategy { |
29 |
|
|
28 |
|
|
30 | 29 |
/** Constant <code>ID="linewrap_squigglesstrategy"</code> */ |
31 | 30 |
public static final String ID = "linewrap_squigglesstrategy"; //$NON-NLS-1$ |
32 | 31 |
|
... | ... | |
41 | 40 |
private StyledText textWidget; |
42 | 41 |
|
43 | 42 |
private int offset; |
44 |
|
|
45 |
/* (non-Javadoc) |
|
46 |
* @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) |
|
47 |
*/ |
|
48 |
/** {@inheritDoc} */ |
|
49 |
public void draw(Annotation annotation, GC gc, StyledText textWidget, int offset, int length, Color color) { |
|
50 |
|
|
43 |
|
|
44 |
@Override |
|
45 |
public void draw(Annotation annotation, GC gc, StyledText textWidget, int offset, int length, Color color) { |
|
46 |
|
|
51 | 47 |
this.gc = gc; |
52 | 48 |
this.color = color; |
53 | 49 |
this.textWidget = textWidget; |
54 | 50 |
this.offset = offset; |
55 |
|
|
51 |
|
|
56 | 52 |
if (gc != null) { |
57 | 53 |
|
58 |
if (length < 1) |
|
59 |
return; |
|
60 |
|
|
54 |
if (length < 1) { |
|
55 |
return; |
|
56 |
} |
|
57 |
|
|
61 | 58 |
baseline = textWidget.getBaseline(offset); |
62 | 59 |
lineHeight = textWidget.getLineHeight(offset); |
63 |
|
|
60 |
|
|
64 | 61 |
Point right = null; |
65 | 62 |
int offsetNewline = offset; |
66 | 63 |
int end = offset + length; |
67 |
|
|
64 |
|
|
68 | 65 |
// Go through the length one character at a time |
69 | 66 |
for (int i = offset; i <= end; i++) { |
70 |
|
|
67 |
|
|
71 | 68 |
// If the y of the current offset is different from that of the last offset, |
72 | 69 |
// we are on a new line |
73 | 70 |
if (right != null && textWidget.getLocationAtOffset(i).y > right.y) { |
74 |
|
|
71 |
|
|
75 | 72 |
// Draw a line of squigglies |
76 | 73 |
drawPolyline(offsetNewline, right); |
77 | 74 |
|
78 | 75 |
// Save offset of line break |
79 | 76 |
offsetNewline = i; |
80 | 77 |
} |
81 |
|
|
78 |
|
|
82 | 79 |
// Get x,y position in case the next char is on a new line |
83 |
right = textWidget.getLocationAtOffset(i);
|
|
84 |
|
|
80 |
right = textWidget.getLocationAtOffset(i); |
|
81 |
|
|
85 | 82 |
} |
86 |
|
|
87 |
// Draw the last line of squigglies
|
|
83 |
|
|
84 |
// Draw the last line of squigglies |
|
88 | 85 |
drawPolyline(offsetNewline, right); |
89 | 86 |
|
90 | 87 |
} else { |
91 | 88 |
textWidget.redrawRange(offset, length, true); |
92 | 89 |
} |
93 | 90 |
} |
94 |
|
|
91 |
|
|
95 | 92 |
/** |
96 | 93 |
* Draws a squiggly line from the offset <code>offsetNewline</code> to |
97 | 94 |
* the x,y coordinates at <code>right</code>. |
98 |
*
|
|
95 |
* |
|
99 | 96 |
* @param offsetNewline |
100 | 97 |
* @param right |
101 | 98 |
*/ |
... | ... | |
103 | 100 |
|
104 | 101 |
// Get offset at last line break |
105 | 102 |
Point left = textWidget.getLocationAtOffset(offsetNewline); |
106 |
|
|
103 |
|
|
107 | 104 |
// Prevent solitary red dot from appearing at EOL |
108 | 105 |
if (left.equals(right)) { |
109 | 106 |
return; |
110 | 107 |
} |
111 |
|
|
108 |
|
|
112 | 109 |
// Only start drawing from 0 if not on the first line |
113 | 110 |
if (offsetNewline != offset) { |
114 |
|
|
111 |
|
|
115 | 112 |
// Offset.x is at the end of the first letter of the new line, not at 0 |
116 | 113 |
left.x = 0; |
117 | 114 |
} |
118 |
|
|
115 |
|
|
119 | 116 |
int[] polyline= computePolyline(left, right, baseline, lineHeight); |
120 | 117 |
|
121 | 118 |
gc.setLineWidth(0); // NOTE: 0 means width is 1 but with optimized performance |
... | ... | |
125 | 122 |
} |
126 | 123 |
|
127 | 124 |
/** |
128 |
* Copied verbatim from {@link org.eclipse.jface.text.source.AnnotationPainter$SquigglesStrategy}
|
|
129 |
*
|
|
125 |
* Copied verbatim from {@link org.eclipse.jface.text.source.AnnotationPainter$SquigglesStrategy} |
|
126 |
* |
|
130 | 127 |
* @see org.eclipse.jface.text.source.AnnotationPainter$SquigglesStrategy |
131 |
*
|
|
128 |
* |
|
132 | 129 |
* @param left |
133 | 130 |
* @param right |
134 | 131 |
* @param baseline |
... | ... | |
141 | 138 |
final int HEIGHT= 2; // can be any number |
142 | 139 |
|
143 | 140 |
int peaks= (right.x - left.x) / WIDTH; |
144 |
if (peaks == 0 && right.x - left.x > 2) |
|
145 |
peaks= 1; |
|
141 |
if (peaks == 0 && right.x - left.x > 2) { |
|
142 |
peaks= 1; |
|
143 |
} |
|
146 | 144 |
|
147 | 145 |
int leftX= left.x; |
148 | 146 |
|
149 | 147 |
// compute (number of point) * 2 |
150 | 148 |
int length= ((2 * peaks) + 1) * 2; |
151 |
if (length < 0) |
|
152 |
return new int[0]; |
|
149 |
if (length < 0) { |
|
150 |
return new int[0]; |
|
151 |
} |
|
153 | 152 |
|
154 | 153 |
int[] coordinates= new int[length]; |
155 | 154 |
|
Also available in: Unified diff
cleanup