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

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

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Composite 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.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  2 s  .  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: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  w  w . java 2  s  .  com
    }
    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.Snippet77.java

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

    final Composite comp = new Composite(shell, SWT.NONE);
    final Table table = new Table(comp, SWT.BORDER | SWT.V_SCROLL);
    table.setHeaderVisible(true);/*  w ww.j a va2 s  .c o  m*/
    table.setLinesVisible(true);
    final TableColumn column1 = new TableColumn(table, SWT.NONE);
    column1.setText("Column 1");
    final TableColumn column2 = new TableColumn(table, SWT.NONE);
    column2.setText("Column 2");
    for (int i = 0; i < 10; i++) {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText(new String[] { "item 0" + i, "item 1" + i });
    }
    comp.addControlListener(ControlListener.controlResizedAdapter(e -> {
        Rectangle area = comp.getClientArea();
        Point size = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
        ScrollBar vBar = table.getVerticalBar();
        int width = area.width - table.computeTrim(0, 0, 0, 0).width - vBar.getSize().x;
        if (size.y > area.height + table.getHeaderHeight()) {
            // Subtract the scrollbar width from the total column width
            // if a vertical scrollbar will be required
            Point vBarSize = vBar.getSize();
            width -= vBarSize.x;
        }
        Point oldSize = table.getSize();
        if (oldSize.x > area.width) {
            // table is getting smaller so make the columns
            // smaller first and then resize the table to
            // match the client area width
            column1.setWidth(width / 3);
            column2.setWidth(width - column1.getWidth());
            table.setSize(area.width, area.height);
        } else {
            // table is getting bigger so make the table
            // bigger first and then make the columns wider
            // to match the client area width
            table.setSize(area.width, area.height);
            column1.setWidth(width / 3);
            column2.setWidth(width - column1.getWidth());
        }
    }));

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

From source file:Snippet77.java

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

    final Composite comp = new Composite(shell, SWT.NONE);
    final Table table = new Table(comp, SWT.BORDER | SWT.V_SCROLL);
    table.setHeaderVisible(true);//from w w  w  .j av a2s  . c  o  m
    table.setLinesVisible(true);
    final TableColumn column1 = new TableColumn(table, SWT.NONE);
    column1.setText("Column 1");
    final TableColumn column2 = new TableColumn(table, SWT.NONE);
    column2.setText("Column 2");
    for (int i = 0; i < 10; i++) {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText(new String[] { "item 0" + i, "item 1" + i });
    }
    comp.addControlListener(new ControlAdapter() {
        public void controlResized(ControlEvent e) {
            Rectangle area = comp.getClientArea();
            Point preferredSize = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
            int width = area.width - 2 * table.getBorderWidth();
            if (preferredSize.y > area.height + table.getHeaderHeight()) {
                // Subtract the scrollbar width from the total column width
                // if a vertical scrollbar will be required
                Point vBarSize = table.getVerticalBar().getSize();
                width -= vBarSize.x;
            }
            Point oldSize = table.getSize();
            if (oldSize.x > area.width) {
                // table is getting smaller so make the columns 
                // smaller first and then resize the table to
                // match the client area width
                column1.setWidth(width / 3);
                column2.setWidth(width - column1.getWidth());
                table.setSize(area.width, area.height);
            } else {
                // table is getting bigger so make the table 
                // bigger first and then make the columns wider
                // to match the client area width
                table.setSize(area.width, area.height);
                column1.setWidth(width / 3);
                column2.setWidth(width - column1.getWidth());
            }
        }
    });

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

