Example usage for org.eclipse.jface.internal.databinding.provisional.swt MenuUpdater MenuUpdater

List of usage examples for org.eclipse.jface.internal.databinding.provisional.swt MenuUpdater MenuUpdater

Introduction

In this page you can find the example usage for org.eclipse.jface.internal.databinding.provisional.swt MenuUpdater MenuUpdater.

Prototype

public MenuUpdater(Menu toUpdate) 

Source Link

Document

Creates an updator for the given menu.

Usage

From source file:org.eclipse.jface.examples.databinding.snippets.Snippet005MenuUpdater.java

License:Open Source License

public static void main(String[] args) {
    final Display display = new Display();
    Realm.runWithDefault(DisplayRealm.getRealm(display), () -> {
        Shell shell = new Shell(display);

        final WritableList<String> menuItemStrings = new WritableList<>();
        display.asyncExec(new Runnable() {
            @Override/*w w w  .ja  va2  s .c om*/
            public void run() {
                System.out.println("adding item");
                menuItemStrings.add(new Date().toString());
                display.timerExec(5000, this);
            }
        });

        Menu bar = new Menu(shell, SWT.BAR);
        shell.setMenuBar(bar);
        MenuItem fileItem = new MenuItem(bar, SWT.CASCADE);
        fileItem.setText("&Test Menu");
        final Menu submenu = new Menu(shell, SWT.DROP_DOWN);
        fileItem.setMenu(submenu);
        new MenuUpdater(submenu) {
            @Override
            protected void updateMenu() {
                System.out.println("updating menu");
                MenuItem[] items = submenu.getItems();
                int itemIndex = 0;
                for (String s : menuItemStrings) {
                    MenuItem item;
                    if (itemIndex < items.length) {
                        item = items[itemIndex++];
                    } else {
                        item = new MenuItem(submenu, SWT.NONE);
                    }
                    item.setText(s);
                }
                while (itemIndex < items.length) {
                    items[itemIndex++].dispose();
                }
            }
        };

        shell.open();

        // The SWT event loop
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    });
    display.dispose();
}