Example usage for java.lang Math max

List of usage examples for java.lang Math max

Introduction

In this page you can find the example usage for java.lang Math max.

Prototype

@HotSpotIntrinsicCandidate
public static double max(double a, double b) 

Source Link

Document

Returns the greater of two double values.

Usage

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

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    shell.setText("Show results as a bar chart in Table");
    final Table table = new Table(shell, SWT.BORDER);
    table.setHeaderVisible(true);//from   ww  w . ja  va2s. co m
    table.setLinesVisible(true);
    TableColumn column1 = new TableColumn(table, SWT.NONE);
    column1.setText("Bug Status");
    column1.setWidth(100);
    final TableColumn column2 = new TableColumn(table, SWT.NONE);
    column2.setText("Percent");
    column2.setWidth(200);
    String[] labels = new String[] { "Resolved", "New", "Won't Fix", "Invalid" };
    for (int i = 0; i < labels.length; i++) {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText(labels[i]);
    }

    /*
     * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
     * Therefore, it is critical for performance that these methods be
     * as efficient as possible.
     */
    table.addListener(SWT.PaintItem, new Listener() {
        int[] percents = new int[] { 50, 30, 5, 15 };

        @Override
        public void handleEvent(Event event) {
            if (event.index == 1) {
                GC gc = event.gc;
                TableItem item = (TableItem) event.item;
                int index = table.indexOf(item);
                int percent = percents[index];
                Color foreground = gc.getForeground();
                Color background = gc.getBackground();
                gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
                gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
                int width = (column2.getWidth() - 1) * percent / 100;
                gc.fillGradientRectangle(event.x, event.y, width, event.height, true);
                Rectangle rect2 = new Rectangle(event.x, event.y, width - 1, event.height - 1);
                gc.drawRectangle(rect2);
                gc.setForeground(display.getSystemColor(SWT.COLOR_LIST_FOREGROUND));
                String text = percent + "%";
                Point size = event.gc.textExtent(text);
                int offset = Math.max(0, (event.height - size.y) / 2);
                gc.drawText(text, event.x + 2, event.y + offset, true);
                gc.setForeground(background);
                gc.setBackground(foreground);
            }
        }
    });

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

From source file:TreeEventMeasurePaintErase.java

