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

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

Introduction

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

Prototype

public StyledText(Composite parent, int style) 

Source Link

Document

Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.

Usage

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

public static void main(String[] args) throws Exception {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 368");
    shell.setLayout(new FillLayout());
    shell.setText("Customize line spacing provider");

    StyledText text = new StyledText(shell, SWT.BORDER | SWT.V_SCROLL);
    text.setText("a\nb\nc\nd");
    text.setLineSpacingProvider(lineIndex -> {
        if (lineIndex == 0) {
            return 10;
        } else if (lineIndex == 1) {
            return 30;
        }/*from w  w w  .j a  v  a 2s.c  o m*/
        return null;
    });

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

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

public static void main(String[] args) throws Exception {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 369");
    shell.setLayout(new FillLayout());
    shell.setText("Line spacing provider in action");

    StyledText text = new StyledText(shell, SWT.BORDER | SWT.V_SCROLL);
    text.setText("// Type your custom line spacing \n10\n5\nabcd\n20\nefgh");

    text.setLineSpacingProvider(lineIndex -> {
        String line = text.getLine(lineIndex).trim();
        try {/*from   w  w w  .  j  av  a2 s .  co  m*/
            return Integer.parseInt(line);
        } catch (NumberFormatException e) {
            return null;
        }
    });

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

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

public static void main(String[] args) throws Exception {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 374");
    shell.setLayout(new FillLayout());
    shell.setText("Customize line vertical indent");

    StyledText text = new StyledText(shell, SWT.BORDER | SWT.V_SCROLL);
    text.setWordWrap(true);/*from   w  ww  .ja  v  a  2s  . com*/
    text.setText("word1 word2 word3 word4");
    text.setLineVerticalIndent(0, 20);

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

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 163");
    shell.setLayout(new FillLayout());
    StyledText text = new StyledText(shell, SWT.BORDER);
    text.setText("0123456789 ABCDEFGHIJKLM NOPQRSTUVWXYZ");
    // make 0123456789 appear bold
    StyleRange style1 = new StyleRange();
    style1.start = 0;//from   ww  w.ja  v a  2  s. c o  m
    style1.length = 10;
    style1.fontStyle = SWT.BOLD;
    text.setStyleRange(style1);
    // make ABCDEFGHIJKLM have a red font
    StyleRange style2 = new StyleRange();
    style2.start = 11;
    style2.length = 13;
    style2.foreground = display.getSystemColor(SWT.COLOR_RED);
    text.setStyleRange(style2);
    // make NOPQRSTUVWXYZ have a blue background
    StyleRange style3 = new StyleRange();
    style3.start = 25;
    style3.length = 13;
    style3.background = display.getSystemColor(SWT.COLOR_BLUE);
    text.setStyleRange(style3);

    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    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(/*from   w w  w.  ja v  a 2  s .  c o  m*/
            "\"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.Snippet244.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 244");
    shell.setLayout(new FillLayout());
    final StyledText text = new StyledText(shell, SWT.NONE);
    StyleRange style = new StyleRange();
    style.borderColor = display.getSystemColor(SWT.COLOR_RED);
    style.borderStyle = SWT.BORDER_SOLID;
    StyleRange[] styles = { style };//from  w  w  w .  j  a v a 2 s . co m
    String contents = "This demonstrates drawing a box\naround every occurrence of the word\nbox in the StyledText";
    text.setText(contents);
    int index = contents.indexOf(SEARCH_STRING);
    while (index != -1) {
        text.setStyleRanges(0, 0, new int[] { index, SEARCH_STRING.length() }, styles);
        index = contents.indexOf(SEARCH_STRING, index + 1);
    }
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("StyledText with underline and strike through");
    shell.setLayout(new FillLayout());
    StyledText text = new StyledText(shell, SWT.BORDER);
    text.setText("0123456789 ABCDEFGHIJKLM NOPQRSTUVWXYZ");
    // make 0123456789 appear underlined
    StyleRange style1 = new StyleRange();
    style1.start = 0;//from   w w w .  jav a 2 s. com
    style1.length = 10;
    style1.underline = true;
    text.setStyleRange(style1);
    // make ABCDEFGHIJKLM have a strike through
    StyleRange style2 = new StyleRange();
    style2.start = 11;
    style2.length = 13;
    style2.strikeout = true;
    text.setStyleRange(style2);
    // make NOPQRSTUVWXYZ appear underlined and have a strike through
    StyleRange style3 = new StyleRange();
    style3.start = 25;
    style3.length = 13;
    style3.underline = true;
    style3.strikeout = true;
    text.setStyleRange(style3);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 268");
    shell.setLayout(new FillLayout());
    StyledText styledText = new StyledText(shell, SWT.V_SCROLL);
    String multiLineString = "";
    for (int i = 0; i < 200; i++) {
        multiLineString = multiLineString.concat("This is line number " + i + " in the multi-line string.\n");
    }/*www . jav a 2  s . com*/
    styledText.setText(multiLineString);
    shell.setSize(styledText.computeSize(SWT.DEFAULT, 400));
    shell.open();
    // move cursor over styled text
    Rectangle clientArea = shell.getClientArea();
    Point location = shell.getLocation();
    Event event = new Event();
    event.type = SWT.MouseMove;
    event.x = location.x + clientArea.width / 2;
    event.y = location.y + clientArea.height / 2;
    display.post(event);
    styledText.addListener(SWT.MouseWheel, e -> System.out.println("Mouse Wheel event " + e));
    new Thread() {
        Event event;

        @Override
        public void run() {
            for (int i = 0; i < 50; i++) {
                event = new Event();
                event.type = SWT.MouseWheel;
                event.detail = SWT.SCROLL_LINE;
                event.count = -2;
                if (!display.isDisposed())
                    display.post(event);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }
            }
        }
    }.start();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("StyledText Bullet Example");
    shell.setLayout(new FillLayout());
    final StyledText styledText = new StyledText(shell,
            SWT.FULL_SELECTION | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
    StringBuilder text = new StringBuilder();
    text.append("Here is StyledText with some bulleted lists:\n\n");
    for (int i = 0; i < 4; i++)
        text.append("Red Bullet List Item " + i + "\n");
    text.append("\n");
    for (int i = 0; i < 2; i++)
        text.append("Numbered List Item " + i + "\n");
    for (int i = 0; i < 4; i++)
        text.append("Sub List Item " + i + "\n");
    for (int i = 0; i < 2; i++)
        text.append("Numbered List Item " + (2 + i) + "\n");
    text.append("\n");
    for (int i = 0; i < 4; i++)
        text.append("Custom Draw List Item " + i + "\n");
    styledText.setText(text.toString());

    StyleRange style0 = new StyleRange();
    style0.metrics = new GlyphMetrics(0, 0, 40);
    style0.foreground = display.getSystemColor(SWT.COLOR_RED);
    Bullet bullet0 = new Bullet(style0);
    StyleRange style1 = new StyleRange();
    style1.metrics = new GlyphMetrics(0, 0, 50);
    style1.foreground = display.getSystemColor(SWT.COLOR_BLUE);
    Bullet bullet1 = new Bullet(ST.BULLET_NUMBER | ST.BULLET_TEXT, style1);
    bullet1.text = ".";
    StyleRange style2 = new StyleRange();
    style2.metrics = new GlyphMetrics(0, 0, 80);
    style2.foreground = display.getSystemColor(SWT.COLOR_GREEN);
    Bullet bullet2 = new Bullet(ST.BULLET_TEXT, style2);
    bullet2.text = "\u2713";
    StyleRange style3 = new StyleRange();
    style3.metrics = new GlyphMetrics(0, 0, 50);
    Bullet bullet3 = new Bullet(ST.BULLET_CUSTOM, style2);

    styledText.setLineBullet(2, 4, bullet0);
    styledText.setLineBullet(7, 2, bullet1);
    styledText.setLineBullet(9, 4, bullet2);
    styledText.setLineBullet(13, 2, bullet1);
    styledText.setLineBullet(16, 4, bullet3);

    styledText.addPaintObjectListener(event -> {
        Display display1 = event.display;
        StyleRange style = event.style;//  w w w  .j ava 2 s.  com
        Font font = style.font;
        if (font == null)
            font = styledText.getFont();
        TextLayout layout = new TextLayout(display1);
        layout.setAscent(event.ascent);
        layout.setDescent(event.descent);
        layout.setFont(font);
        layout.setText("\u2023 1." + event.bulletIndex + ")");
        layout.draw(event.gc, event.x + 10, event.y);
        layout.dispose();
    });
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    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;/*from w w w . jav a2  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();
    }
}