Search code examples
javaswtdisposejface

SWT - Opening / Disposing Shells - General Practice


I am looking for general practices for Disposing Shells.

Main Application Menu Click Executes the following sequence

DialogHandler Class = (Executes My Application Base GUI Class)

 @Override 
 public Object execute(final ExecutionEvent event) throws ExecutionException {
  if (dlg == null){
     try {
        AbstractAIFApplication app = AIFDesktop.getActiveDesktop().getCurrentApplication();
        session = (TCSession) app.getSession();
        TCUserService userService = session.getUserService();
        AplotVersion.negotiateVersion(userService);
        AplotQueryCapabilities.initialize(userService);
        shell = HandlerUtil.getActiveWorkbenchWindowChecked(event).getShell();
        dlg = new AplotBaseDialog(shell, session); 
     }
     catch (Exception ex) {
        MessageBox.post(HandlerUtil.getActiveWorkbenchWindowChecked(event).getShell(), ex, true);
     }
  }
  dlg.open();
  return null;
}// end execute()

Question. Being I am opening the Dialog in DialogHandler, do I have to dispose of it in that class?

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

My GUI Base Class AplotBaseDialog = (Extends TitleAreaDialog)

The constructor is receiving the Shell from DialogHandler Class.

Question: Do I put the While(!shell.isDisposed()) code in this class?

I am also opening a couple of dialogs from buttons in the AplotBaseDialog class.

 private void showPDFCreateDialog() {
   pdfDialog = new AplotCreatePDFDialog(this, getShell(), session);
   pdfDialog.open();
   pdfDialog.getShell().setSize(700, 400);
 }

Question: Do I have to include the dispose code in each of these dialog classes or does having a dispose() in the close button code good enough?

Question: Is this the proper way to create and open the dialog?

Question: Right now, I do not set the Display anywhere in my application code.

Display display = Display.getDefault();

I am just passing a Shell from the Parent Dialog to the Child Dialog and use asyncExec when needed.

Display.getDefault().asyncExec(new Runnable() {
  public void run() {
  }
} 

Question: Do I need to have to have while(!shell.isDisposed()) somewhere in my application code?


Solution

  • JFace Dialog does the below functionality for you. You dont need to do anything unless you want build your own dialog instead extending JFace Dialog

    //this is for keep reading events from event table
    while(!shell.isDisposed()){
            if(!display.readAndDispatch())
              display.sleep();
          }
          display.dispose();