Example usage for org.eclipse.swt.widgets Button getSelection

List of usage examples for org.eclipse.swt.widgets Button getSelection

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Button getSelection.

Prototype

public boolean getSelection() 

Source Link

Document

Returns true if the receiver is selected, and false otherwise.

Usage

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

public static void main(String[] args) {

    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 175");
    shell.setLayout(new GridLayout(3, false));

    Button b = new Button(shell, SWT.PUSH);
    b.setText("Button 0");

    final Button bHidden = new Button(shell, SWT.PUSH);
    bHidden.setText("Button 1");
    GridData data = new GridData();
    data.exclude = true;/*from   w w w. jav  a 2 s  . c o  m*/
    data.horizontalSpan = 2;
    data.horizontalAlignment = SWT.FILL;
    bHidden.setLayoutData(data);

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 2");
    b = new Button(shell, SWT.PUSH);
    b.setText("Button 3");
    b = new Button(shell, SWT.PUSH);
    b.setText("Button 4");

    b = new Button(shell, SWT.CHECK);
    b.setText("hide");
    b.setSelection(true);
    b.addListener(SWT.Selection, e -> {
        Button b1 = (Button) e.widget;
        GridData data1 = (GridData) bHidden.getLayoutData();
        data1.exclude = b1.getSelection();
        bHidden.setVisible(!data1.exclude);
        shell.layout(false);
    });
    shell.setSize(400, 400);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    display = new Display();
    shell = new Shell(display);
    shell.setText("Snippet 336");
    shell.setLayout(new GridLayout());
    TabFolder folder = new TabFolder(shell, SWT.NONE);
    folder.setLayoutData(new GridData(GridData.FILL_BOTH));

    //Progress tab
    TabItem item = new TabItem(folder, SWT.NONE);
    item.setText("Progress");
    Composite composite = new Composite(folder, SWT.NONE);
    composite.setLayout(new GridLayout());
    item.setControl(composite);/*from w w  w  .jav  a 2  s. c  o  m*/
    Listener listener = event -> {
        Button button = (Button) event.widget;
        if (!button.getSelection())
            return;
        TaskItem item1 = getTaskBarItem();
        if (item1 != null) {
            int state = ((Integer) button.getData()).intValue();
            item1.setProgressState(state);
        }
    };
    Group group = new Group(composite, SWT.NONE);
    group.setText("State");
    group.setLayout(new GridLayout());
    group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    Button button;
    String[] stateLabels = { "SWT.DEFAULT", "SWT.INDETERMINATE", "SWT.NORMAL", "SWT.ERROR", "SWT.PAUSED" };
    int[] states = { SWT.DEFAULT, SWT.INDETERMINATE, SWT.NORMAL, SWT.ERROR, SWT.PAUSED };
    for (int i = 0; i < states.length; i++) {
        button = new Button(group, SWT.RADIO);
        button.setText(stateLabels[i]);
        button.setData(Integer.valueOf(states[i]));
        button.addListener(SWT.Selection, listener);
        if (i == 0)
            button.setSelection(true);
    }
    group = new Group(composite, SWT.NONE);
    group.setText("Value");
    group.setLayout(new GridLayout(2, false));
    group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    Label label = new Label(group, SWT.NONE);
    label.setText("Progress");
    final Scale scale = new Scale(group, SWT.NONE);
    scale.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    scale.addListener(SWT.Selection, event -> {
        TaskItem item1 = getTaskBarItem();
        if (item1 != null)
            item1.setProgress(scale.getSelection());
    });

    //Overlay text tab
    item = new TabItem(folder, SWT.NONE);
    item.setText("Text");
    composite = new Composite(folder, SWT.NONE);
    composite.setLayout(new GridLayout());
    item.setControl(composite);
    group = new Group(composite, SWT.NONE);
    group.setText("Enter a short text:");
    group.setLayout(new GridLayout(2, false));
    group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    final Text text = new Text(group, SWT.BORDER | SWT.SINGLE);
    GridData data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 2;
    text.setLayoutData(data);
    button = new Button(group, SWT.PUSH);
    button.setText("Set");
    button.addListener(SWT.Selection, event -> {
        TaskItem item1 = getTaskBarItem();
        if (item1 != null)
            item1.setOverlayText(text.getText());
    });
    button = new Button(group, SWT.PUSH);
    button.setText("Clear");
    button.addListener(SWT.Selection, event -> {
        text.setText("");
        TaskItem item1 = getTaskBarItem();
        if (item1 != null)
            item1.setOverlayText("");
    });

    //Overlay image tab
    item = new TabItem(folder, SWT.NONE);
    item.setText("Image");
    composite = new Composite(folder, SWT.NONE);
    composite.setLayout(new GridLayout());
    item.setControl(composite);
    Listener listener3 = event -> {
        Button button1 = (Button) event.widget;
        if (!button1.getSelection())
            return;
        TaskItem item1 = getTaskBarItem();
        if (item1 != null) {
            String text1 = button1.getText();
            Image image = null;
            if (!text1.equals("NONE"))
                image = new Image(display, Snippet336.class.getResourceAsStream(text1));
            Image oldImage = item1.getOverlayImage();
            item1.setOverlayImage(image);
            if (oldImage != null)
                oldImage.dispose();
        }
    };
    group = new Group(composite, SWT.NONE);
    group.setText("Images");
    group.setLayout(new GridLayout());
    group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    button = new Button(group, SWT.RADIO);
    button.setText("NONE");
    button.addListener(SWT.Selection, listener3);
    button.setSelection(true);
    String[] images = { "eclipse.png", "pause.gif", "run.gif", "warning.gif" };
    for (int i = 0; i < images.length; i++) {
        button = new Button(group, SWT.RADIO);
        button.setText(images[i]);
        button.addListener(SWT.Selection, listener3);
    }
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 315");
    shell.setLayout(new GridLayout());
    final Button button = new Button(shell, SWT.CHECK);
    button.setLayoutData(//from  w  w w  .j a va  2s  . c o  m
            new GridData(GridData.GRAB_VERTICAL | GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_CENTER));
    button.setText("Tri-state");
    /* Make the button toggle between three states */
    button.addListener(SWT.Selection, e -> {
        if (button.getSelection()) {
            if (!button.getGrayed()) {
                button.setGrayed(true);
            }
        } else {
            if (button.getGrayed()) {
                button.setGrayed(false);
                button.setSelection(true);
            }
        }
    });
    /* Read the tri-state button (application code) */
    button.addListener(SWT.Selection, e -> {
        if (button.getGrayed()) {
            System.out.println("Grayed");
        } else {
            if (button.getSelection()) {
                System.out.println("Selected");
            } else {
                System.out.println("Not selected");
            }
        }
    });
    shell.setSize(300, 300);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:GridLayoutWidgetExclude.java

public static void main(String[] args) {

    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(3, false));

    Button b = new Button(shell, SWT.PUSH);
    b.setText("Button 0");

    final Button bHidden = new Button(shell, SWT.PUSH);
    bHidden.setText("Button 1");
    GridData data = new GridData();
    data.exclude = true;//  w w  w  .  j  a  v a  2  s.c o m
    data.horizontalSpan = 2;
    data.horizontalAlignment = SWT.FILL;
    bHidden.setLayoutData(data);

    b = new Button(shell, SWT.PUSH);
    b.setText("Button 2");
    b = new Button(shell, SWT.PUSH);
    b.setText("Button 3");
    b = new Button(shell, SWT.PUSH);
    b.setText("Button 4");

    b = new Button(shell, SWT.CHECK);
    b.setText("hide");
    b.setSelection(true);
    b.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            Button b = (Button) e.widget;
            GridData data = (GridData) bHidden.getLayoutData();
            data.exclude = b.getSelection();
            bHidden.setVisible(!data.exclude);
            shell.layout(false);
        }
    });
    shell.setSize(400, 400);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:MainClass.java

