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

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

Introduction

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

Prototype

public void setLayoutData(Object layoutData) 

Source Link

Document

Sets the layout data associated with the receiver to the argument.

Usage

From source file:PaintExample.java

/**
 * Creates the GUI./*from w  w w  .  j  a  v a2s  . c om*/
 */
public void createGUI(Composite parent) {
    GridLayout gridLayout;
    GridData gridData;

    /*** Create principal GUI layout elements ***/
    Composite displayArea = new Composite(parent, SWT.NONE);
    gridLayout = new GridLayout();
    gridLayout.numColumns = 1;
    displayArea.setLayout(gridLayout);

    // Creating these elements here avoids the need to instantiate the GUI elements
    // in strict layout order.  The natural layout ordering is an artifact of using
    // SWT layouts, but unfortunately it is not the same order as that required to
    // instantiate all of the non-GUI application elements to satisfy referential
    // dependencies.  It is possible to reorder the initialization to some extent, but
    // this can be very tedious.

    // paint canvas
    final Canvas paintCanvas = new Canvas(displayArea,
            SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND);
    gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
    paintCanvas.setLayoutData(gridData);
    paintCanvas.setBackground(paintColorWhite);

    // color selector frame
    final Composite colorFrame = new Composite(displayArea, SWT.NONE);
    gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL);
    colorFrame.setLayoutData(gridData);

    // tool settings frame
    final Composite toolSettingsFrame = new Composite(displayArea, SWT.NONE);
    gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL);
    toolSettingsFrame.setLayoutData(gridData);

    // status text
    final Text statusText = new Text(displayArea, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY);
    gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL);
    statusText.setLayoutData(gridData);

    /*** Create the remaining application elements inside the principal GUI layout elements ***/
    // paintSurface
    paintSurface = new PaintSurface(paintCanvas, statusText, paintColorWhite);

    // finish initializing the tool data
    tools[Pencil_tool].data = new PencilTool(toolSettings, paintSurface);
    tools[Airbrush_tool].data = new AirbrushTool(toolSettings, paintSurface);
    tools[Line_tool].data = new LineTool(toolSettings, paintSurface);
    tools[PolyLine_tool].data = new PolyLineTool(toolSettings, paintSurface);
    tools[Rectangle_tool].data = new RectangleTool(toolSettings, paintSurface);
    tools[RoundedRectangle_tool].data = new RoundedRectangleTool(toolSettings, paintSurface);
    tools[Ellipse_tool].data = new EllipseTool(toolSettings, paintSurface);
    tools[Text_tool].data = new TextTool(toolSettings, paintSurface);

    // colorFrame      
    gridLayout = new GridLayout();
    gridLayout.numColumns = 3;
    gridLayout.marginHeight = 0;
    gridLayout.marginWidth = 0;
    colorFrame.setLayout(gridLayout);

    // activeForegroundColorCanvas, activeBackgroundColorCanvas
    activeForegroundColorCanvas = new Canvas(colorFrame, SWT.BORDER);
    gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
    gridData.heightHint = 24;
    gridData.widthHint = 24;
    activeForegroundColorCanvas.setLayoutData(gridData);

    activeBackgroundColorCanvas = new Canvas(colorFrame, SWT.BORDER);
    gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
    gridData.heightHint = 24;
    gridData.widthHint = 24;
    activeBackgroundColorCanvas.setLayoutData(gridData);

    // paletteCanvas
    final Canvas paletteCanvas = new Canvas(colorFrame, SWT.BORDER | SWT.NO_BACKGROUND);
    gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.heightHint = 24;
    paletteCanvas.setLayoutData(gridData);
    paletteCanvas.addListener(SWT.MouseDown, new Listener() {
        public void handleEvent(Event e) {
            Rectangle bounds = paletteCanvas.getClientArea();
            Color color = getColorAt(bounds, e.x, e.y);

            if (e.button == 1)
                setForegroundColor(color);
            else
                setBackgroundColor(color);
        }

        private Color getColorAt(Rectangle bounds, int x, int y) {
            if (bounds.height <= 1 && bounds.width <= 1)
                return paintColorWhite;
            final int row = (y - bounds.y) * numPaletteRows / bounds.height;
            final int col = (x - bounds.x) * numPaletteCols / bounds.width;
            return paintColors[Math.min(Math.max(row * numPaletteCols + col, 0), paintColors.length - 1)];
        }
    });
    Listener refreshListener = new Listener() {
        public void handleEvent(Event e) {
            if (e.gc == null)
                return;
            Rectangle bounds = paletteCanvas.getClientArea();
            for (int row = 0; row < numPaletteRows; ++row) {
                for (int col = 0; col < numPaletteCols; ++col) {
                    final int x = bounds.width * col / numPaletteCols;
                    final int y = bounds.height * row / numPaletteRows;
                    final int width = Math.max(bounds.width * (col + 1) / numPaletteCols - x, 1);
                    final int height = Math.max(bounds.height * (row + 1) / numPaletteRows - y, 1);
                    e.gc.setBackground(paintColors[row * numPaletteCols + col]);
                    e.gc.fillRectangle(bounds.x + x, bounds.y + y, width, height);
                }
            }
        }
    };
    paletteCanvas.addListener(SWT.Resize, refreshListener);
    paletteCanvas.addListener(SWT.Paint, refreshListener);
    //paletteCanvas.redraw();

    // toolSettingsFrame
    gridLayout = new GridLayout();
    gridLayout.numColumns = 4;
    gridLayout.marginHeight = 0;
    gridLayout.marginWidth = 0;
    toolSettingsFrame.setLayout(gridLayout);

    Label label = new Label(toolSettingsFrame, SWT.NONE);
    label.setText(getResourceString("settings.AirbrushRadius.text"));

    final Scale airbrushRadiusScale = new Scale(toolSettingsFrame, SWT.HORIZONTAL);
    airbrushRadiusScale.setMinimum(5);
    airbrushRadiusScale.setMaximum(50);
    airbrushRadiusScale.setSelection(toolSettings.airbrushRadius);
    airbrushRadiusScale.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL));
    airbrushRadiusScale.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            toolSettings.airbrushRadius = airbrushRadiusScale.getSelection();
            updateToolSettings();
        }
    });

    label = new Label(toolSettingsFrame, SWT.NONE);
    label.setText(getResourceString("settings.AirbrushIntensity.text"));

    final Scale airbrushIntensityScale = new Scale(toolSettingsFrame, SWT.HORIZONTAL);
    airbrushIntensityScale.setMinimum(1);
    airbrushIntensityScale.setMaximum(100);
    airbrushIntensityScale.setSelection(toolSettings.airbrushIntensity);
    airbrushIntensityScale.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL));
    airbrushIntensityScale.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            toolSettings.airbrushIntensity = airbrushIntensityScale.getSelection();
            updateToolSettings();
        }
    });
}

