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

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

Introduction

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

Prototype

public void setStyleRanges(int[] ranges, StyleRange[] styles) 

Source Link

Document

Sets styles to be used for rendering the widget content.

Usage

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

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 332");
    GridLayout layout = new GridLayout();
    layout.marginHeight = layout.marginWidth = 10;
    shell.setLayout(layout);//from  w  w w .j av a2  s  .c  o m
    StyledText text = new StyledText(shell, SWT.MULTI | SWT.BORDER);
    final String segment = "Eclipse";
    String string = "Force RTL direction on this segment \"" + segment + "\".";
    text.setText(string);
    int[] segments = { string.indexOf(segment), segment.length() };
    StyleRange[] ranges = { new StyleRange(0, 0, display.getSystemColor(SWT.COLOR_RED), null) };
    text.setStyleRanges(segments, ranges);
    Font font = new Font(display, "Tahoma", 16, 0);
    text.setFont(font);
    text.addBidiSegmentListener(event -> {
        String string1 = event.lineText;
        int start = string1.indexOf(segment);
        event.segments = new int[] { start, start + segment.length() };
        event.segmentsChars = new char[] { '\u202e', '\u202C' };
    });
    Combo combo = new Combo(shell, SWT.SIMPLE);
    combo.setFont(font);
    combo.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
    combo.setItems("Option 1...", "Option 2...", "Option 3...", "Option 4...");
    combo.select(1);
    combo.addSegmentListener(event -> {
        event.segments = new int[] { 0, event.lineText.length() };
        event.segmentsChars = new char[] { '\u202e', '\u202c' };
    });
    shell.setSize(500, 250);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    font.dispose();
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 316");
    shell.setLayout(new FillLayout());
    StyledText text = new StyledText(shell, SWT.V_SCROLL | SWT.H_SCROLL);
    text.setText("StyledText with margins.");
    Font font = new Font(display, "Tahoma", 14, SWT.ITALIC);
    text.setText(/* www .ja  va 2  s  .c om*/
            "\"If you go down to the woods today\nYou'd better not go alone\nIt's lovely down in the woods today\nBut safer to stay at home\"");
    StyleRange italic = new StyleRange();
    italic.font = font;
    text.setStyleRanges(new int[] { 0, text.getCharCount() }, new StyleRange[] { italic });
    text.setMargins(30, 30, 30, 30);
    Color color = new Color(display, 138, 226, 255);
    text.setMarginColor(color);
    shell.setSize(500, 300);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    font.dispose();
    color.dispose();
    display.dispose();
}

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;/*www  .  ja v a 2s  . co  m*/
    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();
    }
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 356");
    FillLayout layout = new FillLayout();
    layout.marginHeight = layout.marginWidth = 10;
    shell.setLayout(layout);/* w ww. ja v a 2  s.  co  m*/

    String string = "This is sample text with a link and some other link here.";
    final StyledText styledText = new StyledText(shell, SWT.MULTI | SWT.BORDER);
    styledText.setText(string);

    String link1 = "link";
    String link2 = "here";
    StyleRange style = new StyleRange();
    style.underline = true;
    style.underlineStyle = SWT.UNDERLINE_LINK;

    int[] ranges = { string.indexOf(link1), link1.length(), string.indexOf(link2), link2.length() };
    StyleRange[] styles = { style, style };
    styledText.setStyleRanges(ranges, styles);

    styledText.addListener(SWT.MouseDown, event -> {
        // It is up to the application to determine when and how a link should be activated.
        // In this snippet links are activated on mouse down when the control key is held down
        if ((event.stateMask & SWT.MOD1) != 0) {
            int offset = styledText.getOffsetAtPoint(new Point(event.x, event.y));
            if (offset != -1) {
                StyleRange style1 = null;
                try {
                    style1 = styledText.getStyleRangeAtOffset(offset);
                } catch (IllegalArgumentException e) {
                    // no character under event.x, event.y
                }
                if (style1 != null && style1.underline && style1.underlineStyle == SWT.UNDERLINE_LINK) {
                    System.out.println("Click on a Link");
                }
            }
        }
    });
    shell.setSize(600, 400);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 211");
    shell.setLayout(new FillLayout());
    StyledText styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER);
    styledText.setText(text);/*  www . j  a va 2  s . co m*/
    FontData data = styledText.getFont().getFontData()[0];
    Font font1 = new Font(display, data.getName(), data.getHeight() * 2, data.getStyle());
    Font font2 = new Font(display, data.getName(), data.getHeight() * 4 / 5, data.getStyle());
    StyleRange[] styles = new StyleRange[8];
    styles[0] = new StyleRange();
    styles[0].font = font1;
    styles[1] = new StyleRange();
    styles[1].rise = data.getHeight() / 3;
    styles[2] = new StyleRange();
    styles[2].background = display.getSystemColor(SWT.COLOR_GREEN);
    styles[3] = new StyleRange();
    styles[3].foreground = display.getSystemColor(SWT.COLOR_MAGENTA);
    styles[4] = new StyleRange();
    styles[4].font = font2;
    styles[4].foreground = display.getSystemColor(SWT.COLOR_BLUE);
    styles[4].underline = true;
    styles[5] = new StyleRange();
    styles[5].rise = -data.getHeight() / 3;
    styles[5].strikeout = true;
    styles[5].underline = true;
    styles[6] = new StyleRange();
    styles[6].font = font1;
    styles[6].foreground = display.getSystemColor(SWT.COLOR_YELLOW);
    styles[6].background = display.getSystemColor(SWT.COLOR_BLUE);
    styles[7] = new StyleRange();
    styles[7].rise = data.getHeight() / 3;
    styles[7].underline = true;
    styles[7].fontStyle = SWT.BOLD;
    styles[7].foreground = display.getSystemColor(SWT.COLOR_RED);
    styles[7].background = display.getSystemColor(SWT.COLOR_BLACK);

    int[] ranges = new int[] { 16, 4, 61, 13, 107, 10, 122, 10, 134, 3, 143, 6, 160, 7, 168, 7 };
    styledText.setStyleRanges(ranges, styles);

    shell.setSize(300, 300);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    font1.dispose();
    font2.dispose();
    display.dispose();
}

