Example usage for org.eclipse.swt.widgets Tree Tree

List of usage examples for org.eclipse.swt.widgets Tree Tree

Introduction

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

Prototype

public Tree(Composite parent, int style) 

Source Link

Document

Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.

Usage

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 360");
    shell.setLayout(new GridLayout());

    // create a a tree with 3 columns and fill with data
    final Tree tree = new Tree(shell, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
    tree.setLayoutData(new GridData(GridData.FILL_BOTH));
    tree.setHeaderVisible(true);//w w w .  ja va 2 s .  com
    TreeColumn column1 = new TreeColumn(tree, SWT.NONE);
    TreeColumn column2 = new TreeColumn(tree, SWT.NONE);
    TreeColumn column3 = new TreeColumn(tree, SWT.NONE);
    for (int i = 0; i < 9; i++) {
        TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText(new String[] { "root " + i + " 0", "root " + i + " 1", "root " + i + " 2" });
        for (int j = 0; j < 9; j++) {
            TreeItem item2 = new TreeItem(item, SWT.NONE);
            item2.setText(new String[] { "child " + j + " 0", "child " + j + " 1", "child " + j + " 2" });
            TreeItem current = item2;
            for (int k = 0; k < 5; k++) {
                TreeItem item3 = new TreeItem(current, SWT.NONE);
                item3.setText(new String[] { "descendent " + k + " 0", "descendent " + k + " 1",
                        "descendent " + k + " 2" });
                current = item3;
            }
        }
    }
    column1.setWidth(200);
    column2.setWidth(100);
    column3.setWidth(100);

    // create a TreeCursor to navigate around the tree
    final TreeCursor cursor = new TreeCursor(tree, SWT.NONE);
    // create an editor to edit the cell when the user hits "ENTER"
    // while over a cell in the tree
    final ControlEditor editor = new ControlEditor(cursor);
    editor.grabHorizontal = true;
    editor.grabVertical = true;

    cursor.addSelectionListener(new SelectionAdapter() {
        // when the TreeEditor is over a cell, select the corresponding row
        // in the tree
        @Override
        public void widgetSelected(SelectionEvent e) {
            tree.setSelection(new TreeItem[] { cursor.getRow() });
        }

        // when the user hits "ENTER" in the TreeCursor, pop up a text
        // editor so that they can change the text of the cell
        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
            final Text text = new Text(cursor, SWT.NONE);
            TreeItem row = cursor.getRow();
            int column = cursor.getColumn();
            text.setText(row.getText(column));
            text.addKeyListener(keyPressedAdapter(event -> {
                // close the text editor and copy the data over
                // when the user hits "ENTER"
                if (event.character == SWT.CR) {
                    TreeItem localRow = cursor.getRow();
                    int localColumn = cursor.getColumn();
                    localRow.setText(localColumn, text.getText());
                    text.dispose();
                }
                // close the text editor when the user hits "ESC"
                if (event.character == SWT.ESC) {
                    text.dispose();
                }
            }));
            editor.setEditor(text);
            text.setFocus();
        }
    });
    // Hide the TreeCursor when the user hits the "MOD1" or "MOD2" key.
    // This allows the user to select multiple items in the tree.
    cursor.addKeyListener(keyPressedAdapter(e -> {
        if (e.keyCode == SWT.MOD1 || e.keyCode == SWT.MOD2 || (e.stateMask & SWT.MOD1) != 0
                || (e.stateMask & SWT.MOD2) != 0) {
            cursor.setVisible(false);
        }
    }));
    // Show the TreeCursor when the user releases the "MOD2" or "MOD1" key.
    // This signals the end of the multiple selection task.
    tree.addKeyListener(new KeyListener() {
        @Override
        public void keyReleased(KeyEvent e) {
            if (e.keyCode == SWT.MOD1 && (e.stateMask & SWT.MOD2) != 0)
                return;
            if (e.keyCode == SWT.MOD2 && (e.stateMask & SWT.MOD1) != 0)
                return;
            if (e.keyCode != SWT.MOD1 && (e.stateMask & SWT.MOD1) != 0)
                return;
            if (e.keyCode != SWT.MOD2 && (e.stateMask & SWT.MOD2) != 0)
                return;

            TreeItem[] selection = tree.getSelection();
            TreeItem row = (selection.length == 0) ? tree.getItem(tree.indexOf(tree.getTopItem()))
                    : selection[0];
            tree.showItem(row);
            cursor.setSelection(row, cursor.getColumn());
            cursor.setVisible(true);
            cursor.setFocus();
        }

        @Override
        public void keyPressed(KeyEvent e) {
            switch (e.keyCode) {
            case SWT.ARROW_LEFT:
            case SWT.ARROW_RIGHT: {
                if ((e.stateMask & SWT.MOD1) != 0) {
                    TreeItem[] selection = tree.getSelection();
                    if (selection.length > 0) {
                        selection[0].setExpanded(e.keyCode == SWT.ARROW_RIGHT);
                    }
                    break;
                }
            }
            }
        }
    });

    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);
        }//  w ww . j a v  a2  s.  c om
    }
    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:DragTreeLeaf.java

