jkteater
jkteater

Reputation: 1391

SWT - Grey out and disable current shell

When I have a operation running in the back ground, I am setting my cursor to busy until the process completes. Is there a way to also grey out and disable the current Display/Dialog/Shell until the process completes. I want to visually let the user know that something is working and they have to wait.

EDIT

plotButton.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event arg0) {
         getShell().setEnabled(!getShell().getEnabled());
         getShell().setCursor(new Cursor(Display.getCurrent(), SWT.CURSOR_WAIT));    
         recursiveSetEnabled(getShell(), getShell().getEnabled());
         startPrinterListOperation(); <== This is method that runs operation
      }
  });

Method that runs a printer operation.

private void startPrinterListOperation() {
  listOp = new AplotPrinterListOperation(appReg.getString("aplot.message.GETPRINTERLIST"), session);
  listOp.addOperationListener(new MyOperationListener(this) {
     public void endOperationImpl() {
        try {
           printers.clear();
           printers.addAll((ArrayList<PrinterProfile>) listOp.getPrinters());
           Display.getDefault().asyncExec(new Runnable() {
              public void run() {
                showAplotPlotterDialog(); <== When operation returns - opens selection dialog
              }
           });
        }
        finally {

           listOp.removeOperationListener(this);
           listOp = null;
        }
     }
  });
  session.queueOperation(listOp);
} // end startPrinterListOperation()

showAplotPlotterDialog() (Seperate Class) opens a dialog with network printers, then with a button push sends a job to the selected printer. When that operation finishes the Plotter Dialog closes - This is the end of that method - baseDialog is the MAIN GUI

finally {
           plotOp.removeOperationListener(this);
           plotOp = null;
           Display.getDefault().asyncExec(new Runnable() {
              public void run() {
                baseDialog.removeAllTableRows();
                baseDialog.plotRequestCompleted = true;
                baseDialog.setResultsButtonVisibility();
                getShell().close();

              }
           });
        }

Upvotes: 2

Views: 2691

Answers (3)

joelmatth
joelmatth

Reputation: 56

Baz's answer was a great start for me, but doesn't act on Combo since it extends Composite. By making the call to setEnabled unconditional, every Control (including Combo) are enabled/disabled correctly.

private static void recursiveSetEnabled(Control control, boolean enabled) {
    if (control instanceof Composite)
    {
        Composite comp = (Composite) control;

        for (Control c : comp.getChildren())
            recursiveSetEnabled(c, enabled);
    }

    control.setEnabled(enabled);
}

Upvotes: 1

Baz
Baz

Reputation: 36884

The following should do what you want. It will recursively disable and grey out all the Controls in your Shell. The Shell itself does not have a setGrayed method, but this will work:

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

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

    button.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event arg0) {
            shell.setEnabled(!shell.getEnabled());
            shell.setCursor(new Cursor(display, SWT.CURSOR_WAIT));      
            recursiveSetEnabled(shell, shell.getEnabled());
        }
    });

    new Text(shell, SWT.NONE).setText("TEXT");

    shell.setSize(400, 400);
    shell.open();

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

private static void recursiveSetEnabled(Control control, boolean enabled) {
    if (control instanceof Composite)
    {
        Composite comp = (Composite) control;

        for (Control c : comp.getChildren())
            recursiveSetEnabled(c, enabled);
    }
    else
    {
        control.setEnabled(enabled);
    }
}

Upvotes: 3

sambi reddy
sambi reddy

Reputation: 3085

Use

BusyIndicator.showWhile(Display.getDefault(), new Runnable()
{

    public void run()
    {
        //operation
    }
});

It sets the busy cursor on all Shells (Window, Dialog, ...) for the current Display until the Runnable.run() is executed.

Upvotes: 1

Related Questions