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

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

Introduction

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

Prototype

public Control[] getChildren() 

Source Link

Document

Returns a (possibly empty) array containing the receiver's children.

Usage

From source file:Snippet115.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new RowLayout(SWT.VERTICAL));
    Composite c1 = new Composite(shell, SWT.BORDER | SWT.NO_RADIO_GROUP);
    c1.setLayout(new RowLayout());
    Composite c2 = new Composite(shell, SWT.BORDER | SWT.NO_RADIO_GROUP);
    c2.setLayout(new RowLayout());
    final Composite[] composites = new Composite[] { c1, c2 };
    Listener radioGroup = new Listener() {
        public void handleEvent(Event event) {
            for (int i = 0; i < composites.length; i++) {
                Composite composite = composites[i];
                Control[] children = composite.getChildren();
                for (int j = 0; j < children.length; j++) {
                    Control child = children[j];
                    if (child instanceof Button) {
                        Button button = (Button) child;
                        if ((button.getStyle() & SWT.RADIO) != 0)
                            button.setSelection(false);
                    }//from  w  w  w.j  a  v a  2 s .c o  m
                }
            }
            Button button = (Button) event.widget;
            button.setSelection(true);
        }
    };
    for (int i = 0; i < 4; i++) {
        Button button = new Button(c1, SWT.RADIO);
        button.setText("Button " + i);
        button.addListener(SWT.Selection, radioGroup);
    }
    for (int i = 0; i < 4; i++) {
        Button button = new Button(c2, SWT.RADIO);
        button.setText("Button " + (i + 4));
        button.addListener(SWT.Selection, radioGroup);
    }
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 115");
    shell.setLayout(new RowLayout(SWT.VERTICAL));
    Composite c1 = new Composite(shell, SWT.BORDER | SWT.NO_RADIO_GROUP);
    c1.setLayout(new RowLayout());
    Composite c2 = new Composite(shell, SWT.BORDER | SWT.NO_RADIO_GROUP);
    c2.setLayout(new RowLayout());
    final Composite[] composites = new Composite[] { c1, c2 };
    Listener radioGroup = event -> {/*from w ww.  j  a v  a2 s  .c  om*/
        for (int i = 0; i < composites.length; i++) {
            Composite composite = composites[i];
            Control[] children = composite.getChildren();
            for (int j = 0; j < children.length; j++) {
                Control child = children[j];
                if (child instanceof Button) {
                    Button button1 = (Button) child;
                    if ((button1.getStyle() & SWT.RADIO) != 0)
                        button1.setSelection(false);
                }
            }
        }
        Button button2 = (Button) event.widget;
        button2.setSelection(true);
    };
    for (int i = 0; i < 4; i++) {
        Button button = new Button(c1, SWT.RADIO);
        button.setText("Button " + i);
        button.addListener(SWT.Selection, radioGroup);
    }
    for (int i = 0; i < 4; i++) {
        Button button = new Button(c2, SWT.RADIO);
        button.setText("Button " + (i + 4));
        button.addListener(SWT.Selection, radioGroup);
    }
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 6");
    shell.setLayout(new GridLayout());
    final Composite c = new Composite(shell, SWT.NONE);
    GridLayout layout = new GridLayout();
    layout.numColumns = 3;/*from ww w .  j a  v a2  s . c om*/
    c.setLayout(layout);
    for (int i = 0; i < 10; i++) {
        Button b = new Button(c, SWT.PUSH);
        b.setText("Button " + i);
    }

    Button b = new Button(shell, SWT.PUSH);
    b.setText("add a new button at row 2 column 1");
    final int[] index = new int[1];
    b.addListener(SWT.Selection, e -> {
        Button s = new Button(c, SWT.PUSH);
        s.setText("Special " + index[0]);
        index[0]++;
        Control[] children = c.getChildren();
        s.moveAbove(children[3]);
        shell.layout(new Control[] { s });
    });

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

From source file:Snippet6.java

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    final Composite c = new Composite(shell, SWT.NONE);
    GridLayout layout = new GridLayout();
    layout.numColumns = 3;//w  ww.  ja  va2s.co  m
    c.setLayout(layout);
    for (int i = 0; i < 10; i++) {
        Button b = new Button(c, SWT.PUSH);
        b.setText("Button " + i);
    }

    Button b = new Button(shell, SWT.PUSH);
    b.setText("add a new button at row 2 column 1");
    final int[] index = new int[1];
    b.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            Button s = new Button(c, SWT.PUSH);
            s.setText("Special " + index[0]);
            index[0]++;
            Control[] children = c.getChildren();
            s.moveAbove(children[3]);
            shell.layout(new Control[] { s });
        }
    });

    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);
    }// w ww.j  a  v a  2  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: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 a  v  a  2 s .c  o 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;
}

From source file:RadialLayout.java

protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
    Point maxDimensions = calculateMaxDimensions(composite.getChildren());
    int stepsPerHemisphere = stepsPerHemisphere(composite.getChildren().length);

    int maxWidth = maxDimensions.x;
    int maxHeight = maxDimensions.y;

    int dimensionMultiplier = (stepsPerHemisphere + 1);
    int controlWidth = maxWidth * dimensionMultiplier;
    int controlHeight = maxHeight * dimensionMultiplier;
    int diameter = Math.max(controlWidth, controlHeight);
    Point preferredSize = new Point(diameter, diameter);

    if (wHint != SWT.DEFAULT) {
        if (preferredSize.x > wHint) {
            preferredSize.x = wHint;//from   w  w  w .  j a v a2  s .co m
        }
    }

    if (hHint != SWT.DEFAULT) {
        if (preferredSize.y > hHint) {
            preferredSize.y = hHint;
        }
    }

    return preferredSize;
}

From source file:RadialLayout.java

protected void layout(Composite composite, boolean flushCache) {
    Point[] positions = calculateControlPositions(composite);
    Control[] controls = composite.getChildren();
    for (int i = 0; i < controls.length; i++) {
        Point preferredSize = controls[i].computeSize(SWT.DEFAULT, SWT.DEFAULT);
        controls[i].setBounds(positions[i].x, positions[i].y, preferredSize.x, preferredSize.y);
    }/*from  w w w.j a  va  2s . com*/
}

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);
    }/*from   w  w  w .  java2 s  .c o  m*/
    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:BorderLayout.java

protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {

    if (sizes == null || flushCache == true)
        refreshSizes(composite.getChildren());
    int w = wHint;
    int h = hHint;
    if (w == SWT.DEFAULT)
        w = width;/*w w w  . j  ava  2  s.c o m*/
    if (h == SWT.DEFAULT)
        h = height;

    return new Point(w, h);
}