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

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

Introduction

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

Prototype

public Composite(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: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);/* w w  w.ja va2 s . c om*/

    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.Snippet338.java

public static void main(String[] args) {
    Display display = new Display();
    Shell parentShell = new Shell(display);
    parentShell.setText("Snippet 338");
    parentShell.setBounds(10, 10, 100, 100);
    parentShell.open();/*  ww  w  .  j a va 2  s . co  m*/

    Shell childShell = new Shell(parentShell);
    childShell.setLayout(new GridLayout());
    TabFolder folder = new TabFolder(childShell, SWT.NONE);
    folder.setLayout(new FillLayout());
    TabItem tab1 = new TabItem(folder, SWT.NONE);
    tab1.setText("Tab &1");
    new TabItem(folder, SWT.NONE).setText("Tab &2");
    Composite composite = new Composite(folder, SWT.NONE);
    composite.setLayout(new GridLayout());
    tab1.setControl(composite);
    Text text1 = new Text(composite, SWT.SINGLE);

    /* canvas represents a custom control */
    final Canvas canvas = new Canvas(composite, SWT.BORDER);
    canvas.setLayoutData(new GridData(300, 200));
    canvas.addListener(SWT.Paint, event -> {
        if (canvas.isFocusControl()) {
            event.gc.drawText(
                    "focus is here, custom traverse keys:\n\tN: Tab next\n\tP: Tab previous\n\tR: Return\n\tE: Esc\n\tT: Tab Folder next page",
                    0, 0);
        } else {
            event.gc.drawString("focus is not in this control", 0, 0);
        }
    });
    canvas.addListener(SWT.KeyDown, event -> {
        int traversal = SWT.NONE;
        switch (event.keyCode) {
        case 'n':
            traversal = SWT.TRAVERSE_TAB_NEXT;
            break;
        case 'p':
            traversal = SWT.TRAVERSE_TAB_PREVIOUS;
            break;
        case 'r':
            traversal = SWT.TRAVERSE_RETURN;
            break;
        case 'e':
            traversal = SWT.TRAVERSE_ESCAPE;
            break;
        case 't':
            traversal = SWT.TRAVERSE_PAGE_NEXT;
            break;
        }
        if (traversal != SWT.NONE) {
            event.doit = true; /* this will be the Traverse event's initial doit value */
            canvas.traverse(traversal, event);
        }
    });
    canvas.addFocusListener(focusLostAdapter(e -> canvas.redraw()));
    canvas.addFocusListener(focusGainedAdapter(e -> canvas.redraw()));

    Text text2 = new Text(composite, SWT.SINGLE);
    Button button = new Button(childShell, SWT.PUSH);
    button.setText("Default &Button");
    button.addListener(SWT.Selection, event -> System.out.println("Default button selected"));
    childShell.setDefaultButton(button);

    Listener printTraverseListener = event -> {
        StringBuilder buffer = new StringBuilder("Traverse ");
        buffer.append(event.widget);
        buffer.append(" type=");
        switch (event.detail) {
        case SWT.TRAVERSE_ARROW_NEXT:
            buffer.append("TRAVERSE_ARROW_NEXT");
            break;
        case SWT.TRAVERSE_ARROW_PREVIOUS:
            buffer.append("TRAVERSE_ARROW_NEXT");
            break;
        case SWT.TRAVERSE_ESCAPE:
            buffer.append("TRAVERSE_ESCAPE");
            break;
        case SWT.TRAVERSE_MNEMONIC:
            buffer.append("TRAVERSE_MNEMONIC");
            break;
        case SWT.TRAVERSE_PAGE_NEXT:
            buffer.append("TRAVERSE_PAGE_NEXT");
            break;
        case SWT.TRAVERSE_PAGE_PREVIOUS:
            buffer.append("TRAVERSE_PAGE_PREVIOUS");
            break;
        case SWT.TRAVERSE_RETURN:
            buffer.append("TRAVERSE_RETURN");
            break;
        case SWT.TRAVERSE_TAB_NEXT:
            buffer.append("TRAVERSE_TAB_NEXT");
            break;
        case SWT.TRAVERSE_TAB_PREVIOUS:
            buffer.append("TRAVERSE_TAB_PREVIOUS");
            break;
        }
        buffer.append(" doit=" + event.doit);
        buffer.append(" keycode=" + event.keyCode);
        buffer.append(" char=" + event.character);
        buffer.append(" stateMask=" + event.stateMask);
        System.out.println(buffer.toString());
    };
    childShell.addListener(SWT.Traverse, printTraverseListener);
    folder.addListener(SWT.Traverse, printTraverseListener);
    composite.addListener(SWT.Traverse, printTraverseListener);
    canvas.addListener(SWT.Traverse, printTraverseListener);
    button.addListener(SWT.Traverse, printTraverseListener);
    text1.addListener(SWT.Traverse, printTraverseListener);
    text2.addListener(SWT.Traverse, printTraverseListener);

    childShell.pack();
    childShell.open();
    text1.setFocus();
    while (!parentShell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:ScrolledCompositeCreate.java

public static void main(String[] args) {
    Display display = new Display();
    Image image1 = display.getSystemImage(SWT.ICON_WORKING);
    Image image2 = display.getSystemImage(SWT.ICON_QUESTION);
    Image image3 = display.getSystemImage(SWT.ICON_ERROR);

    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    final ScrolledComposite scrollComposite = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.BORDER);

    final Composite parent = new Composite(scrollComposite, SWT.NONE);
    for (int i = 0; i <= 50; i++) {
        Label label = new Label(parent, SWT.NONE);
        if (i % 3 == 0)
            label.setImage(image1);//from   w w  w .  j  av  a  2 s.  com
        if (i % 3 == 1)
            label.setImage(image2);
        if (i % 3 == 2)
            label.setImage(image3);
    }
    RowLayout layout = new RowLayout(SWT.HORIZONTAL);
    layout.wrap = true;
    parent.setLayout(layout);

    scrollComposite.setContent(parent);
    scrollComposite.setExpandVertical(true);
    scrollComposite.setExpandHorizontal(true);
    scrollComposite.addControlListener(new ControlAdapter() {
        public void controlResized(ControlEvent e) {
            Rectangle r = scrollComposite.getClientArea();
            scrollComposite.setMinSize(parent.computeSize(r.width, SWT.DEFAULT));
        }
    });

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

From source file:TableColumnFirstFix.java

public static void main(String[] args) {
    int rowCount = 40;
    int columnCount = 15;
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    Composite parent = new Composite(shell, SWT.BORDER);
    GridLayout layout = new GridLayout(2, false);
    layout.marginWidth = layout.marginHeight = layout.horizontalSpacing = 0;
    parent.setLayout(layout);/*from ww  w.j  a  v  a 2s .  com*/
    final Table leftTable = new Table(parent, SWT.MULTI | SWT.FULL_SELECTION);
    leftTable.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true));
    leftTable.setHeaderVisible(true);
    final Table rightTable = new Table(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
    rightTable.setHeaderVisible(true);
    GridData table2Data = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 2);
    rightTable.setLayoutData(table2Data);
    // Create columns
    TableColumn column1 = new TableColumn(leftTable, SWT.NONE);
    column1.setText("Name");
    column1.setWidth(150);
    for (int i = 0; i < columnCount; i++) {
        TableColumn column = new TableColumn(rightTable, SWT.NONE);
        column.setText("Value " + i);
        column.setWidth(200);
    }
    // Create rows
    for (int i = 0; i < rowCount; i++) {
        TableItem item = new TableItem(leftTable, SWT.NONE);
        item.setText("item " + i);
        item = new TableItem(rightTable, SWT.NONE);
        for (int j = 0; j < columnCount; j++) {
            item.setText(j, "Item " + i + " value @ " + j);
        }
    }
    // Make selection the same in both tables
    leftTable.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            rightTable.setSelection(leftTable.getSelectionIndices());
        }
    });
    rightTable.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            leftTable.setSelection(rightTable.getSelectionIndices());
        }
    });
    // On Windows, the selection is gray if the table does not have focus.
    // To make both tables appear in focus, draw teh selection background here.
    // This part only works on version 3.2 or later.
    Listener eraseListener = new Listener() {
        public void handleEvent(Event event) {
            if ((event.detail & SWT.SELECTED) != 0) {
                GC gc = event.gc;
                Rectangle rect = event.getBounds();
                gc.setForeground(display.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
                gc.setBackground(display.getSystemColor(SWT.COLOR_LIST_SELECTION));
                gc.fillRectangle(rect);
                event.detail &= ~SWT.SELECTED;
            }
        }
    };

    leftTable.addListener(SWT.EraseItem, eraseListener);
    rightTable.addListener(SWT.EraseItem, eraseListener);
    // Make vertical scrollbars scroll together
    ScrollBar vBarLeft = leftTable.getVerticalBar();
    vBarLeft.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            rightTable.setTopIndex(leftTable.getTopIndex());
        }
    });
    ScrollBar vBarRight = rightTable.getVerticalBar();
    vBarRight.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            leftTable.setTopIndex(rightTable.getTopIndex());
        }
    });
    // Horizontal bar on second table takes up a little extra space.
    // To keep vertical scroll bars in sink, force table1 to end above
    // horizontal scrollbar
    ScrollBar hBarRight = rightTable.getHorizontalBar();
    Label spacer = new Label(parent, SWT.NONE);
    GridData spacerData = new GridData();
    spacerData.heightHint = hBarRight.getSize().y;
    spacer.setVisible(false);
    parent.setBackground(leftTable.getBackground());

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

