/**
  * @param out
  * @param eventValue
  * @param widget
  * @param widgetId
  */
 @Override
 public void processEvent(SourcePrinter out, String eventValue, String widget, String widgetId) {
   out.println(
       widget
           + ".getSelectionModel().addSelectionChangeHandler(new "
           + getEventHandlerClass().getCanonicalName()
           + "(){");
   out.println(
       "public void " + getEventName() + "(" + getEventClass().getCanonicalName() + " event){");
   printEvtCall(out, eventValue, "event");
   out.println("}");
   out.println("});");
 }
 @Override
 public void processEvent(SourcePrinter out, String eventValue, String widget, String widgetId) {
   out.println(
       widget
           + ".add"
           + getEventHandlerClass().getSimpleName()
           + "(new "
           + getEventHandlerClass().getCanonicalName()
           + "(){");
   out.println("public void onValueChange(" + getEventClass().getCanonicalName() + " event){");
   printEvtCall(out, eventValue, "event");
   out.println("}");
   out.println("});");
 }
    @Override
    public void processChildren(SourcePrinter out, StackMenuContext context)
        throws CruxGeneratorException {
      String item = getWidgetCreator().createVariableName("item");
      String className = StackMenuItem.class.getCanonicalName();

      String label = context.getChildElement().optString("label");
      label = getWidgetCreator().resolveI18NString(label);

      String key = context.getChildElement().optString("key");
      key = EscapeUtils.quote(key);

      out.println(className + " " + item + " = new " + className + "(" + key + ", " + label + ");");
      setItemAttributes(out, context, item);
      String parentWidget = context.itemStack.getFirst();
      out.println(parentWidget + ".add(" + item + ");");

      context.itemStack.addFirst(item);
    }
    /**
     * Sets the item attributes before adding it to the parent.
     *
     * @param out
     * @param context
     * @param item
     */
    private void setItemAttributes(SourcePrinter out, StackMenuContext context, String item) {
      String open = context.readChildProperty("open");
      if (!StringUtils.isEmpty(open)) {
        out.println(item + ".setOpen(" + Boolean.parseBoolean(open) + ");");
      }

      String style = context.readChildProperty("style");
      if (!StringUtils.isEmpty(style)) {
        if (styleProcessor == null) {
          styleProcessor = new StyleProcessor(getWidgetCreator());
        }
        styleProcessor.processAttribute(out, context, style);
      }

      String styleName = context.readChildProperty("styleName");
      if (!StringUtils.isEmpty(styleName)) {
        out.println(item + ".setStyleName(" + EscapeUtils.quote(styleName) + ");");
      }

      String tooltip = context.readChildProperty("tooltip");
      if (!StringUtils.isEmpty(tooltip)) {
        out.println(item + ".setTitle(" + EscapeUtils.quote(tooltip) + ");");
      }
    }
 @Override
 public void instantiateWidget(SourcePrinter out, WidgetCreatorContext context)
     throws CruxGeneratorException {
   String className = getWidgetClassName();
   boolean movable = context.readBooleanWidgetProperty("movable", true);
   out.println(
       "final "
           + className
           + " "
           + context.getWidget()
           + " = "
           + className
           + ".createIfSupported("
           + movable
           + ")");
 }
 private void generateMethodParamToHeaderCodeForSimpleType(
     SourcePrinter srcWriter,
     String builderVarName,
     String headerName,
     JType parameterType,
     String parameterexpression,
     String parameterCheckExpression) {
   JClassType jClassType = parameterType.isClassOrInterface();
   srcWriter.println("if (" + parameterCheckExpression + "){");
   if (jClassType != null) {
     if (jClassType.isAssignableTo(stringType)) {
       srcWriter.println(
           builderVarName
               + ".setHeader("
               + EscapeUtils.quote(headerName)
               + ", URL.encodePathSegment("
               + parameterexpression
               + "));");
     } else if (jClassType.isAssignableTo(dateType)) {
       srcWriter.println(
           builderVarName
               + ".setHeader("
               + EscapeUtils.quote(headerName)
               + ", URL.encodePathSegment("
               + "Long.toString("
               + parameterexpression
               + ".getTime())));");
     } else {
       srcWriter.println(
           builderVarName
               + ".setHeader("
               + EscapeUtils.quote(headerName)
               + ", URL.encodePathSegment("
               + "\"\"+"
               + parameterexpression
               + "));");
     }
   } else {
     srcWriter.println(
         builderVarName
             + ".setHeader("
             + EscapeUtils.quote(headerName)
             + ", URL.encodePathSegment("
             + "\"\"+"
             + parameterexpression
             + "));");
   }
   srcWriter.println("}");
 }
  public void generateMethodParamToBodyCode(
      SourcePrinter srcWriter, RestMethodInfo methodInfo, String builder, String httpMethod) {
    JParameter[] parameters = methodInfo.method.getParameters();
    boolean formEncoded = false;
    boolean hasBodyObject = false;

    String formString = getFormString(methodInfo);
    if (!StringUtils.isEmpty(formString)) {
      srcWriter.println("String requestData = " + EscapeUtils.quote(formString) + ";");
    }
    for (int i = 0; i < methodInfo.parameterAnnotations.length; i++) {
      Annotation[] annotations = methodInfo.parameterAnnotations[i];
      if (annotations == null || annotations.length == 0) { // JSON on body
        if (hasBodyObject) {
          throw new CruxGeneratorException(
              "Invalid Method: "
                  + methodInfo.method.getEnclosingType().getName()
                  + "."
                  + methodInfo.method.getName()
                  + "(). "
                  + "Request body can not contain more than one body parameter (JSON serialized object).");
        }

        hasBodyObject = true;
        String serializerName =
            new JSonSerializerProxyCreator(context, logger, parameters[i].getType()).create();
        srcWriter.println(
            builder
                + ".setHeader(\""
                + HttpHeaderNames.CONTENT_TYPE
                + "\", \"application/json\");");
        srcWriter.println(
            "JSONValue serialized = new "
                + serializerName
                + "().encode("
                + parameters[i].getName()
                + ");");
        srcWriter.println(
            "String requestData = (serialized==null||serialized.isNull()!=null)?null:serialized.toString();");
      } else {
        for (Annotation annotation : annotations) {
          JParameter parameter = parameters[i];
          JType parameterType = parameter.getType();
          String parameterName = parameter.getName();
          formEncoded =
              generateMethodParamToBodyCodeForAnnotatedParameter(
                  srcWriter,
                  builder,
                  parameters,
                  formEncoded,
                  i,
                  annotation,
                  parameterType,
                  parameterName);
        }
      }
    }
    if (hasBodyObject && formEncoded) {
      throw new CruxGeneratorException(
          "Invalid Method: "
              + methodInfo.method.getEnclosingType().getName()
              + "."
              + methodInfo.method.getName()
              + "(). "
              + "Request body can not contain form parameters and a JSON serialized object.");
    }
    if (hasBodyObject || formEncoded) {
      if (httpMethod.equals("GET")) {
        throw new CruxGeneratorException(
            "Invalid Method: "
                + methodInfo.method.getEnclosingType().getName()
                + "."
                + methodInfo.method.getName()
                + "(). "
                + "Can not use request body parameters on a GET operation.");
      }
      srcWriter.println(builder + ".setRequestData(requestData);");
    }
  }
 private void generateMethodParamToCookieCodeForSimpleType(
     SourcePrinter srcWriter,
     String cookieName,
     JType parameterType,
     String parameterexpression,
     String parameterCheckExpression) {
   JClassType jClassType = parameterType.isClassOrInterface();
   if (jClassType != null) {
     if (jClassType.isAssignableTo(stringType)) {
       srcWriter.println(
           Cookies.class.getCanonicalName()
               + ".setCookie("
               + EscapeUtils.quote(cookieName)
               + ", "
               + "("
               + parameterCheckExpression
               + "?"
               + parameterexpression
               + ":\"\"), new "
               + Date.class.getCanonicalName()
               + "(2240532000000L), null, \"/\", false);");
     } else if (jClassType.isAssignableTo(dateType)) {
       srcWriter.println(
           Cookies.class.getCanonicalName()
               + ".setCookie("
               + EscapeUtils.quote(cookieName)
               + ", "
               + "("
               + parameterCheckExpression
               + "?Long.toString("
               + parameterexpression
               + ".getTime()):\"\"), new "
               + Date.class.getCanonicalName()
               + "(2240532000000L), null, \"/\", false);");
     } else {
       srcWriter.println(
           Cookies.class.getCanonicalName()
               + ".setCookie("
               + EscapeUtils.quote(cookieName)
               + ", "
               + "("
               + parameterCheckExpression
               + "?(\"\"+"
               + parameterexpression
               + "):\"\"), new "
               + Date.class.getCanonicalName()
               + "(2240532000000L), null, \"/\", false);");
     }
   } else {
     srcWriter.println(
         Cookies.class.getCanonicalName()
             + ".setCookie("
             + EscapeUtils.quote(cookieName)
             + ", "
             + "("
             + parameterCheckExpression
             + "?(\"\"+"
             + parameterexpression
             + "):\"\"), new "
             + Date.class.getCanonicalName()
             + "(2240532000000L), null, \"/\", false);");
   }
 }
 private boolean generateMethodParamToBodyCodeForAnnotatedParameter(
     SourcePrinter srcWriter,
     String builder,
     JParameter[] parameters,
     boolean formEncoded,
     int i,
     Annotation annotation,
     JType parameterType,
     String parameterName) {
   if (annotation instanceof FormParam) {
     if (!formEncoded) {
       srcWriter.println(
           builder
               + ".setHeader(\""
               + HttpHeaderNames.CONTENT_TYPE
               + "\", \"application/x-www-form-urlencoded\");");
       formEncoded = true;
     }
   }
   if (JClassUtils.isSimpleType(parameterType)) {
     if (annotation instanceof FormParam) {
       generateMethodParamToCodeForSimpleType(
           srcWriter,
           "requestData",
           parameterType,
           ((FormParam) annotation).value(),
           parameterName,
           (parameterType.isPrimitive() != null ? "true" : parameterName + "!=null"),
           annotation);
     }
     if (annotation instanceof HeaderParam) {
       generateMethodParamToHeaderCodeForSimpleType(
           srcWriter,
           builder,
           ((HeaderParam) annotation).value(),
           parameterType,
           parameterName,
           (parameterType.isPrimitive() != null ? "true" : parameterName + "!=null"));
     }
     if (annotation instanceof CookieParam) {
       generateMethodParamToCookieCodeForSimpleType(
           srcWriter,
           ((CookieParam) annotation).value(),
           parameterType,
           parameterName,
           (parameterType.isPrimitive() != null ? "true" : parameterName + "!=null"));
     }
   } else {
     if (annotation instanceof FormParam) {
       generateMethodParamToCodeForComplexType(
           srcWriter,
           "requestData",
           parameterType,
           ((FormParam) annotation).value(),
           parameterName,
           parameterName + "!=null",
           annotation);
     }
     if (annotation instanceof HeaderParam) {
       generateMethodParamToHeaderCodeForComplexType(
           srcWriter,
           builder,
           ((HeaderParam) annotation).value(),
           parameterType,
           parameterName,
           parameterName + "!=null");
     }
     if (annotation instanceof CookieParam) {
       generateMethodParamToCookieCodeForComplexType(
           srcWriter,
           ((CookieParam) annotation).value(),
           parameterType,
           parameterName,
           parameterName + "!=null");
     }
   }
   return formEncoded;
 }
    @Override
    public void processChildren(SourcePrinter out, WidgetCreatorContext context)
        throws CruxGeneratorException {
      if (beforeFocusEvtBind == null)
        beforeFocusEvtBind = new BeforeFocusEvtBind(getWidgetCreator());
      if (beforeBlurEvtBind == null) beforeBlurEvtBind = new BeforeBlurEvtBind(getWidgetCreator());

      String name = context.readChildProperty("name");
      String id = context.readChildProperty("id");
      if (StringUtils.isEmpty(id)) {
        id = name;
      }
      boolean closeable = true;
      String strCloseable = context.readChildProperty("closeable");
      if (strCloseable != null && strCloseable.trim().length() > 0) {
        closeable = Boolean.parseBoolean(strCloseable);
      }
      boolean lazy = true;
      String strLazy = context.readChildProperty("lazy");
      if (strLazy != null && strLazy.trim().length() > 0) {
        lazy = Boolean.parseBoolean(strLazy);
      }
      String beforeFocusEvt = context.readChildProperty(beforeFocusEvtBind.getEventName());
      String beforeBlurEvt = context.readChildProperty(beforeBlurEvtBind.getEventName());

      String rootWidget = context.getWidget();
      out.println(
          TabContainer.class.getCanonicalName()
              + ".createView("
              + EscapeUtils.quote(name)
              + ", "
              + EscapeUtils.quote(id)
              + ", new "
              + CreateCallback.class.getCanonicalName()
              + "(){");
      out.println("public void onViewCreated(View view){");
      boolean hasEvents =
          !StringUtils.isEmpty(beforeFocusEvt) || !StringUtils.isEmpty(beforeBlurEvt);
      if (hasEvents) {
        out.print("if (");
      }
      out.print(rootWidget + ".add(view, " + lazy + ", " + closeable + ", true)");
      if (hasEvents) {
        out.print("){");
      } else {
        out.println(";");
      }
      String tab = ViewFactoryCreator.createVariableName("tab");
      out.print(
          Tab.class.getCanonicalName()
              + " "
              + tab
              + " = "
              + rootWidget
              + ".getTab("
              + EscapeUtils.quote(id)
              + ");");

      if (!StringUtils.isEmpty(beforeFocusEvt)) {
        beforeFocusEvtBind.processEvent(out, beforeFocusEvt, tab, null);
      }
      if (!StringUtils.isEmpty(beforeBlurEvt)) {
        beforeBlurEvtBind.processEvent(out, beforeBlurEvt, tab, null);
      }
      if (hasEvents) {
        out.println("}");
      }
      out.println("}");
      out.print("});");
    }