From source file:TableColumnResizeTableResize.java

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

    final Composite comp = new Composite(shell, SWT.NONE);
    final Table table = new Table(comp, SWT.BORDER | SWT.V_SCROLL);
    table.setHeaderVisible(true);/*from w ww  .  j a  v  a2 s. c o m*/
    table.setLinesVisible(true);
    final TableColumn column1 = new TableColumn(table, SWT.NONE);
    column1.setText("Column 1");
    final TableColumn column2 = new TableColumn(table, SWT.NONE);
    column2.setText("Column 2");
    for (int i = 0; i < 10; i++) {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText(new String[] { "item 0" + i, "item 1" + i });
    }
    comp.addControlListener(new ControlAdapter() {
        public void controlResized(ControlEvent e) {
            Rectangle area = comp.getClientArea();
            Point preferredSize = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
            int width = area.width - 2 * table.getBorderWidth();
            if (preferredSize.y > area.height + table.getHeaderHeight()) {
                // Subtract the scrollbar width from the total column width
                // if a vertical scrollbar will be required
                Point vBarSize = table.getVerticalBar().getSize();
                width -= vBarSize.x;
            }
            Point oldSize = table.getSize();
            if (oldSize.x > area.width) {
                // table is getting smaller so make the columns
                // smaller first and then resize the table to
                // match the client area width
                column1.setWidth(width / 3);
                column2.setWidth(width - column1.getWidth());
                table.setSize(area.width, area.height);
            } else {
                // table is getting bigger so make the table
                // bigger first and then make the columns wider
                // to match the client area width
                table.setSize(area.width, area.height);
                column1.setWidth(width / 3);
                column2.setWidth(width - column1.getWidth());
            }
        }
    });

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

From source file:widgetTest3.java

/** * Images ** */

public static void doubleBuffering(Composite composite) {
    //       Create canvas
    final Canvas canvas = new Canvas(composite, SWT.BORDER);
    //       Get white system color
    Color white = canvas.getDisplay().getSystemColor(SWT.COLOR_WHITE);
    //       Set canvas background to white
    canvas.setBackground(white);/*from   ww w  .j  a  v  a  2 s  .co m*/
    //       Add paint listener
    canvas.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent e) {
            // Get Display instance from the event object
            Display display = e.display;
            // Get black and red system color - don't dispose these
            Color black = display.getSystemColor(SWT.COLOR_BLACK);
            Color red = display.getSystemColor(SWT.COLOR_RED);
            // Get the graphics context from event object
            GC gc = e.gc;
            // Get the widget that caused the event
            Composite source = (Composite) e.widget;
            // Get the size of this widgets client area
            Rectangle rect = source.getClientArea();
            // Create buffer for double buffering
            Image buffer = new Image(display, rect.width, rect.height);
            // Create graphics context for this buffer
            GC bufferGC = new GC(buffer);
            // perform drawing operations
            bufferGC.setBackground(red);
            bufferGC.fillRectangle(5, 5, rect.width - 10, rect.height - 10);
            bufferGC.setForeground(black);
            bufferGC.drawRectangle(5, 5, rect.width - 10, rect.height - 10);
            bufferGC.setBackground(source.getBackground());
            bufferGC.fillRectangle(10, 10, rect.width - 20, rect.height - 20);
            // Now draw the buffered image to the target drawable
            gc.drawImage(buffer, 0, 0);
            // Dispose of the buffer's graphics context
            bufferGC.dispose();
            // Dispose of the buffer
            buffer.dispose();
        }
    });
}

From source file:org.eclipse.swt.examples.launcher.SplitLayout.java

/**
 * @see Layout#layout(Composite, boolean)
 *///from www. jav  a 2s. co m
@Override
protected void layout(Composite composite, boolean flushCache) {
    Rectangle clientArea = composite.getClientArea();
    computeSize(composite, clientArea.width, clientArea.height, false);

    Control[] children = composite.getChildren();
    clientArea.x += marginLeft;
    clientArea.y += marginTop;
    clientArea.width -= marginRight + marginLeft;
    clientArea.height -= marginBottom + marginTop;
    Point position = new Point(clientArea.x, clientArea.y);

    for (Control child : children) {
        final Rectangle bounds;
        if (splitDirection == splitHorizontally) {
            int height = clientArea.height / children.length;
            bounds = new Rectangle(position.x, position.y, clientArea.width, height);
            position.y += height + spacing;
        } else {
            int width = clientArea.width / children.length;
            bounds = new Rectangle(position.x, position.y, width, clientArea.height);
            position.x += width + spacing;
        }
        bounds.width = Math.max(bounds.width, 0);
        bounds.height = Math.max(bounds.height, 0);
        child.setBounds(bounds);
    }
}

From source file:YetAnotherBorderLayout.java

/**
 * This does the work of laying out our controls.
 * /* ww w  .  j av  a2 s. c  om*/
 * @see org.eclipse.swt.widgets.Layout#layout(org.eclipse.swt.widgets.Composite,
 *      boolean)
 */
