简体   繁体   English

在SWT中,父外壳程序和非对话框子外壳程序之间进行通信的最佳方法是什么?

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

I have a large existing application written in SWT I have to modify. 我有一个用SWT编写的大型现有应用程序,我必须进行修改。

The GUI consists in shell opening non Dialog child shells. GUI包括通过外壳打开非Dialog子外壳。

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. 转换所有子级以扩展Dialog类。 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": 我在“ SWT:开发人员笔记本”中阅读:

No event loop is required for the ChildShell. 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. 我没有在SWT中进行实验,而且示例很少。 So what is the SWT way to do this? 那么,SWT的实现方式是什么? Can I use the parent loop for such purpose? 我可以将父循环用于此目的吗?

Thanks. 谢谢。

I will suggest you to use ShellListener on the child shell. 我建议您在子外壳上使用ShellListener Then you can override the shellClosed method. 然后,您可以覆盖shellClosed方法。

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()); 您也可以使用DisposeListener但是在这种情况下,不能使用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. 要处理此问题,请将文本保存在String变量中,然后使用string变量填充父级文本框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM