代码之家  ›  专栏  ›  技术社区  ›  Daniel

检测窗口生成器

  •  1
  • Daniel  · 技术社区  · 14 年前

    我正在使用GoogleWindowBuilderPro进行SWT,我们在这里使用了很多自定义组件。组件依赖于在我们的框架内使用,但这使得它们在窗口生成器中不可用(在框架外使用时,例如在窗口生成器中,会抛出执行)。

    如何检测窗口生成器正在使用我们的组件跳过依赖于框架的代码?

    2 回复  |  直到 14 年前
        1
  •  2
  •   Daniel    14 年前

    /**
     * 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;
    }
    
        2
  •  1
  •   MichaelS    12 年前

    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
    }