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

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

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Label 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:edu.isistan.carcha.plugin.editors.DXMIEditor.java

/**
 * Creates the DDD list page./*from ww  w. ja v a  2  s  .  c o m*/
 */
void createListPage() {
    final Composite composite = new Composite(getContainer(), SWT.NONE);
    composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 4));
    composite.setLayout(new GridLayout());
    Label concernLabel = new Label(composite, SWT.BORDER);
    concernLabel.setText("Design Decisions");
    concernLabel.setToolTipText("This are the Design Decisions detected in the architectural document");
    GridData gridData = new GridData(SWT.LEFT, SWT.LEFT, false, false);
    concernLabel.setLayoutData(gridData);

    ddsViewer = new TableViewer(composite, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    createColumns(composite, ddsViewer);

    final Table table = ddsViewer.getTable();
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    ddsViewer.setContentProvider(new ArrayContentProvider());

    getSite().setSelectionProvider(ddsViewer);
    // define layout for the viewer
    gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
    ddsViewer.getControl().setLayoutData(gridData);

    int index = addPage(composite);
    setPageText(index, "List");
}

From source file:org.eclipse.swt.examples.controlexample.StyledTextTab.java

/**
 * Creates the "StyledText Style" group.
 *//*w  w w  . j  ava 2 s .  co  m*/
void createStyledTextStyleGroup() {
    styledTextStyleGroup = new Group(controlGroup, SWT.NONE);
    styledTextStyleGroup.setText(ControlExample.getResourceString("StyledText_Styles"));
    styledTextStyleGroup.setLayout(new GridLayout(6, false));
    GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
    data.horizontalSpan = 2;
    styledTextStyleGroup.setLayoutData(data);

    /* Get images */
    boldImage = createBitmapImage(display, "bold");
    italicImage = createBitmapImage(display, "italic");
    redImage = createBitmapImage(display, "red");
    yellowImage = createBitmapImage(display, "yellow");
    underlineImage = createBitmapImage(display, "underline");
    strikeoutImage = createBitmapImage(display, "strikeout");

    /* Create controls to modify the StyledText */
    Label label = new Label(styledTextStyleGroup, SWT.NONE);
    label.setText(ControlExample.getResourceString("StyledText_Style_Instructions"));
    label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 4, 1));
    resetButton = new Button(styledTextStyleGroup, SWT.PUSH);
    resetButton.setText(ControlExample.getResourceString("Clear"));
    resetButton.setLayoutData(new GridData(SWT.END, SWT.CENTER, true, false, 2, 1));
    label = new Label(styledTextStyleGroup, SWT.NONE);
    label.setText(ControlExample.getResourceString("Bold"));
    label.setLayoutData(new GridData(SWT.END, SWT.CENTER, true, false));
    boldButton = new Button(styledTextStyleGroup, SWT.PUSH);
    boldButton.setImage(boldImage);
    label = new Label(styledTextStyleGroup, SWT.NONE);
    label.setText(ControlExample.getResourceString("Underline"));
    label.setLayoutData(new GridData(SWT.END, SWT.CENTER, true, false));
    underlineButton = new Button(styledTextStyleGroup, SWT.PUSH);
    underlineButton.setImage(underlineImage);
    label = new Label(styledTextStyleGroup, SWT.NONE);
    label.setText(ControlExample.getResourceString("Foreground_Style"));
    label.setLayoutData(new GridData(SWT.END, SWT.CENTER, true, false));
    redButton = new Button(styledTextStyleGroup, SWT.PUSH);
    redButton.setImage(redImage);
    label = new Label(styledTextStyleGroup, SWT.NONE);
    label.setText(ControlExample.getResourceString("Italic"));
    label.setLayoutData(new GridData(SWT.END, SWT.CENTER, true, false));
    italicButton = new Button(styledTextStyleGroup, SWT.PUSH);
    italicButton.setImage(italicImage);
    label = new Label(styledTextStyleGroup, SWT.NONE);
    label.setText(ControlExample.getResourceString("Strikeout"));
    label.setLayoutData(new GridData(SWT.END, SWT.CENTER, true, false));
    strikeoutButton = new Button(styledTextStyleGroup, SWT.PUSH);
    strikeoutButton.setImage(strikeoutImage);
    label = new Label(styledTextStyleGroup, SWT.NONE);
    label.setText(ControlExample.getResourceString("Background_Style"));
    label.setLayoutData(new GridData(SWT.END, SWT.CENTER, true, false));
    yellowButton = new Button(styledTextStyleGroup, SWT.PUSH);
    yellowButton.setImage(yellowImage);
    SelectionListener styleListener = widgetSelectedAdapter(e -> {
        Point sel = styledText.getSelectionRange();
        if ((sel == null) || (sel.y == 0))
            return;
        StyleRange style;
        for (int i = sel.x; i < sel.x + sel.y; i++) {
            StyleRange range = styledText.getStyleRangeAtOffset(i);
            if (range != null && e.widget != resetButton) {
                style = (StyleRange) range.clone();
                style.start = i;
                style.length = 1;
            } else {
                style = new StyleRange(i, 1, null, null, SWT.NORMAL);
            }
            if (e.widget == boldButton) {
                style.fontStyle ^= SWT.BOLD;
            } else if (e.widget == italicButton) {
                style.fontStyle ^= SWT.ITALIC;
            } else if (e.widget == underlineButton) {
                style.underline = !style.underline;
            } else if (e.widget == strikeoutButton) {
                style.strikeout = !style.strikeout;
            }
            styledText.setStyleRange(style);
        }
        styledText.setSelectionRange(sel.x + sel.y, 0);
    });
    SelectionListener colorListener = widgetSelectedAdapter(e -> {
        Point sel = styledText.getSelectionRange();
        if ((sel == null) || (sel.y == 0))
            return;
        Color fg = null, bg = null;
        if (e.widget == redButton) {
            fg = display.getSystemColor(SWT.COLOR_RED);
        } else if (e.widget == yellowButton) {
            bg = display.getSystemColor(SWT.COLOR_YELLOW);
        }
        StyleRange style;
        for (int i = sel.x; i < sel.x + sel.y; i++) {
            StyleRange range = styledText.getStyleRangeAtOffset(i);
            if (range != null) {
                style = (StyleRange) range.clone();
                style.start = i;
                style.length = 1;
                if (fg != null)
                    style.foreground = style.foreground != null ? null : fg;
                if (bg != null)
                    style.background = style.background != null ? null : bg;
            } else {
                style = new StyleRange(i, 1, fg, bg, SWT.NORMAL);
            }
            styledText.setStyleRange(style);
        }
        styledText.setSelectionRange(sel.x + sel.y, 0);
    });
    resetButton.addSelectionListener(styleListener);
    boldButton.addSelectionListener(styleListener);
    italicButton.addSelectionListener(styleListener);
    underlineButton.addSelectionListener(styleListener);
    strikeoutButton.addSelectionListener(styleListener);
    redButton.addSelectionListener(colorListener);
    yellowButton.addSelectionListener(colorListener);
    yellowButton.addDisposeListener(e -> {
        boldImage.dispose();
        italicImage.dispose();
        redImage.dispose();
        yellowImage.dispose();
        underlineImage.dispose();
        strikeoutImage.dispose();
    });
}

