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

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

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Button 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:TreeEditorButton.java

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Text Tree Editor");
    shell.setLayout(new FillLayout());

    final Tree tree = new Tree(shell, SWT.SINGLE);
    for (int i = 0; i < 3; i++) {
        TreeItem iItem = new TreeItem(tree, SWT.NONE);
        iItem.setText("Item " + (i + 1));
        for (int j = 0; j < 3; j++) {
            TreeItem jItem = new TreeItem(iItem, SWT.NONE);
            jItem.setText("Sub Item " + (j + 1));
            for (int k = 0; k < 3; k++) {
                new TreeItem(jItem, SWT.NONE).setText("Sub Sub Item " + (k + 1));
            }/*from   w ww  . ja v  a  2 s.  c  o  m*/
            jItem.setExpanded(true);
        }
        iItem.setExpanded(true);
    }

    final TreeEditor editor = new TreeEditor(tree);
    editor.horizontalAlignment = SWT.LEFT;
    editor.grabHorizontal = true;

    tree.addMouseListener(new MouseAdapter() {
        public void mouseDown(MouseEvent event) {
            final TreeItem item = tree.getSelection()[0];

            final Button bn = new Button(tree, SWT.NONE);
            bn.setText("click");
            bn.setFocus();

            bn.addSelectionListener(new SelectionListener() {

                public void widgetSelected(SelectionEvent arg0) {
                    int style = SWT.ICON_QUESTION | SWT.YES | SWT.NO;

                    MessageBox messageBox = new MessageBox(shell, style);
                    messageBox.setMessage("Message");
                    int rc = messageBox.open();

                    item.setText(rc + "");
                    bn.dispose();
                }

                public void widgetDefaultSelected(SelectionEvent arg0) {
                }
            });

            editor.setEditor(bn, item);
        }
    });

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

From source file:BugTracker.java

