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:DragTextInStyledText.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    int style = SWT.MULTI | SWT.WRAP | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER;
    final StyledText text1 = new StyledText(shell, style);
    text1.setText(string1);//from w  w  w.j  av  a 2s.  c  om
    DragSource source = new DragSource(text1, DND.DROP_COPY | DND.DROP_MOVE);
    source.setTransfer(new Transfer[] { TextTransfer.getInstance() });
    source.addDragListener(new DragSourceAdapter() {
        Point selection;

        public void dragStart(DragSourceEvent e) {
            selection = text1.getSelection();
            e.doit = selection.x != selection.y;
        }

        public void dragSetData(DragSourceEvent e) {
            e.data = text1.getText(selection.x, selection.y - 1);
        }

        public void dragFinished(DragSourceEvent e) {
            if (e.detail == DND.DROP_MOVE) {
                text1.replaceTextRange(selection.x, selection.y - selection.x, "");
            }
            selection = null;
        }
    });

    final StyledText text2 = new StyledText(shell, style);
    text2.setText(string2);
    DropTarget target = new DropTarget(text2, DND.DROP_DEFAULT | DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK);
    target.setTransfer(new Transfer[] { TextTransfer.getInstance() });
    target.addDropListener(new DropTargetAdapter() {
        public void dragEnter(DropTargetEvent e) {
            if (e.detail == DND.DROP_DEFAULT)
                e.detail = DND.DROP_COPY;
        }

        public void dragOperationChanged(DropTargetEvent e) {
            if (e.detail == DND.DROP_DEFAULT)
                e.detail = DND.DROP_COPY;
        }

        public void drop(DropTargetEvent e) {
            text2.insert((String) e.data);
        }
    });
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.examples.accessibility.ControlsWithLabelsExample.java