protected void layout(Composite composite, boolean flushCache) {
    getControls(composite);
    Rectangle rect = composite.getClientArea();
    int left = rect.x, right = rect.width, top = rect.y, bottom = rect.height;
    if (north != null) {
        Point pt = getSize(north, flushCache);
        north.setBounds(left, top, rect.width, pt.y);
        top += pt.y;
    }
    if (south != null) {
        Point pt = getSize(south, flushCache);
        south.setBounds(left, rect.height - pt.y, rect.width, pt.y);
        bottom -= pt.y;
    }
    if (east != null) {
        Point pt = getSize(east, flushCache);
        east.setBounds(rect.width - pt.x, top, pt.x, (bottom - top));
        right -= pt.x;
    }
    if (west != null) {
        Point pt = getSize(west, flushCache);
        west.setBounds(left, top, pt.x, (bottom - top));
        left += pt.x;
    }
    if (center != null) {
        center.setBounds(left, top, (right - left), (bottom - top));
    }
}

From source file:BorderLayout.java

protected void layout(Composite composite, boolean flushCache) {
    if (flushCache || sizes == null)
        refreshSizes(composite.getChildren());

    Rectangle clientArea = composite.getClientArea();

    // Enough space for all.
    if (controls[NORTH] != null) {
        controls[NORTH].setBounds(clientArea.x, clientArea.y, clientArea.width, sizes[NORTH].y);
    }//ww w  .  j  av a 2  s  .  c  om
    if (controls[SOUTH] != null) {
        controls[SOUTH].setBounds(clientArea.x, clientArea.y + clientArea.height - sizes[SOUTH].y,
                clientArea.width, sizes[SOUTH].y);
    }
    if (controls[WEST] != null) {
        controls[WEST].setBounds(clientArea.x, clientArea.y + sizes[NORTH].y, sizes[WEST].x,
                clientArea.height - sizes[NORTH].y - sizes[SOUTH].y);
    }
    if (controls[EAST] != null) {
        controls[EAST].setBounds(clientArea.x + clientArea.width - sizes[EAST].x, clientArea.y + sizes[NORTH].y,
                sizes[EAST].x, clientArea.height - sizes[NORTH].y - sizes[SOUTH].y);
    }
    if (controls[CENTER] != null) {
        controls[CENTER].setBounds(clientArea.x + sizes[WEST].x, clientArea.y + sizes[NORTH].y,
                clientArea.width - sizes[WEST].x - sizes[EAST].x,
                clientArea.height - sizes[NORTH].y - sizes[SOUTH].y);
    }

}

From source file:RadialLayout.java

private Point[] calculateControlPositions(Composite composite) {
    int controlCount = composite.getChildren().length;
    int stepsPerHemisphere = stepsPerHemisphere(controlCount);
    Point[] positions = new Point[controlCount];

    Point maxControlDimensions = calculateMaxDimensions(composite.getChildren());
    int maxControlWidth = maxControlDimensions.x;

    Rectangle clientArea = composite.getClientArea();
    int smallestDimension = Math.min(clientArea.width, clientArea.height);
    int radius = (smallestDimension / 2) - maxControlWidth;
    Point center = new Point(clientArea.width / 2, clientArea.height / 2);
    long radiusSquared = radius * radius;

    int stepXDistance = calculateStepDistance(radius * 2, stepsPerHemisphere);

    int signMultiplier = 1;
    int x = -radius;
    int y;/*w  w  w  .  j ava  2s.  co m*/
    Control[] controls = composite.getChildren();
    for (int i = 0; i < controlCount; i++) {
        Point currSize = controls[i].getSize();
        long xSquared = x * x;

        int sqrRoot = (int) Math.sqrt(radiusSquared - xSquared);
        y = signMultiplier * sqrRoot;
        int translatedX = x + center.x;
        int translatedY = y + center.y;
        positions[i] = new Point(translatedX - (currSize.x / 2), translatedY - (currSize.y / 2));

        x = x + (signMultiplier * stepXDistance);
        //we've finished the upper hemisphere, now do the lower
        if (x >= radius) {
            x = radius - (x - radius);
            signMultiplier = -1;
        }
    }

    return positions;
}