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

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

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Label setText.

Prototype

public void setText(String text) 

Source Link

Document

Sets the receiver's text.

Usage

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

private void createDragSource() {
    if (dragSource != null)
        dragSource.dispose();/* w ww.  j  a va  2  s .  c  o m*/
    dragSource = new DragSource(dragControl, dragOperation);
    dragSource.setTransfer(dragTypes);
    dragSource.addDragListener(new DragSourceListener() {
        @Override
        public void dragFinished(org.eclipse.swt.dnd.DragSourceEvent event) {
            dragConsole.append(">>dragFinished\n");
            printEvent(event);
            dragDataText = dragDataRTF = dragDataHTML = dragDataURL = null;
            dragDataFiles = null;
            if (event.detail == DND.DROP_MOVE) {
                switch (dragControlType) {
                case BUTTON_CHECK:
                case BUTTON_TOGGLE:
                case BUTTON_RADIO: {
                    Button b = (Button) dragControl;
                    b.setText("");
                    break;
                }
                case STYLED_TEXT: {
                    StyledText text = (StyledText) dragControl;
                    text.insert("");
                    break;
                }
                case TABLE: {
                    Table table = (Table) dragControl;
                    TableItem[] items = table.getSelection();
                    for (TableItem item : items) {
                        item.dispose();
                    }
                    break;
                }
                case TEXT: {
                    Text text = (Text) dragControl;
                    text.insert("");
                    break;
                }
                case TREE: {
                    Tree tree = (Tree) dragControl;
                    TreeItem[] items = tree.getSelection();
                    for (TreeItem item : items) {
                        item.dispose();
                    }
                    break;
                }
                case CANVAS: {
                    dragControl.setData("STRINGS", null);
                    dragControl.redraw();
                    break;
                }
                case LABEL: {
                    Label label = (Label) dragControl;
                    label.setText("");
                    break;
                }
                case LIST: {
                    List list = (List) dragControl;
                    int[] indices = list.getSelectionIndices();
                    list.remove(indices);
                    break;
                }
                case COMBO: {
                    Combo combo = (Combo) dragControl;
                    combo.setText("");
                    break;
                }
                }
            }
        }

        @Override
        public void dragSetData(org.eclipse.swt.dnd.DragSourceEvent event) {
            dragConsole.append(">>dragSetData\n");
            printEvent(event);
            if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
                event.data = dragDataText;
            }
            if (RTFTransfer.getInstance().isSupportedType(event.dataType)) {
                event.data = dragDataRTF;
            }
            if (HTMLTransfer.getInstance().isSupportedType(event.dataType)) {
                event.data = dragDataHTML;
            }
            if (URLTransfer.getInstance().isSupportedType(event.dataType)) {
                event.data = dragDataURL;
            }
            if (FileTransfer.getInstance().isSupportedType(event.dataType)) {
                event.data = dragDataFiles;
            }
        }

        @Override
        public void dragStart(org.eclipse.swt.dnd.DragSourceEvent event) {
            dragConsole.append(">>dragStart\n");
            printEvent(event);
            dragDataFiles = fileList.getItems();
            switch (dragControlType) {
            case BUTTON_CHECK:
            case BUTTON_TOGGLE:
            case BUTTON_RADIO: {
                Button b = (Button) dragControl;
                dragDataText = b.getSelection() ? "true" : "false";
                break;
            }
            case STYLED_TEXT: {
                StyledText text = (StyledText) dragControl;
                String s = text.getSelectionText();
                if (s.length() == 0) {
                    event.doit = false;
                } else {
                    dragDataText = s;
                }
                break;
            }
            case TABLE: {
                Table table = (Table) dragControl;
                TableItem[] items = table.getSelection();
                if (items.length == 0) {
                    event.doit = false;
                } else {
                    StringBuilder buffer = new StringBuilder();
                    for (int i = 0; i < items.length; i++) {
                        buffer.append(items[i].getText());
                        if (items.length > 1 && i < items.length - 1) {
                            buffer.append("\n");
                        }
                    }
                    dragDataText = buffer.toString();
                }
                break;
            }
            case TEXT: {
                Text text = (Text) dragControl;
                String s = text.getSelectionText();
                if (s.length() == 0) {
                    event.doit = false;
                } else {
                    dragDataText = s;
                }
                break;
            }
            case TREE: {
                Tree tree = (Tree) dragControl;
                TreeItem[] items = tree.getSelection();
                if (items.length == 0) {
                    event.doit = false;
                } else {
                    StringBuilder buffer = new StringBuilder();
                    for (int i = 0; i < items.length; i++) {
                        buffer.append(items[i].getText());
                        if (items.length > 1 && i < items.length - 1) {
                            buffer.append("\n");
                        }
                    }
                    dragDataText = buffer.toString();
                }
                break;
            }
            case CANVAS: {
                String[] strings = (String[]) dragControl.getData("STRINGS");
                if (strings == null || strings.length == 0) {
                    event.doit = false;
                } else {
                    StringBuilder buffer = new StringBuilder();
                    for (int i = 0; i < strings.length; i++) {
                        buffer.append(strings[i]);
                        if (strings.length > 1 && i < strings.length - 1) {
                            buffer.append("\n");
                        }
                    }
                    dragDataText = buffer.toString();
                }
                break;
            }
            case LABEL: {
                Label label = (Label) dragControl;
                String string = label.getText();
                if (string.length() == 0) {
                    event.doit = false;
                } else {
                    dragDataText = string;
                }
                break;
            }
            case LIST: {
                List list = (List) dragControl;
                String[] selection = list.getSelection();
                if (selection.length == 0) {
                    event.doit = false;
                } else {
                    StringBuilder buffer = new StringBuilder();
                    for (int i = 0; i < selection.length; i++) {
                        buffer.append(selection[i]);
                        if (selection.length > 1 && i < selection.length - 1) {
                            buffer.append("\n");
                        }
                    }
                    dragDataText = buffer.toString();
                }
                break;
            }
            case COMBO: {
                Combo combo = (Combo) dragControl;
                String string = combo.getText();
                Point selection = combo.getSelection();
                if (selection.x == selection.y) {
                    event.doit = false;
                } else {
                    dragDataText = string.substring(selection.x, selection.y);
                }
                break;
            }
            default:
                throw new SWTError(SWT.ERROR_NOT_IMPLEMENTED);
            }
            if (dragDataText != null) {
                dragDataRTF = "{\\rtf1{\\colortbl;\\red255\\green0\\blue0;}\\cf1\\b " + dragDataText + "}";
                dragDataHTML = "<b>" + dragDataText + "</b>";
                dragDataURL = "http://" + dragDataText.replace(' ', '.');
                try {
                    new URL(dragDataURL);
                } catch (MalformedURLException e) {
                    dragDataURL = null;
                }
            }

            for (Transfer dragType : dragTypes) {
                if (dragType instanceof TextTransfer && dragDataText == null) {
                    event.doit = false;
                }
                if (dragType instanceof RTFTransfer && dragDataRTF == null) {
                    event.doit = false;
                }
                if (dragType instanceof HTMLTransfer && dragDataHTML == null) {
                    event.doit = false;
                }
                if (dragType instanceof URLTransfer && dragDataURL == null) {
                    event.doit = false;
                }
                if (dragType instanceof FileTransfer && (dragDataFiles == null || dragDataFiles.length == 0)) {
                    event.doit = false;
                }
            }
        }
    });
}