public static void main(String[] args) {

    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.BORDER);
    for (int i = 0; i < 3; i++) {
        TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText("item " + i);
        for (int j = 0; j < 3; j++) {
            TreeItem subItem = new TreeItem(item, SWT.NONE);
            subItem.setText("item " + i + " " + j);
            for (int k = 0; k < 3; k++) {
                TreeItem subsubItem = new TreeItem(subItem, SWT.NONE);
                subsubItem.setText("item " + i + " " + j + " " + k);
            }/*from   ww w  .  ja va  2s .  c o m*/
        }
    }

    Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
    int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK;

    final DragSource source = new DragSource(tree, operations);
    source.setTransfer(types);
    final TreeItem[] dragSourceItem = new TreeItem[1];
    source.addDragListener(new DragSourceListener() {
        public void dragStart(DragSourceEvent event) {
            TreeItem[] selection = tree.getSelection();
            if (selection.length > 0 && selection[0].getItemCount() == 0) {
                event.doit = true;
                dragSourceItem[0] = selection[0];
            } else {
                event.doit = false;
            }
        };

        public void dragSetData(DragSourceEvent event) {
            event.data = dragSourceItem[0].getText();
        }

        public void dragFinished(DragSourceEvent event) {
            if (event.detail == DND.DROP_MOVE)
                dragSourceItem[0].dispose();
            dragSourceItem[0] = null;
        }
    });

    DropTarget target = new DropTarget(tree, operations);
    target.setTransfer(types);
    target.addDropListener(new DropTargetAdapter() {
        public void dragOver(DropTargetEvent event) {
            event.feedback = DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL;
            if (event.item != null) {
                TreeItem item = (TreeItem) event.item;
                Point pt = display.map(null, tree, event.x, event.y);
                Rectangle bounds = item.getBounds();
                if (pt.y < bounds.y + bounds.height / 3) {
                    event.feedback |= DND.FEEDBACK_INSERT_BEFORE;
                } else if (pt.y > bounds.y + 2 * bounds.height / 3) {
                    event.feedback |= DND.FEEDBACK_INSERT_AFTER;
                } else {
                    event.feedback |= DND.FEEDBACK_SELECT;
                }
            }
        }

        public void drop(DropTargetEvent event) {
            if (event.data == null) {
                event.detail = DND.DROP_NONE;
                return;
            }
            String text = (String) event.data;
            if (event.item == null) {
                TreeItem item = new TreeItem(tree, SWT.NONE);
                item.setText(text);
            } else {
                TreeItem item = (TreeItem) event.item;
                Point pt = display.map(null, tree, event.x, event.y);
                Rectangle bounds = item.getBounds();
                TreeItem parent = item.getParentItem();
                if (parent != null) {
                    TreeItem[] items = parent.getItems();
                    int index = 0;
                    for (int i = 0; i < items.length; i++) {
                        if (items[i] == item) {
                            index = i;
                            break;
                        }
                    }
                    if (pt.y < bounds.y + bounds.height / 3) {
                        TreeItem newItem = new TreeItem(parent, SWT.NONE, index);
                        newItem.setText(text);
                    } else if (pt.y > bounds.y + 2 * bounds.height / 3) {
                        TreeItem newItem = new TreeItem(parent, SWT.NONE, index + 1);
                        newItem.setText(text);
                    } else {
                        TreeItem newItem = new TreeItem(item, SWT.NONE);
                        newItem.setText(text);
                    }

                } else {
                    TreeItem[] items = tree.getItems();
                    int index = 0;
                    for (int i = 0; i < items.length; i++) {
                        if (items[i] == item) {
                            index = i;
                            break;
                        }
                    }
                    if (pt.y < bounds.y + bounds.height / 3) {
                        TreeItem newItem = new TreeItem(tree, SWT.NONE, index);
                        newItem.setText(text);
                    } else if (pt.y > bounds.y + 2 * bounds.height / 3) {
                        TreeItem newItem = new TreeItem(tree, SWT.NONE, index + 1);
                        newItem.setText(text);
                    } else {
                        TreeItem newItem = new TreeItem(item, SWT.NONE);
                        newItem.setText(text);
                    }
                }

            }
        }
    });

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