From source file:AddressBookDemo.java

private void createLine(Composite parent, int ncol) {
    Label line = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL | SWT.BOLD);
    GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.horizontalSpan = ncol;/*from  w w  w.j  ava 2  s.  com*/
    line.setLayoutData(gridData);
}

From source file:ReservationData.java

public void createControl(Composite parent) {
    Composite composite = new Composite(parent, SWT.NULL);
    GridLayout gridLayout = new GridLayout(2, false);
    composite.setLayout(gridLayout);//from   w w w  . j a va 2s . c  o m

    new Label(composite, SWT.NULL).setText("Arrival date: ");

    Composite compositeArrival = new Composite(composite, SWT.NULL);
    compositeArrival.setLayout(new RowLayout());

    String[] months = new String[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
            "Nov", "Dec" };

    Calendar calendar = new GregorianCalendar(); // today.
    ((ReservationWizard) getWizard()).data.arrivalDate = calendar.getTime();

    comboArrivalMonth = new Combo(compositeArrival, SWT.BORDER | SWT.READ_ONLY);
    for (int i = 0; i < months.length; i++)
        comboArrivalMonth.add(months[i]);
    comboArrivalMonth.select(calendar.get(Calendar.MONTH));

    comboArrivalDay = new Combo(compositeArrival, SWT.BORDER | SWT.READ_ONLY);
    for (int i = 0; i < 31; i++)
        comboArrivalDay.add("" + (i + 1));
    comboArrivalDay.select(calendar.get(Calendar.DAY_OF_MONTH) - 1);

    comboArrivalYear = new Combo(compositeArrival, SWT.BORDER | SWT.READ_ONLY);
    for (int i = 2004; i < 2010; i++)
        comboArrivalYear.add("" + i);
    comboArrivalYear.select(calendar.get(Calendar.YEAR) - 2004);

    calendar.add(Calendar.DATE, 1); // tomorrow. 
    ((ReservationWizard) getWizard()).data.departureDate = calendar.getTime();

    new Label(composite, SWT.NULL).setText("Departure date: ");

    Composite compositeDeparture = new Composite(composite, SWT.NULL | SWT.READ_ONLY);
    compositeDeparture.setLayout(new RowLayout());

    comboDepartureMonth = new Combo(compositeDeparture, SWT.NULL | SWT.READ_ONLY);
    for (int i = 0; i < months.length; i++)
        comboDepartureMonth.add(months[i]);
    comboDepartureMonth.select(calendar.get(Calendar.MONTH));

    comboDepartureDay = new Combo(compositeDeparture, SWT.NULL | SWT.READ_ONLY);
    for (int i = 0; i < 31; i++)
        comboDepartureDay.add("" + (i + 1));
    comboDepartureDay.select(calendar.get(Calendar.DAY_OF_MONTH) - 1);

    comboDepartureYear = new Combo(compositeDeparture, SWT.NULL | SWT.READ_ONLY);
    for (int i = 2004; i < 2010; i++)
        comboDepartureYear.add("" + i);
    comboDepartureYear.select(calendar.get(Calendar.YEAR) - 2004);

    // draws a line. 
    Label line = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);

    GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.horizontalSpan = 2;
    line.setLayoutData(gridData);

    new Label(composite, SWT.NULL).setText("Room type: ");
    comboRoomTypes = new Combo(composite, SWT.BORDER | SWT.READ_ONLY);
    comboRoomTypes.add("Standard room (rate: $198)");
    comboRoomTypes.add("Deluxe room (rate: $298)");
    comboRoomTypes.select(0);

    Listener selectionListener = new Listener() {
        public void handleEvent(Event event) {
            int arrivalDay = comboArrivalDay.getSelectionIndex() + 1;
            int arrivalMonth = comboArrivalMonth.getSelectionIndex();
            int arrivalYear = comboArrivalYear.getSelectionIndex() + 2004;

            int departureDay = comboDepartureDay.getSelectionIndex() + 1;
            int departureMonth = comboDepartureMonth.getSelectionIndex();
            int departureYear = comboDepartureYear.getSelectionIndex() + 2004;

            setDates(arrivalDay, arrivalMonth, arrivalYear, departureDay, departureMonth, departureYear);
        }
    };

    comboArrivalDay.addListener(SWT.Selection, selectionListener);
    comboArrivalMonth.addListener(SWT.Selection, selectionListener);
    comboArrivalYear.addListener(SWT.Selection, selectionListener);
    comboDepartureDay.addListener(SWT.Selection, selectionListener);
    comboDepartureMonth.addListener(SWT.Selection, selectionListener);
    comboDepartureYear.addListener(SWT.Selection, selectionListener);

    comboRoomTypes.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            ((ReservationWizard) getWizard()).data.roomType = comboRoomTypes.getSelectionIndex();
        }
    });

    setControl(composite);

}

