JFace ErrorDialog:如何在细节部分显示内容? [英] JFace ErrorDialog: how do I show something in the details portion?

查看:375
本文介绍了JFace ErrorDialog:如何在细节部分显示内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ErrorDialog.openError 获取对话框标题,消息和状态(其本身都有消息)的参数。

ErrorDialog.openError takes arguments for dialog title, message, and status (which has a message itself).

我想在主区域显示异常消息,在详细信息区域显示调用堆栈。
但是,这两个变体都显示主区域中的调用堆栈:

I want to show the exception's message in the main area, and the call stack in the details area. However, both of these variations show the call stack in the main area:

void showException(Throwable e) {
    Status status = 
        new Status(IStatus.ERROR, "SCS Admin", e.getLocalizedMessage(), e);
    e.printStackTrace;
    ErrorDialog.openError(getShell(), null, Util.getStackTrace(e), status);
}

void showException(Throwable e) {
    Status status = 
        new Status(IStatus.ERROR, "SCS Admin", Util.getStackTrace(e), e);
    e.printStackTrace;
    ErrorDialog.openError(getShell(), null, e.getLocalizedMessage(), status);
}

如何切换它?

推荐答案

在默认的JFace ErrorDialog中,只显示完整异常堆栈跟踪的方法(与printStackTrace()生成的相同)是将堆栈跟踪的每一行构建为一个状态。最后将这些状态设置为MultiStatus的childen。

In default JFace ErrorDialog only way to show full exception stack trace (same as produced by printStackTrace()) is to build each row of stack trace as one status. And finally set these statuses as childen of MultiStatus.

以下是我在RCP应用程序中使用的实用程序方法的示例:

Here's example of utility method I use in our RCP apps:

/**
 * Shows JFace ErrorDialog but improved by constructing full stack trace in
 * detail area.
 */
public static void errorDialogWithStackTrace(String msg, Throwable t) {

    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    t.printStackTrace(pw);

    final String trace = sw.toString(); // stack trace as a string

    // Temp holder of child statuses
    List<Status> childStatuses = new ArrayList<>();

    // Split output by OS-independend new-line
    for (String line : trace.split(System.getProperty("line.separator"))) {
        // build & add status
        childStatuses.add(new Status(IStatus.ERROR, Activator.PLUGIN_ID, line));
    }

    MultiStatus ms = new MultiStatus(Activator.PLUGIN_ID, IStatus.ERROR,
            childStatuses.toArray(new Status[] {}), // convert to array of statuses
            t.getLocalizedMessage(), t);

    ErrorDialog.openError(null, PxConstants.DIALOG_TITLE, msg, ms);
}

这篇关于JFace ErrorDialog:如何在细节部分显示内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