From source file:org.eclipse.swt.examples.texteditor.TextEditor.java

void setLink() {
    final Shell dialog = new Shell(shell, SWT.APPLICATION_MODAL | SWT.SHELL_TRIM);
    dialog.setLayout(new GridLayout(2, false));
    dialog.setText(getResourceString("SetLink")); //$NON-NLS-1$
    Label label = new Label(dialog, SWT.NONE);
    label.setText(getResourceString("URL")); //$NON-NLS-1$
    final Text text = new Text(dialog, SWT.SINGLE);
    text.setLayoutData(new GridData(200, SWT.DEFAULT));
    if (link != null) {
        text.setText(link);//w ww  .  j  a v a 2 s.co m
        text.selectAll();
    }
    final Button okButton = new Button(dialog, SWT.PUSH);
    okButton.setText(getResourceString("Ok")); //$NON-NLS-1$
    final Button cancelButton = new Button(dialog, SWT.PUSH);
    cancelButton.setText(getResourceString("Cancel")); //$NON-NLS-1$
    Listener listener = event -> {
        if (event.widget == okButton) {
            link = text.getText();
            setStyle(UNDERLINE_LINK);
        }
        dialog.dispose();
    };
    okButton.addListener(SWT.Selection, listener);
    cancelButton.addListener(SWT.Selection, listener);
    dialog.setDefaultButton(okButton);
    dialog.pack();
    dialog.open();
    while (!dialog.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
}

From source file:com.planetmayo.debrief.satc_rcp.views.MaintainContributionsView.java

private void initPreferencesGroup(Composite parent) {
    GridData gridData = new GridData();
    gridData.horizontalAlignment = SWT.FILL;
    gridData.grabExcessHorizontalSpace = true;

    Group group = new Group(parent, SWT.SHADOW_ETCHED_IN);
    GridLayout layout = new GridLayout(1, false);
    group.setLayoutData(gridData);/*w w w . j  a  va 2  s .co  m*/
    group.setLayout(layout);
    group.setText("Preferences");

    final ScrolledComposite scrolled = new ScrolledComposite(group, SWT.H_SCROLL);
    scrolled.setLayoutData(new GridData(GridData.FILL_BOTH));
    final Composite preferencesComposite = UIUtils.createScrolledBody(scrolled, SWT.NONE);
    preferencesComposite.setLayout(new GridLayout(6, false));

    scrolled.addListener(SWT.Resize, new Listener() {

        @Override
        public void handleEvent(Event e) {
            scrolled.setMinSize(preferencesComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        }
    });
    scrolled.setAlwaysShowScrollBars(true);
    scrolled.setContent(preferencesComposite);
    scrolled.setMinSize(preferencesComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    scrolled.setExpandHorizontal(true);
    scrolled.setExpandVertical(true);

    liveConstraints = new Button(preferencesComposite, SWT.TOGGLE);
    liveConstraints.setText("Auto-Recalc of Constraints");
    liveConstraints.setEnabled(false);
    liveConstraints
            .setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_CENTER));

    recalculate = new Button(preferencesComposite, SWT.DEFAULT);
    recalculate.setText("Calculate Solution");
    recalculate.setEnabled(false);
    recalculate.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            if (activeSolver != null) {
                // ok - make sure the performance tab is open
                graphTabs.setSelection(performanceTab);

                activeSolver.run(true, true);
                main.setSize(0, 0);
                main.getParent().layout(true, true);
            }
        }
    });
    recalculate.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER | GridData.VERTICAL_ALIGN_CENTER));

    cancelGeneration = new Button(preferencesComposite, SWT.PUSH);
    cancelGeneration.setText("Cancel");
    cancelGeneration.setVisible(false);
    cancelGeneration.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            if (activeSolver != null) {
                activeSolver.cancel();
            }
        }
    });

    suppressCuts = new Button(preferencesComposite, SWT.CHECK);
    suppressCuts.setText("Suppress Cuts");
    suppressCuts.setVisible(true);
    suppressCuts.setEnabled(false);
    suppressCuts.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            if (activeSolver != null) {
                boolean doSuppress = suppressCuts.getSelection();
                activeSolver.setAutoSuppress(doSuppress);
            }
        }
    });

    showOSCourse = new Button(preferencesComposite, SWT.CHECK);
    showOSCourse.setText("Plot O/S Course");
    showOSCourse.setVisible(true);
    showOSCourse.setEnabled(false);
    showOSCourse.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            if (activeSolver != null) {
                redoOwnshipStates();
            }
        }
    });

    Composite precisionPanel = new Composite(preferencesComposite, SWT.NONE);
    precisionPanel.setLayoutData(new GridData(
            GridData.HORIZONTAL_ALIGN_END | GridData.GRAB_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER));

    GridLayout precisionLayout = new GridLayout(2, false);
    precisionLayout.horizontalSpacing = 5;
    precisionPanel.setLayout(precisionLayout);

    Label precisionLabel = new Label(precisionPanel, SWT.NONE);
    precisionLabel.setText("Precision:");
    precisionLabel.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER));

    precisionsCombo = new ComboViewer(precisionPanel);
    precisionsCombo.getCombo().setEnabled(false);
    precisionsCombo.setContentProvider(new ArrayContentProvider());
    precisionsCombo.setLabelProvider(new LabelProvider() {

        @Override
        public String getText(Object element) {
            return ((Precision) element).getLabel();
        }
    });
    precisionsCombo.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            ISelection sel = precisionsCombo.getSelection();
            IStructuredSelection cSel = (IStructuredSelection) sel;
            Precision precision = (Precision) cSel.getFirstElement();
            if (activeSolver != null) {
                activeSolver.setPrecision(precision);
            }
        }
    });
}

