Search code examples
javaswtjfacetreeviewer

Java TreeViewer SWT.RIGHT_TO_LEFT


I work on a Eclipse Plugin which have two TreeViewer, one of them with the parameter SWT.RIGHT_TO_LEFT. This looks like this: Plugin Screen

As you see the Tree Structure is drawn from right to left and that is what i need. But and here is my problem, the Text gets also inverted but only when special characters are in the string. PID.5.7 is transformed to 7.5.PID. Here is how i create TreeViewer and the Text:

xmlTreeOut = new TreeViewer(composite, SWT.RIGHT_TO_LEFT | SWT.DOUBLE_BUFFERED);
...
xmlTreeOut.getTree().addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
        //loop over treeItems and for each
        ...
        TextLayout textLayout = new TextLayout(Display.getDefault());
        textLayout.setText(xmlName + xmlValue);
        textLayout.draw(event.gc, root.getBounds().x + 2, root.getBounds().y + 1);
        ...
    }
});

Does anyone have a fix for this problem? I would like to have my String exactly as I write it in the TextLayout.

EDIT:

I added the mentioned

textLayout.setOrientation(SWT.RIGHT_TO_LEFT);

Now my output is like this:

enter image description here

As you can see it worked for Strings like PID.7.5 but if there are Strings seperated with space and only containing special characters its inversed. Like "MSH.1 |" becomes "| MSH.1"

(removed the Style2 and use only one style in TextLayout)

EDIT 2:

WORKAROUND SOLUTION:

I test now if my String starts with a "word" which has no a-z, A-Z I simply add a "x" with StyleColor = WHITE in front. This way all values are displeyed correctly.


Solution

  • I haven't tested this, but the method TextLayout#setOrientation(int orientation) looks like what you need:

    Sets the orientation of the receiver, which must be one of SWT.LEFT_TO_RIGHT or SWT.RIGHT_TO_LEFT.

    Just set it to SWT.LEFT_TO_RIGHT for both TreeViewers (or maybe SWT.RIGHT_TO_LEFT for the right TreeViewer to invert the previous right-to-left ?).