From source file:org.ewhoxford.swt.bloodpressure.ui.MeasurePageTab.java

/**
 * Creates the control widgets./*ww w.j av a2s .c om*/
 */
void createControlWidgets() {

    /* Controls the type of FillLayout */
    Group bpGroup = new Group(controlGroup, SWT.NONE);
    bpGroup.setText(BPMainWindow.getResourceString("BP_and_HR"));
    bpGroup.setLayout(new GridLayout(2, true));
    bpGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    messageLabel = new Label(controlGroup, SWT.NONE);
    messageLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
    messageLabel.setText("");

    Label label1 = new Label(bpGroup, SWT.NONE);
    label1.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
    label1.setText("SBP");

    sbpTextbox = new Text(bpGroup, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
    sbpTextbox.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    sbpTextbox.setText("0");
    sbpTextbox.setEditable(false);

    Label label2 = new Label(bpGroup, SWT.NONE);
    label2.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
    label2.setText("DBP");

    dbpTextbox = new Text(bpGroup, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
    dbpTextbox.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    dbpTextbox.setText("0");
    dbpTextbox.setEditable(false);

    Label label3 = new Label(bpGroup, SWT.NONE);
    label3.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
    label3.setText("HR");

    hrTextbox = new Text(bpGroup, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
    hrTextbox.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    hrTextbox.setText("0");
    hrTextbox.setEditable(false);

    Label label5 = new Label(controlGroup, SWT.NONE);
    label5.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
    label5.setText("");

    Label label4 = new Label(controlGroup, SWT.NONE);
    label4.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
    label4.setText("Notes:");

    notesText = new Text(controlGroup, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
    notesText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    notesText.setText("Comment on the blood pressure");
    notesText.computeSize(100, 500);
    notesText.setEnabled(true);
    final Group optionGroup = new Group(controlGroup, SWT.NONE);
    optionGroup.setText(BPMainWindow.getResourceString("Options"));
    optionGroup.setLayout(new GridLayout());
    optionGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    newMeasure = new Button(optionGroup, SWT.BUTTON1);
    newMeasure.setText("New Measure");
    newMeasure.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    newMeasure.setSelection(true);
    newMeasure.addSelectionListener(selectionListener);

    createCSV = new Button(optionGroup, SWT.CHECK);
    createCSV.setText("create CSV File");
    createCSV.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    saveButton = new Button(optionGroup, SWT.BUTTON1);
    saveButton.setText("Save");
    saveButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    saveButton.setEnabled(false);
    saveButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            String savedFileName = "";
            String notes = "";

            if (notesText.getText().length() != 0) {
                notes = notesText.getText();
            }

            Long time = System.currentTimeMillis();

            if (createCSV.getSelection()) {

                try {
                    savedFileName = FileManager.saveFile(bloodPressureValue, arrayPressure, arrayTime, time,
                            notes);
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } finally {
                    addNewMeasureAndFile(savedFileName, bloodPressureValue, notes, time);
                }

            } else {
                addNewMeasureAndFile(savedFileName, bloodPressureValue, notes, time);

                if (createCSV.getSelection()) {

                    MessageDialog.openInformation(optionGroup.getShell(), "Saved",
                            BPMainWindow.getResourceString("alert_dialog_data_saved_to_database_and_csv"));

                } else {

                    MessageDialog.openInformation(optionGroup.getShell(), "Saved",
                            BPMainWindow.getResourceString("alert_dialog_data_saved_to_database"));
                }
            }
        }
    });
    // #### End of Set up click listeners for all the buttons
}

From source file:org.eclipse.swt.examples.clipboard.ClipboardExample.java

void createFileTransfer(Composite copyParent, Composite pasteParent) {
    //File Transfer
    Label l = new Label(copyParent, SWT.NONE);
    l.setText("FileTransfer:"); //$NON-NLS-1$
    GridData data = new GridData();
    data.verticalSpan = 3;/*from ww w  .j a va2 s  .  com*/
    l.setLayoutData(data);

    final Table copyFileTable = new Table(copyParent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
    data = new GridData(GridData.FILL_BOTH);
    data.widthHint = HSIZE;
    data.heightHint = VSIZE;
    data.verticalSpan = 3;
    copyFileTable.setLayoutData(data);

    Button b = new Button(copyParent, SWT.PUSH);
    b.setText("Select file(s)");
    b.addSelectionListener(widgetSelectedAdapter(e -> {
        FileDialog dialog = new FileDialog(shell, SWT.OPEN | SWT.MULTI);
        String result = dialog.open();
        if (result != null && result.length() > 0) {
            String path = dialog.getFilterPath();
            String[] names = dialog.getFileNames();
            for (String name : names) {
                TableItem item = new TableItem(copyFileTable, SWT.NONE);
                item.setText(path + File.separator + name);
            }
        }
    }));
    b = new Button(copyParent, SWT.PUSH);
    b.setText("Select directory");
    b.addSelectionListener(widgetSelectedAdapter(e -> {
        DirectoryDialog dialog = new DirectoryDialog(shell, SWT.OPEN);
        String result = dialog.open();
        if (result != null && result.length() > 0) {
            //copyFileTable.removeAll();
            TableItem item = new TableItem(copyFileTable, SWT.NONE);
            item.setText(result);
        }
    }));

    b = new Button(copyParent, SWT.PUSH);
    b.setText("Copy");
    b.addSelectionListener(widgetSelectedAdapter(e -> {
        TableItem[] items = copyFileTable.getItems();
        if (items.length > 0) {
            status.setText("");
            String[] itemsData = new String[items.length];
            for (int i = 0; i < itemsData.length; i++) {
                itemsData[i] = items[i].getText();
            }
            clipboard.setContents(new Object[] { itemsData }, new Transfer[] { FileTransfer.getInstance() });
        } else {
            status.setText("No file to copy");
        }
    }));

    l = new Label(pasteParent, SWT.NONE);
    l.setText("FileTransfer:"); //$NON-NLS-1$
    final Table pasteFileTable = new Table(pasteParent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
    data = new GridData(GridData.FILL_BOTH);
    data.widthHint = HSIZE;
    data.heightHint = VSIZE;
    pasteFileTable.setLayoutData(data);
    b = new Button(pasteParent, SWT.PUSH);
    b.setText("Paste");
    b.addSelectionListener(widgetSelectedAdapter(e -> {
        String[] textData = (String[]) clipboard.getContents(FileTransfer.getInstance());
        if (textData != null && textData.length > 0) {
            status.setText("");
            pasteFileTable.removeAll();
            for (String element : textData) {
                TableItem item = new TableItem(pasteFileTable, SWT.NONE);
                item.setText(element);
            }
        } else {
            status.setText("No file to paste");
        }
    }));
}

From source file:TabbedShellExample.java

public GridComposite(Composite c) {
    super(c, SWT.BORDER);
    GridLayout gl = new GridLayout();
    gl.numColumns = 3;//w  w w  .  j  a  v  a2 s . c  om
    this.setLayout(gl);
    final Label l1 = new Label(this, SWT.BORDER);
    l1.setText("Column One");
    final Label l2 = new Label(this, SWT.BORDER);
    l2.setText("Column Two");
    final Label l3 = new Label(this, SWT.BORDER);
    l3.setText("Column Three");
    final Text t1 = new Text(this, SWT.SINGLE | SWT.BORDER);
    final Text t2 = new Text(this, SWT.SINGLE | SWT.BORDER);
    final Text t3 = new Text(this, SWT.SINGLE | SWT.BORDER);
    final Text t4 = new Text(this, SWT.SINGLE | SWT.BORDER);
    final Text t5 = new Text(this, SWT.SINGLE | SWT.BORDER);
    final Text t6 = new Text(this, SWT.SINGLE | SWT.BORDER);

    GridData gd = new GridData();
    gd.horizontalAlignment = GridData.CENTER;
    l1.setLayoutData(gd);

    gd = new GridData();
    gd.horizontalAlignment = GridData.CENTER;
    l2.setLayoutData(gd);

    gd = new GridData();
    gd.horizontalAlignment = GridData.CENTER;
    l3.setLayoutData(gd);

    gd = new GridData(GridData.FILL_HORIZONTAL);
    t1.setLayoutData(gd);

    gd = new GridData(GridData.FILL_HORIZONTAL);
    t2.setLayoutData(gd);

    gd = new GridData(GridData.FILL_HORIZONTAL);
    t3.setLayoutData(gd);

    gd = new GridData(GridData.FILL_HORIZONTAL);
    t4.setLayoutData(gd);

    gd = new GridData(GridData.FILL_HORIZONTAL);
    t5.setLayoutData(gd);

    gd = new GridData(GridData.FILL_HORIZONTAL);
    t6.setLayoutData(gd);
}

From source file:org.eclipse.swt.examples.clipboard.ClipboardExample.java

void createImageTransfer(Composite copyParent, Composite pasteParent) {
    final Image[] copyImage = new Image[] { null };
    Label l = new Label(copyParent, SWT.NONE);
    l.setText("ImageTransfer:"); //$NON-NLS-1$
    GridData data = new GridData();
    data.verticalSpan = 2;//from   w w w .j  a  v a2  s . com
    l.setLayoutData(data);

    final Canvas copyImageCanvas = new Canvas(copyParent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
    data = new GridData(GridData.FILL_BOTH);
    data.verticalSpan = 2;
    data.widthHint = HSIZE;
    data.heightHint = VSIZE;
    copyImageCanvas.setLayoutData(data);

    final Point copyOrigin = new Point(0, 0);
    final ScrollBar copyHBar = copyImageCanvas.getHorizontalBar();
    copyHBar.setEnabled(false);
    copyHBar.addListener(SWT.Selection, e -> {
        if (copyImage[0] != null) {
            int hSelection = copyHBar.getSelection();
            int destX = -hSelection - copyOrigin.x;
            Rectangle rect = copyImage[0].getBounds();
            copyImageCanvas.scroll(destX, 0, 0, 0, rect.width, rect.height, false);
            copyOrigin.x = -hSelection;
        }
    });
    final ScrollBar copyVBar = copyImageCanvas.getVerticalBar();
    copyVBar.setEnabled(false);
    copyVBar.addListener(SWT.Selection, e -> {
        if (copyImage[0] != null) {
            int vSelection = copyVBar.getSelection();
            int destY = -vSelection - copyOrigin.y;
            Rectangle rect = copyImage[0].getBounds();
            copyImageCanvas.scroll(0, destY, 0, 0, rect.width, rect.height, false);
            copyOrigin.y = -vSelection;
        }
    });
    copyImageCanvas.addListener(SWT.Paint, e -> {
        if (copyImage[0] != null) {
            GC gc = e.gc;
            gc.drawImage(copyImage[0], copyOrigin.x, copyOrigin.y);
            Rectangle rect = copyImage[0].getBounds();
            Rectangle client = copyImageCanvas.getClientArea();
            int marginWidth = client.width - rect.width;
            if (marginWidth > 0) {
                gc.fillRectangle(rect.width, 0, marginWidth, client.height);
            }
            int marginHeight = client.height - rect.height;
            if (marginHeight > 0) {
                gc.fillRectangle(0, rect.height, client.width, marginHeight);
            }
            gc.dispose();
        }
    });
    Button openButton = new Button(copyParent, SWT.PUSH);
    openButton.setText("Open Image");
    openButton.addSelectionListener(widgetSelectedAdapter(e -> {
        FileDialog dialog = new FileDialog(shell, SWT.OPEN);
        dialog.setText("Open an image file or cancel");
        String string = dialog.open();
        if (string != null) {
            if (copyImage[0] != null) {
                System.out.println("CopyImage");
                copyImage[0].dispose();
            }
            copyImage[0] = new Image(e.display, string);
            copyVBar.setEnabled(true);
            copyHBar.setEnabled(true);
            copyOrigin.x = 0;
            copyOrigin.y = 0;
            Rectangle rect = copyImage[0].getBounds();
            Rectangle client = copyImageCanvas.getClientArea();
            copyHBar.setMaximum(rect.width);
            copyVBar.setMaximum(rect.height);
            copyHBar.setThumb(Math.min(rect.width, client.width));
            copyVBar.setThumb(Math.min(rect.height, client.height));
            copyImageCanvas.scroll(0, 0, 0, 0, rect.width, rect.height, true);
            copyVBar.setSelection(0);
            copyHBar.setSelection(0);
            copyImageCanvas.redraw();
        }
    }));
    Button b = new Button(copyParent, SWT.PUSH);
    b.setText("Copy");
    b.addSelectionListener(widgetSelectedAdapter(e -> {
        if (copyImage[0] != null) {
            status.setText("");
            // Fetch ImageData at current zoom and save in the clip-board.
            clipboard.setContents(new Object[] { copyImage[0].getImageDataAtCurrentZoom() },
                    new Transfer[] { ImageTransfer.getInstance() });
        } else {
            status.setText("No image to copy");
        }
    }));

    final Image[] pasteImage = new Image[] { null };
    l = new Label(pasteParent, SWT.NONE);
    l.setText("ImageTransfer:");
    final Canvas pasteImageCanvas = new Canvas(pasteParent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
    data = new GridData(GridData.FILL_BOTH);
    data.widthHint = HSIZE;
    data.heightHint = VSIZE;
    pasteImageCanvas.setLayoutData(data);
    final Point pasteOrigin = new Point(0, 0);
    final ScrollBar pasteHBar = pasteImageCanvas.getHorizontalBar();
    pasteHBar.setEnabled(false);
    pasteHBar.addListener(SWT.Selection, e -> {
        if (pasteImage[0] != null) {
            int hSelection = pasteHBar.getSelection();
            int destX = -hSelection - pasteOrigin.x;
            Rectangle rect = pasteImage[0].getBounds();
            pasteImageCanvas.scroll(destX, 0, 0, 0, rect.width, rect.height, false);
            pasteOrigin.x = -hSelection;
        }
    });
    final ScrollBar pasteVBar = pasteImageCanvas.getVerticalBar();
    pasteVBar.setEnabled(false);
    pasteVBar.addListener(SWT.Selection, e -> {
        if (pasteImage[0] != null) {
            int vSelection = pasteVBar.getSelection();
            int destY = -vSelection - pasteOrigin.y;
            Rectangle rect = pasteImage[0].getBounds();
            pasteImageCanvas.scroll(0, destY, 0, 0, rect.width, rect.height, false);
            pasteOrigin.y = -vSelection;
        }
    });
    pasteImageCanvas.addListener(SWT.Paint, e -> {
        if (pasteImage[0] != null) {
            GC gc = e.gc;
            gc.drawImage(pasteImage[0], pasteOrigin.x, pasteOrigin.y);
            Rectangle rect = pasteImage[0].getBounds();
            Rectangle client = pasteImageCanvas.getClientArea();
            int marginWidth = client.width - rect.width;
            if (marginWidth > 0) {
                gc.fillRectangle(rect.width, 0, marginWidth, client.height);
            }
            int marginHeight = client.height - rect.height;
            if (marginHeight > 0) {
                gc.fillRectangle(0, rect.height, client.width, marginHeight);
            }
        }
    });
    b = new Button(pasteParent, SWT.PUSH);
    b.setText("Paste");
    b.addSelectionListener(widgetSelectedAdapter(e -> {
        ImageData imageData = (ImageData) clipboard.getContents(ImageTransfer.getInstance());
        if (imageData != null) {
            if (pasteImage[0] != null) {
                System.out.println("PasteImage");
                pasteImage[0].dispose();
            }
            status.setText("");
            // Consume the ImageData at current zoom as-is.
            pasteImage[0] = new Image(e.display, new AutoScaleImageDataProvider(imageData));
            pasteVBar.setEnabled(true);
            pasteHBar.setEnabled(true);
            pasteOrigin.x = 0;
            pasteOrigin.y = 0;
            Rectangle rect = pasteImage[0].getBounds();
            Rectangle client = pasteImageCanvas.getClientArea();
            pasteHBar.setMaximum(rect.width);
            pasteVBar.setMaximum(rect.height);
            pasteHBar.setThumb(Math.min(rect.width, client.width));
            pasteVBar.setThumb(Math.min(rect.height, client.height));
            pasteImageCanvas.scroll(0, 0, 0, 0, rect.width, rect.height, true);
            pasteVBar.setSelection(0);
            pasteHBar.setSelection(0);
            pasteImageCanvas.redraw();
        } else {
            status.setText("No image to paste");
        }
    }));
}

From source file:Sample.java

public Sample() {
    shell.setText("Book Entry Demo");

    GridLayout gridLayout = new GridLayout(4, false);
    gridLayout.verticalSpacing = 8;/*  w ww. j  a v a  2 s. c  o  m*/

    shell.setLayout(gridLayout);

    // Title
    Label label = new Label(shell, SWT.NULL);
    label.setText("Title: ");

    Text title = new Text(shell, SWT.SINGLE | SWT.BORDER);
    GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
    gridData.horizontalSpan = 3;
    title.setLayoutData(gridData);

    // Author(s)
    label = new Label(shell, SWT.NULL);
    label.setText("Author(s): ");

    Text authors = new Text(shell, SWT.SINGLE | SWT.BORDER);
    gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
    gridData.horizontalSpan = 3;
    authors.setLayoutData(gridData);

    // Cover
    label = new Label(shell, SWT.NULL);
    label.setText("Cover: ");

    gridData = new GridData();
    gridData.verticalSpan = 3;
    label.setLayoutData(gridData);

    CLabel cover = new CLabel(shell, SWT.NULL);

    gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.horizontalSpan = 1;
    gridData.verticalSpan = 3;
    gridData.heightHint = 100;
    gridData.widthHint = 100;

    cover.setLayoutData(gridData);

    // Details.
    label = new Label(shell, SWT.NULL);
    label.setText("Pages");

    Text pages = new Text(shell, SWT.SINGLE | SWT.BORDER);
    pages.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));

    label = new Label(shell, SWT.NULL);
    label.setText("Publisher");

    Text pubisher = new Text(shell, SWT.SINGLE | SWT.BORDER);
    pubisher.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));

    label = new Label(shell, SWT.NULL);
    label.setText("Rating");

    Combo rating = new Combo(shell, SWT.READ_ONLY);
    rating.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
    rating.add("5");
    rating.add("4");
    rating.add("3");
    rating.add("2");
    rating.add("1");

    // Abstract.

    label = new Label(shell, SWT.NULL);
    label.setText("Abstract:");

    Text bookAbstract = new Text(shell, SWT.WRAP | SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
    gridData.horizontalSpan = 3;
    gridData.grabExcessVerticalSpace = true;

    bookAbstract.setLayoutData(gridData);

    // Button.
    Button enter = new Button(shell, SWT.PUSH);
    enter.setText("Enter");

    gridData = new GridData();
    gridData.horizontalSpan = 4;
    gridData.horizontalAlignment = GridData.END;
    enter.setLayoutData(gridData);

    // Fill information.

    title.setText("Professional Java Interfaces with SWT/JFace");
    authors.setText("Jack Li Guojie");
    pages.setText("500pp");
    pubisher.setText("John Wiley & Sons");
    cover.setBackground(new Image(display, "java2s.gif"));
    bookAbstract.setText("This book provides a comprehensive guide for \n"
            + "you to create Java user interfaces with SWT/JFace. ");

    shell.pack();
    shell.open();

    // Set up the event loop.
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            // If no more entries in event queue
            display.sleep();
        }
    }

    display.dispose();
}

