... | ... | @@ -326,6 +326,40 @@ This method is the first one with some complexity in it. However we are for now |
|
|
|
|
|
This subsection serves the purpose of elaborating the data which are stored inside the editor. And the most important one is the concept of an ```EditorLine```. In the sense of the ```StyledText``` widget of SWT, a line does always start At a character and ends at a line break ```\n```. So whenever wrapping is enabled, there is no real way to tell into how many *lines* the actual lines was separated. This widget should not follow this approach, instead we define an ```EditorLine``` as every snippet of text that is displayed in the same row. (So that wrapping actually creates or changes the amount of lines!).
|
|
|
|
|
|
So what does such an ```EditorLine``` actually store:
|
|
|
|
|
|
```java
|
|
|
public class EditorLine {
|
|
|
|
|
|
private String content;
|
|
|
// offset in character of this line
|
|
|
private Point offset;
|
|
|
private int lineHeight;
|
|
|
private int lineWidth;
|
|
|
private Map<Integer, Point> charOffset;
|
|
|
private int topYOffset;
|
|
|
private int lineNumber;
|
|
|
private boolean isVisible;
|
|
|
|
|
|
private List<StyleRange> styleRanges;
|
|
|
|
|
|
public EditorLine(String content, Point offset, int lineHeight, int lineWidth) {
|
|
|
super();
|
|
|
this.content = content;
|
|
|
this.offset = offset;
|
|
|
this.lineHeight = lineHeight;
|
|
|
this.lineWidth = lineWidth;
|
|
|
|
|
|
if (offset.x >= offset.y) {
|
|
|
throw new IllegalArgumentException("Invalid offset! " + offset + " for content " + content);
|
|
|
}
|
|
|
|
|
|
styleRanges = new ArrayList<>();
|
|
|
}
|
|
|
|
|
|
```
|
|
|
The line stores its string-content, the offset of this string in the original document, the width and height (both in pixel), the current number in the document (which might change if the widget gets resized!!). Also it stores, whether it is currently visible and its current topYOffset (this ranges from 0 to the height of the widget, this is only relevant if we are currently visible). The last attribute (the Map) is very interesting. Whenever someone clicks on a line in the editor, we have to place the caret accordingly and this requires us to know exactly where each character in a line is located. The line calculates all those positions and stores it in its map. A point stores for every Character in the lines content the xOffset (a pixelvalue) when this character starts and another xOffset (again a pixelvalue) when this character ends. This information is stored in a point.
|
|
|
|
|
|
## Implementing the Line Wrap
|
|
|
|
|
|
We stopped at the discussion of the method ```determineWrappedLines(...)``` which does calculate the lines of our editor. This method produces all lines, no matter if we have an activated wrap or not. The idea behind this method is to separate the document at every newline character ````\n```. Then measure the resulting text snippet. If the snippet exceeds the width of the visible area of the widget, we need to wrap this line, else we can just create a new EditorLine. Obviously, if wrap is not active, we can always create the line without wrapping it. Let us check the code
|
... | ... | |