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

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

Introduction

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

Prototype

public Point computeSize(int wHint, int hHint) 

Source Link

Document

Returns the preferred size of the receiver.

Usage

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");
    }/*from  www  .  ja va2  s  .  c  om*/
    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.Snippet343.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 343");
    shell.setLayout(new FillLayout());
    shell.setSize(400, 400);//www  .  j  a v  a  2s  .  com
    SashForm sashForm = new SashForm(shell, SWT.HORIZONTAL);
    Composite leftComposite = new Composite(sashForm, SWT.NONE);
    leftComposite.setLayout(new FillLayout());
    Composite rightComposite = new Composite(sashForm, SWT.NONE);
    rightComposite.setLayout(new FillLayout());
    ExpandBar expandBar = new ExpandBar(leftComposite, SWT.NONE);
    final ExpandItem expandItem1 = new ExpandItem(expandBar, SWT.NONE);
    expandItem1.setText("item 1");
    new ExpandItem(expandBar, SWT.NONE).setText("item 2"); /* expandItem2 */

    final StyledText text = new StyledText(expandBar, SWT.MULTI | SWT.WRAP);
    expandItem1.setControl(text);
    text.setText("initial text that will wrap if it's long enough");

    /* update the item's height if needed in response to changes in the text's size */
    final int TRIAL_WIDTH = 100;
    final int trimWidth = text.computeTrim(0, 0, TRIAL_WIDTH, 100).width - TRIAL_WIDTH;
    text.addListener(SWT.Modify, event -> {
        Point size = text.computeSize(text.getSize().x - trimWidth, SWT.DEFAULT);
        if (expandItem1.getHeight() != size.y) {
            expandItem1.setHeight(size.y);
        }
    });
    expandBar.addListener(SWT.Resize, event -> display.asyncExec(() -> {
        /*
         * The following is done asynchronously to allow the Text's width
         * to be changed before re-calculating its preferred height.
         */
        if (text.isDisposed())
            return;
        Point size = text.computeSize(text.getSize().x - trimWidth, SWT.DEFAULT);
        if (expandItem1.getHeight() != size.y) {
            expandItem1.setHeight(size.y);
        }
    }));

    shell.open();
    /* set the item's initial height */
    Point size = text.computeSize(expandBar.getClientArea().width, SWT.DEFAULT);
    expandItem1.setHeight(size.y);
    expandItem1.setExpanded(true);

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