From source file:org.eclipse.swt.examples.texteditor.TextEditor.java

void createToolBar() {
    coolBar = new CoolBar(shell, SWT.FLAT);
    ToolBar styleToolBar = new ToolBar(coolBar, SWT.FLAT);
    boldControl = new ToolItem(styleToolBar, SWT.CHECK);
    boldControl.setImage(iBold);// w w w  . ja va2 s  .  com
    boldControl.setToolTipText(getResourceString("Bold")); //$NON-NLS-1$
    boldControl.addSelectionListener(widgetSelectedAdapter(event -> setStyle(BOLD)));

    italicControl = new ToolItem(styleToolBar, SWT.CHECK);
    italicControl.setImage(iItalic);
    italicControl.setToolTipText(getResourceString("Italic")); //$NON-NLS-1$
    italicControl.addSelectionListener(widgetSelectedAdapter(event -> setStyle(ITALIC)));

    final Menu underlineMenu = new Menu(shell, SWT.POP_UP);
    underlineSingleItem = new MenuItem(underlineMenu, SWT.RADIO);
    underlineSingleItem.setText(getResourceString("Single_menuitem")); //$NON-NLS-1$
    underlineSingleItem.addSelectionListener(widgetSelectedAdapter(event -> {
        if (underlineSingleItem.getSelection()) {
            setStyle(UNDERLINE_SINGLE);
        }
    }));
    underlineSingleItem.setSelection(true);

    underlineDoubleItem = new MenuItem(underlineMenu, SWT.RADIO);
    underlineDoubleItem.setText(getResourceString("Double_menuitem")); //$NON-NLS-1$
    underlineDoubleItem.addSelectionListener(widgetSelectedAdapter(event -> {
        if (underlineDoubleItem.getSelection()) {
            setStyle(UNDERLINE_DOUBLE);
        }
    }));

    underlineSquiggleItem = new MenuItem(underlineMenu, SWT.RADIO);
    underlineSquiggleItem.setText(getResourceString("Squiggle_menuitem")); //$NON-NLS-1$
    underlineSquiggleItem.addSelectionListener(widgetSelectedAdapter(event -> {
        if (underlineSquiggleItem.getSelection()) {
            setStyle(UNDERLINE_SQUIGGLE);
        }
    }));

    underlineErrorItem = new MenuItem(underlineMenu, SWT.RADIO);
    underlineErrorItem.setText(getResourceString("Error_menuitem")); //$NON-NLS-1$
    underlineErrorItem.addSelectionListener(widgetSelectedAdapter(event -> {
        if (underlineErrorItem.getSelection()) {
            setStyle(UNDERLINE_ERROR);
        }
    }));

    MenuItem underlineColorItem = new MenuItem(underlineMenu, SWT.PUSH);
    underlineColorItem.setText(getResourceString("Color_menuitem")); //$NON-NLS-1$
    underlineColorItem.addSelectionListener(widgetSelectedAdapter(event -> {
        ColorDialog dialog = new ColorDialog(shell);
        RGB rgb = underlineColor != null ? underlineColor.getRGB() : null;
        dialog.setRGB(rgb);
        RGB newRgb = dialog.open();
        if (newRgb != null) {
            if (!newRgb.equals(rgb)) {
                disposeResource(underlineColor);
                underlineColor = new Color(display, newRgb);
            }
            if (underlineSingleItem.getSelection())
                setStyle(UNDERLINE_SINGLE);
            else if (underlineDoubleItem.getSelection())
                setStyle(UNDERLINE_DOUBLE);
            else if (underlineErrorItem.getSelection())
                setStyle(UNDERLINE_ERROR);
            else if (underlineSquiggleItem.getSelection())
                setStyle(UNDERLINE_SQUIGGLE);
        }
    }));

    final ToolItem underlineControl = new ToolItem(styleToolBar, SWT.DROP_DOWN);
    underlineControl.setImage(iUnderline);
    underlineControl.setToolTipText(getResourceString("Underline")); //$NON-NLS-1$
    underlineControl.addSelectionListener(widgetSelectedAdapter(event -> {
        if (event.detail == SWT.ARROW) {
            Rectangle rect = underlineControl.getBounds();
            Point pt = new Point(rect.x, rect.y + rect.height);
            underlineMenu.setLocation(display.map(underlineControl.getParent(), null, pt));
            underlineMenu.setVisible(true);
        } else {
            if (underlineSingleItem.getSelection())
                setStyle(UNDERLINE_SINGLE);
            else if (underlineDoubleItem.getSelection())
                setStyle(UNDERLINE_DOUBLE);
            else if (underlineErrorItem.getSelection())
                setStyle(UNDERLINE_ERROR);
            else if (underlineSquiggleItem.getSelection())
                setStyle(UNDERLINE_SQUIGGLE);
        }
    }));

    ToolItem strikeoutControl = new ToolItem(styleToolBar, SWT.DROP_DOWN);
    strikeoutControl.setImage(iStrikeout);
    strikeoutControl.setToolTipText(getResourceString("Strikeout")); //$NON-NLS-1$
    strikeoutControl.addSelectionListener(widgetSelectedAdapter(event -> {
        if (event.detail == SWT.ARROW) {
            ColorDialog dialog = new ColorDialog(shell);
            RGB rgb = strikeoutColor != null ? strikeoutColor.getRGB() : null;
            dialog.setRGB(rgb);
            RGB newRgb = dialog.open();
            if (newRgb == null)
                return;
            if (!newRgb.equals(rgb)) {
                disposeResource(strikeoutColor);
                strikeoutColor = new Color(display, newRgb);
            }
        }
        setStyle(STRIKEOUT);
    }));

    final Menu borderMenu = new Menu(shell, SWT.POP_UP);
    borderSolidItem = new MenuItem(borderMenu, SWT.RADIO);
    borderSolidItem.setText(getResourceString("Solid")); //$NON-NLS-1$
    borderSolidItem.addSelectionListener(widgetSelectedAdapter(event -> {
        if (borderSolidItem.getSelection()) {
            setStyle(BORDER_SOLID);
        }
    }));
    borderSolidItem.setSelection(true);

    borderDashItem = new MenuItem(borderMenu, SWT.RADIO);
    borderDashItem.setText(getResourceString("Dash")); //$NON-NLS-1$
    borderDashItem.addSelectionListener(widgetSelectedAdapter(event -> {
        if (borderDashItem.getSelection()) {
            setStyle(BORDER_DASH);
        }
    }));

    borderDotItem = new MenuItem(borderMenu, SWT.RADIO);
    borderDotItem.setText(getResourceString("Dot")); //$NON-NLS-1$
    borderDotItem.addSelectionListener(widgetSelectedAdapter(event -> {
        if (borderDotItem.getSelection()) {
            setStyle(BORDER_DOT);
        }
    }));

    MenuItem borderColorItem = new MenuItem(borderMenu, SWT.PUSH);
    borderColorItem.setText(getResourceString("Color_menuitem")); //$NON-NLS-1$
    borderColorItem.addSelectionListener(widgetSelectedAdapter(event -> {
        ColorDialog dialog = new ColorDialog(shell);
        RGB rgb = borderColor != null ? borderColor.getRGB() : null;
        dialog.setRGB(rgb);
        RGB newRgb = dialog.open();
        if (newRgb != null) {
            if (!newRgb.equals(rgb)) {
                disposeResource(borderColor);
                borderColor = new Color(display, newRgb);
            }
            if (borderDashItem.getSelection())
                setStyle(BORDER_DASH);
            else if (borderDotItem.getSelection())
                setStyle(BORDER_DOT);
            else if (borderSolidItem.getSelection())
                setStyle(BORDER_SOLID);
        }
    }));

    final ToolItem borderControl = new ToolItem(styleToolBar, SWT.DROP_DOWN);
    borderControl.setImage(iBorderStyle);
    borderControl.setToolTipText(getResourceString("Box")); //$NON-NLS-1$
    borderControl.addSelectionListener(widgetSelectedAdapter(event -> {
        if (event.detail == SWT.ARROW) {
            Rectangle rect = borderControl.getBounds();
            Point pt = new Point(rect.x, rect.y + rect.height);
            borderMenu.setLocation(display.map(borderControl.getParent(), null, pt));
            borderMenu.setVisible(true);
        } else {
            if (borderDashItem.getSelection())
                setStyle(BORDER_DASH);
            else if (borderDotItem.getSelection())
                setStyle(BORDER_DOT);
            else if (borderSolidItem.getSelection())
                setStyle(BORDER_SOLID);
        }
    }));

    ToolItem foregroundItem = new ToolItem(styleToolBar, SWT.DROP_DOWN);
    foregroundItem.setImage(iTextForeground);
    foregroundItem.setToolTipText(getResourceString("TextForeground")); //$NON-NLS-1$
    foregroundItem.addSelectionListener(widgetSelectedAdapter(event -> {
        if (event.detail == SWT.ARROW || textForeground == null) {
            ColorDialog dialog = new ColorDialog(shell);
            RGB rgb = textForeground != null ? textForeground.getRGB() : null;
            dialog.setRGB(rgb);
            RGB newRgb = dialog.open();
            if (newRgb == null)
                return;
            if (!newRgb.equals(rgb)) {
                disposeResource(textForeground);
                textForeground = new Color(display, newRgb);
            }
        }
        setStyle(FOREGROUND);
    }));

    ToolItem backgroundItem = new ToolItem(styleToolBar, SWT.DROP_DOWN);
    backgroundItem.setImage(iTextBackground);
    backgroundItem.setToolTipText(getResourceString("TextBackground")); //$NON-NLS-1$
    backgroundItem.addSelectionListener(widgetSelectedAdapter(event -> {
        if (event.detail == SWT.ARROW || textBackground == null) {
            ColorDialog dialog = new ColorDialog(shell);
            RGB rgb = textBackground != null ? textBackground.getRGB() : null;
            dialog.setRGB(rgb);
            RGB newRgb = dialog.open();
            if (newRgb == null)
                return;
            if (!newRgb.equals(rgb)) {
                disposeResource(textBackground);
                textBackground = new Color(display, newRgb);
            }
        }
        setStyle(BACKGROUND);
    }));

    ToolItem baselineUpItem = new ToolItem(styleToolBar, SWT.PUSH);
    baselineUpItem.setImage(iBaselineUp);
    String tooltip = "IncreaseFont"; //$NON-NLS-1$
    if (USE_BASELINE)
        tooltip = "IncreaseBaseline"; //$NON-NLS-1$
    baselineUpItem.setToolTipText(getResourceString(tooltip));
    baselineUpItem.addSelectionListener(widgetSelectedAdapter(event -> {
        if (USE_BASELINE) {
            setStyle(BASELINE_UP);
        } else {
            adjustFontSize(1);
        }
    }));

    ToolItem baselineDownItem = new ToolItem(styleToolBar, SWT.PUSH);
    baselineDownItem.setImage(iBaselineDown);
    tooltip = "DecreaseFont"; //$NON-NLS-1$
    if (USE_BASELINE)
        tooltip = "DecreaseBaseline"; //$NON-NLS-1$
    baselineDownItem.setToolTipText(getResourceString(tooltip));
    baselineDownItem.addSelectionListener(widgetSelectedAdapter(event -> {
        if (USE_BASELINE) {
            setStyle(BASELINE_DOWN);
        } else {
            adjustFontSize(-1);
        }
    }));
    ToolItem linkItem = new ToolItem(styleToolBar, SWT.PUSH);
    linkItem.setImage(iLink);
    linkItem.setToolTipText(getResourceString("Link")); //$NON-NLS-1$
    linkItem.addSelectionListener(widgetSelectedAdapter(event -> setLink()));

    CoolItem coolItem = new CoolItem(coolBar, SWT.NONE);
    coolItem.setControl(styleToolBar);

    Composite composite = new Composite(coolBar, SWT.NONE);
    GridLayout layout = new GridLayout(2, false);
    layout.marginHeight = 1;
    composite.setLayout(layout);
    fontNameControl = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
    fontNameControl.setItems(getFontNames());
    fontNameControl.setVisibleItemCount(12);
    fontSizeControl = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
    fontSizeControl.setItems(FONT_SIZES);
    fontSizeControl.setVisibleItemCount(8);
    SelectionListener adapter = widgetSelectedAdapter(event -> {
        String name = fontNameControl.getText();
        int size = Integer.parseInt(fontSizeControl.getText());
        disposeResource(textFont);
        textFont = new Font(display, name, size, SWT.NORMAL);
        setStyle(FONT);
    });
    fontSizeControl.addSelectionListener(adapter);
    fontNameControl.addSelectionListener(adapter);
    coolItem = new CoolItem(coolBar, SWT.NONE);
    coolItem.setControl(composite);

    ToolBar alignmentToolBar = new ToolBar(coolBar, SWT.FLAT);
    blockSelectionItem = new ToolItem(alignmentToolBar, SWT.CHECK);
    blockSelectionItem.setImage(iBlockSelection);
    blockSelectionItem.setToolTipText(getResourceString("BlockSelection")); //$NON-NLS-1$
    blockSelectionItem.addSelectionListener(
            widgetSelectedAdapter(event -> styledText.invokeAction(ST.TOGGLE_BLOCKSELECTION)));

    leftAlignmentItem = new ToolItem(alignmentToolBar, SWT.RADIO);
    leftAlignmentItem.setImage(iLeftAlignment);
    leftAlignmentItem.setToolTipText(getResourceString("AlignLeft")); //$NON-NLS-1$
    leftAlignmentItem.setSelection(true);
    leftAlignmentItem.addSelectionListener(widgetSelectedAdapter(event -> {
        Point selection = styledText.getSelection();
        int lineStart = styledText.getLineAtOffset(selection.x);
        int lineEnd = styledText.getLineAtOffset(selection.y);
        styledText.setLineAlignment(lineStart, lineEnd - lineStart + 1, SWT.LEFT);
    }));
    leftAlignmentItem.setEnabled(false);

    centerAlignmentItem = new ToolItem(alignmentToolBar, SWT.RADIO);
    centerAlignmentItem.setImage(iCenterAlignment);
    centerAlignmentItem.setToolTipText(getResourceString("Center_menuitem")); //$NON-NLS-1$
    centerAlignmentItem.addSelectionListener(widgetSelectedAdapter(event -> {
        Point selection = styledText.getSelection();
        int lineStart = styledText.getLineAtOffset(selection.x);
        int lineEnd = styledText.getLineAtOffset(selection.y);
        styledText.setLineAlignment(lineStart, lineEnd - lineStart + 1, SWT.CENTER);
    }));
    centerAlignmentItem.setEnabled(false);

    rightAlignmentItem = new ToolItem(alignmentToolBar, SWT.RADIO);
    rightAlignmentItem.setImage(iRightAlignment);
    rightAlignmentItem.setToolTipText(getResourceString("AlignRight")); //$NON-NLS-1$
    rightAlignmentItem.addSelectionListener(widgetSelectedAdapter(event -> {
        Point selection = styledText.getSelection();
        int lineStart = styledText.getLineAtOffset(selection.x);
        int lineEnd = styledText.getLineAtOffset(selection.y);
        styledText.setLineAlignment(lineStart, lineEnd - lineStart + 1, SWT.RIGHT);
    }));
    rightAlignmentItem.setEnabled(false);

    justifyAlignmentItem = new ToolItem(alignmentToolBar, SWT.CHECK);
    justifyAlignmentItem.setImage(iJustifyAlignment);
    justifyAlignmentItem.setToolTipText(getResourceString("Justify")); //$NON-NLS-1$
    justifyAlignmentItem.addSelectionListener(widgetSelectedAdapter(event -> {
        Point selection = styledText.getSelection();
        int lineStart = styledText.getLineAtOffset(selection.x);
        int lineEnd = styledText.getLineAtOffset(selection.y);
        styledText.setLineJustify(lineStart, lineEnd - lineStart + 1, justifyAlignmentItem.getSelection());
    }));
    justifyAlignmentItem.setEnabled(false);

    ToolItem bulletListItem = new ToolItem(alignmentToolBar, SWT.PUSH);
    bulletListItem.setImage(iBulletList);
    bulletListItem.setToolTipText(getResourceString("BulletList")); //$NON-NLS-1$
    bulletListItem.addSelectionListener(widgetSelectedAdapter(event -> setBullet(ST.BULLET_DOT)));

    ToolItem numberedListItem = new ToolItem(alignmentToolBar, SWT.PUSH);
    numberedListItem.setImage(iNumberedList);
    numberedListItem.setToolTipText(getResourceString("NumberedList")); //$NON-NLS-1$
    numberedListItem
            .addSelectionListener(widgetSelectedAdapter(event -> setBullet(ST.BULLET_NUMBER | ST.BULLET_TEXT)));

    coolItem = new CoolItem(coolBar, SWT.NONE);
    coolItem.setControl(alignmentToolBar);
    composite = new Composite(coolBar, SWT.NONE);
    layout = new GridLayout(4, false);
    layout.marginHeight = 1;
    composite.setLayout(layout);
    Label label = new Label(composite, SWT.NONE);
    label.setText(getResourceString("Indent")); //$NON-NLS-1$
    Spinner indent = new Spinner(composite, SWT.BORDER);
    indent.addSelectionListener(widgetSelectedAdapter(event -> {
        Spinner spinner = (Spinner) event.widget;
        styledText.setIndent(spinner.getSelection());
    }));
    label = new Label(composite, SWT.NONE);
    label.setText(getResourceString("Spacing")); //$NON-NLS-1$
    Spinner spacing = new Spinner(composite, SWT.BORDER);
    spacing.addSelectionListener(widgetSelectedAdapter(event -> {
        Spinner spinner = (Spinner) event.widget;
        styledText.setLineSpacing(spinner.getSelection());
    }));

    coolItem = new CoolItem(coolBar, SWT.NONE);
    coolItem.setControl(composite);

    // Button to toggle Mouse Navigator in StyledText
    composite = new Composite(coolBar, SWT.NONE);
    composite.setLayout(new GridLayout(1, false));
    Button mouseNavigator = new Button(composite, SWT.CHECK);
    mouseNavigator.setText(getResourceString("MouseNav"));
    mouseNavigator.addSelectionListener(
            widgetSelectedAdapter(event -> styledText.setMouseNavigatorEnabled(mouseNavigator.getSelection())));
    coolItem = new CoolItem(coolBar, SWT.NONE);
    coolItem.setControl(composite);

    // Compute Size for various CoolItems
    CoolItem[] coolItems = coolBar.getItems();
    for (CoolItem item : coolItems) {
        Control control = item.getControl();
        Point size = control.computeSize(SWT.DEFAULT, SWT.DEFAULT);
        item.setMinimumSize(size);
        size = item.computeSize(size.x, size.y);
        item.setPreferredSize(size);
        item.setSize(size);
    }

    coolBar.addControlListener(ControlListener.controlResizedAdapter(event -> handleResize(event)));
}

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

