Java Tutorial/SWT/StyledText LineStyle
Creating a LineStyleListener
To create a LineStyleListener that sets all "e" characters to red
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.LineStyleEvent;
import org.eclipse.swt.custom.LineStyleListener;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class LineStyleListenerCreating {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
final StyledText styledText = new StyledText(shell, SWT.V_SCROLL | SWT.BORDER);
styledText.addLineStyleListener(new LineStyleListener() {
public void lineGetStyle(LineStyleEvent event) {
java.util.List styles = new java.util.ArrayList();
for (int i = 0, n = event.lineText.length(); i < n; i++) {
if (event.lineText.charAt(i) == "e") {
int start = i;
while (i < n && event.lineText.charAt(i) == "e"){
i++;
}
styles.add(new StyleRange(event.lineOffset + start, i - start, display
.getSystemColor(SWT.COLOR_BLUE), null));
}
}
event.styles = (StyleRange[]) styles.toArray(new StyleRange[0]);
}
});
styledText.setBounds(10, 10, 500, 100);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Crossing Lines
StyledText offers three methods for redrawing its contents.
void redraw(): redraws the entire contents of the StyledText.
Using a LineStyleListener
LineStyleListeners examine single lines at a time.
LineStyleEvent Fields
FieldDescriptionint lineOffsetThe zero-based offset, relative to the whole text, of the line the StyledText needs style information for. Note: this is the character offset, not the line number.String lineTextThe text of the line the StyledText needs style information for.StyleRange [] stylesThe array that holds the StyleRange objects you create for the line.
To calcualate the offset for StyleRanges
int styleRangeOffset = offsetIntoLine + event.lineOffset;