public static void main(String[] args) {
    display = new Display();
    shell = new Shell(display);
    shell.setLayout(new GridLayout(4, true));
    shell.setText("All Controls Test");

    new Label(shell, SWT.NONE).setText("Label for Label");
    label = new Label(shell, SWT.NONE);
    label.setText("Label");

    new Label(shell, SWT.NONE).setText("Label for CLabel");
    cLabel = new CLabel(shell, SWT.NONE);
    cLabel.setText("CLabel");

    new Label(shell, SWT.NONE).setText("Label for Push Button");
    buttonPush = new Button(shell, SWT.PUSH);
    buttonPush.setText("Push Button");

    new Label(shell, SWT.NONE).setText("Label for Radio Button");
    buttonRadio = new Button(shell, SWT.RADIO);
    buttonRadio.setText("Radio Button");

    new Label(shell, SWT.NONE).setText("Label for Check Button");
    buttonCheck = new Button(shell, SWT.CHECK);
    buttonCheck.setText("Check Button");

    new Label(shell, SWT.NONE).setText("Label for Toggle Button");
    buttonToggle = new Button(shell, SWT.TOGGLE);
    buttonToggle.setText("Toggle Button");

    new Label(shell, SWT.NONE).setText("Label for Editable Combo");
    combo = new Combo(shell, SWT.BORDER);
    for (int i = 0; i < 4; i++) {
        combo.add("item" + i);
    }//from   w  w  w . j ava  2s .  c om
    combo.select(0);

    new Label(shell, SWT.NONE).setText("Label for Read-Only Combo");
    combo = new Combo(shell, SWT.READ_ONLY | SWT.BORDER);
    for (int i = 0; i < 4; i++) {
        combo.add("item" + i);
    }
    combo.select(0);

    new Label(shell, SWT.NONE).setText("Label for CCombo");
    cCombo = new CCombo(shell, SWT.BORDER);
    for (int i = 0; i < 5; i++) {
        cCombo.add("item" + i);
    }
    cCombo.select(0);

    new Label(shell, SWT.NONE).setText("Label for List");
    list = new List(shell, SWT.SINGLE | SWT.BORDER);
    list.setItems("Item0", "Item1", "Item2");

    new Label(shell, SWT.NONE).setText("Label for Spinner");
    spinner = new Spinner(shell, SWT.BORDER);

    new Label(shell, SWT.NONE).setText("Label for Single-line Text");
    textSingle = new Text(shell, SWT.SINGLE | SWT.BORDER);
    textSingle.setText("Contents of Single-line Text");

    new Label(shell, SWT.NONE).setText("Label for Multi-line Text");
    textMulti = new Text(shell, SWT.MULTI | SWT.BORDER);
    textMulti.setText("\nContents of Multi-line Text\n");

    new Label(shell, SWT.NONE).setText("Label for StyledText");
    styledText = new StyledText(shell, SWT.MULTI | SWT.BORDER);
    styledText.setText("\nContents of Multi-line StyledText\n");

    new Label(shell, SWT.NONE).setText("Label for Table");
    table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    for (int col = 0; col < 3; col++) {
        TableColumn column = new TableColumn(table, SWT.NONE);
        column.setText("Col " + col);
        column.setWidth(50);
    }
    for (int row = 0; row < 3; row++) {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText(new String[] { "C0R" + row, "C1R" + row, "C2R" + row });
    }

    new Label(shell, SWT.NONE).setText("Label for Tree");
    tree = new Tree(shell, SWT.BORDER | SWT.MULTI);
    for (int i = 0; i < 3; i++) {
        TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText("Item" + i);
        for (int j = 0; j < 4; j++) {
            new TreeItem(item, SWT.NONE).setText("Item" + i + j);
        }
    }

    new Label(shell, SWT.NONE).setText("Label for Tree with columns");
    treeTable = new Tree(shell, SWT.BORDER | SWT.MULTI);
    treeTable.setHeaderVisible(true);
    treeTable.setLinesVisible(true);
    for (int col = 0; col < 3; col++) {
        TreeColumn column = new TreeColumn(treeTable, SWT.NONE);
        column.setText("Col " + col);
        column.setWidth(50);
    }
    for (int i = 0; i < 3; i++) {
        TreeItem item = new TreeItem(treeTable, SWT.NONE);
        item.setText(new String[] { "I" + i + "C0", "I" + i + "C1", "I" + i + "C2" });
        for (int j = 0; j < 4; j++) {
            new TreeItem(item, SWT.NONE)
                    .setText(new String[] { "I" + i + j + "C0", "I" + i + j + "C1", "I" + i + j + "C2" });
        }
    }

    new Label(shell, SWT.NONE).setText("Label for ToolBar");
    toolBar = new ToolBar(shell, SWT.FLAT);
    for (int i = 0; i < 3; i++) {
        ToolItem item = new ToolItem(toolBar, SWT.PUSH);
        item.setText("Item" + i);
        item.setToolTipText("ToolItem ToolTip" + i);
    }

    new Label(shell, SWT.NONE).setText("Label for CoolBar");
    coolBar = new CoolBar(shell, SWT.FLAT);
    for (int i = 0; i < 2; i++) {
        CoolItem coolItem = new CoolItem(coolBar, SWT.PUSH);
        ToolBar coolItemToolBar = new ToolBar(coolBar, SWT.FLAT);
        int toolItemWidth = 0;
        for (int j = 0; j < 2; j++) {
            ToolItem item = new ToolItem(coolItemToolBar, SWT.PUSH);
            item.setText("Item" + i + j);
            item.setToolTipText("ToolItem ToolTip" + i + j);
            if (item.getWidth() > toolItemWidth)
                toolItemWidth = item.getWidth();
        }
        coolItem.setControl(coolItemToolBar);
        Point size = coolItemToolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT);
        Point coolSize = coolItem.computeSize(size.x, size.y);
        coolItem.setMinimumSize(toolItemWidth, coolSize.y);
        coolItem.setPreferredSize(coolSize);
        coolItem.setSize(coolSize);
    }

    new Label(shell, SWT.NONE).setText("Label for Canvas");
    canvas = new Canvas(shell, SWT.BORDER);
    canvas.setLayoutData(new GridData(64, 64));
    canvas.addPaintListener(e -> e.gc.drawString("Canvas", 15, 25));
    canvas.setCaret(new Caret(canvas, SWT.NONE));
    /* Hook key listener so canvas will take focus during traversal in. */
    canvas.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
        }

        @Override
        public void keyReleased(KeyEvent e) {
        }
    });
    /* Hook traverse listener to make canvas give up focus during traversal out. */
    canvas.addTraverseListener(e -> e.doit = true);

    new Label(shell, SWT.NONE).setText("Label for Group");
    group = new Group(shell, SWT.NONE);
    group.setText("Group");
    group.setLayout(new FillLayout());
    new Text(group, SWT.SINGLE | SWT.BORDER).setText("Text in Group");

    new Label(shell, SWT.NONE).setText("Label for TabFolder");
    tabFolder = new TabFolder(shell, SWT.NONE);
    for (int i = 0; i < 3; i++) {
        TabItem item = new TabItem(tabFolder, SWT.NONE);
        item.setText("TabItem &" + i);
        item.setToolTipText("TabItem ToolTip" + i);
        Text itemText = new Text(tabFolder, SWT.SINGLE | SWT.BORDER);
        itemText.setText("Text for TabItem " + i);
        item.setControl(itemText);
    }

    new Label(shell, SWT.NONE).setText("Label for CTabFolder");
    cTabFolder = new CTabFolder(shell, SWT.BORDER);
    for (int i = 0; i < 3; i++) {
        CTabItem item = new CTabItem(cTabFolder, SWT.NONE);
        item.setText("CTabItem &" + i);
        item.setToolTipText("CTabItem ToolTip" + i);
        Text itemText = new Text(cTabFolder, SWT.SINGLE | SWT.BORDER);
        itemText.setText("Text for CTabItem " + i);
        item.setControl(itemText);
    }
    cTabFolder.setSelection(cTabFolder.getItem(0));

    new Label(shell, SWT.NONE).setText("Label for Scale");
    scale = new Scale(shell, SWT.NONE);

    new Label(shell, SWT.NONE).setText("Label for Slider");
    slider = new Slider(shell, SWT.NONE);

    new Label(shell, SWT.NONE).setText("Label for ProgressBar");
    progressBar = new ProgressBar(shell, SWT.NONE);
    progressBar.setSelection(50);

    new Label(shell, SWT.NONE).setText("Label for Sash");
    sash = new Sash(shell, SWT.NONE);

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

