@Override
  public JRDesignElement createReportElement(JasperDesign jd) {
    JRDesignTextField element = (JRDesignTextField) super.createReportElement(jd);

    FieldPercentageDialog dialog = new FieldPercentageDialog(Misc.getMainFrame());
    dialog.setJasperDesign(jd);

    dialog.setVisible(true);
    if (dialog.getDialogResult() == JOptionPane.OK_OPTION) {
      JRField f = dialog.getSelectedField();
      ResetTypeEnum resetType = dialog.getSelectedResetTypeValue();
      JRGroup group = null;
      if (resetType == ResetTypeEnum.GROUP) {
        group = dialog.getSelectedGroup();
      }

      // Let's create the variable...

      JRDesignVariable variable = new JRDesignVariable();
      for (int i = 0; ; ++i) {
        String vname = f.getName() + "_SUM";
        if (i > 0) vname += "_" + i;

        if (jd.getVariablesMap().containsKey(vname)) {
          continue;
        }

        variable.setName(vname);
        break;
      }

      variable.setExpression(
          Misc.createExpression(f.getValueClassName(), "$F{" + f.getName() + "}"));
      variable.setValueClassName(f.getValueClassName());
      variable.setCalculation(CalculationEnum.SUM);
      variable.setResetType(resetType);
      if (resetType == ResetTypeEnum.GROUP) {
        variable.setResetGroup(group);
      }
      try {
        jd.addVariable(variable);
      } catch (JRException ex) {
        ex.printStackTrace();
      }

      ((JRDesignExpression) element.getExpression())
          .setText(
              "new Double( $F{"
                  + f.getName()
                  + "}.doubleValue() / $V{"
                  + variable.getName()
                  + "}.doubleValue() )");
      ((JRDesignExpression) element.getExpression()).setValueClassName("java.lang.Double");

      element.setPattern("#,##0.00%");
      setMatchingClassExpression(
          ((JRDesignExpression) element.getExpression()),
          ((JRDesignExpression) element.getExpression()).getValueClassName(),
          true);

      element.setEvaluationTime(EvaluationTimeEnum.AUTO);

      return element;
    }

    return null;
  }
  private String generateExpression(JRExpression expression, byte evaluationType) {
    JRParameter jrParameter = null;
    JRField jrField = null;
    JRVariable jrVariable = null;

    StringBuffer sbuffer = new StringBuffer();

    JRExpressionChunk[] chunks = expression.getChunks();
    JRExpressionChunk chunk = null;
    String chunkText = null;
    if (chunks != null && chunks.length > 0) {
      for (int i = 0; i < chunks.length; i++) {
        chunk = chunks[i];

        chunkText = chunk.getText();
        if (chunkText == null) {
          chunkText = "";
        }

        switch (chunk.getType()) {
          case JRExpressionChunk.TYPE_TEXT:
            {
              sbuffer.append(chunkText);
              break;
            }
          case JRExpressionChunk.TYPE_PARAMETER:
            {
              jrParameter = (JRParameter) parametersMap.get(chunkText);

              sbuffer.append("((");
              sbuffer.append(jrParameter.getValueClassName());
              sbuffer.append(")super.parameter_");
              sbuffer.append(JRStringUtil.getJavaIdentifier(chunkText));
              sbuffer.append(".getValue())");

              break;
            }
          case JRExpressionChunk.TYPE_FIELD:
            {
              jrField = (JRField) fieldsMap.get(chunkText);

              sbuffer.append("((");
              sbuffer.append(jrField.getValueClassName());
              sbuffer.append(")super.field_");
              sbuffer.append(JRStringUtil.getJavaIdentifier(chunkText));
              sbuffer.append(".get");
              sbuffer.append((String) fieldPrefixMap.get(new Byte(evaluationType)));
              sbuffer.append("Value())");

              break;
            }
          case JRExpressionChunk.TYPE_VARIABLE:
            {
              jrVariable = (JRVariable) variablesMap.get(chunkText);

              sbuffer.append("((");
              sbuffer.append(jrVariable.getValueClassName());
              sbuffer.append(")super.variable_");
              sbuffer.append(JRStringUtil.getJavaIdentifier(chunkText));
              sbuffer.append(".get");
              sbuffer.append((String) variablePrefixMap.get(new Byte(evaluationType)));
              sbuffer.append("Value())");

              break;
            }
          case JRExpressionChunk.TYPE_RESOURCE:
            {
              jrParameter = (JRParameter) parametersMap.get(chunkText);

              sbuffer.append("super.evaluator.str(\"");
              sbuffer.append(chunkText);
              sbuffer.append("\")");

              break;
            }
        }
      }
    }

    if (sbuffer.length() == 0) {
      sbuffer.append("null");
    }

    return sbuffer.toString();
  }