jkteater
jkteater

Reputation: 1391

SWT - Only allowing 1 Dialog to be open

I have a button in a base dialog that opens a results dialog.

private void showPlotResultsDialog() {
  resultsDialog = new AplotPlotResultsDialog(getShell());
  resultsDialog.setBlockOnOpen(true);
  resultsDialog.open();

}

The user is allowed to leave the results dialog open as they work. But I have recently noticed that the user can click on the "Open Results Dialog" as many times they want.
With each click a new results dialog opens. There can be a few of the same dialog open, with different data in the table.

  1. Is it possible to check and see if the dialog is already opened when they click the button? If one is already opened, pop a message, saying it is already open and block opening a new one.

Upvotes: 1

Views: 1739

Answers (3)

Mukundhan
Mukundhan

Reputation: 3457

Create the dialog only once, and use it in the listener callback. Do not initialise the dialog in the callback.

eg:

MyDialog viewer = new MyDialog(getShell()); // initiated once
myToolItem.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(SelectionEvent e) {
        if (shellCommand != null) {
            viewer.setTitle("My Title");
            viewer.setContent("My content");
            viewer.open(); // called to focus if already opened
        }
    }
});

Upvotes: 0

jens-na
jens-na

Reputation: 2274

Another possibility is to give your shell (which should be opened only once) a unique ID with the method:

shell.setData("yourID");

If you have a SelectionListener (for example) you can check if the Shell with the ID yourID is already open.

Actions:

  • If the Shell is open somewhere: activate the shell (set focus)
  • If the Shell is not open: open the shell

Example (see comments):

yourButton.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(SelectionEvent e) {

        // Loop through all active shells and check if 
        // the shell is already open
        Shell[] shells = Display.getCurrent().getShells();

        for(Shell shell : shells) {
            String data = (String) shell.getData();

            // only activate the shell and return
            if(data != null && data.equals("yourID")) {
                shell.setFocus();
                return;
            }
        }

        // open the shell and the dialog
        Shell shell = new Shell(Display.getCurrent());
        shell.setData("yourID");
        YourDialog yourDialog = new YourDialog(shell);
        yourDialog.open();
    }
}); 

Upvotes: 1

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51445

Is it possible to check and see if the dialog is already opened when they click the button?

Sure. Just put a check for null in your method. If the instance is not null, a dialog has been opened.

If one is already opened, pop a message, saying it is already open and block opening a new one

it would be better to update the dialog and set the focus on the dialog. Saves the user having to close the popup message, close the dialog, and open the same dialog.

Upvotes: 1

Related Questions