From source file:HighlightOddLine.java

public HighlightOddLine() {
    shell.setLayout(new GridLayout());

    styledText = new StyledText(shell, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    styledText.setLayoutData(new GridData(GridData.FILL_BOTH));

    styledText.addLineBackgroundListener(new LineBackgroundListener() {
        public void lineGetBackground(LineBackgroundEvent event) {
            if (styledText.getLineAtOffset(event.lineOffset) % 2 == 1)
                event.lineBackground = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
        }/*from  w  w  w.  jav a  2s .  c  o m*/
    });

    styledText.setText("Line 0\r\nLine 1\r\nLine 2\r\nLine 3\r\nLine 4\r\nLine 5\r\nLine 6");

    shell.setSize(300, 150);
    shell.open();
    //textUser.forceFocus();

    // Set up the event loop.
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            // If no more entries in event queue
            display.sleep();
        }
    }

    display.dispose();
}

From source file:SetLineBackground.java

public SetLineBackground() {
    init();/*from w  ww  .  j  a  va2s .c  o  m*/

    shell.setLayout(new GridLayout());

    styledText = new StyledText(shell, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.H_SCROLL | SWT.V_SCROLL);

    styledText.setLayoutData(new GridData(GridData.FILL_BOTH));

    Font font = new Font(shell.getDisplay(), "Courier New", 12, SWT.NORMAL);
    styledText.setFont(font);

    styledText.setText("abcdefg\r\nhijklmn");

    StyleRange styleRange1 = new StyleRange();
    styleRange1.start = 2;
    styleRange1.length = 3;
    styleRange1.foreground = shell.getDisplay().getSystemColor(SWT.COLOR_BLUE);
    styleRange1.background = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
    styleRange1.fontStyle = SWT.BOLD;

    styledText.setStyleRange(styleRange1);

    styledText.setLineBackground(0, 1, shell.getDisplay().getSystemColor(SWT.COLOR_GREEN));
    styledText.setLineBackground(1, 1, shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW));

    shell.setSize(300, 120);
    shell.open();
    //textUser.forceFocus();

    // Set up the event loop.
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            // If no more entries in event queue
            display.sleep();
        }
    }

    display.dispose();
}