public static void main(String[] args) {
    Display display = new Display();
    final Image image = display.getSystemImage(SWT.ICON_INFORMATION);
    Shell shell = new Shell(display);
    shell.setText("Images on the right side of the TreeItem");
    shell.setLayout(new FillLayout());
    Tree tree = new Tree(shell, SWT.MULTI | SWT.FULL_SELECTION);
    tree.setHeaderVisible(true);/*from w  w w  . j  a  v a 2  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.
     */
    Listener paintListener = new Listener() {
        public void handleEvent(Event event) {
            switch (event.type) {
            case SWT.MeasureItem: {
                Rectangle rect = image.getBounds();
                event.width += rect.width;
                event.height = Math.max(event.height, rect.height + 2);
                break;
            }
            case SWT.PaintItem: {
                int x = event.x + event.width;
                Rectangle rect = image.getBounds();
                int offset = Math.max(0, (event.height - rect.height) / 2);
                event.gc.drawImage(image, x, event.y + offset);
                break;
            }
            }
        }
    };
    tree.addListener(SWT.MeasureItem, paintListener);
    tree.addListener(SWT.PaintItem, paintListener);

    for (int i = 0; i < columnCount; i++) {
        tree.getColumn(i).pack();
    }
    shell.setSize(500, 200);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    if (image != null)
        image.dispose();
    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.ja  v  a 2  s .c om
    }
    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.Snippet227.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Multiple lines in a TreeItem");
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.MULTI | SWT.FULL_SELECTION);
    tree.setHeaderVisible(true);//  www.  j  a  va 2s  .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);
        column.setWidth(100);
    }
    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.
     */
    Listener paintListener = new Listener() {
        @Override
        public void handleEvent(Event event) {
            switch (event.type) {
            case SWT.MeasureItem: {
                TreeItem item = (TreeItem) event.item;
                String text = getText(item, event.index);
                Point size = event.gc.textExtent(text);
                event.width = size.x;
                event.height = Math.max(event.height, size.y);
                break;
            }
            case SWT.PaintItem: {
                TreeItem item = (TreeItem) event.item;
                String text = getText(item, event.index);
                Point size = event.gc.textExtent(text);
                int offset2 = event.index == 0 ? Math.max(0, (event.height - size.y) / 2) : 0;
                event.gc.drawText(text, event.x, event.y + offset2, true);
                break;
            }
            case SWT.EraseItem: {
                event.detail &= ~SWT.FOREGROUND;
                break;
            }
            }
        }

        String getText(TreeItem item, int column) {
            String text = item.getText(column);
            if (column != 0) {
                TreeItem parent = item.getParentItem();
                int index = parent == null ? tree.indexOf(item) : parent.indexOf(item);
                if ((index + column) % 3 == 1) {
                    text += "\nnew line";
                }
                if ((index + column) % 3 == 2) {
                    text += "\nnew line\nnew line";
                }
            }
            return text;
        }
    };
    tree.addListener(SWT.MeasureItem, paintListener);
    tree.addListener(SWT.PaintItem, paintListener);
    tree.addListener(SWT.EraseItem, paintListener);

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

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

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet365 - Transparent Background");
    RowLayout layout = new RowLayout(SWT.VERTICAL);
    layout.spacing = 20;// w ww. j ava  2s . c  om
    layout.marginWidth = 10;
    layout.marginHeight = 10;
    shell.setLayout(layout);
    // Standard color background for Shell
    // shell.setBackground(display.getSystemColor(SWT.COLOR_CYAN));

    // Gradient background for Shell
    shell.addListener(SWT.Resize, event -> {
        Rectangle rect = shell.getClientArea();
        Image newImage = new Image(display, Math.max(1, rect.width), 1);
        GC gc = new GC(newImage);
        gc.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
        gc.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
        gc.fillGradientRectangle(rect.x, rect.y, rect.width, 1, false);
        gc.dispose();
        shell.setBackgroundImage(newImage);
        if (oldImage != null)
            oldImage.dispose();
        oldImage = newImage;
    });

    // Transparent
    buttonCheckBox = new Button(shell, SWT.CHECK | SWT.None);
    buttonCheckBox.setText("SET TRANSPARENT");
    buttonCheckBox.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
    buttonCheckBox.setSelection(false);
    buttonCheckBox.addSelectionListener(widgetSelectedAdapter(e -> {
        boolean transparent = ((Button) e.getSource()).getSelection();
        if (transparent) {
            // ContainerGroup
            containerGroup.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            canvas.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            composite.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            tabFolder.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            for (TabItem item : tabFolder.getItems()) {
                item.getControl().setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            }

            // Native
            nativeGroup.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            toolBar.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            coolBar.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            label.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            link.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            scale.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            radio.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            check.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            group.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            sash.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            slider.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            list.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));

            // Custom
            customGroup.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            cLabel.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            cTab.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_TRANSPARENT));
            gradientCTab.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_TRANSPARENT));
            sashForm.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_TRANSPARENT));
            for (Control control : sashForm.getChildren()) {
                control.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_TRANSPARENT));
            }
            // Default
            push.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            defaultBackgroundGroup.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            combo.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            progressBar.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            dateTime.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            ccombo.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            text.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            styledText.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            expandBar.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            table.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
            tree.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));

            // ItemGroup
            itemGroup.setBackground(display.getSystemColor(SWT.COLOR_TRANSPARENT));
        } else {
            // Native
            nativeGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            toolBar.setBackground(null);
            coolBar.setBackground(null);
            label.setBackground(null);
            link.setBackground(null);
            scale.setBackground(null);
            RGB rgb = display.getSystemColor(SWT.COLOR_CYAN).getRGB();
            radio.setBackground(new Color(display, new RGBA(rgb.red, rgb.blue, rgb.green, 255)));
            check.setBackgroundImage(getBackgroundImage(display));
            group.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            sash.setBackground(display.getSystemColor(SWT.COLOR_DARK_CYAN));
            slider.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
            list.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
            text.setBackground(display.getSystemColor(SWT.COLOR_CYAN));

            // ContainerGroup
            containerGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            canvas.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            composite.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            tabFolder.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            for (TabItem item : tabFolder.getItems()) {
                item.getControl().setBackground(display.getSystemColor(SWT.COLOR_CYAN));
            }
            // Custom
            customGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            cLabel.setBackground((Color) null);
            styledText.setBackground((Color) null);
            sashForm.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            for (Control control : sashForm.getChildren()) {
                control.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            }
            cTab.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));

            gradientCTab.setBackground(new Color[] { display.getSystemColor(SWT.COLOR_RED),
                    display.getSystemColor(SWT.COLOR_WHITE) }, new int[] { 90 }, true);

            // Default
            defaultBackgroundGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            push.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            combo.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
            ccombo.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
            dateTime.setBackground(null);
            progressBar.setBackground(null);
            expandBar.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
            table.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
            tree.setBackground(display.getSystemColor(SWT.COLOR_CYAN));

            // ItemGroup
            itemGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
        }

    }));

    // ContainerGroup
    containerGroup = new Composite(shell, SWT.NONE);
    containerGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
    containerGroup.setToolTipText("CONTAINER");
    layout = new RowLayout();
    layout.spacing = 20;
    containerGroup.setLayout(layout);

    // Native
    nativeGroup = new Composite(shell, SWT.NONE);
    nativeGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
    nativeGroup.setToolTipText("NATIVE");
    layout = new RowLayout();
    layout.spacing = 20;
    nativeGroup.setLayout(layout);

    // Custom
    customGroup = new Composite(shell, SWT.NONE);
    customGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
    customGroup.setToolTipText("CUSTOM");
    layout = new RowLayout();
    layout.spacing = 20;
    customGroup.setLayout(layout);

    // AsDesigned
    defaultBackgroundGroup = new Composite(shell, SWT.NONE);
    defaultBackgroundGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
    defaultBackgroundGroup.setToolTipText("Default Background");
    layout = new RowLayout();
    layout.spacing = 20;
    defaultBackgroundGroup.setLayout(layout);

    // ItemGroup
    itemGroup = new Composite(shell, SWT.NONE);
    itemGroup.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
    itemGroup.setToolTipText("ITEM");
    layout = new RowLayout();
    layout.spacing = 20;
    itemGroup.setLayout(layout);

    // Label
    label = new Label(nativeGroup, SWT.NONE);
    label.setText("Label");

    // Radio button
    radio = new Button(nativeGroup, SWT.RADIO);
    radio.setText("Radio Button");
    radio.setSelection(true);
    radio.setBackground(display.getSystemColor(SWT.COLOR_CYAN));

    // Checkbox button with image
    check = new Button(nativeGroup, SWT.CHECK);
    check.setText("CheckBox Image");
    check.setSelection(true);
    check.setBackgroundImage(getBackgroundImage(display));

    // Push Button
    push = new Button(defaultBackgroundGroup, SWT.PUSH);
    push.setText("Push Button");

    // Toolbar
    toolBar = new ToolBar(nativeGroup, SWT.FLAT);
    toolBar.pack();
    ToolItem item = new ToolItem(toolBar, SWT.PUSH);
    item.setText("ToolBar_Item");

    // Coolbar
    coolBar = new CoolBar(nativeGroup, SWT.BORDER);
    for (int i = 0; i < 2; i++) {
        CoolItem item2 = new CoolItem(coolBar, SWT.NONE);
        Button button = new Button(coolBar, SWT.PUSH);
        button.setText("Button " + i);
        Point size = button.computeSize(SWT.DEFAULT, SWT.DEFAULT);
        item2.setPreferredSize(item2.computeSize(size.x, size.y));
        item2.setControl(button);
    }
    // Scale
    scale = new Scale(nativeGroup, SWT.None);
    scale.setMaximum(100);
    scale.setSelection(20);

    // Link
    link = new Link(nativeGroup, SWT.NONE);
    link.setText("<a>Sample link</a>");

    // List
    list = new List(nativeGroup, SWT.BORDER);
    list.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
    list.add("List_one");
    list.add("List_two");
    list.add("List_three");
    list.add("List_four");

    // Canvas
    canvas = new Canvas(containerGroup, SWT.NONE);
    canvas.setToolTipText("Canvas");
    canvas.addPaintListener(e -> {
        GC gc = e.gc;
        gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
        gc.drawRectangle(e.x + 1, e.y + 1, e.width - 2, e.height - 2);
        gc.drawArc(2, 2, e.width - 4, e.height - 4, 0, 360);
    });

    // Composite
    composite = new Composite(containerGroup, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
    composite.setToolTipText("Composite");

    // TabFolder
    tabFolder = new TabFolder(containerGroup, SWT.BORDER);
    tabFolder.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
    for (int i = 0; i < 2; i++) {
        TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
        tabItem.setText("TabItem " + i);
        Label label = new Label(tabFolder, SWT.PUSH);
        label.setText("Page " + i);
        tabItem.setControl(label);
        tabItem.getControl().setBackground(display.getSystemColor(SWT.COLOR_CYAN));
    }
    tabFolder.pack();

    // Group
    group = new Group(containerGroup, SWT.NONE);
    group.setText("Group");

    // Sash
    sash = new Sash(containerGroup, SWT.HORIZONTAL | SWT.BORDER);
    sash.setBackground(display.getSystemColor(SWT.COLOR_DARK_CYAN));
    sash.setLayoutData(new RowData(100, 100));
    sash.setToolTipText("Sash");

    // SashForm
    sashForm = new SashForm(containerGroup, SWT.HORIZONTAL | SWT.BORDER);
    Label leftLabel = new Label(sashForm, SWT.NONE);
    leftLabel.setText("SashForm\nLeft\n...\n...\n...\n...\n...");
    Label rightLabel = new Label(sashForm, SWT.NONE);
    rightLabel.setText("SashForm\nRight\n...\n...\n...\n...\n...");

    // DateTime
    dateTime = new DateTime(defaultBackgroundGroup, SWT.NONE);
    dateTime.setBackground(display.getSystemColor(SWT.COLOR_CYAN));

    // Text
    text = new Text(nativeGroup, SWT.BORDER);
    text.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
    text.setText("text");

    // ProgressBar
    progressBar = new ProgressBar(defaultBackgroundGroup, SWT.NONE);
    progressBar.setMaximum(100);
    progressBar.setSelection(80);

    // Combo
    combo = new Combo(defaultBackgroundGroup, SWT.BORDER);
    combo.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
    combo.add("combo");
    combo.setText("combo");

    // Slider
    slider = new Slider(nativeGroup, SWT.HORIZONTAL | SWT.BORDER);
    slider.setSelection(20);
    slider.setBackground(display.getSystemColor(SWT.COLOR_CYAN));

    // CCombo
    ccombo = new CCombo(defaultBackgroundGroup, SWT.BORDER);
    ccombo.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
    ccombo.add("ccombo");
    ccombo.setText("ccombo");

    // CLable
    cLabel = new CLabel(customGroup, SWT.NONE);
    cLabel.setText("CLabel");

    // Text
    styledText = new StyledText(customGroup, SWT.BORDER);
    styledText.setFont(new Font(display, "Tahoma", 18, SWT.BOLD | SWT.ITALIC));
    styledText.setForeground(display.getSystemColor(SWT.COLOR_DARK_BLUE));
    styledText.setText("Styled Text");
    styledText.append("\n");
    styledText.append("Example_string");
    styledText.append("\n");
    styledText.append("One_Two");
    styledText.append("\n");
    styledText.append("Two_Three");

    // CTabFolder
    cTab = new CTabFolder(containerGroup, SWT.BORDER);
    for (int i = 0; i < 2; i++) {
        CTabItem cTabItem = new CTabItem(cTab, SWT.CLOSE, i);
        cTabItem.setText("CTabItem " + (i + 1));
    }
    cTab.setSelection(0);

    // Gradient CTabFolder
    gradientCTab = new CTabFolder(customGroup, SWT.BORDER);
    gradientCTab.setBackground(
            new Color[] { display.getSystemColor(SWT.COLOR_WHITE), display.getSystemColor(SWT.COLOR_RED) },
            new int[] { 90 }, true);
    for (int i = 0; i < 2; i++) {
        CTabItem cTabItem = new CTabItem(gradientCTab, SWT.CLOSE, i);
        cTabItem.setText("CTabItem " + (i + 1));
    }
    gradientCTab.setSelection(0);

    // Table
    table = new Table(itemGroup, SWT.V_SCROLL);
    table.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
    table.setLinesVisible(true);
    table.setHeaderVisible(true);
    TableItem tableItem = new TableItem(table, SWT.NONE);
    tableItem.setText("TableItem - One");
    tableItem = new TableItem(table, SWT.NONE);
    tableItem.setText("TableItem - Two");

    // Tree
    tree = new Tree(itemGroup, SWT.NONE);
    TreeItem treeItem = new TreeItem(tree, SWT.NONE);
    treeItem.setText("Parent");
    TreeItem childItem = new TreeItem(treeItem, SWT.NONE);
    childItem.setText("Child1");
    childItem = new TreeItem(treeItem, SWT.NONE);
    childItem.setText("Child2");
    treeItem.setExpanded(true);
    tree.setBackground(display.getSystemColor(SWT.COLOR_CYAN));

    // ExpandBar
    expandBar = new ExpandBar(itemGroup, SWT.V_SCROLL);
    expandBar.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
    for (int i = 1; i <= 2; i++) {
        ExpandItem item1 = new ExpandItem(expandBar, SWT.NONE, 0);
        item1.setText("Expand_Bar_Entry " + i);
        item1.setExpanded(true);
        item1.setHeight(20);
    }

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

From source file:TreeItemLines.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Multiple lines in a TreeItem");
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.MULTI | SWT.FULL_SELECTION);
    tree.setHeaderVisible(true);/*from w  w w  .  j  a  v a2  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);
        column.setWidth(100);
    }
    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.
     */
    Listener paintListener = new Listener() {
        public void handleEvent(Event event) {
            switch (event.type) {
            case SWT.MeasureItem: {
                TreeItem item = (TreeItem) event.item;
                String text = getText(item, event.index);
                Point size = event.gc.textExtent(text);
                event.width = size.x;
                event.height = Math.max(event.height, size.y);
                break;
            }
            case SWT.PaintItem: {
                TreeItem item = (TreeItem) event.item;
                String text = getText(item, event.index);
                Point size = event.gc.textExtent(text);
                int offset2 = event.index == 0 ? Math.max(0, (event.height - size.y) / 2) : 0;
                event.gc.drawText(text, event.x, event.y + offset2, true);
                break;
            }
            case SWT.EraseItem: {
                event.detail &= ~SWT.FOREGROUND;
                break;
            }
            }
        }

        String getText(TreeItem item, int column) {
            String text = item.getText(column);
            if (column != 0) {
                TreeItem parent = item.getParentItem();
                int index = parent == null ? tree.indexOf(item) : parent.indexOf(item);
                if ((index + column) % 3 == 1) {
                    text += "\nnew line";
                }
                if ((index + column) % 3 == 2) {
                    text += "\nnew line\nnew line";
                }
            }
            return text;
        }
    };
    tree.addListener(SWT.MeasureItem, paintListener);
    tree.addListener(SWT.PaintItem, paintListener);
    tree.addListener(SWT.EraseItem, paintListener);

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