protected Control createContents(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout(1, true));

    final Button indeterminate = new Button(composite, SWT.CHECK);
    indeterminate.setText("Indeterminate");
    Button showProgress = new Button(composite, SWT.NONE);
    showProgress.setText("Show Progress");

    final Shell shell = parent.getShell();

    showProgress.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            try {
                new ProgressMonitorDialog(shell).run(true, true,
                        new LongRunningOperation(indeterminate.getSelection()));
            } catch (InvocationTargetException e) {
                MessageDialog.openError(shell, "Error", e.getMessage());
            } catch (InterruptedException e) {
                MessageDialog.openInformation(shell, "Cancelled", e.getMessage());
            }//  w ww . j  ava2 s.  c  o  m
        }
    });

    parent.pack();
    return composite;
}

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

/**
 * Handle the Create button selection event.
 *
 * @param event org.eclipse.swt.events.SelectionEvent
 *//*from  ww w  .  ja va 2 s. com*/
public void createButtonSelected(SelectionEvent event) {

    /*
     * Remember the example shells so they
     * can be disposed by the user.
     */
    if (shellCount >= shells.length) {
        Shell[] newShells = new Shell[shells.length + 4];
        System.arraycopy(shells, 0, newShells, 0, shells.length);
        shells = newShells;
    }

    /* Compute the shell style */
    int style = SWT.NONE;
    if (noTrimButton.getSelection())
        style |= SWT.NO_TRIM;
    if (noMoveButton.getSelection())
        style |= SWT.NO_MOVE;
    if (closeButton.getSelection())
        style |= SWT.CLOSE;
    if (titleButton.getSelection())
        style |= SWT.TITLE;
    if (minButton.getSelection())
        style |= SWT.MIN;
    if (maxButton.getSelection())
        style |= SWT.MAX;
    if (borderButton.getSelection())
        style |= SWT.BORDER;
    if (resizeButton.getSelection())
        style |= SWT.RESIZE;
    if (onTopButton.getSelection())
        style |= SWT.ON_TOP;
    if (toolButton.getSelection())
        style |= SWT.TOOL;
    if (sheetButton.getSelection())
        style |= SWT.SHEET;
    if (modelessButton.getSelection())
        style |= SWT.MODELESS;
    if (primaryModalButton.getSelection())
        style |= SWT.PRIMARY_MODAL;
    if (applicationModalButton.getSelection())
        style |= SWT.APPLICATION_MODAL;
    if (systemModalButton.getSelection())
        style |= SWT.SYSTEM_MODAL;

    /* Create the shell with or without a parent */
    if (noParentButton.getSelection()) {
        shells[shellCount] = new Shell(style);
    } else {
        shells[shellCount] = new Shell(shell, style);
    }
    final Shell currentShell = shells[shellCount];
    currentShell.setBackgroundMode(SWT.INHERIT_DEFAULT);
    final Button button = new Button(currentShell, SWT.CHECK);
    button.setBounds(20, 20, 120, 30);
    button.setText(ControlExample.getResourceString("FullScreen"));
    button.addSelectionListener(widgetSelectedAdapter(e -> currentShell.setFullScreen(button.getSelection())));
    Button close = new Button(currentShell, SWT.PUSH);
    close.setBounds(160, 20, 120, 30);
    close.setText(ControlExample.getResourceString("Close"));
    close.addListener(SWT.Selection, event1 -> {
        currentShell.dispose();
        shellCount--;
    });

    /* Set the size, title, and image, and open the shell */
    currentShell.setSize(300, 100);
    currentShell.setText(ControlExample.getResourceString("Title") + shellCount);
    if (imageButton.getSelection())
        currentShell.setImage(instance.images[ControlExample.ciTarget]);
    if (backgroundImageButton.getSelection())
        currentShell.setBackgroundImage(instance.images[ControlExample.ciBackground]);
    hookListeners(currentShell);
    currentShell.open();
    shellCount++;
}

