Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SWT Radio-Buttons resets radio-button selection after opening and closing FileChooser from same dialog #1599

Open
Lukas-sys opened this issue Nov 18, 2024 · 0 comments
Labels
good first issue Good for newcomers

Comments

@Lukas-sys
Copy link

Describe the bug
If a dialog contains radio buttons and a button for opening a file dialog, after selecting the second radio button and opening and closing the file dialogue using the button, the selection of the radio button becomes the first element again.

To Reproduce

import static org.assertj.core.api.Assertions.assertThat;

import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.junit.jupiter.api.Test;


public class SWTRadioButtonFocusTest {

  private SimpleDialog _dialog;

  @Test
  /**
   * This is a user test - only opens the Dialog to test the behavior by self
   */
  void testRadioButtons() {
      Shell shell = new Shell();
      SimpleDialog dialog = new SimpleDialog(shell, true);
      dialog.open();
  }

  @Test
  void testRadioButtonsAuto() {
      Shell shell = new Shell();
      _dialog = new SimpleDialog(shell, false);
      _dialog.setBlockOnOpen(false);
      _dialog.open();

      _dialog.getRadioButton1().setSelection(false);
      _dialog.getRadioButton2().setSelection(true);
      _dialog.getRadioButton2().notifyListeners(SWT.Selection, new Event()); //select radio button 2

      assertThat(_dialog.getRadioButton2().getSelection()).isTrue(); //check if radio button 2 is selected

      _dialog.getFileChooserButton().notifyListeners(SWT.Selection, new Event()); //click the browse button to open fileChooser

      assertThat(_dialog.getRadioButton2().getSelection()).isTrue(); //after closing the fileChooser, radio button 2 should be still selected - but radio button 1 is selected instead
  }

  private class SimpleDialog extends TitleAreaDialog {

    private boolean _userTest;
    private Button _radio1;
    private Button _radio2;
    private Button _button;

    public SimpleDialog(Shell parentShell, boolean userTest) {
      this(parentShell);
      _userTest = userTest;
    }

    public SimpleDialog(Shell parentShell) {
      super(parentShell);
    }

    @Override
    public void create() {
      super.create();
      setTitle("SWT Radio Buttons");
      setMessage("Select Radio Button 2 and then open the FileChooser and close it afterwards.");
    }

    @Override
    protected Control createDialogArea(Composite parent) {
      Composite area = (Composite) super.createDialogArea(parent);
      Composite container = new Composite(area, SWT.NONE);
      GridDataFactory.fillDefaults().applyTo(container);
      GridLayoutFactory.fillDefaults().applyTo(container);

      createRadioButtons(container);
      createFileChooserButton(container);

      return area;

    }

    private void createRadioButtons(Composite parent) {
      _radio1 = new Button(parent, SWT.RADIO);
      _radio1.setText("Option 1");
      GridDataFactory.fillDefaults().grab(true, false).applyTo(_radio1);

      _radio2 = new Button(parent, SWT.RADIO);
      _radio2.setText("Option 2");
      GridDataFactory.fillDefaults().grab(true, false).applyTo(_radio2);
    }

    private void createFileChooserButton(Composite parent) {
      _button = new Button(parent, SWT.PUSH);
      _button.setText("Browse...");
      _button.addSelectionListener(createSelectionAdapter());
    }

    private SelectionAdapter createSelectionAdapter() {
      if (_userTest) {
        return new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            /**
             * The error only occurs if the current shell is used! If a new shell is created, there is no error behavior.
             */
            FileDialog fileDialog = new FileDialog(getShell(), SWT.OPEN);
            fileDialog.setText("Select File");
            fileDialog.open();
          }
        };
      } else {
        return new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            SimpleDialog simpleDialog = new SimpleDialog(getShell());
            simpleDialog.setBlockOnOpen(false);
            simpleDialog.open();
            simpleDialog.close();
          }
        };
      }
    }

    public Button getRadioButton1() {
      return _radio1;
    }

    public Button getRadioButton2() {
      return _radio2;
    }

    public Button getFileChooserButton() {
      return _button;
    }
  }
}
  1. select the radio button "option 2"
  2. open the file dialog via the "browse"-button
  3. close the file dialog

Expected behavior
The selection of the radio buttons should not change after opening and closing a FileDialog

Environment:

  1. Select the platform(s) on which the behavior is seen:
    • All OS
    • Windows
    • Linux
    • macOS
  1. JRE/JDK version: corretto-jdk11.0.23.9.1_x86_64
  2. SWT version: 3.126.0.v20240528-0813

Workaround
Any known workarounds for the problem?
setFocus() on radio button in SelectionListener or new Shell for the FileDialog.

@HeikoKlare HeikoKlare added the good first issue Good for newcomers label Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants