在 SWT 中,父 shell 和非对话框子 shell 之间通信的最佳方式是什么?

发布于 2024-10-25 06:59:00 字数 537 浏览 0 评论 0原文

我有一个用 SWT 编写的大型现有应用程序,我必须对其进行修改。

GUI 包含打开非 Dialog 子 shell 的 shell。

现在,当子 shell 关闭时,我必须更新父 shell 上的信息。

我想象了两个选择:

  1. 将所有子级转换为扩展 Dialog 类。问题是它需要大量重构。
  2. 传递父逻辑类的引用,以便在关闭子级之前我可以调用父级的方法。我不喜欢这个设计。

如果在父代码中我可以监听子 shell 事件并根据子 shell 上发生的情况采取行动,那就太好了。这是一种可观察模式。我在《SWT:开发者笔记本》中读到:

ChildShell 不需要事件循环。为什么?因为父 shell 的事件循环处理在父 shell 中打开的所有对象的事件分派。子级保持打开状态,直到用户将其关闭或父级关闭为止。

我没有在 SWT 中进行过实验,而且例子也很少。那么 SWT 的方法是什么呢?我可以使用父循环来达到这样的目的吗?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春如此纠结 2024-11-01 06:59:00

我建议您在子 shell 上使用 ShellListener 。然后您可以重写 shellClosed 方法。

示例

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 ();
    }
} 

注意

您还可以使用 DisposeListener 但在这种情况下您不能使用 text.setText(childText.getText());(参见上面的示例)。要处理此问题,请将文本保存在字符串变量中,然后使用该字符串变量填充父文本框。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文