开发者

In SWT, what's the best way to communicate between a parent shell and a non dialog child shell?

开发者 https://www.devze.com 2023-02-19 04:12 出处:网络
I have a large existing application written in SWT I have to modify. The GUI consists in shell opening non Dialog child shells.

I have a large existing application written in SWT I have to modify.

The GUI consists in shell opening non Dialog child shells.

Now I have to update information on parent shell when child shell is closed.

I imagined two options:

  1. Convert all child to extend Dialog class. Problem is it requires large refactoring.
  2. Pass a reference of the parent logic class so that before closing child I can call a method of the parent. I don't like this design.

It would be nice if in parent code I could listen the child shell events and take action depending of what happen on child shell. This is kind of Observable pattern. I read in "SWT: a developper notebook":

No event loop is required for the ChildShell. Why? Because the event loop for the parent shell handles the dispatching of events for all objects opened within开发者_如何学运维 the parent. The child remains open until it is closed by the user or until the parent is closed.

I'm not experimented in SWT and examples are scarce. So what is the SWT way to do this? Can I use the parent loop for such purpose?

Thanks.


I will suggest you to use ShellListener on the child shell. Then you can override the shellClosed method.

Example

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.events.ShellListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class Test {

    private static Text text;

    public static void main (String [] args) 
    {
        Display display = new Display ();
        final Shell shell = new Shell (display);
        shell.setLayout(new GridLayout());
        shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        shell.setSize(200, 100);
        shell.setText("Parent Shell");

        Label label = new Label(shell, SWT.NONE);
        label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        label.setText("The text from child shell ...");

        text = new Text(shell, SWT.BORDER);
        text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

        Button openChild = new Button(shell, SWT.PUSH);
        openChild.setText("Open Child ...");
        openChild.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                openChild(shell);
            }
        });

        shell.open ();

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

    private static void openChild(Shell parent)
    {
        final Shell dialog = new Shell (parent, SWT.DIALOG_TRIM);
        dialog.setLayout(new GridLayout());
        dialog.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        dialog.setSize(200, 100);
        dialog.setText("Child Shell");

        Label childLabel = new Label(dialog, SWT.NONE);
        childLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        childLabel.setText("Type something here ...");

        final Text childText = new Text(dialog, SWT.BORDER);
        childText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

        Button okButton = new Button (dialog, SWT.PUSH);
        okButton.setText ("&OK");
        okButton.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                dialog.close();
            }
        });


        dialog.addShellListener(new ShellListener() {
            public void shellIconified(ShellEvent e) {
            }
            public void shellDeiconified(ShellEvent e) {
            }
            public void shellDeactivated(ShellEvent e) {
            }
            public void shellClosed(ShellEvent e) {
                if(text != null && !text.isDisposed())
                    text.setText(childText.getText());
            }
            public void shellActivated(ShellEvent e) {
            }
        });


        dialog.setDefaultButton (okButton);
        dialog.open ();
    }
} 

Note

You can also use DisposeListener but in this scenario you can not use text.setText(childText.getText()); (see the example above). To handle this, save the text in a String variable and then use the string variable to populate the parents text box.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号