From source file:SystemFileTree.java

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

    RGB color = shell.getBackground().getRGB();
    Label separator1 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    Label locationLb = new Label(shell, SWT.NONE);
    locationLb.setText("Location:");
    Composite locationComp = new Composite(shell, SWT.EMBEDDED);
    Label separator2 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    final Composite comp = new Composite(shell, SWT.NONE);
    final Tree fileTree = new Tree(comp, SWT.SINGLE | SWT.BORDER);
    Sash sash = new Sash(comp, SWT.VERTICAL);
    Composite tableComp = new Composite(comp, SWT.EMBEDDED);
    Label separator3 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    Composite statusComp = new Composite(shell, SWT.EMBEDDED);

    java.awt.Frame locationFrame = SWT_AWT.new_Frame(locationComp);
    final java.awt.TextField locationText = new java.awt.TextField();
    locationFrame.add(locationText);/*from   w  w w  .  j  ava2s .co  m*/

    java.awt.Frame statusFrame = SWT_AWT.new_Frame(statusComp);
    statusFrame.setBackground(new java.awt.Color(color.red, color.green, color.blue));
    final java.awt.Label statusLabel = new java.awt.Label();
    statusFrame.add(statusLabel);
    statusLabel.setText("Select a file");

    sash.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            if (e.detail == SWT.DRAG)
                return;
            GridData data = (GridData) fileTree.getLayoutData();
            Rectangle trim = fileTree.computeTrim(0, 0, 0, 0);
            data.widthHint = e.x - trim.width;
            comp.layout();
        }
    });

    File[] roots = File.listRoots();
    for (int i = 0; i < roots.length; i++) {
        File file = roots[i];
        TreeItem treeItem = new TreeItem(fileTree, SWT.NONE);
        treeItem.setText(file.getAbsolutePath());
        treeItem.setData(file);
        new TreeItem(treeItem, SWT.NONE);
    }
    fileTree.addListener(SWT.Expand, new Listener() {
        public void handleEvent(Event e) {
            TreeItem item = (TreeItem) e.item;
            if (item == null)
                return;
            if (item.getItemCount() == 1) {
                TreeItem firstItem = item.getItems()[0];
                if (firstItem.getData() != null)
                    return;
                firstItem.dispose();
            } else {
                return;
            }
            File root = (File) item.getData();
            File[] files = root.listFiles();
            if (files == null)
                return;
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                if (file.isDirectory()) {
                    TreeItem treeItem = new TreeItem(item, SWT.NONE);
                    treeItem.setText(file.getName());
                    treeItem.setData(file);
                    new TreeItem(treeItem, SWT.NONE);
                }
            }
        }
    });
    fileTree.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            TreeItem item = (TreeItem) e.item;
            if (item == null)
                return;
            final File root = (File) item.getData();
            statusLabel.setText(root.getAbsolutePath());
            locationText.setText(root.getAbsolutePath());
        }
    });

    GridLayout layout = new GridLayout(4, false);
    layout.marginWidth = layout.marginHeight = 0;
    layout.horizontalSpacing = layout.verticalSpacing = 1;
    shell.setLayout(layout);
    GridData data;
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    separator1.setLayoutData(data);
    data = new GridData();
    data.horizontalSpan = 1;
    data.horizontalIndent = 10;
    locationLb.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 2;
    data.heightHint = locationText.getPreferredSize().height;
    locationComp.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 1;
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    separator2.setLayoutData(data);
    data = new GridData(GridData.FILL_BOTH);
    data.horizontalSpan = 4;
    comp.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    separator3.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    data.heightHint = statusLabel.getPreferredSize().height;
    statusComp.setLayoutData(data);

    layout = new GridLayout(3, false);
    layout.marginWidth = layout.marginHeight = 0;
    layout.horizontalSpacing = layout.verticalSpacing = 1;
    comp.setLayout(layout);
    data = new GridData(GridData.FILL_VERTICAL);
    data.widthHint = 200;
    fileTree.setLayoutData(data);
    data = new GridData(GridData.FILL_VERTICAL);
    sash.setLayoutData(data);
    data = new GridData(GridData.FILL_BOTH);
    tableComp.setLayoutData(data);

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

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 274");
    Tree tree = new Tree(shell, SWT.BORDER | SWT.CHECK);
    tree.addListener(SWT.Selection, event -> {
        if (event.detail == SWT.CHECK) {
            TreeItem item = (TreeItem) event.item;
            boolean checked = item.getChecked();
            checkItems(item, checked);/* w ww  .  j a  v  a2 s . c om*/
            checkPath(item.getParentItem(), checked, false);
        }
    });
    for (int i = 0; i < 4; i++) {
        TreeItem itemI = new TreeItem(tree, SWT.NONE);
        itemI.setText("Item " + i);
        for (int j = 0; j < 4; j++) {
            TreeItem itemJ = new TreeItem(itemI, SWT.NONE);
            itemJ.setText("Item " + i + " " + j);
            for (int k = 0; k < 4; k++) {
                TreeItem itemK = new TreeItem(itemJ, SWT.NONE);
                itemK.setText("Item " + i + " " + j + " " + k);
            }
        }
    }
    Rectangle clientArea = shell.getClientArea();
    tree.setBounds(clientArea.x, clientArea.y, 200, 200);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 254");
    RowLayout layout = new RowLayout(SWT.VERTICAL);
    layout.fill = true;//from   w w w .j  a  v a 2 s.c om
    layout.wrap = false;
    shell.setLayout(layout);
    final Tree tree = new Tree(shell, SWT.NONE);
    for (int i = 0; i < 32; i++) {
        TreeItem item0 = new TreeItem(tree, SWT.NONE);
        item0.setText("Item " + i + " is quite long");
        for (int j = 0; j < 3; j++) {
            TreeItem item1 = new TreeItem(item0, SWT.NONE);
            item1.setText("Item " + i + " " + j + " is quite long");
            for (int k = 0; k < 3; k++) {
                TreeItem item2 = new TreeItem(item1, SWT.NONE);
                item2.setText("Item " + i + " " + j + " " + k + " is quite long");
                for (int l = 0; l < 3; l++) {
                    TreeItem item3 = new TreeItem(item2, SWT.NONE);
                    item3.setText("Item " + i + " " + j + " " + k + " " + l + " is quite long");
                }
            }
        }
    }
    tree.setLayoutData(new RowData(200, 200));
    final Button button = new Button(shell, SWT.PUSH);
    button.setText("Visible Items []");
    button.addListener(SWT.Selection, e -> {
        int visibleCount = 0;
        Rectangle rect = tree.getClientArea();
        TreeItem item = tree.getTopItem();
        while (item != null) {
            visibleCount++;
            Rectangle itemRect = item.getBounds();
            if (itemRect.y + itemRect.height > rect.y + rect.height) {
                break;
            }
            item = nextItem(tree, item);
        }
        button.setText("Visible Items [" + visibleCount + "]");
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

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

    final Tree tree = new Tree(shell, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
    tree.setHeaderVisible(true);//from  w  w w .ja v  a2 s  . c o m
    final Menu headerMenu = new Menu(shell, SWT.POP_UP);
    final TreeColumn columnName = new TreeColumn(tree, SWT.NONE);
    columnName.setText("Name");
    columnName.setWidth(150);
    createMenuItem(headerMenu, columnName);
    final TreeColumn columnSize = new TreeColumn(tree, SWT.NONE);
    columnSize.setText("Size");
    columnSize.setWidth(150);
    createMenuItem(headerMenu, columnSize);
    final TreeColumn columnType = new TreeColumn(tree, SWT.NONE);
    columnType.setText("Type");
    columnType.setWidth(150);
    createMenuItem(headerMenu, columnType);
    final TreeColumn columnDate = new TreeColumn(tree, SWT.NONE);
    columnDate.setText("Date");
    columnDate.setWidth(150);
    createMenuItem(headerMenu, columnDate);
    final TreeColumn columnOwner = new TreeColumn(tree, SWT.NONE);
    columnOwner.setText("Owner");
    columnOwner.setWidth(0);
    columnOwner.setResizable(false);
    createMenuItem(headerMenu, columnOwner);

    for (int i = 0; i < files.length; i++) {
        TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText(files[i]);
        TreeItem subItem = new TreeItem(item, SWT.NONE);
        subItem.setText("node");
    }

    final Menu treeMenu = new Menu(shell, SWT.POP_UP);
    MenuItem item = new MenuItem(treeMenu, SWT.PUSH);
    item.setText("Open");
    item = new MenuItem(treeMenu, SWT.PUSH);
    item.setText("Open With");
    new MenuItem(treeMenu, SWT.SEPARATOR);
    item = new MenuItem(treeMenu, SWT.PUSH);
    item.setText("Cut");
    item = new MenuItem(treeMenu, SWT.PUSH);
    item.setText("Copy");
    item = new MenuItem(treeMenu, SWT.PUSH);
    item.setText("Paste");
    new MenuItem(treeMenu, SWT.SEPARATOR);
    item = new MenuItem(treeMenu, SWT.PUSH);
    item.setText("Delete");

    tree.addListener(SWT.MenuDetect, event -> {
        Point pt = display.map(null, tree, new Point(event.x, event.y));
        Rectangle clientArea = tree.getClientArea();
        boolean header = clientArea.y <= pt.y && pt.y < (clientArea.y + tree.getHeaderHeight());
        tree.setMenu(header ? headerMenu : treeMenu);
    });

    /* IMPORTANT: Dispose the menus (only the current menu, set with setMenu(), will be automatically disposed) */
    tree.addListener(SWT.Dispose, event -> {
        headerMenu.dispose();
        treeMenu.dispose();
    });

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

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

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("SWT and Swing/AWT Example");

    Listener exitListener = e -> {// w ww. j a  va2 s.c  o m
        MessageBox dialog = new MessageBox(shell, SWT.OK | SWT.CANCEL | SWT.ICON_QUESTION);
        dialog.setText("Question");
        dialog.setMessage("Exit?");
        if (e.type == SWT.Close)
            e.doit = false;
        if (dialog.open() != SWT.OK)
            return;
        shell.dispose();
    };
    Listener aboutListener = e -> {
        final Shell s = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
        s.setText("About");
        GridLayout layout = new GridLayout(1, false);
        layout.verticalSpacing = 20;
        layout.marginHeight = layout.marginWidth = 10;
        s.setLayout(layout);
        Label label = new Label(s, SWT.NONE);
        label.setText("SWT and AWT Example.");
        Button button = new Button(s, SWT.PUSH);
        button.setText("OK");
        GridData data = new GridData();
        data.horizontalAlignment = GridData.CENTER;
        button.setLayoutData(data);
        button.addListener(SWT.Selection, event -> s.dispose());
        s.pack();
        Rectangle parentBounds = shell.getBounds();
        Rectangle bounds = s.getBounds();
        int x = parentBounds.x + (parentBounds.width - bounds.width) / 2;
        int y = parentBounds.y + (parentBounds.height - bounds.height) / 2;
        s.setLocation(x, y);
        s.open();
        while (!s.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
    };
    shell.addListener(SWT.Close, exitListener);
    Menu mb = new Menu(shell, SWT.BAR);
    MenuItem fileItem = new MenuItem(mb, SWT.CASCADE);
    fileItem.setText("&File");
    Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
    fileItem.setMenu(fileMenu);
    MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH);
    exitItem.setText("&Exit\tCtrl+X");
    exitItem.setAccelerator(SWT.CONTROL + 'X');
    exitItem.addListener(SWT.Selection, exitListener);
    MenuItem aboutItem = new MenuItem(fileMenu, SWT.PUSH);
    aboutItem.setText("&About\tCtrl+A");
    aboutItem.setAccelerator(SWT.CONTROL + 'A');
    aboutItem.addListener(SWT.Selection, aboutListener);
    shell.setMenuBar(mb);

    RGB color = shell.getBackground().getRGB();
    Label separator1 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    Label locationLb = new Label(shell, SWT.NONE);
    locationLb.setText("Location:");
    Composite locationComp = new Composite(shell, SWT.EMBEDDED);
    ToolBar toolBar = new ToolBar(shell, SWT.FLAT);
    ToolItem exitToolItem = new ToolItem(toolBar, SWT.PUSH);
    exitToolItem.setText("&Exit");
    exitToolItem.addListener(SWT.Selection, exitListener);
    ToolItem aboutToolItem = new ToolItem(toolBar, SWT.PUSH);
    aboutToolItem.setText("&About");
    aboutToolItem.addListener(SWT.Selection, aboutListener);
    Label separator2 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    final Composite comp = new Composite(shell, SWT.NONE);
    final Tree fileTree = new Tree(comp, SWT.SINGLE | SWT.BORDER);
    Sash sash = new Sash(comp, SWT.VERTICAL);
    Composite tableComp = new Composite(comp, SWT.EMBEDDED);
    Label separator3 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    Composite statusComp = new Composite(shell, SWT.EMBEDDED);

    java.awt.Frame locationFrame = SWT_AWT.new_Frame(locationComp);
    final java.awt.TextField locationText = new java.awt.TextField();
    locationFrame.add(locationText);

    java.awt.Frame fileTableFrame = SWT_AWT.new_Frame(tableComp);
    java.awt.Panel panel = new java.awt.Panel(new java.awt.BorderLayout());
    fileTableFrame.add(panel);
    final JTable fileTable = new JTable(new FileTableModel(null));
    fileTable.setDoubleBuffered(true);
    fileTable.setShowGrid(false);
    fileTable.createDefaultColumnsFromModel();
    JScrollPane scrollPane = new JScrollPane(fileTable);
    panel.add(scrollPane);

    java.awt.Frame statusFrame = SWT_AWT.new_Frame(statusComp);
    statusFrame.setBackground(new java.awt.Color(color.red, color.green, color.blue));
    final java.awt.Label statusLabel = new java.awt.Label();
    statusFrame.add(statusLabel);
    statusLabel.setText("Select a file");

    sash.addListener(SWT.Selection, e -> {
        if (e.detail == SWT.DRAG)
            return;
        GridData data = (GridData) fileTree.getLayoutData();
        Rectangle trim = fileTree.computeTrim(0, 0, 0, 0);
        data.widthHint = e.x - trim.width;
        comp.layout();
    });

    File[] roots = File.listRoots();
    for (int i = 0; i < roots.length; i++) {
        File file = roots[i];
        TreeItem treeItem = new TreeItem(fileTree, SWT.NONE);
        treeItem.setText(file.getAbsolutePath());
        treeItem.setData(file);
        new TreeItem(treeItem, SWT.NONE);
    }
    fileTree.addListener(SWT.Expand, e -> {
        TreeItem item = (TreeItem) e.item;
        if (item == null)
            return;
        if (item.getItemCount() == 1) {
            TreeItem firstItem = item.getItems()[0];
            if (firstItem.getData() != null)
                return;
            firstItem.dispose();
        } else {
            return;
        }
        File root = (File) item.getData();
        File[] files = root.listFiles();
        if (files == null)
            return;
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            if (file.isDirectory()) {
                TreeItem treeItem = new TreeItem(item, SWT.NONE);
                treeItem.setText(file.getName());
                treeItem.setData(file);
                new TreeItem(treeItem, SWT.NONE);
            }
        }
    });
    fileTree.addListener(SWT.Selection, e -> {
        TreeItem item = (TreeItem) e.item;
        if (item == null)
            return;
        final File root = (File) item.getData();
        EventQueue.invokeLater(() -> {
            statusLabel.setText(root.getAbsolutePath());
            locationText.setText(root.getAbsolutePath());
            fileTable.setModel(new FileTableModel(root.listFiles()));
        });
    });

    GridLayout layout = new GridLayout(4, false);
    layout.marginWidth = layout.marginHeight = 0;
    layout.horizontalSpacing = layout.verticalSpacing = 1;
    shell.setLayout(layout);
    GridData data;
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    separator1.setLayoutData(data);
    data = new GridData();
    data.horizontalSpan = 1;
    data.horizontalIndent = 10;
    locationLb.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 2;
    data.heightHint = locationText.getPreferredSize().height;
    locationComp.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 1;
    toolBar.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    separator2.setLayoutData(data);
    data = new GridData(GridData.FILL_BOTH);
    data.horizontalSpan = 4;
    comp.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    separator3.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    data.heightHint = statusLabel.getPreferredSize().height;
    statusComp.setLayoutData(data);

    layout = new GridLayout(3, false);
    layout.marginWidth = layout.marginHeight = 0;
    layout.horizontalSpacing = layout.verticalSpacing = 1;
    comp.setLayout(layout);
    data = new GridData(GridData.FILL_VERTICAL);
    data.widthHint = 200;
    fileTree.setLayoutData(data);
    data = new GridData(GridData.FILL_VERTICAL);
    sash.setLayoutData(data);
    data = new GridData(GridData.FILL_BOTH);
    tableComp.setLayoutData(data);

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

From source file:Snippet135.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("SWT and Swing/AWT Example");

    Listener exitListener = new Listener() {
        public void handleEvent(Event e) {
            MessageBox dialog = new MessageBox(shell, SWT.OK | SWT.CANCEL | SWT.ICON_QUESTION);
            dialog.setText("Question");
            dialog.setMessage("Exit?");
            if (e.type == SWT.Close)
                e.doit = false;// w w  w.j  ava  2s  .c o m
            if (dialog.open() != SWT.OK)
                return;
            shell.dispose();
        }
    };
    Listener aboutListener = new Listener() {
        public void handleEvent(Event e) {
            final Shell s = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
            s.setText("About");
            GridLayout layout = new GridLayout(1, false);
            layout.verticalSpacing = 20;
            layout.marginHeight = layout.marginWidth = 10;
            s.setLayout(layout);
            Label label = new Label(s, SWT.NONE);
            label.setText("SWT and AWT Example.");
            Button button = new Button(s, SWT.PUSH);
            button.setText("OK");
            GridData data = new GridData();
            data.horizontalAlignment = GridData.CENTER;
            button.setLayoutData(data);
            button.addListener(SWT.Selection, new Listener() {
                public void handleEvent(Event event) {
                    s.dispose();
                }
            });
            s.pack();
            Rectangle parentBounds = shell.getBounds();
            Rectangle bounds = s.getBounds();
            int x = parentBounds.x + (parentBounds.width - bounds.width) / 2;
            int y = parentBounds.y + (parentBounds.height - bounds.height) / 2;
            s.setLocation(x, y);
            s.open();
            while (!s.isDisposed()) {
                if (!display.readAndDispatch())
                    display.sleep();
            }
        }
    };
    shell.addListener(SWT.Close, exitListener);
    Menu mb = new Menu(shell, SWT.BAR);
    MenuItem fileItem = new MenuItem(mb, SWT.CASCADE);
    fileItem.setText("&File");
    Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
    fileItem.setMenu(fileMenu);
    MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH);
    exitItem.setText("&Exit\tCtrl+X");
    exitItem.setAccelerator(SWT.CONTROL + 'X');
    exitItem.addListener(SWT.Selection, exitListener);
    MenuItem aboutItem = new MenuItem(fileMenu, SWT.PUSH);
    aboutItem.setText("&About\tCtrl+A");
    aboutItem.setAccelerator(SWT.CONTROL + 'A');
    aboutItem.addListener(SWT.Selection, aboutListener);
    shell.setMenuBar(mb);

    RGB color = shell.getBackground().getRGB();
    Label separator1 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    Label locationLb = new Label(shell, SWT.NONE);
    locationLb.setText("Location:");
    Composite locationComp = new Composite(shell, SWT.EMBEDDED);
    ToolBar toolBar = new ToolBar(shell, SWT.FLAT);
    ToolItem exitToolItem = new ToolItem(toolBar, SWT.PUSH);
    exitToolItem.setText("&Exit");
    exitToolItem.addListener(SWT.Selection, exitListener);
    ToolItem aboutToolItem = new ToolItem(toolBar, SWT.PUSH);
    aboutToolItem.setText("&About");
    aboutToolItem.addListener(SWT.Selection, aboutListener);
    Label separator2 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    final Composite comp = new Composite(shell, SWT.NONE);
    final Tree fileTree = new Tree(comp, SWT.SINGLE | SWT.BORDER);
    Sash sash = new Sash(comp, SWT.VERTICAL);
    Composite tableComp = new Composite(comp, SWT.EMBEDDED);
    Label separator3 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    Composite statusComp = new Composite(shell, SWT.EMBEDDED);

    java.awt.Frame locationFrame = SWT_AWT.new_Frame(locationComp);
    final java.awt.TextField locationText = new java.awt.TextField();
    locationFrame.add(locationText);

    java.awt.Frame fileTableFrame = SWT_AWT.new_Frame(tableComp);
    java.awt.Panel panel = new java.awt.Panel(new java.awt.BorderLayout());
    fileTableFrame.add(panel);
    final JTable fileTable = new JTable(new FileTableModel(null));
    fileTable.setDoubleBuffered(true);
    fileTable.setShowGrid(false);
    fileTable.createDefaultColumnsFromModel();
    JScrollPane scrollPane = new JScrollPane(fileTable);
    panel.add(scrollPane);

    java.awt.Frame statusFrame = SWT_AWT.new_Frame(statusComp);
    statusFrame.setBackground(new java.awt.Color(color.red, color.green, color.blue));
    final java.awt.Label statusLabel = new java.awt.Label();
    statusFrame.add(statusLabel);
    statusLabel.setText("Select a file");

    sash.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            if (e.detail == SWT.DRAG)
                return;
            GridData data = (GridData) fileTree.getLayoutData();
            Rectangle trim = fileTree.computeTrim(0, 0, 0, 0);
            data.widthHint = e.x - trim.width;
            comp.layout();
        }
    });

    File[] roots = File.listRoots();
    for (int i = 0; i < roots.length; i++) {
        File file = roots[i];
        TreeItem treeItem = new TreeItem(fileTree, SWT.NONE);
        treeItem.setText(file.getAbsolutePath());
        treeItem.setData(file);
        new TreeItem(treeItem, SWT.NONE);
    }
    fileTree.addListener(SWT.Expand, new Listener() {
        public void handleEvent(Event e) {
            TreeItem item = (TreeItem) e.item;
            if (item == null)
                return;
            if (item.getItemCount() == 1) {
                TreeItem firstItem = item.getItems()[0];
                if (firstItem.getData() != null)
                    return;
                firstItem.dispose();
            } else {
                return;
            }
            File root = (File) item.getData();
            File[] files = root.listFiles();
            if (files == null)
                return;
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                if (file.isDirectory()) {
                    TreeItem treeItem = new TreeItem(item, SWT.NONE);
                    treeItem.setText(file.getName());
                    treeItem.setData(file);
                    new TreeItem(treeItem, SWT.NONE);
                }
            }
        }
    });
    fileTree.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            TreeItem item = (TreeItem) e.item;
            if (item == null)
                return;
            final File root = (File) item.getData();
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    statusLabel.setText(root.getAbsolutePath());
                    locationText.setText(root.getAbsolutePath());
                    fileTable.setModel(new FileTableModel(root.listFiles()));
                }
            });
        }
    });

    GridLayout layout = new GridLayout(4, false);
    layout.marginWidth = layout.marginHeight = 0;
    layout.horizontalSpacing = layout.verticalSpacing = 1;
    shell.setLayout(layout);
    GridData data;
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    separator1.setLayoutData(data);
    data = new GridData();
    data.horizontalSpan = 1;
    data.horizontalIndent = 10;
    locationLb.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 2;
    data.heightHint = locationText.getPreferredSize().height;
    locationComp.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 1;
    toolBar.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    separator2.setLayoutData(data);
    data = new GridData(GridData.FILL_BOTH);
    data.horizontalSpan = 4;
    comp.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    separator3.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    data.heightHint = statusLabel.getPreferredSize().height;
    statusComp.setLayoutData(data);

    layout = new GridLayout(3, false);
    layout.marginWidth = layout.marginHeight = 0;
    layout.horizontalSpacing = layout.verticalSpacing = 1;
    comp.setLayout(layout);
    data = new GridData(GridData.FILL_VERTICAL);
    data.widthHint = 200;
    fileTree.setLayoutData(data);
    data = new GridData(GridData.FILL_VERTICAL);
    sash.setLayoutData(data);
    data = new GridData(GridData.FILL_BOTH);
    tableComp.setLayoutData(data);

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

