Example usage for org.eclipse.swt.widgets Composite dispose

List of usage examples for org.eclipse.swt.widgets Composite dispose

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Composite dispose.

Prototype



public void dispose() 

Source Link

Document

Disposes of the operating system resources associated with the receiver and all its descendents.

Usage

From source file:PopupMenuComposite.java

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

    shell.setLayout(new GridLayout(2, true));

    final Composite composite1 = new Composite(shell, SWT.BORDER);
    composite1.setBackground(display.getSystemColor(SWT.COLOR_BLACK));

    final Composite composite2 = new Composite(shell, SWT.BORDER);
    composite2.setBackground(display.getSystemColor(SWT.COLOR_BLUE));

    Menu menu = new Menu(composite1);
    MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
    menuItem.setText("Popup menu");

    menuItem.addDisposeListener(new DisposeListener() {
        public void widgetDisposed(DisposeEvent e) {
            System.out.println("Menu item is disposed.");
        }// w  ww .j  ava2 s  .co m
    });

    menuItem.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            System.out.println("Dispsoing ...");
            composite2.setMenu(null);
            composite2.dispose();
        }
    });

    composite1.setMenu(menu);
    composite2.setMenu(menu);

    shell.pack();
    shell.open();

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            // If no more entries in event queue
            display.sleep();
        }
    }

    display.dispose();
}

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

