Example usage for org.eclipse.swt.custom StyledText getAccessible

List of usage examples for org.eclipse.swt.custom StyledText getAccessible

Introduction

In this page you can find the example usage for org.eclipse.swt.custom StyledText getAccessible.

Prototype

public Accessible getAccessible() 

Source Link

Document

Returns the accessible object for the receiver.

Usage

From source file:org.eclipse.swt.snippets.Snippet357.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 357");
    shell.setLayout(new FillLayout());

    final StyledText text = new StyledText(shell, SWT.BORDER | SWT.MULTI);
    text.setText("The quick brown fox jumps over the lazy dog.\nThat's all folks!");
    TextStyle textStyle = new TextStyle(new Font(display, "Courier", 12, SWT.BOLD),
            display.getSystemColor(SWT.COLOR_RED), null);
    textStyle.strikeout = true;/*from   ww w  .  j ava2 s .  com*/
    textStyle.underline = true;
    textStyle.underlineStyle = SWT.UNDERLINE_SINGLE;
    text.setStyleRanges(new int[] { 4, 5 }, new StyleRange[] { new StyleRange(textStyle) });

    text.getAccessible().addAccessibleEditableTextListener(new AccessibleEditableTextAdapter() {
        @Override
        public void setTextAttributes(AccessibleTextAttributeEvent e) {
            TextStyle textStyle = e.textStyle;
            if (textStyle != null) {
                /* Copy all of the TextStyle fields into the new StyleRange. */
                StyleRange style = new StyleRange(textStyle);
                /* Create new graphics resources because the old ones are only valid during the event. */
                if (textStyle.font != null)
                    style.font = new Font(display, textStyle.font.getFontData());
                if (textStyle.foreground != null)
                    style.foreground = new Color(display, textStyle.foreground.getRGB());
                if (textStyle.background != null)
                    style.background = new Color(display, textStyle.background.getRGB());
                if (textStyle.underlineColor != null)
                    style.underlineColor = new Color(display, textStyle.underlineColor.getRGB());
                if (textStyle.strikeoutColor != null)
                    style.strikeoutColor = new Color(display, textStyle.strikeoutColor.getRGB());
                if (textStyle.borderColor != null)
                    style.borderColor = new Color(display, textStyle.borderColor.getRGB());
                /* Set the StyleRange into the StyledText. */
                style.start = e.start;
                style.length = e.end - e.start;
                text.setStyleRange(style);
                e.result = ACC.OK;
            } else {
                text.setStyleRanges(e.start, e.end - e.start, null, null);
            }
        }
    });

    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
}