void createEditEventDialog(Shell parent, int x, int y, final int index) {
    final Shell dialog = new Shell(parent, SWT.DIALOG_TRIM | SWT.RESIZE | SWT.APPLICATION_MODAL);
    dialog.setLayout(new GridLayout());
    dialog.setText(ControlExample.getResourceString("Edit_Event"));
    Label label = new Label(dialog, SWT.NONE);
    label.setText(ControlExample.getResourceString("Edit_Event_Fields", EVENT_INFO[index].name));

    Group group = new Group(dialog, SWT.NONE);
    group.setLayout(new GridLayout(2, false));
    group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    final int fields = EVENT_INFO[index].settableFields;
    final int eventType = EVENT_INFO[index].type;
    setFieldsMask = EVENT_INFO[index].setFields;
    setFieldsEvent = EVENT_INFO[index].event;

    if ((fields & DOIT) != 0) {
        new Label(group, SWT.NONE).setText("doit");
        final Combo doitCombo = new Combo(group, SWT.READ_ONLY);
        doitCombo.setItems("", "true", "false");
        if ((setFieldsMask & DOIT) != 0)
            doitCombo.setText(Boolean.toString(setFieldsEvent.doit));
        doitCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        doitCombo.addSelectionListener(widgetSelectedAdapter(e -> {
            String newValue = doitCombo.getText();
            if (newValue.length() == 0) {
                setFieldsMask &= ~DOIT;
            } else {
                setFieldsEvent.type = eventType;
                setFieldsEvent.doit = newValue.equals("true");
                setFieldsMask |= DOIT;/* www  .  j ava 2  s  .c om*/
            }
        }));
    }

    if ((fields & DETAIL) != 0) {
        new Label(group, SWT.NONE).setText("detail");
        int detailType = fields & 0xFF;
        final Combo detailCombo = new Combo(group, SWT.READ_ONLY);
        detailCombo.setItems(DETAIL_CONSTANTS[detailType]);
        detailCombo.add("", 0);
        detailCombo.setVisibleItemCount(detailCombo.getItemCount());
        if ((setFieldsMask & DETAIL) != 0)
            detailCombo.setText(DETAIL_CONSTANTS[detailType][setFieldsEvent.detail]);
        detailCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        detailCombo.addSelectionListener(widgetSelectedAdapter(e -> {
            String newValue = detailCombo.getText();
            if (newValue.length() == 0) {
                setFieldsMask &= ~DETAIL;
            } else {
                setFieldsEvent.type = eventType;
                for (int i = 0; i < DETAIL_VALUES.length; i += 2) {
                    if (newValue.equals(DETAIL_VALUES[i])) {
                        setFieldsEvent.detail = ((Integer) DETAIL_VALUES[i + 1]).intValue();
                        break;
                    }
                }
                setFieldsMask |= DETAIL;
            }
        }));
    }

    if ((fields & TEXT) != 0) {
        new Label(group, SWT.NONE).setText("text");
        final Text textText = new Text(group, SWT.BORDER);
        if ((setFieldsMask & TEXT) != 0)
            textText.setText(setFieldsEvent.text);
        textText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        textText.addModifyListener(e -> {
            String newValue = textText.getText();
            if (newValue.length() == 0) {
                setFieldsMask &= ~TEXT;
            } else {
                setFieldsEvent.type = eventType;
                setFieldsEvent.text = newValue;
                setFieldsMask |= TEXT;
            }
        });
    }

    if ((fields & X) != 0) {
        new Label(group, SWT.NONE).setText("x");
        final Text xText = new Text(group, SWT.BORDER);
        if ((setFieldsMask & X) != 0)
            xText.setText(Integer.toString(setFieldsEvent.x));
        xText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        xText.addModifyListener(e -> {
            String newValue = xText.getText();
            try {
                int newIntValue = Integer.parseInt(newValue);
                setFieldsEvent.type = eventType;
                setFieldsEvent.x = newIntValue;
                setFieldsMask |= X;
            } catch (NumberFormatException ex) {
                setFieldsMask &= ~X;
            }
        });
    }

    if ((fields & Y) != 0) {
        new Label(group, SWT.NONE).setText("y");
        final Text yText = new Text(group, SWT.BORDER);
        if ((setFieldsMask & Y) != 0)
            yText.setText(Integer.toString(setFieldsEvent.y));
        yText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        yText.addModifyListener(e -> {
            String newValue = yText.getText();
            try {
                int newIntValue = Integer.parseInt(newValue);
                setFieldsEvent.type = eventType;
                setFieldsEvent.y = newIntValue;
                setFieldsMask |= Y;
            } catch (NumberFormatException ex) {
                setFieldsMask &= ~Y;
            }
        });
    }

    if ((fields & WIDTH) != 0) {
        new Label(group, SWT.NONE).setText("width");
        final Text widthText = new Text(group, SWT.BORDER);
        if ((setFieldsMask & WIDTH) != 0)
            widthText.setText(Integer.toString(setFieldsEvent.width));
        widthText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        widthText.addModifyListener(e -> {
            String newValue = widthText.getText();
            try {
                int newIntValue = Integer.parseInt(newValue);
                setFieldsEvent.type = eventType;
                setFieldsEvent.width = newIntValue;
                setFieldsMask |= WIDTH;
            } catch (NumberFormatException ex) {
                setFieldsMask &= ~WIDTH;
            }
        });
    }

    if ((fields & HEIGHT) != 0) {
        new Label(group, SWT.NONE).setText("height");
        final Text heightText = new Text(group, SWT.BORDER);
        if ((setFieldsMask & HEIGHT) != 0)
            heightText.setText(Integer.toString(setFieldsEvent.height));
        heightText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        heightText.addModifyListener(e -> {
            String newValue = heightText.getText();
            try {
                int newIntValue = Integer.parseInt(newValue);
                setFieldsEvent.type = eventType;
                setFieldsEvent.height = newIntValue;
                setFieldsMask |= HEIGHT;
            } catch (NumberFormatException ex) {
                setFieldsMask &= ~HEIGHT;
            }
        });
    }

    Button ok = new Button(dialog, SWT.PUSH);
    ok.setText(ControlExample.getResourceString("OK"));
    GridData data = new GridData(70, SWT.DEFAULT);
    data.horizontalAlignment = SWT.RIGHT;
    ok.setLayoutData(data);
    ok.addSelectionListener(widgetSelectedAdapter(e -> {
        EVENT_INFO[index].setFields = setFieldsMask;
        EVENT_INFO[index].event = setFieldsEvent;
        dialog.dispose();
    }));

    dialog.setDefaultButton(ok);
    dialog.pack();
    dialog.setLocation(x, y);
    dialog.open();
}