From source file:ShowProgress.java

/**
 * Creates the main window's contents//from   w w  w .  j  a  v a 2 s  .  c om
 * 
 * @param parent the main window
 * @return Control
 */
protected Control createContents(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout(1, true));

    // Create the indeterminate checkbox
    final Button indeterminate = new Button(composite, SWT.CHECK);
    indeterminate.setText("Indeterminate");

    // Create the ShowProgress button
    Button showProgress = new Button(composite, SWT.NONE);
    showProgress.setText("Show Progress");

    final Shell shell = parent.getShell();

    // Display the ProgressMonitorDialog
    showProgress.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            try {
                new ProgressMonitorDialog(shell).run(true, true,
                        new LongRunningOperation(indeterminate.getSelection()));
            } catch (InvocationTargetException e) {
                MessageDialog.openError(shell, "Error", e.getMessage());
            } catch (InterruptedException e) {
                MessageDialog.openInformation(shell, "Cancelled", e.getMessage());
            }
        }
    });

    parent.pack();
    return composite;
}

From source file:at.ac.tuwien.inso.subcat.ui.widgets.TrendView.java

public TrendView(Composite parent, int style) {
    super(parent, style);
    initCharts();//from  ww w  .j  a va  2  s  . com

    setLayout(new FillLayout());

    SashForm sashForm = new SashForm(this, SWT.VERTICAL);

    timeChart = new TimeChartControlPanel<SelectedChart>(sashForm, SWT.BORDER);
    timeChart.addChartSelectionEntry("Trend View", SelectedChart.LINE);
    timeChart.addChartSelectionEntry("Bar View", SelectedChart.BAR);
    timeChart.addChartSelectionEntry("Both", SelectedChart.BOTH);

    JFreeChart chart = createChart(SelectedChart.LINE);
    setChart(chart);

    scrolledComposite = new ScrolledComposite(sashForm, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
    scrolledComposite.setLayout(new FillLayout());

    optionComposite = new Composite(scrolledComposite, SWT.NONE);
    optionComposite.setLayout(new GridLayout(3, false));

    scrolledComposite.setContent(optionComposite);
    scrolledComposite.setMinSize(optionComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));

    scrolledComposite.setExpandVertical(true);
    scrolledComposite.setExpandHorizontal(true);
    scrolledComposite.setAlwaysShowScrollBars(false);

    // Signals:
    boxListener = new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            Button button = (Button) e.getSource();
            ChartIdentifier boxData = (ChartIdentifier) button.getData();
            assert (boxData != null);

            boolean checked = button.getSelection();

            if (boxData.paint == null) {
                boxData.paint = drawingSupplier.getNextPaint();
            }

            if (checked) {
                java.awt.Color paintColor = (java.awt.Color) boxData.paint;
                Color color = new Color(null, paintColor.getRed(), paintColor.getGreen(), paintColor.getBlue());
                button.setBackground(color);
            } else {
                button.setBackground(null);
            }

            for (TrendViewListener listener : listeners) {
                listener.optionSelected(boxData, checked);
            }
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
        }
    };

    comboListener = new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            Combo combo = (Combo) e.getSource();
            DropDownData data = (DropDownData) combo.getData();
            assert (data != null);
            DropDownConfig config = data.getConfig();

            for (TrendViewListener listener : listeners) {
                listener.comboChanged(config);
            }
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
        }
    };
}