From source file:SampleStyledText.java

public SampleStyledText() {
    init();/*from www  .  ja v  a 2  s .c o m*/

    shell.setLayout(new GridLayout());

    styledText = new StyledText(shell, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);

    styledText.setLayoutData(new GridData(GridData.FILL_BOTH));

    Font font = new Font(shell.getDisplay(), "Courier New", 12, SWT.NORMAL);
    styledText.setFont(font);

    styledText.setText("123456789\r\nABCDEFGHI");

    StyleRange styleRange1 = new StyleRange();
    styleRange1.start = 2;
    styleRange1.length = 16;
    styleRange1.foreground = shell.getDisplay().getSystemColor(SWT.COLOR_BLUE);
    styleRange1.background = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
    styleRange1.fontStyle = SWT.BOLD;

    StyleRange styleRange2 = new StyleRange();
    styleRange2.start = 14;
    styleRange2.length = 3;
    styleRange2.fontStyle = SWT.NORMAL;
    styleRange2.foreground = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
    styleRange2.background = shell.getDisplay().getSystemColor(SWT.COLOR_BLUE);

    //      styledText.setStyleRange(styleRange1);
    //      styledText.setStyleRange(styleRange2);

    //styledText.setStyleRanges(new StyleRange[]{styleRange1, styleRange2});
    //styledText.setStyleRanges(new StyleRange[]{styleRange2, styleRange1});

    //styledText.setLineBackground(1, 1, shell.getDisplay().getSystemColor(SWT.COLOR_GRAY));

    //      styledText.setSelection(4);
    //      System.out.println(printStyleRanges(styledText.getStyleRanges()) );
    //      styledText.insert("000");

    System.out.println(printStyleRanges(styledText.getStyleRanges()));

    //      styledText.setStyleRanges(new StyleRange[]{styleRange1});
    //      System.out.println(printStyleRanges(styledText.getStyleRanges()) );

    //shell.pack();
    shell.setSize(300, 120);
    shell.open();
    //textUser.forceFocus();

    // Set up the event loop.
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            // If no more entries in event queue
            display.sleep();
        }
    }

    display.dispose();
}

From source file:RedEListener.java

/**
 * Creates the main window contents//from ww w  .  ja va 2  s.  c  om
 * 
 * @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 | SWT.H_SCROLL | SWT.V_SCROLL);

    // Add the syntax coloring handler
    styledText.addLineStyleListener(new LineStyleListener() {
        public void lineGetStyle(LineStyleEvent event) {
            // Create a collection to hold the StyleRanges
            java.util.List styles = new java.util.ArrayList();

            // Iterate through the text
            for (int i = 0, n = event.lineText.length(); i < n; i++) {
                // Check for 'e'
                if (event.lineText.charAt(i) == 'e') {
                    // Found an 'e'; combine all subsequent e's into the same StyleRange
                    int start = i;
                    for (; i < n && event.lineText.charAt(i) == 'e'; i++)
                        ;

                    // Create the StyleRange and add it to the collection
                    styles.add(new StyleRange(event.lineOffset + start, i - start, red, null));
                }
            }
            // Set the styles for the line
            event.styles = (StyleRange[]) styles.toArray(new StyleRange[0]);
        }
    });
}

From source file:LineBackgroundListenerTest.java

/**
 * Creates the main window's contents/* w w  w .j av a 2  s .  co m*/
 * 
 * @param shell the main window
 */