From source file:ImageAnalyzer.java

int showBMPDialog() {
    final int[] bmpType = new int[1];
    bmpType[0] = SWT.IMAGE_BMP;/* w w w.  j  av a2s .  co  m*/
    SelectionListener radioSelected = new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Button radio = (Button) event.widget;
            if (radio.getSelection())
                bmpType[0] = ((Integer) radio.getData()).intValue();
        }
    };
    // need to externalize strings
    final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM);

    dialog.setText("Save_as");
    dialog.setLayout(new GridLayout());

    Label label = new Label(dialog, SWT.NONE);
    label.setText("Save_as");

    Button radio = new Button(dialog, SWT.RADIO);
    radio.setText("Save_as_type_no_compress");
    radio.setSelection(true);
    radio.setData(new Integer(SWT.IMAGE_BMP));
    radio.addSelectionListener(radioSelected);

    radio = new Button(dialog, SWT.RADIO);
    radio.setText("Save_as_type_rle_compress");
    radio.setData(new Integer(SWT.IMAGE_BMP_RLE));
    radio.addSelectionListener(radioSelected);

    radio = new Button(dialog, SWT.RADIO);
    radio.setText("Save_as_type_os2");
    radio.setData(new Integer(SWT.IMAGE_OS2_BMP));
    radio.addSelectionListener(radioSelected);

    label = new Label(dialog, SWT.SEPARATOR | SWT.HORIZONTAL);
    label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    Button ok = new Button(dialog, SWT.PUSH);
    ok.setText("OK");
    GridData data = new GridData();
    data.horizontalAlignment = SWT.CENTER;
    data.widthHint = 75;
    ok.setLayoutData(data);
    ok.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            dialog.close();
        }
    });

    dialog.pack();
    dialog.open();
    while (!dialog.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    return bmpType[0];
}

