Example usage for org.eclipse.swt.widgets Display wake

List of usage examples for org.eclipse.swt.widgets Display wake

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Display wake.

Prototype

public void wake() 

Source Link

Document

Notifies the client to send a request in order to wake up a sleeping UI thread and to perform pending UI updates.

Usage

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

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 130");
    shell.setLayout(new GridLayout());
    final Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
    text.setLayoutData(new GridData(GridData.FILL_BOTH));
    final int[] nextId = new int[1];
    Button b = new Button(shell, SWT.PUSH);
    b.setText("invoke long running job");
    b.addSelectionListener(widgetSelectedAdapter(e -> {
        Runnable longJob = new Runnable() {
            boolean done = false;
            int id;

            @Override/* w  ww .  j  a  v  a  2  s. c om*/
            public void run() {
                Thread thread = new Thread(() -> {
                    id = nextId[0]++;
                    display.syncExec(() -> {
                        if (text.isDisposed())
                            return;
                        text.append("\nStart long running task " + id);
                    });
                    for (int i = 0; i < 100000; i++) {
                        if (display.isDisposed())
                            return;
                        System.out.println("do task that takes a long time in a separate thread " + id);
                    }
                    if (display.isDisposed())
                        return;
                    display.syncExec(() -> {
                        if (text.isDisposed())
                            return;
                        text.append("\nCompleted long running task " + id);
                    });
                    done = true;
                    display.wake();
                });
                thread.start();
                while (!done && !shell.isDisposed()) {
                    if (!display.readAndDispatch())
                        display.sleep();
                }
            }
        };
        BusyIndicator.showWhile(display, longJob);
    }));
    shell.setSize(250, 150);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:BusyCursorDisplay.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    final Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
    text.setLayoutData(new GridData(GridData.FILL_BOTH));
    final int[] nextId = new int[1];
    Button b = new Button(shell, SWT.PUSH);
    b.setText("invoke long running job");
    b.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            Runnable longJob = new Runnable() {
                boolean done = false;

                int id;

                public void run() {
                    Thread thread = new Thread(new Runnable() {
                        public void run() {
                            id = nextId[0]++;
                            display.syncExec(new Runnable() {
                                public void run() {
                                    if (text.isDisposed())
                                        return;
                                    text.append("\nStart long running task " + id);
                                }//from w ww  .  j  a  v a 2  s. c  o  m
                            });
                            for (int i = 0; i < 100000; i++) {
                                if (display.isDisposed())
                                    return;
                                System.out.println("do task that takes a long time in a separate thread " + id);
                            }
                            if (display.isDisposed())
                                return;
                            display.syncExec(new Runnable() {
                                public void run() {
                                    if (text.isDisposed())
                                        return;
                                    text.append("\nCompleted long running task " + id);
                                }
                            });
                            done = true;
                            display.wake();
                        }
                    });
                    thread.start();
                    while (!done && !shell.isDisposed()) {
                        if (!display.readAndDispatch())
                            display.sleep();
                    }
                }
            };
            BusyIndicator.showWhile(display, longJob);
        }
    });
    shell.setSize(250, 150);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}