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

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

Introduction

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

Prototype

public boolean isDisposed() 

Source Link

Usage

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

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

    Shell shell = createShell();//from  w  w w.  j a v  a  2s .c o m
    shell.open();

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

From source file:ProgressBarUpdateAnotherThread.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    final ProgressBar bar = new ProgressBar(shell, SWT.SMOOTH);
    bar.setBounds(10, 10, 200, 32);/*  w  ww.  j ava2s . co m*/
    shell.open();
    final int maximum = bar.getMaximum();
    new Thread() {
        public void run() {
            for (final int[] i = new int[1]; i[0] <= maximum; i[0]++) {
                try {
                    Thread.sleep(100);
                } catch (Throwable th) {
                }
                if (display.isDisposed())
                    return;
                display.asyncExec(new Runnable() {
                    public void run() {
                        if (bar.isDisposed())
                            return;
                        bar.setSelection(i[0]);
                    }
                });
            }
        }
    }.start();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 56");
    final ProgressBar bar = new ProgressBar(shell, SWT.SMOOTH);
    Rectangle clientArea = shell.getClientArea();
    bar.setBounds(clientArea.x, clientArea.y, 200, 32);
    shell.open();//from w  w  w . j a  v a2s  .com
    final int maximum = bar.getMaximum();
    new Thread() {
        @Override
        public void run() {
            for (final int[] i = new int[1]; i[0] <= maximum; i[0]++) {
                try {
                    Thread.sleep(100);
                } catch (Throwable th) {
                }
                if (display.isDisposed())
                    return;
                display.asyncExec(() -> {
                    if (bar.isDisposed())
                        return;
                    bar.setSelection(i[0]);
                });
            }
        }
    }.start();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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/*from   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: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");
    }/*  w ww.  ja  va 2 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: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);
                                }//w w w .  jav a2 s  .c  om
                            });
                            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();
}

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

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 320");
    shell.setLayout(new GridLayout());
    final Text text = new Text(shell, SWT.SINGLE | SWT.BORDER);
    text.setLayoutData(new GridData(150, SWT.DEFAULT));
    shell.pack();/*from w w w . j  a v a2s. c om*/
    shell.open();

    final Shell popupShell = new Shell(display, SWT.ON_TOP);
    popupShell.setLayout(new FillLayout());
    final Table table = new Table(popupShell, SWT.SINGLE);
    for (int i = 0; i < 5; i++) {
        new TableItem(table, SWT.NONE);
    }

    text.addListener(SWT.KeyDown, event -> {
        switch (event.keyCode) {
        case SWT.ARROW_DOWN:
            int index = (table.getSelectionIndex() + 1) % table.getItemCount();
            table.setSelection(index);
            event.doit = false;
            break;
        case SWT.ARROW_UP:
            index = table.getSelectionIndex() - 1;
            if (index < 0)
                index = table.getItemCount() - 1;
            table.setSelection(index);
            event.doit = false;
            break;
        case SWT.CR:
            if (popupShell.isVisible() && table.getSelectionIndex() != -1) {
                text.setText(table.getSelection()[0].getText());
                popupShell.setVisible(false);
            }
            break;
        case SWT.ESC:
            popupShell.setVisible(false);
            break;
        }
    });
    text.addListener(SWT.Modify, event -> {
        String string = text.getText();
        if (string.length() == 0) {
            popupShell.setVisible(false);
        } else {
            TableItem[] items = table.getItems();
            for (int i = 0; i < items.length; i++) {
                items[i].setText(string + '-' + i);
            }
            Rectangle textBounds = display.map(shell, null, text.getBounds());
            popupShell.setBounds(textBounds.x, textBounds.y + textBounds.height, textBounds.width, 150);
            popupShell.setVisible(true);
        }
    });

    table.addListener(SWT.DefaultSelection, event -> {
        text.setText(table.getSelection()[0].getText());
        popupShell.setVisible(false);
    });
    table.addListener(SWT.KeyDown, event -> {
        if (event.keyCode == SWT.ESC) {
            popupShell.setVisible(false);
        }
    });

    Listener focusOutListener = event -> display.asyncExec(() -> {
        if (display.isDisposed())
            return;
        Control control = display.getFocusControl();
        if (control == null || (control != text && control != table)) {
            popupShell.setVisible(false);
        }
    });
    table.addListener(SWT.FocusOut, focusOutListener);
    text.addListener(SWT.FocusOut, focusOutListener);

    shell.addListener(SWT.Move, event -> popupShell.setVisible(false));

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

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

static Shell createShell() {
    final Shell shell = new Shell(SWT.SHELL_TRIM);
    shell.setText("Snippet 348");
    createMenuBar(shell);/*from  w w w . j  av a2 s  . c  o  m*/

    shell.addDisposeListener(e -> {
        Display d = Display.getCurrent();
        Menu bar = d.getMenuBar();
        boolean hasAppMenuBar = (bar != null);
        if (!hasAppMenuBar) {
            shell.getMenuBar().dispose();
            Shell[] shells = d.getShells();
            if ((shells.length == 1) && (shells[0] == shell)) {
                if (!d.isDisposed())
                    d.dispose();
            }
        }
    });

    return shell;
}

From source file:cookxml.cookswt.util.SwtUtils.java

/**
 * Open all the shells inside the Display object and dispose the Display
 * after all shells are disposed.//w  w w. j  a v  a 2 s.  co m
 *
 * @param display the Display object.
 */
public static void showDisplay(Display display) {
    // open shells for display
    Shell[] shells = display.getShells();
    for (int i = 0; i < shells.length; ++i) {
        if (!shells[i].isDisposed() && !shells[i].isVisible())
            shells[i].open();
    }

    // exit after all shells are disposed
    while (!display.isDisposed()) {
        shells = display.getShells();
        boolean canExit = true;
        for (int i = 0; i < shells.length; ++i) {
            if (!shells[i].isDisposed()) {
                canExit = false;
                break;
            }
        }
        if (canExit)
            break;
        if (!display.readAndDispatch())
            display.sleep();
    }
    if (!display.isDisposed())
        display.dispose();
}