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

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

Introduction

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

Prototype

public void replaceStyleRanges(int start, int length, StyleRange[] ranges) 

Source Link

Document

Replaces the styles in the given range with new styles.

Usage

From source file:ReplaceStyleRanges.java

License:asdf

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);

    final StyledText styledText = new StyledText(shell, SWT.V_SCROLL | SWT.BORDER);
    styledText.setText("asdfasdfasdfasdf12345678910234567890");

    StyleRange[] ranges = new StyleRange[2];
    ranges[0] = new StyleRange(0, 3, display.getSystemColor(SWT.COLOR_GREEN), null);
    ranges[1] = new StyleRange(3, 6, display.getSystemColor(SWT.COLOR_BLUE), null);

    styledText.setStyleRanges(ranges);/*from   w w w .  ja  v a 2  s  . c  om*/

    styledText.replaceStyleRanges(5, 9, ranges);

    styledText.setBounds(10, 10, 500, 100);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

From source file:SyntaxColoring.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);

    final StyledText styledText = new StyledText(shell, SWT.V_SCROLL | SWT.BORDER);

    final String PUNCTUATION = "(){}";
    styledText.addExtendedModifyListener(new ExtendedModifyListener() {
        public void modifyText(ExtendedModifyEvent event) {
            int end = event.start + event.length - 1;

            if (event.start <= end) {
                String text = styledText.getText(event.start, end);
                java.util.List ranges = new java.util.ArrayList();

                for (int i = 0, n = text.length(); i < n; i++) {
                    if (PUNCTUATION.indexOf(text.charAt(i)) > -1) {
                        ranges.add(new StyleRange(event.start + i, 1, display.getSystemColor(SWT.COLOR_BLUE),
                                null, SWT.BOLD));
                    }//w w w  .j  av  a2 s.co  m
                }
                if (!ranges.isEmpty()) {
                    styledText.replaceStyleRanges(event.start, event.length,
                            (StyleRange[]) ranges.toArray(new StyleRange[0]));
                }
            }
        }
    });

    styledText.setBounds(10, 10, 500, 100);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

From source file:SyntaxTest.java

/**
 * Creates the main window contents//ww  w  .  j  av  a2 s . co  m
 * 
 * @param shell the main window
 */
private void createContents(Shell shell) {
    shell.setLayout(new FillLayout());

    // Create the StyledText
    final StyledText styledText = new StyledText(shell, SWT.BORDER);

    // Add the syntax coloring handler
    styledText.addExtendedModifyListener(new ExtendedModifyListener() {
        public void modifyText(ExtendedModifyEvent event) {
            // Determine the ending offset
            int end = event.start + event.length - 1;

            // If they typed something, get it
            if (event.start <= end) {
                // Get the text
                String text = styledText.getText(event.start, end);

                // Create a collection to hold the StyleRanges
                java.util.List ranges = new java.util.ArrayList();

                // Turn any punctuation red
                for (int i = 0, n = text.length(); i < n; i++) {
                    if (PUNCTUATION.indexOf(text.charAt(i)) > -1) {
                        ranges.add(new StyleRange(event.start + i, 1, red, null, SWT.BOLD));
                    }
                }

                // If we have any ranges to set, set them
                if (!ranges.isEmpty()) {
                    styledText.replaceStyleRanges(event.start, event.length,
                            (StyleRange[]) ranges.toArray(new StyleRange[0]));
                }
            }
        }
    });
}

From source file:StyleRangeTest.java

/**
 * Creates the main window contents//from   w w  w  .  j a  v  a 2s  .  c  o m
 * 
 * @param shell the main window
 */
private void createContents(Shell shell) {
    shell.setLayout(new FillLayout());

    // Create the StyledText
    StyledText styledText = new StyledText(shell, SWT.BORDER);

    // Set the text
    styledText.setText("Go Gators");

    /*
     * The multiple setStyleRange() method // Turn all of the text orange, with
     * the default background color styledText.setStyleRange(new StyleRange(0, 9,
     * orange, null));
     *  // Turn "Gators" blue styledText.setStyleRange(new StyleRange(3, 6, blue,
     * null));
     */

    /*
     * The setStyleRanges() method // Create the array to hold the StyleRanges
     * StyleRange[] ranges = new StyleRange[2];
     *  // Create the first StyleRange, making sure not to overlap. Include the
     * space. ranges[0] = new StyleRange(0, 3, orange, null);
     *  // Create the second StyleRange ranges[1] = new StyleRange(3, 6, blue,
     * null);
     *  // Replace all the StyleRanges for the StyledText
     * styledText.setStyleRanges(ranges);
     */

    /* The replaceStyleRanges() method */
    // Create the array to hold the StyleRanges
    StyleRange[] ranges = new StyleRange[2];

    // Create the first StyleRange, making sure not to overlap. Include the
    // space.
    ranges[0] = new StyleRange(0, 3, orange, null);

    // Create the second StyleRange
    ranges[1] = new StyleRange(3, 6, blue, null);

    // Replace only the StyleRanges in the affected area
    styledText.replaceStyleRanges(0, 9, ranges);
}