jkteater
jkteater

Reputation: 1391

SWT - Inheriting parent dialog shell?

I am really needing to understand how parent/child dialogs work.

My users use a OTB Application called Teamcenter. I am writing a add on application that is invoked from a menu selection in the Teamcenter Application.

When they click the menu item, that executes a handler class and that creates the base dialog for my application.

public class AplotDialogHandler extends AbstractHandler {
  private static AplotBaseDialog dlg = null;

  public AplotDialogHandler() {

  }// end Constructor

  //////////////////////////////////////////////////////////////////////////
  //                            execute()                                 //
  //////////////////////////////////////////////////////////////////////////
  @Override 
  public Object execute(final ExecutionEvent event) throws ExecutionException {
     if (dlg == null) {
        try {
           AbstractAIFApplication app = AIFDesktop.getActiveDesktop().getCurrentApplication();
        TCSession session = (TCSession) app.getSession();
        TCUserService userService = session.getUserService();

        AplotVersion.negotiateVersion(userService);
        AplotQueryCapabilities.initialize(userService);

        dlg = new AplotBaseDialog(null, session);
     }
     catch (Exception ex) {
        MessageBox.post(HandlerUtil.getActiveWorkbenchWindowChecked(event).getShell(), ex, true);
     }
  }

  dlg.create();
  dlg.getShell().setSize(700, 400);
  dlg.open();

  return null;
 }// end execute()
}// end EdiDialogHandler()

Question 1. It seems like my application is not tied to the Teamcenter application. Meaning that I can close Teamcenter and my Application stays open.

Question 2. Should I get the workspace shell and pass it in the base dialog? But even when my application is open, the user still needs to be able to use the Teamcenter application to select data to send to my application

Question 3. When opening dialogs from my base dialog, should I always pass the base dialog shell to those dialogs?

Question 4. Is there a standard way I should close down the dialogs when the user is done?

Upvotes: 0

Views: 3814

Answers (1)

sambi reddy
sambi reddy

Reputation: 3085

You need to pass the parent Shell to the dialog so that when you close parent shell, child shells will also be closed.

You should make your dialog MODELESS ( use SWT.MODELSS as style. Note: it is Hint) so that it will not block your parent shell.

Here is sample code:

 public static void main(String[] args) {

    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));
    shell.setSize(200, 200);
    Button b = new Button(shell, SWT.NONE);
    b.setText("Click");
    b.addSelectionListener(new SelectionListener() {

      @Override
      public void widgetSelected(SelectionEvent e) {


         CDialog dialog = new CDialog(shell);

         dialog.open();
      }

      @Override
      public void widgetDefaultSelected(SelectionEvent e) {

        // TODO Auto-generated method stub

      }
    });

    shell.open();



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


  private static class CDialog extends Dialog
  {

    /**
     * @param parentShell
     */
    protected CDialog(Shell parentShell) {

      super(parentShell);
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected Control createDialogArea(Composite parent) {

      Composite comp =  (Composite) super.createDialogArea(parent);

      Label lbl = new Label(comp, SWT.NONE);
      lbl.setText("Test modeless dialog");

      return comp;
    }
    /* (non-Javadoc)
     * @see org.eclipse.jface.window.Window#getShellStyle()
     */
    @Override
    protected int getShellStyle() {
      return SWT.DIALOG_TRIM|SWT.MODELESS;
    }

  }

Upvotes: 1

Related Questions