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

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

Introduction

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

Prototype

public Rectangle getClientArea() 

Source Link

Document

Returns a rectangle which describes the area of the receiver which is capable of displaying data (that is, not covered by the "trimmings").

Usage

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;/*w  w w  .  ja va2 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.Snippet226.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Custom gradient selection for Tree");
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.MULTI | SWT.FULL_SELECTION);
    tree.setHeaderVisible(true);//  ww  w  .  j ava2  s . c  o  m
    tree.setLinesVisible(true);
    int columnCount = 4;
    for (int i = 0; i < columnCount; i++) {
        TreeColumn column = new TreeColumn(tree, SWT.NONE);
        column.setText("Column " + i);
    }
    int itemCount = 3;
    for (int i = 0; i < itemCount; i++) {
        TreeItem item1 = new TreeItem(tree, SWT.NONE);
        item1.setText("item " + i);
        for (int c = 1; c < columnCount; c++) {
            item1.setText(c, "item [" + i + "-" + c + "]");
        }
        for (int j = 0; j < itemCount; j++) {
            TreeItem item2 = new TreeItem(item1, SWT.NONE);
            item2.setText("item [" + i + " " + j + "]");
            for (int c = 1; c < columnCount; c++) {
                item2.setText(c, "item [" + i + " " + j + "-" + c + "]");
            }
            for (int k = 0; k < itemCount; k++) {
                TreeItem item3 = new TreeItem(item2, SWT.NONE);
                item3.setText("item [" + i + " " + j + " " + k + "]");
                for (int c = 1; c < columnCount; c++) {
                    item3.setText(c, "item [" + i + " " + j + " " + k + "-" + c + "]");
                }
            }
        }
    }

    /*
     * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
     * Therefore, it is critical for performance that these methods be
     * as efficient as possible.
     */
    tree.addListener(SWT.EraseItem, event -> {
        event.detail &= ~SWT.HOT;
        if ((event.detail & SWT.SELECTED) != 0) {
            GC gc = event.gc;
            Rectangle area = tree.getClientArea();
            /*
             * If you wish to paint the selection beyond the end of
             * last column, you must change the clipping region.
             */
            int columnCount1 = tree.getColumnCount();
            if (event.index == columnCount1 - 1 || columnCount1 == 0) {
                int width = area.x + area.width - event.x;
                if (width > 0) {
                    Region region = new Region();
                    gc.getClipping(region);
                    region.add(event.x, event.y, width, event.height);
                    gc.setClipping(region);
                    region.dispose();
                }
            }
            gc.setAdvanced(true);
            if (gc.getAdvanced())
                gc.setAlpha(127);
            Rectangle rect = event.getBounds();
            Color foreground = gc.getForeground();
            Color background = gc.getBackground();
            gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
            gc.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
            gc.fillGradientRectangle(0, rect.y, 500, rect.height, false);
            // restore colors for subsequent drawing
            gc.setForeground(foreground);
            gc.setBackground(background);
            event.detail &= ~SWT.SELECTED;
        }
    });
    for (int i = 0; i < columnCount; i++) {
        tree.getColumn(i).pack();
    }
    tree.setSelection(tree.getItem(0));
    shell.setSize(500, 200);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:TreeTableGradient.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Custom gradient selection for Tree");
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.MULTI | SWT.FULL_SELECTION);
    tree.setHeaderVisible(true);/*w w w. java2 s  . c om*/
    tree.setLinesVisible(true);
    int columnCount = 4;
    for (int i = 0; i < columnCount; i++) {
        TreeColumn column = new TreeColumn(tree, SWT.NONE);
        column.setText("Column " + i);
    }
    int itemCount = 3;
    for (int i = 0; i < itemCount; i++) {
        TreeItem item1 = new TreeItem(tree, SWT.NONE);
        item1.setText("item " + i);
        for (int c = 1; c < columnCount; c++) {
            item1.setText(c, "item [" + i + "-" + c + "]");
        }
        for (int j = 0; j < itemCount; j++) {
            TreeItem item2 = new TreeItem(item1, SWT.NONE);
            item2.setText("item [" + i + " " + j + "]");
            for (int c = 1; c < columnCount; c++) {
                item2.setText(c, "item [" + i + " " + j + "-" + c + "]");
            }
            for (int k = 0; k < itemCount; k++) {
                TreeItem item3 = new TreeItem(item2, SWT.NONE);
                item3.setText("item [" + i + " " + j + " " + k + "]");
                for (int c = 1; c < columnCount; c++) {
                    item3.setText(c, "item [" + i + " " + j + " " + k + "-" + c + "]");
                }
            }
        }
    }

    /*
     * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
     * Therefore, it is critical for performance that these methods be as
     * efficient as possible.
     */
    tree.addListener(SWT.EraseItem, new Listener() {
        public void handleEvent(Event event) {
            if ((event.detail & SWT.SELECTED) != 0) {
                GC gc = event.gc;
                Rectangle area = tree.getClientArea();
                /*
                 * If you wish to paint the selection beyond the end of last column,
                 * you must change the clipping region.
                 */
                int columnCount = tree.getColumnCount();
                if (event.index == columnCount - 1 || columnCount == 0) {
                    int width = area.x + area.width - event.x;
                    if (width > 0) {
                        Region region = new Region();
                        gc.getClipping(region);
                        region.add(event.x, event.y, width, event.height);
                        gc.setClipping(region);
                        region.dispose();
                    }
                }
                gc.setAdvanced(true);
                if (gc.getAdvanced())
                    gc.setAlpha(127);
                Rectangle rect = event.getBounds();
                Color foreground = gc.getForeground();
                Color background = gc.getBackground();
                gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
                gc.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
                gc.fillGradientRectangle(0, rect.y, 500, rect.height, false);
                // restore colors for subsequent drawing
                gc.setForeground(foreground);
                gc.setBackground(background);
                event.detail &= ~SWT.SELECTED;
            }
        }
    });
    for (int i = 0; i < columnCount; i++) {
        tree.getColumn(i).pack();
    }
    tree.setSelection(tree.getItem(0));
    shell.setSize(500, 200);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            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 ava  2 s.  co  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: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);//ww  w. j a va 2  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: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 .java2  s. c o  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:org.eclipse.swt.snippets.Snippet221.java

static void Scroll(Tree tree, int x, int y) {
    TreeItem item = tree.getItem(new Point(x, y));
    if (item == null)
        return;//w w w  .  j  a v  a2 s . co  m
    Rectangle area = tree.getClientArea();
    int headerHeight = tree.getHeaderHeight();
    int itemHeight = tree.getItemHeight();
    TreeItem nextItem = null;
    if (y < area.y + headerHeight + 2 * itemHeight) {
        nextItem = PreviousItem(tree, item);
    }
    if (y > area.y + area.height - 2 * itemHeight) {
        nextItem = NextItem(tree, item);
    }
    if (nextItem != null)
        tree.showItem(nextItem);
}