From source file:org.eclipse.swt.examples.imageanalyzer.ImageAnalyzer.java

int showBMPDialog() {
    final int[] bmpType = new int[1];
    bmpType[0] = SWT.IMAGE_BMP;/* ww w .  j  a  v  a2s . com*/
    SelectionListener radioSelected = widgetSelectedAdapter(event -> {
        Button radio = (Button) event.widget;
        if (radio.getSelection())
            bmpType[0] = ((Integer) radio.getData()).intValue();
    });
    // need to externalize strings
    final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM);

    dialog.setText(bundle.getString("Save_as_type"));
    dialog.setLayout(new GridLayout());

    Label label = new Label(dialog, SWT.NONE);
    label.setText(bundle.getString("Save_as_type_label"));

    Button radio = new Button(dialog, SWT.RADIO);
    radio.setText(bundle.getString("Save_as_type_no_compress"));
    radio.setSelection(true);
    radio.setData(Integer.valueOf(SWT.IMAGE_BMP));
    radio.addSelectionListener(radioSelected);

    radio = new Button(dialog, SWT.RADIO);
    radio.setText(bundle.getString("Save_as_type_rle_compress"));
    radio.setData(Integer.valueOf(SWT.IMAGE_BMP_RLE));
    radio.addSelectionListener(radioSelected);

    radio = new Button(dialog, SWT.RADIO);
    radio.setText(bundle.getString("Save_as_type_os2"));
    radio.setData(Integer.valueOf(SWT.IMAGE_OS2_BMP));
    radio.addSelectionListener(radioSelected);

    label = new Label(dialog, SWT.SEPARATOR | SWT.HORIZONTAL);
    label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    Button ok = new Button(dialog, SWT.PUSH);
    ok.setText(bundle.getString("OK"));
    GridData data = new GridData();
    data.horizontalAlignment = SWT.CENTER;
    data.widthHint = 75;
    ok.setLayoutData(data);
    ok.addSelectionListener(widgetSelectedAdapter(e -> dialog.close()));

    dialog.pack();
    dialog.open();
    while (!dialog.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    return bmpType[0];
}

