简体   繁体   English

JFace ErrorDialog:如何在细节部分显示内容?

[英]JFace ErrorDialog: how do I show something in the details portion?

ErrorDialog.openError takes arguments for dialog title, message, and status (which has a message itself). ErrorDialog.openError接受对话框标题,消息和状态(具有消息本身)的参数。

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

How can I switch it around? 我该如何切换它?

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. 在默认的JFace ErrorDialog中,只显示完整异常堆栈跟踪的方法(与printStackTrace()生成的相同)是将堆栈跟踪的每一行构建为一个状态。 And finally set these statuses as childen of MultiStatus. 最后将这些状态设置为MultiStatus的孩子。

Here's example of utility method I use in our RCP apps: 以下是我在RCP应用中使用的实用方法示例:

/**
 * 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);
}

You could wrap the exception with a new that contains the stacktrace as message. 您可以使用包含stacktrace作为消息的new来包装异常。

public void showException(final Exception ex) {
    Display.getDefault().syncExec(new Runnable() {
        @Override
        public void run() {
            StringWriter sw = new StringWriter();
            ex.printStackTrace(new PrintWriter(sw));
            IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), new Exception(sw.toString()));
            ErrorDialog.openError(Display.getDefault().getActiveShell(), "Error", null, status);
        }
    });
}

Looks like you're mixing up the 2nd and 3rd parameter on the openError. 看起来你在openError上混合了第二个和第三个参数。 The 3rd parameter is the message to be shown. 第3个参数是要显示的消息。 Since you're giving the stacktrace it shows it. 既然你给了堆栈跟踪它就会显示它。

Once you get that fixed you might want to look at using a MultiStatus. 一旦解决了问题,您可能需要查看使用MultiStatus。

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

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