public BugTracker() {
    GridLayout gridLayout = new GridLayout();
    shell.setLayout(gridLayout);/* w  w w.  ja va  2s.com*/
    shell.setText("Bug Tracking System");

    // Action.
    Action actionAddNew = new Action("New bug") {
        public void run() {
            // Append.
            TableItem item = new TableItem(table, SWT.NULL);
            item.setImage(bugIcon);
            table.select(table.getItemCount() - 1);
        }
    };

    Action actionDelete = new Action("Delete selected") {
        public void run() {
            int index = table.getSelectionIndex();
            if (index < 0) {
                System.out.println("Please select an item first. ");
                return;
            }
            MessageBox messageBox = new MessageBox(shell, SWT.YES | SWT.NO);
            messageBox.setText("Confirmation");
            messageBox.setMessage("Are you sure to remove the bug with id #" + table.getItem(index).getText(0));
            if (messageBox.open() == SWT.YES) {
                table.remove(index);
            }
        }
    };

    Action actionSave = new Action("Save") {
        public void run() {
            saveBugs();
        }
    };

    ToolBar toolBar = new ToolBar(shell, SWT.RIGHT | SWT.FLAT);

    ToolBarManager manager = new ToolBarManager(toolBar);
    manager.add(actionAddNew);
    manager.add(actionDelete);
    manager.add(new Separator());
    manager.add(actionSave);
    manager.update(true);

    table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
    table.setLayoutData(new GridData(GridData.FILL_BOTH));

    table.setLinesVisible(true);
    table.setHeaderVisible(true);

    TableColumn tcID = new TableColumn(table, SWT.LEFT);
    tcID.setText("ID");

    TableColumn tcSummary = new TableColumn(table, SWT.NULL);
    tcSummary.setText("Summary");

    TableColumn tcAssignedTo = new TableColumn(table, SWT.NULL);
    tcAssignedTo.setText("Assigned to");

    TableColumn tcSolved = new TableColumn(table, SWT.NULL);
    tcSolved.setText("Solved");

    tcID.setWidth(60);
    tcSummary.setWidth(200);
    tcAssignedTo.setWidth(80);
    tcSolved.setWidth(50);

    table.pack();

    // Table editor.
    final TableEditor editor = new TableEditor(table);

    table.addListener(SWT.MouseDown, new Listener() {
        public void handleEvent(Event event) {
            // Locate the cell position.
            Point point = new Point(event.x, event.y);
            final TableItem item = table.getItem(point);
            if (item == null)
                return;
            int column = -1;
            for (int i = 0; i < table.getColumnCount(); i++) {
                Rectangle rect = item.getBounds(i);
                if (rect.contains(point))
                    column = i;
            }
            if (column != 3)
                return;

            // Cell position located, now open the table editor.

            final Button button = new Button(table, SWT.CHECK);
            button.setSelection(item.getText(column).equalsIgnoreCase("YES"));

            editor.horizontalAlignment = SWT.LEFT;
            editor.grabHorizontal = true;

            editor.setEditor(button, item, column);

            final int selectedColumn = column;
            Listener buttonListener = new Listener() {
                public void handleEvent(final Event e) {
                    switch (e.type) {
                    case SWT.FocusOut:
                        item.setText(selectedColumn, button.getSelection() ? "YES" : "NO");
                        button.dispose();
                        break;
                    case SWT.Traverse:
                        switch (e.detail) {
                        case SWT.TRAVERSE_RETURN:
                            item.setText(selectedColumn, button.getSelection() ? "YES" : "NO");
                            //FALL THROUGH
                        case SWT.TRAVERSE_ESCAPE:
                            button.dispose();
                            e.doit = false;
                        }
                        break;
                    }
                }
            };

            button.addListener(SWT.FocusOut, buttonListener);
            button.addListener(SWT.Traverse, buttonListener);

            button.setFocus();

        }
    });

    table.addListener(SWT.MouseDown, new Listener() {
        public void handleEvent(Event event) {
            // Locate the cell position.
            Point point = new Point(event.x, event.y);
            final TableItem item = table.getItem(point);
            if (item == null)
                return;
            int column = -1;
            for (int i = 0; i < table.getColumnCount(); i++) {
                Rectangle rect = item.getBounds(i);
                if (rect.contains(point))
                    column = i;
            }
            if (column < 0 || column >= 3)
                return;

            // Cell position located, now open the table editor.

            final Text text = new Text(table, SWT.NONE);
            text.setText(item.getText(column));

            editor.horizontalAlignment = SWT.LEFT;
            editor.grabHorizontal = true;

            editor.setEditor(text, item, column);

            final int selectedColumn = column;
            Listener textListener = new Listener() {
                public void handleEvent(final Event e) {
                    switch (e.type) {
                    case SWT.FocusOut:
                        item.setText(selectedColumn, text.getText());
                        text.dispose();
                        break;
                    case SWT.Traverse:
                        switch (e.detail) {
                        case SWT.TRAVERSE_RETURN:
                            item.setText(selectedColumn, text.getText());
                            //FALL THROUGH
                        case SWT.TRAVERSE_ESCAPE:
                            text.dispose();
                            e.doit = false;
                        }
                        break;
                    }
                }
            };

            text.addListener(SWT.FocusOut, textListener);
            text.addListener(SWT.Traverse, textListener);

            text.setFocus();
        }

    });

    loadBugs();

    table.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            System.out.println("Selected: " + table.getSelection()[0]);
        }
    });

    Listener sortListener = new Listener() {
        public void handleEvent(Event event) {
            if (!(event.widget instanceof TableColumn))
                return;
            TableColumn tc = (TableColumn) event.widget;
            sortTable(table, table.indexOf(tc));
            System.out.println("The table is sorted by column #" + table.indexOf(tc));
        }
    };

    for (int i = 0; i < table.getColumnCount(); i++)
        ((TableColumn) table.getColumn(i)).addListener(SWT.Selection, sortListener);

    shell.pack();
    shell.open();
    //textUser.forceFocus();

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

    display.dispose();
}