From source file:LayoutExample.java

/**
 * Refreshes the composite and draws all controls in the layout example.
 *///w w w  . ja v  a2  s. com
void refreshLayoutComposite() {
    /* Remove children that are already laid out */
    children = layoutComposite.getChildren();
    for (int i = 0; i < children.length; i++) {
        children[i].dispose();
    }
    /* Add all children listed in the table */
    TableItem[] items = table.getItems();
    children = new Control[items.length];
    String[] itemValues = new String[] { LayoutExample.getResourceString("Item", new String[] { "1" }),
            LayoutExample.getResourceString("Item", new String[] { "2" }),
            LayoutExample.getResourceString("Item", new String[] { "3" }) };
    for (int i = 0; i < items.length; i++) {
        String control = items[i].getText(1);
        if (control.equals("Button")) {
            Button button = new Button(layoutComposite, SWT.PUSH);
            button.setText(LayoutExample.getResourceString("Button_Index",
                    new String[] { new Integer(i).toString() }));
            children[i] = button;
        } else if (control.equals("Canvas")) {
            Canvas canvas = new Canvas(layoutComposite, SWT.BORDER);
            children[i] = canvas;
        } else if (control.equals("Combo")) {
            Combo combo = new Combo(layoutComposite, SWT.NONE);
            combo.setItems(itemValues);
            combo.setText(
                    LayoutExample.getResourceString("Combo_Index", new String[] { new Integer(i).toString() }));
            children[i] = combo;
        } else if (control.equals("Composite")) {
            Composite composite = new Composite(layoutComposite, SWT.BORDER);
            children[i] = composite;
        } else if (control.equals("CoolBar")) {
            CoolBar coolBar = new CoolBar(layoutComposite, SWT.NONE);
            ToolBar toolBar = new ToolBar(coolBar, SWT.BORDER);
            ToolItem item = new ToolItem(toolBar, 0);
            item.setText(LayoutExample.getResourceString("Item", new String[] { "1" }));
            item = new ToolItem(toolBar, 0);
            item.setText(LayoutExample.getResourceString("Item", new String[] { "2" }));
            CoolItem coolItem1 = new CoolItem(coolBar, 0);
            coolItem1.setControl(toolBar);
            toolBar = new ToolBar(coolBar, SWT.BORDER);
            item = new ToolItem(toolBar, 0);
            item.setText(LayoutExample.getResourceString("Item", new String[] { "3" }));
            item = new ToolItem(toolBar, 0);
            item.setText(LayoutExample.getResourceString("Item", new String[] { "4" }));
            CoolItem coolItem2 = new CoolItem(coolBar, 0);
            coolItem2.setControl(toolBar);
            Point size = toolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT);
            coolItem1.setSize(coolItem1.computeSize(size.x, size.y));
            coolItem2.setSize(coolItem2.computeSize(size.x, size.y));
            coolBar.setSize(coolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT));
            children[i] = coolBar;
        } else if (control.equals("Group")) {
            Group group = new Group(layoutComposite, SWT.NONE);
            group.setText(
                    LayoutExample.getResourceString("Group_Index", new String[] { new Integer(i).toString() }));
            children[i] = group;
        } else if (control.equals("Label")) {
            Label label = new Label(layoutComposite, SWT.NONE);
            label.setText(
                    LayoutExample.getResourceString("Label_Index", new String[] { new Integer(i).toString() }));
            children[i] = label;
        } else if (control.equals("List")) {
            List list = new List(layoutComposite, SWT.BORDER);
            list.setItems(itemValues);
            children[i] = list;
        } else if (control.equals("ProgressBar")) {
            ProgressBar progress = new ProgressBar(layoutComposite, SWT.NONE);
            progress.setSelection(50);
            children[i] = progress;
        } else if (control.equals("Scale")) {
            Scale scale = new Scale(layoutComposite, SWT.NONE);
            children[i] = scale;
        } else if (control.equals("Slider")) {
            Slider slider = new Slider(layoutComposite, SWT.NONE);
            children[i] = slider;
        } else if (control.equals("StyledText")) {
            StyledText styledText = new StyledText(layoutComposite,
                    SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
            styledText.setText(LayoutExample.getResourceString("StyledText_Index",
                    new String[] { new Integer(i).toString() }));
            children[i] = styledText;
        } else if (control.equals("Table")) {
            Table table = new Table(layoutComposite, SWT.BORDER);
            table.setLinesVisible(true);
            TableItem item1 = new TableItem(table, 0);
            item1.setText(LayoutExample.getResourceString("Item", new String[] { "1" }));
            TableItem item2 = new TableItem(table, 0);
            item2.setText(LayoutExample.getResourceString("Item", new String[] { "2" }));
            children[i] = table;
        } else if (control.equals("Text")) {
            Text text = new Text(layoutComposite, SWT.BORDER);
            text.setText(
                    LayoutExample.getResourceString("Text_Index", new String[] { new Integer(i).toString() }));
            children[i] = text;
        } else if (control.equals("ToolBar")) {
            ToolBar toolBar = new ToolBar(layoutComposite, SWT.BORDER);
            ToolItem item1 = new ToolItem(toolBar, 0);
            item1.setText(LayoutExample.getResourceString("Item", new String[] { "1" }));
            ToolItem item2 = new ToolItem(toolBar, 0);
            item2.setText(LayoutExample.getResourceString("Item", new String[] { "2" }));
            children[i] = toolBar;
        } else {
            Tree tree = new Tree(layoutComposite, SWT.BORDER);
            TreeItem item1 = new TreeItem(tree, 0);
            item1.setText(LayoutExample.getResourceString("Item", new String[] { "1" }));
            TreeItem item2 = new TreeItem(tree, 0);
            item2.setText(LayoutExample.getResourceString("Item", new String[] { "2" }));
            children[i] = tree;
        }
    }
}