From source file:at.ac.tuwien.inso.subcat.ui.widgets.TrendChart.java

public TrendChart(Composite parent, int style) {
    super(parent, style);
    initCharts();// w  w w .ja v  a2  s .  c  o  m

    setLayout(new FillLayout());

    SashForm sashForm = new SashForm(this, SWT.VERTICAL);

    timeChart = new TimeChartControlPanel<SelectedChart>(sashForm, SWT.BORDER);
    timeChart.addChartSelectionEntry("Trend View", SelectedChart.LINE);
    timeChart.addChartSelectionEntry("Bar View", SelectedChart.BAR);
    timeChart.addChartSelectionEntry("Both", SelectedChart.BOTH);

    JFreeChart chart = createChart(SelectedChart.LINE);
    setChart(chart);

    scrolledComposite = new ScrolledComposite(sashForm, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
    scrolledComposite.setLayout(new FillLayout());

    optionComposite = new Composite(scrolledComposite, SWT.NONE);
    optionComposite.setLayout(new GridLayout(3, false));

    scrolledComposite.setContent(optionComposite);
    scrolledComposite.setMinSize(optionComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));

    scrolledComposite.setExpandVertical(true);
    scrolledComposite.setExpandHorizontal(true);
    scrolledComposite.setAlwaysShowScrollBars(false);

    // Signals:
    boxListener = new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            Button button = (Button) e.getSource();
            ChartIdentifier boxData = (ChartIdentifier) button.getData();
            assert (boxData != null);

            boolean checked = button.getSelection();

            if (boxData.paint == null) {
                boxData.paint = drawingSupplier.getNextPaint();
            }

            if (checked) {
                java.awt.Color paintColor = (java.awt.Color) boxData.paint;
                Color color = new Color(null, paintColor.getRed(), paintColor.getGreen(), paintColor.getBlue());
                button.setBackground(color);
            } else {
                button.setBackground(null);
            }

            for (TrendChartListener listener : listeners) {
                listener.optionSelected(boxData, checked);
            }
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
        }
    };

    comboListener = new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            Combo combo = (Combo) e.getSource();
            DropDownData data = (DropDownData) combo.getData();
            assert (data != null);
            DropDownConfig config = data.getConfig();

            for (TrendChartListener listener : listeners) {
                listener.comboChanged(config);
            }
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
        }
    };
}

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