private void createContents(Shell shell) {
    shell.setLayout(new FillLayout());
    StyledText styledText = new StyledText(shell, SWT.BORDER);

    // Add the line background listener
    styledText.addLineBackgroundListener(new LineBackgroundListener() {
        public void lineGetBackground(LineBackgroundEvent event) {
            if (event.lineText.indexOf("SWT") > -1) {
                event.lineBackground = red;
            }
        }
    });

}

From source file:Ch5Undoable.java

private void buildControls() {
    this.setLayout(new FillLayout());
    styledText = new StyledText(this, SWT.MULTI | SWT.V_SCROLL);

    styledText.addExtendedModifyListener(new ExtendedModifyListener() {
        public void modifyText(ExtendedModifyEvent event) {
            String currText = styledText.getText();
            String newText = currText.substring(event.start, event.start + event.length);
            if (newText != null && newText.length() > 0) {
                if (undoStack.size() == MAX_STACK_SIZE) {
                    undoStack.remove(undoStack.size() - 1);
                }/*w w w .  j ava  2  s.  c o  m*/
                undoStack.add(0, newText);
            }
        }
    });

    styledText.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            switch (e.keyCode) {
            case SWT.F1:
                undo();
                break;
            case SWT.F2:
                redo();
                break;
            default:
                //ignore everything else
            }
        }
    });
}

From source file:SearchStyleText.java

public SearchStyleText() {
    shell.setLayout(new GridLayout(2, false));

    styledText = new StyledText(shell, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    GridData gridData = new GridData(GridData.FILL_BOTH);
    gridData.horizontalSpan = 2;/*from  w  ww  .  ja  v a 2  s .c  o  m*/
    styledText.setLayoutData(gridData);

    keywordText = new Text(shell, SWT.SINGLE | SWT.BORDER);
    keywordText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    Font font = new Font(shell.getDisplay(), "Courier New", 12, SWT.NORMAL);
    styledText.setFont(font);

    button = new Button(shell, SWT.PUSH);
    button.setText("Search");
    button.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            keyword = keywordText.getText();
            styledText.redraw();
        }
    });

    styledText.addLineStyleListener(new LineStyleListener() {
        public void lineGetStyle(LineStyleEvent event) {
            if (keyword == null || keyword.length() == 0) {
                event.styles = new StyleRange[0];
                return;
            }

            String line = event.lineText;
            int cursor = -1;

            LinkedList list = new LinkedList();
            while ((cursor = line.indexOf(keyword, cursor + 1)) >= 0) {
                list.add(getHighlightStyle(event.lineOffset + cursor, keyword.length()));
            }

            event.styles = (StyleRange[]) list.toArray(new StyleRange[list.size()]);
        }
    });

    keyword = "SW";

    styledText.setText("AWT, SWING \r\nSWT & JFACE");

    shell.pack();
    shell.open();
    //textUser.forceFocus();

    // Set up the event loop.
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            // If no more entries in event queue
            display.sleep();
        }
    }

    display.dispose();
}

From source file:MultiLineComment.java

/**
 * Creates the main window contents//from  www.j av  a 2s .  co m
 * 
 * @param shell the main window
 */
private void createContents(Shell shell) {
    shell.setLayout(new FillLayout());
    final StyledText styledText = new StyledText(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);

    // Add the line style listener
    final MultiLineCommentListener lineStyleListener = new MultiLineCommentListener();
    styledText.addLineStyleListener(lineStyleListener);

    // Add the modification listener
    styledText.addExtendedModifyListener(new ExtendedModifyListener() {
        public void modifyText(ExtendedModifyEvent event) {
            // Recalculate the comments
            lineStyleListener.refreshMultilineComments(styledText.getText());

            // Redraw the text
            styledText.redraw();
        }
    });
}