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

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

Introduction

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

Prototype

public Shell[] getShells() 

Source Link

Document

Returns a (possibly empty) array containing all shells which have not been disposed and have the receiver as their display.

Usage

From source file:org.eclipse.swt.examples.layoutexample.LayoutExample.java

/**
 * Invokes as a standalone program.// w w  w . j  a  v a  2s  . c om
 */
public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    new LayoutExample(shell);
    shell.setText(getResourceString("window.title"));
    shell.addShellListener(ShellListener.shellClosedAdapter(e -> {
        Shell[] shells = display.getShells();
        for (Shell currentShell : shells) {
            if (currentShell != shell)
                currentShell.close();
        }
    }));
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
}

From source file:LayoutExample.java

/**
 * Invokes as a standalone program.//from w ww. ja  va  2s . c  o  m
 */
public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    new LayoutExample(shell);
    shell.setText(getResourceString("window.title"));
    shell.addShellListener(new ShellAdapter() {
        public void shellClosed(ShellEvent e) {
            Shell[] shells = display.getShells();
            for (int i = 0; i < shells.length; i++) {
                if (shells[i] != shell)
                    shells[i].close();
            }
        }
    });
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
}

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

static void createMenuBar(Shell s) {
    Menu bar = Display.getCurrent().getMenuBar();
    boolean hasAppMenuBar = (bar != null);
    if (bar == null) {
        bar = new Menu(s, SWT.BAR);
    }//ww  w . j  a  va 2s . c om

    // Populate the menu bar once if this is a screen menu bar.
    // Otherwise, we need to make a new menu bar for each shell.
    if (!createdScreenBar || !hasAppMenuBar) {
        MenuItem item = new MenuItem(bar, SWT.CASCADE);
        item.setText("File");
        Menu menu = new Menu(item);
        item.setMenu(menu);
        menu.addMenuListener(new MenuListener() {

            @Override
            public void menuHidden(MenuEvent e) {
                System.out.println("Menu closed: " + e);
            }

            @Override
            public void menuShown(MenuEvent e) {
                System.out.println("Menu open: " + e);
            }

        });
        MenuItem newWindow = new MenuItem(menu, SWT.PUSH);
        newWindow.setText("New Window");
        newWindow.setAccelerator(SWT.MOD1 | 'N');
        newWindow.addListener(SWT.Selection, event -> {
            Shell s1 = createShell();
            s1.open();
        });
        if (!SWT.getPlatform().equals("cocoa")) {
            MenuItem exit = new MenuItem(menu, SWT.PUSH);
            exit.setText("Exit");
            exit.addListener(SWT.Selection, event -> {
                Display d = Display.getCurrent();
                Shell[] shells = d.getShells();
                for (Shell shell : shells) {
                    shell.close();
                }
            });
        }
        if (!hasAppMenuBar)
            s.setMenuBar(bar);
        createdScreenBar = true;
    }
}

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);//w  ww  . j ava2  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.// ww w  .  j a  v  a2 s .  c om
 *
 * @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();
}