From source file:ScrollWidgetViewFocusIn.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    Button b1 = new Button(shell, SWT.PUSH);
    b1.setText("top");
    b1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
    final ScrolledComposite sc = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    sc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    Composite c = new Composite(sc, SWT.NONE);
    c.setLayout(new GridLayout(10, true));
    for (int i = 0; i < 300; i++) {
        Button b = new Button(c, SWT.PUSH);
        b.setText("Button " + i);
    }/*from   w  w w  .ja va2  s. c o  m*/
    Button b2 = new Button(shell, SWT.PUSH);
    b2.setText("bottom");
    b2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));

    sc.setContent(c);
    sc.setExpandHorizontal(true);
    sc.setExpandVertical(true);
    sc.setMinSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT));

    Listener listener = new Listener() {
        public void handleEvent(Event e) {
            Control child = (Control) e.widget;
            Rectangle bounds = child.getBounds();
            Rectangle area = sc.getClientArea();
            Point origin = sc.getOrigin();
            if (origin.x > bounds.x)
                origin.x = Math.max(0, bounds.x);
            if (origin.y > bounds.y)
                origin.y = Math.max(0, bounds.y);
            if (origin.x + area.width < bounds.x + bounds.width)
                origin.x = Math.max(0, bounds.x + bounds.width - area.width);
            if (origin.y + area.height < bounds.y + bounds.height)
                origin.y = Math.max(0, bounds.y + bounds.height - area.height);
            sc.setOrigin(origin);
        }
    };
    Control[] controls = c.getChildren();
    for (int i = 0; i < controls.length; i++) {
        controls[i].addListener(SWT.Activate, listener);
    }
    shell.setSize(300, 500);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    display = new Display();
    shell = new Shell(display);
    shell.setText("Snippet 336");
    shell.setLayout(new GridLayout());
    TabFolder folder = new TabFolder(shell, SWT.NONE);
    folder.setLayoutData(new GridData(GridData.FILL_BOTH));

    //Progress tab
    TabItem item = new TabItem(folder, SWT.NONE);
    item.setText("Progress");
    Composite composite = new Composite(folder, SWT.NONE);
    composite.setLayout(new GridLayout());
    item.setControl(composite);//w w w .ja va 2s  . co m
    Listener listener = event -> {
        Button button = (Button) event.widget;
        if (!button.getSelection())
            return;
        TaskItem item1 = getTaskBarItem();
        if (item1 != null) {
            int state = ((Integer) button.getData()).intValue();
            item1.setProgressState(state);
        }
    };
    Group group = new Group(composite, SWT.NONE);
    group.setText("State");
    group.setLayout(new GridLayout());
    group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    Button button;
    String[] stateLabels = { "SWT.DEFAULT", "SWT.INDETERMINATE", "SWT.NORMAL", "SWT.ERROR", "SWT.PAUSED" };
    int[] states = { SWT.DEFAULT, SWT.INDETERMINATE, SWT.NORMAL, SWT.ERROR, SWT.PAUSED };
    for (int i = 0; i < states.length; i++) {
        button = new Button(group, SWT.RADIO);
        button.setText(stateLabels[i]);
        button.setData(Integer.valueOf(states[i]));
        button.addListener(SWT.Selection, listener);
        if (i == 0)
            button.setSelection(true);
    }
    group = new Group(composite, SWT.NONE);
    group.setText("Value");
    group.setLayout(new GridLayout(2, false));
    group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    Label label = new Label(group, SWT.NONE);
    label.setText("Progress");
    final Scale scale = new Scale(group, SWT.NONE);
    scale.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    scale.addListener(SWT.Selection, event -> {
        TaskItem item1 = getTaskBarItem();
        if (item1 != null)
            item1.setProgress(scale.getSelection());
    });

    //Overlay text tab
    item = new TabItem(folder, SWT.NONE);
    item.setText("Text");
    composite = new Composite(folder, SWT.NONE);
    composite.setLayout(new GridLayout());
    item.setControl(composite);
    group = new Group(composite, SWT.NONE);
    group.setText("Enter a short text:");
    group.setLayout(new GridLayout(2, false));
    group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    final Text text = new Text(group, SWT.BORDER | SWT.SINGLE);
    GridData data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 2;
    text.setLayoutData(data);
    button = new Button(group, SWT.PUSH);
    button.setText("Set");
    button.addListener(SWT.Selection, event -> {
        TaskItem item1 = getTaskBarItem();
        if (item1 != null)
            item1.setOverlayText(text.getText());
    });
    button = new Button(group, SWT.PUSH);
    button.setText("Clear");
    button.addListener(SWT.Selection, event -> {
        text.setText("");
        TaskItem item1 = getTaskBarItem();
        if (item1 != null)
            item1.setOverlayText("");
    });

    //Overlay image tab
    item = new TabItem(folder, SWT.NONE);
    item.setText("Image");
    composite = new Composite(folder, SWT.NONE);
    composite.setLayout(new GridLayout());
    item.setControl(composite);
    Listener listener3 = event -> {
        Button button1 = (Button) event.widget;
        if (!button1.getSelection())
            return;
        TaskItem item1 = getTaskBarItem();
        if (item1 != null) {
            String text1 = button1.getText();
            Image image = null;
            if (!text1.equals("NONE"))
                image = new Image(display, Snippet336.class.getResourceAsStream(text1));
            Image oldImage = item1.getOverlayImage();
            item1.setOverlayImage(image);
            if (oldImage != null)
                oldImage.dispose();
        }
    };
    group = new Group(composite, SWT.NONE);
    group.setText("Images");
    group.setLayout(new GridLayout());
    group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    button = new Button(group, SWT.RADIO);
    button.setText("NONE");
    button.addListener(SWT.Selection, listener3);
    button.setSelection(true);
    String[] images = { "eclipse.png", "pause.gif", "run.gif", "warning.gif" };
    for (int i = 0; i < images.length; i++) {
        button = new Button(group, SWT.RADIO);
        button.setText(images[i]);
        button.addListener(SWT.Selection, listener3);
    }
    shell.pack();
    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  a  va 2s  .c  o m
    }
    final TreeItem[] lastItem = new TreeItem[1];
    final TreeEditor editor = new TreeEditor(tree);
    tree.addListener(SWT.Selection, event -> {
        final TreeItem item = (TreeItem) event.item;
        if (item != null && item == lastItem[0]) {
            boolean showBorder = true;
            final Composite composite = new Composite(tree, SWT.NONE);
            if (showBorder)
                composite.setBackground(black);
            final Text text = new Text(composite, SWT.NONE);
            final int inset = showBorder ? 1 : 0;
            composite.addListener(SWT.Resize, e1 -> {
                Rectangle rect1 = composite.getClientArea();
                text.setBounds(rect1.x + inset, rect1.y + inset, rect1.width - inset * 2,
                        rect1.height - inset * 2);
            });
            Listener textListener = e2 -> {
                switch (e2.type) {
                case SWT.FocusOut:
                    item.setText(text.getText());
                    composite.dispose();
                    break;
                case SWT.Verify:
                    String newText = text.getText();
                    String leftText = newText.substring(0, e2.start);
                    String rightText = newText.substring(e2.end, newText.length());
                    GC gc = new GC(text);
                    Point size = gc.textExtent(leftText + e2.text + rightText);
                    gc.dispose();
                    size = text.computeSize(size.x, SWT.DEFAULT);
                    editor.horizontalAlignment = SWT.LEFT;
                    Rectangle itemRect = item.getBounds(), rect2 = tree.getClientArea();
                    editor.minimumWidth = Math.max(size.x, itemRect.width) + inset * 2;
                    int left = itemRect.x, right = rect2.x + rect2.width;
                    editor.minimumWidth = Math.min(editor.minimumWidth, right - left);
                    editor.minimumHeight = size.y + inset * 2;
                    editor.layout();
                    break;
                case SWT.Traverse:
                    switch (e2.detail) {
                    case SWT.TRAVERSE_RETURN:
                        item.setText(text.getText());
                        //FALL THROUGH
                    case SWT.TRAVERSE_ESCAPE:
                        composite.dispose();
                        e2.doit = false;
                    }
                    break;
                }
            };
            text.addListener(SWT.FocusOut, textListener);
            text.addListener(SWT.Traverse, textListener);
            text.addListener(SWT.Verify, textListener);
            editor.setEditor(composite, item);
            text.setText(item.getText());
            text.selectAll();
            text.setFocus();
        }
        lastItem[0] = item;
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    //define a pattern on an image
    final Image image = new Image(display, 1000, 1000);
    Color blue = display.getSystemColor(SWT.COLOR_BLUE);
    Color yellow = display.getSystemColor(SWT.COLOR_YELLOW);
    Color white = display.getSystemColor(SWT.COLOR_WHITE);
    GC gc = new GC(image);
    gc.setBackground(white);//from  ww w. jav  a 2 s. co  m
    gc.setForeground(yellow);
    gc.fillGradientRectangle(0, 0, 1000, 1000, true);
    for (int i = -500; i < 1000; i += 10) {
        gc.setForeground(blue);
        gc.drawLine(i, 0, 500 + i, 1000);
        gc.drawLine(500 + i, 0, i, 1000);
    }
    gc.dispose();
    final Pattern pattern;
    try {
        pattern = new Pattern(display, image);
    } catch (SWTException e) {
        //Advanced Graphics not supported.
        //This new API requires the Cairo Vector engine on GTK and GDI+ on Windows.
        System.out.println(e.getMessage());
        display.dispose();
        return;
    }

    Shell shell = new Shell(display);
    shell.setText("Snippet 200");
    shell.setLayout(new FillLayout());
    Composite c = new Composite(shell, SWT.DOUBLE_BUFFERED);
    c.addListener(SWT.Paint, event -> {
        Rectangle r = ((Composite) event.widget).getClientArea();
        GC gc1 = event.gc;
        gc1.setBackgroundPattern(pattern);
        gc1.fillOval(5, 5, r.width - 10, r.height - 10);
    });
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    image.dispose();
    pattern.dispose();
    display.dispose();
}

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

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Composite comp = new Composite(shell, SWT.NONE);
    comp.setLayout(new FillLayout());
    GLData data = new GLData();
    data.doubleBuffer = true;/*from   ww  w.  j  a  v  a2s. c  o m*/
    final GLCanvas canvas = new GLCanvas(comp, SWT.NONE, data);

    canvas.setCurrent();
    GL.createCapabilities();

    canvas.addListener(SWT.Resize, event -> {
        Rectangle bounds = canvas.getBounds();
        float fAspect = (float) bounds.width / (float) bounds.height;
        canvas.setCurrent();
        GL.createCapabilities();
        GL11.glViewport(0, 0, bounds.width, bounds.height);
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        float near = 0.5f;
        float bottom = -near * (float) Math.tan(45.f / 2);
        float left = fAspect * bottom;
        GL11.glFrustum(left, -left, bottom, -bottom, near, 400.f);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glLoadIdentity();
    });

    GL11.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    GL11.glColor3f(1.0f, 0.0f, 0.0f);
    GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
    GL11.glClearDepth(1.0);
    GL11.glLineWidth(2);
    GL11.glEnable(GL11.GL_DEPTH_TEST);

    shell.setText("SWT/LWJGL Example");
    shell.setSize(640, 480);
    shell.open();

    final Runnable run = new Runnable() {
        int rot = 0;

        @Override
        public void run() {
            if (!canvas.isDisposed()) {
                canvas.setCurrent();
                GL.createCapabilities();
                GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
                GL11.glClearColor(.3f, .5f, .8f, 1.0f);
                GL11.glLoadIdentity();
                GL11.glTranslatef(0.0f, 0.0f, -10.0f);
                float frot = rot;
                GL11.glRotatef(0.15f * rot, 2.0f * frot, 10.0f * frot, 1.0f);
                GL11.glRotatef(0.3f * rot, 3.0f * frot, 1.0f * frot, 1.0f);
                rot++;
                GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);
                GL11.glColor3f(0.9f, 0.9f, 0.9f);
                drawTorus(1, 1.9f + ((float) Math.sin((0.004f * frot))), 15, 15);
                canvas.swapBuffers();
                display.asyncExec(this);
            }
        }
    };
    canvas.addListener(SWT.Paint, event -> run.run());
    display.asyncExec(run);

    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  va 2 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();
}