From source file:org.eclipse.swt.examples.accessibility.ControlsWithLabelsExample.java

public static void main(String[] args) {
    display = new Display();
    shell = new Shell(display);
    shell.setLayout(new GridLayout(4, true));
    shell.setText("All Controls Test");

    new Label(shell, SWT.NONE).setText("Label for Label");
    label = new Label(shell, SWT.NONE);
    label.setText("Label");

    new Label(shell, SWT.NONE).setText("Label for CLabel");
    cLabel = new CLabel(shell, SWT.NONE);
    cLabel.setText("CLabel");

    new Label(shell, SWT.NONE).setText("Label for Push Button");
    buttonPush = new Button(shell, SWT.PUSH);
    buttonPush.setText("Push Button");

    new Label(shell, SWT.NONE).setText("Label for Radio Button");
    buttonRadio = new Button(shell, SWT.RADIO);
    buttonRadio.setText("Radio Button");

    new Label(shell, SWT.NONE).setText("Label for Check Button");
    buttonCheck = new Button(shell, SWT.CHECK);
    buttonCheck.setText("Check Button");

    new Label(shell, SWT.NONE).setText("Label for Toggle Button");
    buttonToggle = new Button(shell, SWT.TOGGLE);
    buttonToggle.setText("Toggle Button");

    new Label(shell, SWT.NONE).setText("Label for Editable Combo");
    combo = new Combo(shell, SWT.BORDER);
    for (int i = 0; i < 4; i++) {
        combo.add("item" + i);
    }//w  ww  .j ava2  s .com
    combo.select(0);

    new Label(shell, SWT.NONE).setText("Label for Read-Only Combo");
    combo = new Combo(shell, SWT.READ_ONLY | SWT.BORDER);
    for (int i = 0; i < 4; i++) {
        combo.add("item" + i);
    }
    combo.select(0);

    new Label(shell, SWT.NONE).setText("Label for CCombo");
    cCombo = new CCombo(shell, SWT.BORDER);
    for (int i = 0; i < 5; i++) {
        cCombo.add("item" + i);
    }
    cCombo.select(0);

    new Label(shell, SWT.NONE).setText("Label for List");
    list = new List(shell, SWT.SINGLE | SWT.BORDER);
    list.setItems("Item0", "Item1", "Item2");

    new Label(shell, SWT.NONE).setText("Label for Spinner");
    spinner = new Spinner(shell, SWT.BORDER);

    new Label(shell, SWT.NONE).setText("Label for Single-line Text");
    textSingle = new Text(shell, SWT.SINGLE | SWT.BORDER);
    textSingle.setText("Contents of Single-line Text");

    new Label(shell, SWT.NONE).setText("Label for Multi-line Text");
    textMulti = new Text(shell, SWT.MULTI | SWT.BORDER);
    textMulti.setText("\nContents of Multi-line Text\n");

    new Label(shell, SWT.NONE).setText("Label for StyledText");
    styledText = new StyledText(shell, SWT.MULTI | SWT.BORDER);
    styledText.setText("\nContents of Multi-line StyledText\n");

    new Label(shell, SWT.NONE).setText("Label for Table");
    table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    for (int col = 0; col < 3; col++) {
        TableColumn column = new TableColumn(table, SWT.NONE);
        column.setText("Col " + col);
        column.setWidth(50);
    }
    for (int row = 0; row < 3; row++) {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText(new String[] { "C0R" + row, "C1R" + row, "C2R" + row });
    }

    new Label(shell, SWT.NONE).setText("Label for Tree");
    tree = new Tree(shell, SWT.BORDER | SWT.MULTI);
    for (int i = 0; i < 3; i++) {
        TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText("Item" + i);
        for (int j = 0; j < 4; j++) {
            new TreeItem(item, SWT.NONE).setText("Item" + i + j);
        }
    }

    new Label(shell, SWT.NONE).setText("Label for Tree with columns");
    treeTable = new Tree(shell, SWT.BORDER | SWT.MULTI);
    treeTable.setHeaderVisible(true);
    treeTable.setLinesVisible(true);
    for (int col = 0; col < 3; col++) {
        TreeColumn column = new TreeColumn(treeTable, SWT.NONE);
        column.setText("Col " + col);
        column.setWidth(50);
    }
    for (int i = 0; i < 3; i++) {
        TreeItem item = new TreeItem(treeTable, SWT.NONE);
        item.setText(new String[] { "I" + i + "C0", "I" + i + "C1", "I" + i + "C2" });
        for (int j = 0; j < 4; j++) {
            new TreeItem(item, SWT.NONE)
                    .setText(new String[] { "I" + i + j + "C0", "I" + i + j + "C1", "I" + i + j + "C2" });
        }
    }

    new Label(shell, SWT.NONE).setText("Label for ToolBar");
    toolBar = new ToolBar(shell, SWT.FLAT);
    for (int i = 0; i < 3; i++) {
        ToolItem item = new ToolItem(toolBar, SWT.PUSH);
        item.setText("Item" + i);
        item.setToolTipText("ToolItem ToolTip" + i);
    }

    new Label(shell, SWT.NONE).setText("Label for CoolBar");
    coolBar = new CoolBar(shell, SWT.FLAT);
    for (int i = 0; i < 2; i++) {
        CoolItem coolItem = new CoolItem(coolBar, SWT.PUSH);
        ToolBar coolItemToolBar = new ToolBar(coolBar, SWT.FLAT);
        int toolItemWidth = 0;
        for (int j = 0; j < 2; j++) {
            ToolItem item = new ToolItem(coolItemToolBar, SWT.PUSH);
            item.setText("Item" + i + j);
            item.setToolTipText("ToolItem ToolTip" + i + j);
            if (item.getWidth() > toolItemWidth)
                toolItemWidth = item.getWidth();
        }
        coolItem.setControl(coolItemToolBar);
        Point size = coolItemToolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT);
        Point coolSize = coolItem.computeSize(size.x, size.y);
        coolItem.setMinimumSize(toolItemWidth, coolSize.y);
        coolItem.setPreferredSize(coolSize);
        coolItem.setSize(coolSize);
    }

    new Label(shell, SWT.NONE).setText("Label for Canvas");
    canvas = new Canvas(shell, SWT.BORDER);
    canvas.setLayoutData(new GridData(64, 64));
    canvas.addPaintListener(e -> e.gc.drawString("Canvas", 15, 25));
    canvas.setCaret(new Caret(canvas, SWT.NONE));
    /* Hook key listener so canvas will take focus during traversal in. */
    canvas.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
        }

        @Override
        public void keyReleased(KeyEvent e) {
        }
    });
    /* Hook traverse listener to make canvas give up focus during traversal out. */
    canvas.addTraverseListener(e -> e.doit = true);

    new Label(shell, SWT.NONE).setText("Label for Group");
    group = new Group(shell, SWT.NONE);
    group.setText("Group");
    group.setLayout(new FillLayout());
    new Text(group, SWT.SINGLE | SWT.BORDER).setText("Text in Group");

    new Label(shell, SWT.NONE).setText("Label for TabFolder");
    tabFolder = new TabFolder(shell, SWT.NONE);
    for (int i = 0; i < 3; i++) {
        TabItem item = new TabItem(tabFolder, SWT.NONE);
        item.setText("TabItem &" + i);
        item.setToolTipText("TabItem ToolTip" + i);
        Text itemText = new Text(tabFolder, SWT.SINGLE | SWT.BORDER);
        itemText.setText("Text for TabItem " + i);
        item.setControl(itemText);
    }

    new Label(shell, SWT.NONE).setText("Label for CTabFolder");
    cTabFolder = new CTabFolder(shell, SWT.BORDER);
    for (int i = 0; i < 3; i++) {
        CTabItem item = new CTabItem(cTabFolder, SWT.NONE);
        item.setText("CTabItem &" + i);
        item.setToolTipText("CTabItem ToolTip" + i);
        Text itemText = new Text(cTabFolder, SWT.SINGLE | SWT.BORDER);
        itemText.setText("Text for CTabItem " + i);
        item.setControl(itemText);
    }
    cTabFolder.setSelection(cTabFolder.getItem(0));

    new Label(shell, SWT.NONE).setText("Label for Scale");
    scale = new Scale(shell, SWT.NONE);

    new Label(shell, SWT.NONE).setText("Label for Slider");
    slider = new Slider(shell, SWT.NONE);

    new Label(shell, SWT.NONE).setText("Label for ProgressBar");
    progressBar = new ProgressBar(shell, SWT.NONE);
    progressBar.setSelection(50);

    new Label(shell, SWT.NONE).setText("Label for Sash");
    sash = new Sash(shell, SWT.NONE);

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