public static void main(String[] args) {
    final Display display = new Display();
    final Color black = display.getSystemColor(SWT.COLOR_BLACK);
    Shell shell = new Shell(display);
    shell.setText("Snippet 111");
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.BORDER);
    for (int i = 0; i < 16; i++) {
        TreeItem itemI = new TreeItem(tree, SWT.NONE);
        itemI.setText("Item " + i);
        for (int j = 0; j < 16; j++) {
            TreeItem itemJ = new TreeItem(itemI, SWT.NONE);
            itemJ.setText("Item " + j);
        }/*from   w  w w .  j a  v  a 2 s . c  o m*/
    }
    final TreeItem[] lastItem = new TreeItem[1];
    final TreeEditor editor = new TreeEditor(tree);
    tree.addListener(SWT.Selection, event -> {
        final TreeItem item = (TreeItem) event.item;
        if (item != null && item == lastItem[0]) {
            boolean showBorder = true;
            final Composite composite = new Composite(tree, SWT.NONE);
            if (showBorder)
                composite.setBackground(black);
            final Text text = new Text(composite, SWT.NONE);
            final int inset = showBorder ? 1 : 0;
            composite.addListener(SWT.Resize, e1 -> {
                Rectangle rect1 = composite.getClientArea();
                text.setBounds(rect1.x + inset, rect1.y + inset, rect1.width - inset * 2,
                        rect1.height - inset * 2);
            });
            Listener textListener = e2 -> {
                switch (e2.type) {
                case SWT.FocusOut:
                    item.setText(text.getText());
                    composite.dispose();
                    break;
                case SWT.Verify:
                    String newText = text.getText();
                    String leftText = newText.substring(0, e2.start);
                    String rightText = newText.substring(e2.end, newText.length());
                    GC gc = new GC(text);
                    Point size = gc.textExtent(leftText + e2.text + rightText);
                    gc.dispose();
                    size = text.computeSize(size.x, SWT.DEFAULT);
                    editor.horizontalAlignment = SWT.LEFT;
                    Rectangle itemRect = item.getBounds(), rect2 = tree.getClientArea();
                    editor.minimumWidth = Math.max(size.x, itemRect.width) + inset * 2;
                    int left = itemRect.x, right = rect2.x + rect2.width;
                    editor.minimumWidth = Math.min(editor.minimumWidth, right - left);
                    editor.minimumHeight = size.y + inset * 2;
                    editor.layout();
                    break;
                case SWT.Traverse:
                    switch (e2.detail) {
                    case SWT.TRAVERSE_RETURN:
                        item.setText(text.getText());
                        //FALL THROUGH
                    case SWT.TRAVERSE_ESCAPE:
                        composite.dispose();
                        e2.doit = false;
                    }
                    break;
                }
            };
            text.addListener(SWT.FocusOut, textListener);
            text.addListener(SWT.Traverse, textListener);
            text.addListener(SWT.Verify, textListener);
            editor.setEditor(composite, item);
            text.setText(item.getText());
            text.selectAll();
            text.setFocus();
        }
        lastItem[0] = item;
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:TreeCellEditor.java

public static void main(String[] args) {
    final Display display = new Display();
    final Color black = display.getSystemColor(SWT.COLOR_BLACK);
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.BORDER);
    for (int i = 0; i < 16; i++) {
        TreeItem itemI = new TreeItem(tree, SWT.NONE);
        itemI.setText("Item " + i);
        for (int j = 0; j < 16; j++) {
            TreeItem itemJ = new TreeItem(itemI, SWT.NONE);
            itemJ.setText("Item " + j);
        }/*from w  w w  .  j  av  a  2  s.co  m*/
    }
    final TreeItem[] lastItem = new TreeItem[1];
    final TreeEditor editor = new TreeEditor(tree);
    tree.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            final TreeItem item = (TreeItem) event.item;
            if (item != null && item == lastItem[0]) {
                boolean isCarbon = SWT.getPlatform().equals("carbon");
                final Composite composite = new Composite(tree, SWT.NONE);
                if (!isCarbon)
                    composite.setBackground(black);
                final Text text = new Text(composite, SWT.NONE);
                final int inset = isCarbon ? 0 : 1;
                composite.addListener(SWT.Resize, new Listener() {
                    public void handleEvent(Event e) {
                        Rectangle rect = composite.getClientArea();
                        text.setBounds(rect.x + inset, rect.y + inset, rect.width - inset * 2,
                                rect.height - inset * 2);
                    }
                });
                Listener textListener = new Listener() {
                    public void handleEvent(final Event e) {
                        switch (e.type) {
                        case SWT.FocusOut:
                            item.setText(text.getText());
                            composite.dispose();
                            break;
                        case SWT.Verify:
                            String newText = text.getText();
                            String leftText = newText.substring(0, e.start);
                            String rightText = newText.substring(e.end, newText.length());
                            GC gc = new GC(text);
                            Point size = gc.textExtent(leftText + e.text + rightText);
                            gc.dispose();
                            size = text.computeSize(size.x, SWT.DEFAULT);
                            editor.horizontalAlignment = SWT.LEFT;
                            Rectangle itemRect = item.getBounds(), rect = tree.getClientArea();
                            editor.minimumWidth = Math.max(size.x, itemRect.width) + inset * 2;
                            int left = itemRect.x, right = rect.x + rect.width;
                            editor.minimumWidth = Math.min(editor.minimumWidth, right - left);
                            editor.minimumHeight = size.y + inset * 2;
                            editor.layout();
                            break;
                        case SWT.Traverse:
                            switch (e.detail) {
                            case SWT.TRAVERSE_RETURN:
                                item.setText(text.getText());
                                // FALL THROUGH
                            case SWT.TRAVERSE_ESCAPE:
                                composite.dispose();
                                e.doit = false;
                            }
                            break;
                        }
                    }
                };
                text.addListener(SWT.FocusOut, textListener);
                text.addListener(SWT.Traverse, textListener);
                text.addListener(SWT.Verify, textListener);
                editor.setEditor(composite, item);
                text.setText(item.getText());
                text.selectAll();
                text.setFocus();
            }
            lastItem[0] = item;
        }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:SharedMenu.java

private void menu() {
    shell.setLayout(new GridLayout(2, true));

    final Composite composite1 = new Composite(shell, SWT.BORDER);
    composite1.setBackground(display.getSystemColor(SWT.COLOR_BLACK));

    final Composite composite2 = new Composite(shell, SWT.BORDER);
    composite2.setBackground(display.getSystemColor(SWT.COLOR_BLUE));

    Menu menu = new Menu(composite1);
    MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
    menuItem.setText("Popup menu");

    menuItem.addDisposeListener(new DisposeListener() {
        public void widgetDisposed(DisposeEvent e) {
            System.out.println("Menu item is disposed.");
        }/*from   ww w.j a  v  a2  s  .  c  om*/
    });

    menuItem.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            System.out.println("Dispsoing ...");
            //composite2.setMenu(null);
            composite2.dispose();
        }
    });

    composite1.setMenu(menu);
    composite2.setMenu(menu);
}