From source file:TableCellProgressBar.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    shell.setText("Show results as a bar chart in Table");
    final Table table = new Table(shell, SWT.BORDER);
    table.setHeaderVisible(true);/*from  w  w w  .  j  a v a  2s.co m*/
    table.setLinesVisible(true);
    TableColumn column1 = new TableColumn(table, SWT.NONE);
    column1.setText("Bug Status");
    column1.setWidth(100);
    final TableColumn column2 = new TableColumn(table, SWT.NONE);
    column2.setText("Percent");
    column2.setWidth(200);
    String[] labels = new String[] { "Resolved", "New", "Won't Fix", "Invalid" };
    for (int i = 0; i < labels.length; i++) {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText(labels[i]);
    }

    /*
     * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
     * Therefore, it is critical for performance that these methods be as
     * efficient as possible.
     */
    table.addListener(SWT.PaintItem, new Listener() {
        int[] percents = new int[] { 50, 30, 5, 15 };

        public void handleEvent(Event event) {
            if (event.index == 1) {
                GC gc = event.gc;
                TableItem item = (TableItem) event.item;
                int index = table.indexOf(item);
                int percent = percents[index];
                Color foreground = gc.getForeground();
                Color background = gc.getBackground();
                gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
                gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
                int width = (column2.getWidth() - 1) * percent / 100;
                gc.fillGradientRectangle(event.x, event.y, width, event.height, true);
                Rectangle rect2 = new Rectangle(event.x, event.y, width - 1, event.height - 1);
                gc.drawRectangle(rect2);
                gc.setForeground(display.getSystemColor(SWT.COLOR_LIST_FOREGROUND));
                String text = percent + "%";
                Point size = event.gc.textExtent(text);
                int offset = Math.max(0, (event.height - size.y) / 2);
                gc.drawText(text, event.x + 2, event.y + offset, true);
                gc.setForeground(background);
                gc.setBackground(foreground);
            }
        }
    });

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

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

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 296");
    shell.setBounds(10, 10, 300, 300);/*from w w w . j a  v  a2s . c om*/
    final ScrolledComposite sc = new ScrolledComposite(shell, SWT.VERTICAL);
    sc.setBounds(10, 10, 280, 200);
    final int clientWidth = sc.getClientArea().width;

    final Tree tree = new Tree(sc, SWT.NONE);
    for (int i = 0; i < 99; i++) {
        TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText("item " + i);
        new TreeItem(item, SWT.NONE).setText("child");
    }
    sc.setContent(tree);
    int prefHeight = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
    tree.setSize(clientWidth, prefHeight);
    /*
     * The following listener ensures that the Tree is always large
     * enough to not need to show its own vertical scrollbar.
     */
    tree.addTreeListener(new TreeListener() {
        @Override
        public void treeExpanded(TreeEvent e) {
            int prefHeight = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
            tree.setSize(clientWidth, prefHeight);
        }

        @Override
        public void treeCollapsed(TreeEvent e) {
            int prefHeight = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
            tree.setSize(clientWidth, prefHeight);
        }
    });
    /*
     * The following listener ensures that a newly-selected item
     * in the Tree is always visible.
     */
    tree.addSelectionListener(widgetSelectedAdapter(e -> {
        TreeItem[] selectedItems = tree.getSelection();
        if (selectedItems.length > 0) {
            Rectangle itemRect = selectedItems[0].getBounds();
            Rectangle area = sc.getClientArea();
            Point origin = sc.getOrigin();
            if (itemRect.x < origin.x || itemRect.y < origin.y
                    || itemRect.x + itemRect.width > origin.x + area.width
                    || itemRect.y + itemRect.height > origin.y + area.height) {
                sc.setOrigin(itemRect.x, itemRect.y);
            }
        }
    }));
    /*
     * The following listener scrolls the Tree one item at a time
     * in response to MouseWheel events.
     */
    tree.addListener(SWT.MouseWheel, event -> {
        Point origin = sc.getOrigin();
        if (event.count < 0) {
            origin.y = Math.min(origin.y + tree.getItemHeight(), tree.getSize().y);
        } else {
            origin.y = Math.max(origin.y - tree.getItemHeight(), 0);
        }
        sc.setOrigin(origin);
    });

    Button downButton = new Button(shell, SWT.PUSH);
    downButton.setBounds(10, 220, 120, 30);
    downButton.setText("Down 10px");
    downButton.addListener(SWT.Selection, event -> sc.setOrigin(0, sc.getOrigin().y + 10));
    Button upButton = new Button(shell, SWT.PUSH);
    upButton.setBounds(140, 220, 120, 30);
    upButton.setText("Up 10px");
    upButton.addListener(SWT.Selection, event -> sc.setOrigin(0, sc.getOrigin().y - 10));
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    shell.setText("Show results as a bar chart in Tree");
    final Tree tree = new Tree(shell, SWT.BORDER);
    tree.setHeaderVisible(true);//  ww w. j a  va2 s.co  m
    tree.setLinesVisible(true);
    TreeColumn column1 = new TreeColumn(tree, SWT.NONE);
    column1.setText("Bug Status");
    column1.setWidth(100);
    final TreeColumn column2 = new TreeColumn(tree, SWT.NONE);
    column2.setText("Percent");
    column2.setWidth(200);
    String[] states = new String[] { "Resolved", "New", "Won't Fix", "Invalid" };
    String[] teams = new String[] { "UI", "SWT", "OSGI" };
    for (int i = 0; i < teams.length; i++) {
        TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText(teams[i]);
        for (int j = 0; j < states.length; j++) {
            TreeItem subItem = new TreeItem(item, SWT.NONE);
            subItem.setText(states[j]);
        }
    }

    /*
     * 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.PaintItem, new Listener() {
        int[] percents = new int[] { 50, 30, 5, 15 };

        @Override
        public void handleEvent(Event event) {
            if (event.index == 1) {
                TreeItem item = (TreeItem) event.item;
                TreeItem parent = item.getParentItem();
                if (parent != null) {
                    GC gc = event.gc;
                    int index = parent.indexOf(item);
                    int percent = percents[index];
                    Color foreground = gc.getForeground();
                    Color background = gc.getBackground();
                    gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
                    gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
                    int width = (column2.getWidth() - 1) * percent / 100;
                    gc.fillGradientRectangle(event.x, event.y, width, event.height, true);
                    Rectangle rect2 = new Rectangle(event.x, event.y, width - 1, event.height - 1);
                    gc.drawRectangle(rect2);
                    gc.setForeground(display.getSystemColor(SWT.COLOR_LIST_FOREGROUND));
                    String text = percent + "%";
                    Point size = event.gc.textExtent(text);
                    int offset = Math.max(0, (event.height - size.y) / 2);
                    gc.drawText(text, event.x + 2, event.y + offset, true);
                    gc.setForeground(background);
                    gc.setBackground(foreground);
                }
            }
        }
    });

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

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

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 322");
    shell.setBounds(10, 10, 300, 300);//  ww  w.j a  v a  2 s  .c  o  m
    final ScrolledComposite sc = new ScrolledComposite(shell, SWT.VERTICAL);
    sc.setBounds(10, 10, 280, 200);
    final int clientWidth = sc.getClientArea().width;

    final Tree tree = new Tree(sc, SWT.NONE);
    for (int i = 0; i < 99; i++) {
        TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText("item " + i);
        new TreeItem(item, SWT.NONE).setText("child");
    }
    sc.setContent(tree);
    int prefHeight = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
    tree.setSize(clientWidth, prefHeight);
    /*
     * The following listener ensures that the Tree is always large
     * enough to not need to show its own vertical scrollbar.
     */
    tree.addTreeListener(new TreeListener() {
        @Override
        public void treeExpanded(TreeEvent e) {
            int prefHeight = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
            tree.setSize(clientWidth, prefHeight);
        }

        @Override
        public void treeCollapsed(TreeEvent e) {
            int prefHeight = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
            tree.setSize(clientWidth, prefHeight);
        }
    });
    /*
     * The following listener ensures that a newly-selected item
     * in the Tree is always visible.
     */
    tree.addSelectionListener(widgetSelectedAdapter(e -> {
        TreeItem[] selectedItems = tree.getSelection();
        if (selectedItems.length > 0) {
            Rectangle itemRect = selectedItems[0].getBounds();
            Rectangle area = sc.getClientArea();
            Point origin = sc.getOrigin();
            if (itemRect.x < origin.x || itemRect.y < origin.y
                    || itemRect.x + itemRect.width > origin.x + area.width
                    || itemRect.y + itemRect.height > origin.y + area.height) {
                sc.setOrigin(itemRect.x, itemRect.y);
            }
        }
    }));
    /*
     * The following listener scrolls the Tree one item at a time
     * in response to MouseWheel events.
     */
    tree.addListener(SWT.MouseWheel, event -> {
        Point origin = sc.getOrigin();
        if (event.count < 0) {
            origin.y = Math.min(origin.y + tree.getItemHeight(), tree.getSize().y);
        } else {
            origin.y = Math.max(origin.y - tree.getItemHeight(), 0);
        }
        sc.setOrigin(origin);
    });

    Button disableButton = new Button(shell, SWT.PUSH);
    disableButton.setBounds(10, 220, 120, 30);
    disableButton.setText("Disable");
    disableButton.addListener(SWT.Selection, event -> tree.setEnabled(false));
    Button enableButton = new Button(shell, SWT.PUSH);
    enableButton.setBounds(140, 220, 120, 30);
    enableButton.setText("Enable");
    enableButton.addListener(SWT.Selection, event -> tree.setEnabled(true));

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