保持TrayDialog打开并提交数据

时间:2014-05-17 09:32:04

标签: java eclipse-rcp

我有一个对话框,它扩展TrayDialog以从用户那里收集一些数据。我想将收集的值传递给另一个类而不关闭对话框。如果我覆盖okPressed()方法,数据将被传回,但窗口将被关闭。

public class MyDialog extends TrayDialog {
    public MyDialog(final Shell shell, final MyData mydata) {
        super(shell);
        this.shell = shell;
        this.mydata = mydata;
    }

    @Override
    protected void okPressed() {
        if (!validateData()) {
            return;
        }
        super.okPressed();
    }

    //rest of the code for text box, buttons in the dialog
}

final MyDialog myDialog = new MyDialog(new Shell(), this.mydata);
if (myDialog.open() == Window.CANCEL) {
    return null;
}
//here get the data that user has entered in the dialog after they press ok

1 个答案:

答案 0 :(得分:2)

如果您不致电super.okPressed(),则不会关闭对话框。 Source

protected void okPressed() {
    setReturnCode(OK);
    close();
}

为了在okPressed动作上传回数据,我建议为MyDialog提供回调。像这样:

public interface MyCallback {
    void okPressed(MyData data);
}

public class MyDialog extends TrayDialog {
public MyDialog(final Shell shell, final MyData mydata, MyCallback callback) {
    super(shell);
    this.shell = shell;
    this.mydata = mydata;
    this.callback = callback;
}

@Override
protected void okPressed() {
    if (!validateData()) {
        return;
    } else {
        callback.onPressed(data);
    }
    //super.okPressed();
}

MyCallback callback = new MyCallback() {

    void okPressed(MyData data) {
        // handle data
    }
}
 final MyDialog myDialog = new MyDialog(new Shell(), this.mydata, callback);
    if (myDialog.open() == Window.CANCEL) {
        return null;
    }

在这种情况下,您需要在某个时刻手动关闭对话框。此外,如果您使用setBlockOnOpen(false);

,Dialog将不会阻止执行线程