From source file:org.eclipse.swt.examples.dnd.DNDExample.java

public void open(Display display) {
    Shell shell = new Shell(display);
    shell.setText("Drag and Drop Example");
    shell.setLayout(new FillLayout());

    itemImage = new Image(display, DNDExample.class.getResourceAsStream("openFolder.gif"));

    ScrolledComposite sc = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL);
    Composite parent = new Composite(sc, SWT.NONE);
    sc.setContent(parent);/*www. j a  v  a  2 s  .c  o  m*/
    parent.setLayout(new FormLayout());

    Label dragLabel = new Label(parent, SWT.LEFT);
    dragLabel.setText("Drag Source:");

    Group dragWidgetGroup = new Group(parent, SWT.NONE);
    dragWidgetGroup.setText("Widget");
    createDragWidget(dragWidgetGroup);

    Composite cLeft = new Composite(parent, SWT.NONE);
    cLeft.setLayout(new GridLayout(2, false));

    Group dragOperationsGroup = new Group(cLeft, SWT.NONE);
    dragOperationsGroup.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));
    dragOperationsGroup.setText("Allowed Operation(s):");
    createDragOperations(dragOperationsGroup);

    Group dragTypesGroup = new Group(cLeft, SWT.NONE);
    dragTypesGroup.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
    dragTypesGroup.setText("Transfer Type(s):");
    createDragTypes(dragTypesGroup);

    dragConsole = new Text(cLeft, SWT.READ_ONLY | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI);
    dragConsole.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
    Menu menu = new Menu(shell, SWT.POP_UP);
    MenuItem item = new MenuItem(menu, SWT.PUSH);
    item.setText("Clear");
    item.addSelectionListener(widgetSelectedAdapter(e -> dragConsole.setText("")));
    item = new MenuItem(menu, SWT.CHECK);
    item.setText("Show Event detail");
    item.addSelectionListener(widgetSelectedAdapter(e -> {
        MenuItem eItem = (MenuItem) e.widget;
        dragEventDetail = eItem.getSelection();
    }));
    dragConsole.setMenu(menu);

    Label dropLabel = new Label(parent, SWT.LEFT);
    dropLabel.setText("Drop Target:");

    Group dropWidgetGroup = new Group(parent, SWT.NONE);
    dropWidgetGroup.setText("Widget");
    createDropWidget(dropWidgetGroup);

    Composite cRight = new Composite(parent, SWT.NONE);
    cRight.setLayout(new GridLayout(2, false));

    Group dropOperationsGroup = new Group(cRight, SWT.NONE);
    dropOperationsGroup.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 2));
    dropOperationsGroup.setText("Allowed Operation(s):");
    createDropOperations(dropOperationsGroup);

    Group dropTypesGroup = new Group(cRight, SWT.NONE);
    dropTypesGroup.setText("Transfer Type(s):");
    createDropTypes(dropTypesGroup);

    Group feedbackTypesGroup = new Group(cRight, SWT.NONE);
    feedbackTypesGroup.setText("Feedback Type(s):");
    createFeedbackTypes(feedbackTypesGroup);

    dropConsole = new Text(cRight, SWT.READ_ONLY | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI);
    dropConsole.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
    menu = new Menu(shell, SWT.POP_UP);
    item = new MenuItem(menu, SWT.PUSH);
    item.setText("Clear");
    item.addSelectionListener(widgetSelectedAdapter(e -> dropConsole.setText("")));
    item = new MenuItem(menu, SWT.CHECK);
    item.setText("Show Event detail");
    item.addSelectionListener(widgetSelectedAdapter(e -> {
        MenuItem eItem = (MenuItem) e.widget;
        dropEventDetail = eItem.getSelection();
    }));
    dropConsole.setMenu(menu);

    if (dragEnabled)
        createDragSource();
    if (dropEnabled)
        createDropTarget();

    int height = 200;
    FormData data = new FormData();
    data.top = new FormAttachment(0, 10);
    data.left = new FormAttachment(0, 10);
    dragLabel.setLayoutData(data);

    data = new FormData();
    data.top = new FormAttachment(dragLabel, 10);
    data.left = new FormAttachment(0, 10);
    data.right = new FormAttachment(50, -10);
    data.height = height;
    dragWidgetGroup.setLayoutData(data);

    data = new FormData();
    data.top = new FormAttachment(dragWidgetGroup, 10);
    data.left = new FormAttachment(0, 10);
    data.right = new FormAttachment(50, -10);
    data.bottom = new FormAttachment(100, -10);
    cLeft.setLayoutData(data);

    data = new FormData();
    data.top = new FormAttachment(0, 10);
    data.left = new FormAttachment(cLeft, 10);
    dropLabel.setLayoutData(data);

    data = new FormData();
    data.top = new FormAttachment(dropLabel, 10);
    data.left = new FormAttachment(cLeft, 10);
    data.right = new FormAttachment(100, -10);
    data.height = height;
    dropWidgetGroup.setLayoutData(data);

    data = new FormData();
    data.top = new FormAttachment(dropWidgetGroup, 10);
    data.left = new FormAttachment(cLeft, 10);
    data.right = new FormAttachment(100, -10);
    data.bottom = new FormAttachment(100, -10);
    cRight.setLayoutData(data);

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

    Point size = shell.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    Rectangle monitorArea = shell.getMonitor().getClientArea();
    shell.setSize(Math.min(size.x, monitorArea.width - 20), Math.min(size.y, monitorArea.height - 20));
    shell.open();

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