/**
 * Handle a decoration button selection event.
 *
 * @param event org.eclipse.swt.events.SelectionEvent
 *///from   ww  w  .  j a v  a2  s.  co  m
public void decorationButtonSelected(SelectionEvent event) {
    Button widget = (Button) event.widget;

    /*
     * Make sure that if the modal style is SWT.APPLICATION_MODAL
     * or SWT.SYSTEM_MODAL the style SWT.CLOSE is also selected.
     * This is to make sure the user can close the shell.
     */
    if (widget == applicationModalButton || widget == systemModalButton) {
        if (widget.getSelection()) {
            closeButton.setSelection(true);
            noTrimButton.setSelection(false);
        }
        return;
    }
    if (widget == closeButton) {
        if (applicationModalButton.getSelection() || systemModalButton.getSelection()) {
            closeButton.setSelection(true);
        }
    }
    /*
     * Make sure that if the SWT.NO_TRIM button is selected
     * then all other decoration buttons are deselected.
     */
    if (widget.getSelection()) {
        if (widget == noTrimButton) {
            if (applicationModalButton.getSelection() || systemModalButton.getSelection()) {
                noTrimButton.setSelection(false);
                return;
            }
            closeButton.setSelection(false);
            titleButton.setSelection(false);
            minButton.setSelection(false);
            maxButton.setSelection(false);
            borderButton.setSelection(false);
            resizeButton.setSelection(false);
        } else {
            noTrimButton.setSelection(false);
        }
    }

    /*
     * Make sure that the SWT.DIALOG_TRIM and SWT.SHELL_TRIM buttons
     * are consistent.
     */
    if (widget == dialogTrimButton || widget == shellTrimButton) {
        if (widget.getSelection() && widget == dialogTrimButton) {
            shellTrimButton.setSelection(false);
        } else {
            dialogTrimButton.setSelection(false);
        }
        //SHELL_TRIM = CLOSE | TITLE | MIN | MAX | RESIZE;
        //DIALOG_TRIM = TITLE | CLOSE | BORDER;
        closeButton.setSelection(widget.getSelection());
        titleButton.setSelection(widget.getSelection());
        minButton.setSelection(widget == shellTrimButton && widget.getSelection());
        maxButton.setSelection(widget == shellTrimButton && widget.getSelection());
        borderButton.setSelection(widget == dialogTrimButton && widget.getSelection());
        resizeButton.setSelection(widget == shellTrimButton && widget.getSelection());
    } else {
        boolean title = titleButton.getSelection();
        boolean close = closeButton.getSelection();
        boolean min = minButton.getSelection();
        boolean max = maxButton.getSelection();
        boolean border = borderButton.getSelection();
        boolean resize = resizeButton.getSelection();
        dialogTrimButton.setSelection(title && close && border && !min && !max && !resize);
        shellTrimButton.setSelection(title && close && min && max && resize && !border);
    }
}