SWY Radial Layout : Layout « SWT JFace Eclipse « Java

SWY Radial Layout

SWT/JFace in Action
GUI Design with Eclipse 3.0
Matthew Scarpino, Stephen Holder, Stanford Ng, and Laurent Mihalkovic

ISBN: 1932394273

Publisher: Manning

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Layout;

public class RadialLayout extends Layout {
  public RadialLayout() {

  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;

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

    return preferredSize;

  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,
      controls[i].setBounds(positions[i].x, positions[i].y,
          preferredSize.x, preferredSize.y);

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

    Point maxControlDimensions = calculateMaxDimensions(composite
    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,

    int signMultiplier = 1;
    int x = -radius;
    int y;
    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;

  private Point calculateMaxDimensions(Control[] controls) {
    Point maxes = new Point(0, 0);

    for (int i = 0; i < controls.length; i++) {
      Point controlSize = controls[i].computeSize(SWT.DEFAULT,
      maxes.x = Math.max(maxes.x, controlSize.x);
      maxes.y = Math.max(maxes.y, controlSize.y);

    return maxes;

  private int stepsPerHemisphere(int totalObjects) {
    return (totalObjects / 2) - 1;

  private int calculateStepDistance(int clientAreaDimensionSize, int stepCount) {
    return clientAreaDimensionSize / (stepCount + 1);



Related examples in the same category

1.SWT Layout Example in OneSWT Layout Example in One
2.SWT GridLayout DemoSWT GridLayout Demo
3.SWT GridLayoutSWT GridLayout
4.Control Size LocationControl Size Location
5.SWT RowLayout SampleSWT RowLayout Sample
6.SWT FillLayout SampleSWT FillLayout Sample
7.SWT Form Layout SampleSWT Form Layout Sample
8.SWT GridLayout SampleSWT GridLayout Sample
9.GridLayout Sample Grab SpaceGridLayout Sample Grab Space
10.Grid Layout SpanGrid Layout Span
11.Layout ComponentsLayout Components
12.Layout TermsLayout Terms
13.StackLayout SampleStackLayout Sample
14.BorderLayout SampleBorderLayout Sample
16.FormLayout SimpleFormLayout Simple
17.A BorderLayoutA BorderLayout
18.FillLayout HorizontalFillLayout Horizontal
19.FillLayout VerticalFillLayout Vertical
20.FormLayout ComplexFormLayout Complex
21.FormLayout Form AttachmentFormLayout Form Attachment
23.GridLayout 2x2GridLayout 2x2
24.Grid Layout ComplexGrid Layout Complex
25.No LayoutNo Layout
26.Row Layout TestRow Layout Test
27.Stack Layout TestStack Layout Test
28.Row Layout HorizontalRow Layout Horizontal
29.FillLayout ExampleFillLayout Example
30.FormLayout ExampleFormLayout Example
31.GridLayout ExampleGridLayout Example
32.GridLayout Example 2GridLayout Example 2
33.Layout ExampleLayout Example
34.RowLayout ExampleRowLayout Example
35.SWT FillLayout
36.Use of Radial Layout
37.GridLayout with All Options
38.Simplest GridLayout
39.SWT GridLayout: GridSpan
40.SWT RowLayout
41.SWT FillLayout Composite
42.SWT GridLayout: align widgets in a vertical columnSWT GridLayout: align widgets in a vertical column
43.SWT GridLayout: align widgets in a horizontal rowSWT GridLayout: align widgets in a horizontal row
44.Exclude a widget from a GridLayoutExclude a widget from a GridLayout
45.Insert widgets into a grid layoutInsert widgets into a grid layout
46.Align widgets in a GridLayoutAlign widgets in a GridLayout
47.FormLayout: create a simple OK and CANCEL dialog using form layoutFormLayout: create a simple OK and CANCEL dialog using form layout
48.FormLayout: center a label and single line text using a form layoutFormLayout: center a label and single line text using a form layout
49.FormLayout: create a simple dialog using form layoutFormLayout: create a simple dialog using form layout