检测Window Builder

时间:2010-10-02 06:11:14

标签: java swt

我正在使用Google Window Builder Pro for SWT,我们在这里使用了很多自定义组件。组件依赖于在我们的框架中使用,但这使得它们在Window Builder中无法使用(在我们的框架之外使用时会抛出异常,例如,在Window Builder中)。

如何检测Window Builder是否正在使用我们的组件跳过依赖于我们框架的代码?

2 个答案:

答案 0 :(得分:2)

我实现了一个Utility函数,它转储StackTrace并从中查找Instantiations中的东西。这非常有效:

/**
 * Designer mode. This is used to detect if the widgets are running
 * from SWT designer, because in this case we have to skip some
 * initialization code.
 */
private static Boolean designerMode;

/**
 * This is used to detect if the widgets are running
 * from SWT designer, because in this case we have to skip some
 * initialization code.
 */
public static boolean isDesignerMode() {
    if( designerMode == null ) {
        String s = StacktraceUtils.getStackTraceAsString(
                new RuntimeException("Just to get the Stacktrace."));
        designerMode = s.contains("com.instantiations.designer");  
    }
    return designerMode;
}

答案 1 :(得分:1)

您可以通过这种方式将Stacktrace打印为String(Daniel的答案中缺少getStackTraceAsString()方法):

public static String getStackTraceAsString(Throwable t) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    t.printStackTrace(pw);
    return sw.toString(); // stack trace as a string
}