From source file:PaintExample.java

/**
 * Handles a mouseDown event./*from www  .j  a va  2  s  . c o m*/
 * 
 * @param event the mouse event detail information
 */
public void mouseDown(MouseEvent event) {
    if (event.button == 1) {
        // draw with left mouse button
        getPaintSurface().commitRubberbandSelection();
    } else {
        // set text with right mouse button
        getPaintSurface().clearRubberbandSelection();
        Shell shell = getPaintSurface().getShell();
        final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
        dialog.setText(PaintExample.getResourceString("tool.Text.dialog.title"));
        dialog.setLayout(new GridLayout());
        Label label = new Label(dialog, SWT.NONE);
        label.setText(PaintExample.getResourceString("tool.Text.dialog.message"));
        label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
        final Text field = new Text(dialog, SWT.SINGLE | SWT.BORDER);
        field.setText(drawText);
        field.selectAll();
        field.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        Composite buttons = new Composite(dialog, SWT.NONE);
        GridLayout layout = new GridLayout(2, true);
        layout.marginWidth = 0;
        buttons.setLayout(layout);
        buttons.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
        Button ok = new Button(buttons, SWT.PUSH);
        ok.setText(PaintExample.getResourceString("OK"));
        ok.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
        ok.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                drawText = field.getText();
                dialog.dispose();
            }
        });
        Button cancel = new Button(buttons, SWT.PUSH);
        cancel.setText(PaintExample.getResourceString("Cancel"));
        cancel.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                dialog.dispose();
            }
        });
        dialog.setDefaultButton(ok);
        dialog.pack();
        dialog.open();
        Display display = dialog.getDisplay();
        while (!shell.isDisposed() && !dialog.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
    }
}

From source file:PaintExample.java

/**
 * Creates the GUI.//from w ww.j  a  va  2s  .c o m
 */
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();
        }
    });
}