From source file:StyledTextStyleRangeFont.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    StyledText styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER);
    styledText.setText(text);/* w  ww .j  a v  a  2  s.c o  m*/
    FontData data = styledText.getFont().getFontData()[0];
    Font font1 = new Font(display, data.getName(), data.getHeight() * 2, data.getStyle());
    Font font2 = new Font(display, data.getName(), data.getHeight() * 4 / 5, data.getStyle());
    StyleRange[] styles = new StyleRange[8];
    styles[0] = new StyleRange();
    styles[0].font = font1;
    styles[1] = new StyleRange();
    styles[1].rise = data.getHeight() / 3;
    styles[2] = new StyleRange();
    styles[2].background = display.getSystemColor(SWT.COLOR_GREEN);
    styles[3] = new StyleRange();
    styles[3].foreground = display.getSystemColor(SWT.COLOR_MAGENTA);
    styles[4] = new StyleRange();
    styles[4].font = font2;
    styles[4].foreground = display.getSystemColor(SWT.COLOR_BLUE);
    ;
    styles[4].underline = true;
    styles[5] = new StyleRange();
    styles[5].rise = -data.getHeight() / 3;
    styles[5].strikeout = true;
    styles[5].underline = true;
    styles[6] = new StyleRange();
    styles[6].font = font1;
    styles[6].foreground = display.getSystemColor(SWT.COLOR_YELLOW);
    styles[6].background = display.getSystemColor(SWT.COLOR_BLUE);
    styles[7] = new StyleRange();
    styles[7].rise = data.getHeight() / 3;
    styles[7].underline = true;
    styles[7].fontStyle = SWT.BOLD;
    styles[7].foreground = display.getSystemColor(SWT.COLOR_RED);
    styles[7].background = display.getSystemColor(SWT.COLOR_BLACK);

    int[] ranges = new int[] { 16, 4, 61, 13, 107, 10, 122, 10, 134, 3, 143, 6, 160, 7, 168, 7 };
    styledText.setStyleRanges(ranges, styles);

    shell.setSize(300, 300);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    font1.dispose();
    font2.dispose();
    display.dispose();
}