From source file:DNDExample.java

public void open(Display display) {
    Shell shell = new Shell(display);
    shell.setText("Drag and Drop Example");
    shell.setLayout(new FillLayout());

    ScrolledComposite sc = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL);
    Composite parent = new Composite(sc, SWT.NONE);
    sc.setContent(parent);//from  w  w w.java  2  s.  c  o m
    parent.setLayout(new FormLayout());

    Label dragLabel = new Label(parent, SWT.LEFT);
    dragLabel.setText("Drag Source:");

    Group dragWidgetGroup = new Group(parent, SWT.NONE);
    dragWidgetGroup.setText("Widget");
    createDragWidget(dragWidgetGroup);

    Composite cLeft = new Composite(parent, SWT.NONE);
    cLeft.setLayout(new GridLayout(2, false));

    Group dragOperationsGroup = new Group(cLeft, SWT.NONE);
    dragOperationsGroup.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));
    dragOperationsGroup.setText("Allowed Operation(s):");
    createDragOperations(dragOperationsGroup);

    Group dragTypesGroup = new Group(cLeft, SWT.NONE);
    dragTypesGroup.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
    dragTypesGroup.setText("Transfer Type(s):");
    createDragTypes(dragTypesGroup);

    dragConsole = new Text(cLeft, SWT.READ_ONLY | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI);
    dragConsole.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
    Menu menu = new Menu(shell, SWT.POP_UP);
    MenuItem item = new MenuItem(menu, SWT.PUSH);
    item.setText("Clear");
    item.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            dragConsole.setText("");
        }
    });
    item = new MenuItem(menu, SWT.CHECK);
    item.setText("Show Event detail");
    item.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            MenuItem item = (MenuItem) e.widget;
            dragEventDetail = item.getSelection();
        }
    });
    dragConsole.setMenu(menu);

    Label dropLabel = new Label(parent, SWT.LEFT);
    dropLabel.setText("Drop Target:");

    Group dropWidgetGroup = new Group(parent, SWT.NONE);
    dropWidgetGroup.setText("Widget");
    createDropWidget(dropWidgetGroup);

    Composite cRight = new Composite(parent, SWT.NONE);
    cRight.setLayout(new GridLayout(2, false));

    Group dropOperationsGroup = new Group(cRight, SWT.NONE);
    dropOperationsGroup.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 2));
    dropOperationsGroup.setText("Allowed Operation(s):");
    createDropOperations(dropOperationsGroup);

    Group dropTypesGroup = new Group(cRight, SWT.NONE);
    dropTypesGroup.setText("Transfer Type(s):");
    createDropTypes(dropTypesGroup);

    Group feedbackTypesGroup = new Group(cRight, SWT.NONE);
    feedbackTypesGroup.setText("Feedback Type(s):");
    createFeedbackTypes(feedbackTypesGroup);

    dropConsole = new Text(cRight, SWT.READ_ONLY | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI);
    dropConsole.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
    menu = new Menu(shell, SWT.POP_UP);
    item = new MenuItem(menu, SWT.PUSH);
    item.setText("Clear");
    item.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            dropConsole.setText("");
        }
    });
    item = new MenuItem(menu, SWT.CHECK);
    item.setText("Show Event detail");
    item.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            MenuItem item = (MenuItem) e.widget;
            dropEventDetail = item.getSelection();
        }
    });
    dropConsole.setMenu(menu);

    int height = 200;
    FormData data = new FormData();
    data.top = new FormAttachment(0, 10);
    data.left = new FormAttachment(0, 10);
    dragLabel.setLayoutData(data);

    data = new FormData();
    data.top = new FormAttachment(dragLabel, 10);
    data.left = new FormAttachment(0, 10);
    data.right = new FormAttachment(50, -10);
    data.height = height;
    dragWidgetGroup.setLayoutData(data);

    data = new FormData();
    data.top = new FormAttachment(dragWidgetGroup, 10);
    data.left = new FormAttachment(0, 10);
    data.right = new FormAttachment(50, -10);
    data.bottom = new FormAttachment(100, -10);
    cLeft.setLayoutData(data);

    data = new FormData();
    data.top = new FormAttachment(0, 10);
    data.left = new FormAttachment(cLeft, 10);
    dropLabel.setLayoutData(data);

    data = new FormData();
    data.top = new FormAttachment(dropLabel, 10);
    data.left = new FormAttachment(cLeft, 10);
    data.right = new FormAttachment(100, -10);
    data.height = height;
    dropWidgetGroup.setLayoutData(data);

    data = new FormData();
    data.top = new FormAttachment(dropWidgetGroup, 10);
    data.left = new FormAttachment(cLeft, 10);
    data.right = new FormAttachment(100, -10);
    data.bottom = new FormAttachment(100, -10);
    cRight.setLayoutData(data);

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

    Point size = shell.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    Rectangle monitorArea = shell.getMonitor().getClientArea();
    shell.setSize(Math.min(size.x, monitorArea.width - 20), Math.min(size.y, monitorArea.height - 20));
    shell.open();

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