From source file:GraphicsExample.java

void createToolBar(final Composite parent) {
    final Display display = parent.getDisplay();

    toolBar = new ToolBar(parent, SWT.FLAT);
    Listener toolBarListener = new Listener() {
        public void handleEvent(Event event) {
            switch (event.type) {
            case SWT.Selection: {
                if (event.widget == playItem) {
                    animate = true;//from  www. ja v a 2  s .  c o  m
                    playItem.setEnabled(!animate);
                    pauseItem.setEnabled(animate);
                } else if (event.widget == pauseItem) {
                    animate = false;
                    playItem.setEnabled(!animate);
                    pauseItem.setEnabled(animate);
                } else if (event.widget == backItem) {
                    final ToolItem toolItem = (ToolItem) event.widget;
                    final ToolBar toolBar = toolItem.getParent();
                    Rectangle toolItemBounds = toolItem.getBounds();
                    Point point = toolBar.toDisplay(new Point(toolItemBounds.x, toolItemBounds.y));
                    backMenu.setLocation(point.x, point.y + toolItemBounds.height);
                    backMenu.setVisible(true);
                }
            }
                break;
            }
        }
    };

    playItem = new ToolItem(toolBar, SWT.PUSH);
    playItem.setText(getResourceString("Play")); //$NON-NLS-1$
    playItem.setImage(loadImage(display, "play.gif")); //$NON-NLS-1$
    playItem.addListener(SWT.Selection, toolBarListener);

    pauseItem = new ToolItem(toolBar, SWT.PUSH);
    pauseItem.setText(getResourceString("Pause")); //$NON-NLS-1$
    pauseItem.setImage(loadImage(display, "pause.gif")); //$NON-NLS-1$
    pauseItem.addListener(SWT.Selection, toolBarListener);

    backItem = new ToolItem(toolBar, SWT.PUSH);
    backItem.setText(getResourceString("Background")); //$NON-NLS-1$
    backItem.addListener(SWT.Selection, toolBarListener);
    String[] names = new String[] { getResourceString("White"), //$NON-NLS-1$
            getResourceString("Black"), //$NON-NLS-1$
            getResourceString("Red"), //$NON-NLS-1$
            getResourceString("Green"), //$NON-NLS-1$
            getResourceString("Blue"), //$NON-NLS-1$
            getResourceString("CustomColor"), //$NON-NLS-1$
    };
    Color[] colors = new Color[] { display.getSystemColor(SWT.COLOR_WHITE),
            display.getSystemColor(SWT.COLOR_BLACK), display.getSystemColor(SWT.COLOR_RED),
            display.getSystemColor(SWT.COLOR_GREEN), display.getSystemColor(SWT.COLOR_BLUE), null, };
    backMenu = new Menu(parent);
    Listener listener = new Listener() {
        public void handleEvent(Event event) {
            MenuItem item = (MenuItem) event.widget;
            if (customMI == item) {
                ColorDialog dialog = new ColorDialog(parent.getShell());
                RGB rgb = dialog.open();
                if (rgb == null)
                    return;
                if (customColor != null)
                    customColor.dispose();
                customColor = new Color(display, rgb);
                if (customImage != null)
                    customImage.dispose();
                customImage = createImage(display, customColor);
                item.setData(new Object[] { customColor, customImage });
                item.setImage(customImage);
            }
            tabBackground = (Object[]) item.getData();
            backItem.setImage((Image) tabBackground[1]);
            canvas.redraw();
        }
    };
    for (int i = 0; i < names.length; i++) {
        MenuItem item = new MenuItem(backMenu, SWT.NONE);
        item.setText(names[i]);
        item.addListener(SWT.Selection, listener);
        Image image = null;
        if (colors[i] != null) {
            image = createImage(display, colors[i]);
            images.addElement(image);
            item.setImage(image);
        } else {
            // custom menu item
            customMI = item;
        }
        item.setData(new Object[] { colors[i], image });
        if (tabBackground == null) {
            tabBackground = (Object[]) item.getData();
            backItem.setImage((Image) tabBackground[1]);
        }
    }

    dbItem = new ToolItem(toolBar, SWT.CHECK);
    dbItem.setText(getResourceString("DoubleBuffer")); //$NON-NLS-1$
    dbItem.setImage(loadImage(display, "db.gif")); //$NON-NLS-1$

    ToolItem separator = new ToolItem(toolBar, SWT.SEPARATOR);
    Composite comp = new Composite(toolBar, SWT.NONE);
    GridData data;
    GridLayout layout = new GridLayout(1, false);
    layout.verticalSpacing = 0;
    layout.marginWidth = layout.marginHeight = 3;
    comp.setLayout(layout);
    timerSpinner = new Spinner(comp, SWT.BORDER | SWT.WRAP);
    data = new GridData(SWT.CENTER, SWT.CENTER, false, false);
    timerSpinner.setLayoutData(data);
    Label label = new Label(comp, SWT.NONE);
    label.setText(getResourceString("Animation")); //$NON-NLS-1$
    data = new GridData(SWT.CENTER, SWT.CENTER, false, false);
    label.setLayoutData(data);
    timerSpinner.setMaximum(1000);
    timerSpinner.setSelection(TIMER);
    timerSpinner.setSelection(TIMER);
    separator.setControl(comp);
    separator.setWidth(comp.computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
}