Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » How can I get private data out of StyledText?(Need rendering details)
How can I get private data out of StyledText? [message #512827] Mon, 08 February 2010 06:30 Go to next message
Doug M is currently offline Doug MFriend
Messages: 18
Registered: December 2009
Junior Member
Hello all. I am developing a specialized word processor that makes extensive use of StyledText. In order to lay out the pages, I need to know the the character offset of the start of each line. Not lines as in document lines, but lines as rendered on the screen. This information exists in the StyledTextRenderer, but is not exposed by any public methods.

I have come up with an ugly and horrendously inefficient way using invokeAction with ST.LINE_START and ST.LINE_UP, and getCaretOffset(). It works but is too slow to lay out the page in real time. What I want to do is get access to the renderer field in StyledText and then access fields and methods in StyledTextRenderer. These fields, methods, and the StyledTextRenderer class are package private to org.eclipse.swt.custom.

I am fairly new to Java with its enforcement of access levels, and would like to know how an experienced Java developer would handle this concrete problem that must find a solution. I have come up with three possibilities.

1) Use reflection to access the StyledText's renderer and the methods and fields in StyledTextRenderer.

2) Rebuild the SWT library with a modified version of, one that exposes the rendering information our application needs.

3) Make a modified copy of and put it into the application. Then also copy the many package private dependencies used by StyledText into the same package of the application.

All of these approaches seem unsatisfactory.

Thanks for any advice.

Re: How can I get private data out of StyledText? [message #513063 is a reply to message #512827] Mon, 08 February 2010 17:11 Go to previous message
Felipe Heidrich is currently offline Felipe HeidrichFriend
Messages: 29
Registered: July 2009
Junior Member
Okay, you need to know the start offset for all lines including wrapped lines as well, right ?

StyledText doesn't make that information public. But you can get it using hit test (getOffsetAtLocation).

I was playing with this code, it seems to work:

public static void main334(String[] args) {
final Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
StyledText text = new StyledText (shell, SWT.WRAP);
StringBuffer buffer = new StringBuffer ();
for (int i = 0; i < 200; i++) {
buffer.append("word and word again ");
if (i % 10 == 0) buffer.append("\n");
text.setSize(600, 400);

//print only logical lines
int lineCount = text.getLineCount();
for (int i = 0; i < lineCount; i++) {
System.out.println("Line " + i + " lineOffset " + text.getOffsetAtLine(i)) ;

//print all lines
int top = text.getClientArea().y + text.getTopMargin();
int bottom = text.getClientArea().height - text.getBottomMargin();
int left = text.getLeftMargin();
while (bottom > top) {
int offset = -1;
try {
offset = text.getOffsetAtLocation(new Point(left, top));
} catch (IllegalArgumentException e) {
if (offset == -1) break;
int lineIndex = text.getLineAtOffset(offset);
int lineOffset = text.getOffsetAtLine(lineIndex);
System.out.println("Offset: " + offset + " lineIndex: " + lineIndex + " firstOffsetInParagraph " + (offset==lineOffset));
top += text.getLineHeight(offset);
while (!shell.isDisposed()) {
if (!display.readAndDispatch())

Previous Topic:SWT Browser loss its focus when Browser.evaluate() is called
Next Topic:Set clipping within PaintListener.paintControl
Goto Forum:

Current Time: Tue Feb 21 16:32:36 GMT 2017

Powered by FUDForum. Page generated in 0.01459 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software