private void jButton2ActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_jButton2ActionPerformed

    // Get all selected paths...
    if (MainFrame.getMainInstance().getActiveReportFrame() == null || getJTableFields() == null) {
      return;
    }

    DefaultTableModel dtm = (DefaultTableModel) getJTableFields().getModel();

    TreePath[] paths = jTree1.getSelectionPaths();
    if (paths == null) return;
    for (int i = 0; i < paths.length; ++i) {
      boolean valid = true;
      TreePath tp = paths[i];

      TreeJRField tjrf =
          (TreeJRField) ((DefaultMutableTreeNode) tp.getLastPathComponent()).getUserObject();
      String returnType = Misc.getJRFieldType(tjrf.getObj().getName());
      it.businesslogic.ireport.JRField field =
          new it.businesslogic.ireport.JRField(tjrf.getField().getName(), returnType);
      field.setDescription(tjrf.getField().getDescription());
      Vector row = new Vector();
      row.addElement(field);
      row.addElement(field.getClassType());
      row.addElement(field.getDescription());

      if (isComboVisible() && jComboBox1.getSelectedItem() instanceof FieldClassWrapper) {
        FieldClassWrapper cw = (FieldClassWrapper) jComboBox1.getSelectedItem();
        field.setName(cw.getFieldName() + "." + field.getDescription());
        field.setDescription(field.getName());
      }

      // Check for duplicates fields...
      boolean found = fieldAlreadyExists(field);
      String baseName = field.getName();
      for (int j = 1; isPathOnDescription() && found; ++j) {
        field.setName(baseName + "_" + j);
        found = fieldAlreadyExists(field);
      }

      if (!found) {
        dtm.addRow(row);
        getJTableFields()
            .getSelectionModel()
            .addSelectionInterval(
                getJTableFields().getRowCount() - 1, getJTableFields().getRowCount() - 1);
      }
    }
  } // GEN-LAST:event_jButton2ActionPerformed
  private boolean fieldAlreadyExists(JRField field) {
    boolean found = false;
    for (int j = 0; j < getJTableFields().getRowCount(); ++j) {
      Object ff = getJTableFields().getValueAt(j, 0);
      if (ff instanceof it.businesslogic.ireport.JRField) {
        if (((it.businesslogic.ireport.JRField) ff).getName().equals(field.getName())) {
          found = true;
          break;
        }
      }
    }

    return found;
  }
  /*
   * This method apply the new value for the specified property
   * The oldValue can be wrong or null if a multiselection was performed
   * return true if the object is modified...
   */
  private boolean applyNewFieldProperty(
      JRField field, String propertyName, Object oldValue, Object newValue) {
    if (propertyName == null) return false;

    boolean objectModified = true;

    if (propertyName.equals("fieldName")) {
      SubDataset paramSubdataset = Misc.getObjectSubDataset(getJrf().getReport(), field);
      if (paramSubdataset != null && newValue != null) {
        for (int i = 0; i < paramSubdataset.getFields().size(); ++i) {
          JRField f = (JRField) paramSubdataset.getFields().get(i);
          if (f.getName().equals(newValue)) {
            ((SheetProperty) this.getSheetProperty(propertyName))
                .setLabelError(
                    I18n.getString(
                        "messages.JRFieldDialog.DuplicatedFieldName",
                        "A field with this name already exists!"));
            ((SheetProperty) this.getSheetProperty(propertyName)).updateLabel();
            return false;
          }
        }

        field.setName("" + newValue);
        ((SheetProperty) this.getSheetProperty(propertyName)).setLabelError(null);
        ((SheetProperty) this.getSheetProperty(propertyName)).updateLabel();
      }
    } else if (propertyName.equals("fieldDescription")) {
      if (newValue != null) {
        field.setDescription("" + newValue);
      }
    } else if (propertyName.equals("fieldProperties")) {
      if (newValue != null && newValue instanceof List) {
        field.setProperties((List) newValue);
      }
    } else if (propertyName.equals("fieldClassType")) {
      if (newValue != null) {
        field.setClassType("" + newValue);
      }
    }

    return objectModified;
  }
  /** Update all the element properties... */
  public void updateSelection(JReportFrame newJrf) {

    // Improving speed...
    // Do nothing if there are elements selected...
    if (newJrf != null && newJrf.getSelectedElements().size() > 0) return;

    // Fix for numbers focus losing...
    if (newJrf == null) {
      this.setSelection(new Vector());
    } else {
      this.setSelection(newJrf.getSelectedObjects());
    }

    setInit(true);

    this.removeAllProperties();

    this.jrf = newJrf;

    if (jrf == null || getSelection().size() == 0) {
      this.recreateSheet();
      return;
    }

    try {
      Vector selectedElements = getSelection();

      boolean sameParameterDescription = true;
      boolean sameParameterDefaultValueExpression = true;
      boolean sameParameterClassType = true;
      boolean sameParameterIsForPrompting = true;

      boolean sameFieldDescription = true;
      boolean sameFieldClassType = true;

      boolean sameVariableResetType = true;
      boolean sameVariableResetGroup = true;
      boolean sameVariableCalculationType = true;
      boolean sameVariableClassType = true;
      boolean sameVariableExpression = true;
      boolean sameVariableInitialValueExpression = true;
      boolean sameVariableIncrementGroup = true;
      boolean sameVariableIncrementType = true;
      boolean sameVariableIncrementerClass = true;

      boolean areAllparameters = true;
      boolean areAllfields = true;
      boolean areAllvariables = true;
      boolean isTheFirstElement = true;

      boolean areBuiltInParameters = false; // True if one ore more parameter is builtin...
      boolean areBuiltInVariables = false; // True if one ore more parameter is builtin...

      SubDataset subdataset = null;

      for (int i = 0; i < selectedElements.size(); ++i) {
        Object obj = selectedElements.elementAt(i);

        if (!(obj instanceof JRParameter)) {
          areAllparameters = false;
        }

        if (!(obj instanceof JRField)) {
          areAllfields = false;
        }

        if (!(obj instanceof JRVariable)) {
          areAllvariables = false;
        }

        if (isTheFirstElement) {
          subdataset = Misc.getObjectSubDataset(jrf.getReport(), obj);
          updateAllComboBoxes(subdataset);
        } else if (subdataset != null) {
          SubDataset s2 = Misc.getObjectSubDataset(subdataset, obj);
          if (s2 != subdataset) {
            subdataset = null;
          }
        }

        if (areAllparameters) {
          JRParameter param = (JRParameter) selectedElements.elementAt(i);

          if (!areBuiltInParameters) {
            areBuiltInParameters = param.isBuiltin();
          }

          if (selectedElements.size() == 1) {
            // Single parameter selectes...
            setTextArea(isTheFirstElement, param.getName(), spParameterName);
            spParameterProperties.setValue(param.getProperties());
          }
          if (sameParameterDescription)
            sameParameterDescription =
                setTextArea(isTheFirstElement, param.getDescription(), spParameterDescription);
          if (sameParameterDefaultValueExpression)
            sameParameterDefaultValueExpression =
                setTextArea(
                    isTheFirstElement,
                    param.getDefaultValueExpression(),
                    spParameterDefaultValueExpression);
          if (sameParameterClassType)
            sameParameterClassType =
                setGenericSheetProperty(
                    isTheFirstElement, param.getClassType(), spParameterClassType);
          if (sameParameterIsForPrompting)
            sameParameterIsForPrompting =
                this.setCheckBox(
                    isTheFirstElement, param.isIsForPrompting(), false, spParameterIsForPrompting);
        }

        if (areAllfields) {
          JRField field = (JRField) selectedElements.elementAt(i);

          if (selectedElements.size() == 1) {
            // Single parameter selectes...
            setTextArea(isTheFirstElement, field.getName(), spFieldName);
            spFieldProperties.setValue(field.getProperties());
          }
          if (sameFieldDescription)
            sameFieldDescription =
                setTextArea(isTheFirstElement, field.getDescription(), spFieldDescription);
          if (sameFieldClassType)
            sameFieldClassType =
                setGenericSheetProperty(isTheFirstElement, field.getClassType(), spFieldClassType);
        }

        if (areAllvariables) {
          JRVariable variable = (JRVariable) selectedElements.elementAt(i);

          if (!areBuiltInVariables) {
            areBuiltInVariables = variable.isBuiltin();
          }

          if (selectedElements.size() == 1) {
            // Single parameter selectes...
            setTextArea(isTheFirstElement, variable.getName(), spVariableName);
          }

          if (subdataset != null) {
            if (sameVariableResetType)
              sameVariableResetType =
                  setTagComboBox(isTheFirstElement, variable.getResetType(), spVariableResetType);
            if (sameVariableResetGroup)
              sameVariableResetGroup =
                  setTagComboBox(isTheFirstElement, variable.getResetGroup(), spVariableResetGroup);
            if (sameVariableIncrementType)
              sameVariableIncrementType =
                  setTagComboBox(
                      isTheFirstElement, variable.getIncrementType(), spVariableIncrementType);
            if (sameVariableIncrementGroup)
              sameVariableIncrementGroup =
                  setTagComboBox(
                      isTheFirstElement, variable.getIncrementGroup(), spVariableIncrementGroup);
          }

          if (sameVariableCalculationType)
            sameVariableCalculationType =
                setTagComboBox(
                    isTheFirstElement, variable.getCalculation(), spVariableCalculationType);
          if (sameVariableIncrementerClass)
            sameVariableIncrementerClass =
                setTextArea(
                    isTheFirstElement,
                    variable.getIncrementerFactoryClass(),
                    spVariableIncrementerClass);
          if (sameVariableClassType)
            sameVariableClassType =
                setGenericSheetProperty(
                    isTheFirstElement, variable.getClassType(), spVariableClassType);
          if (sameVariableExpression)
            sameVariableExpression =
                setTextArea(isTheFirstElement, variable.getExpression(), spVariableExpression);
          if (sameVariableInitialValueExpression)
            sameVariableInitialValueExpression =
                setTextArea(
                    isTheFirstElement,
                    variable.getInitialValueExpression(),
                    spVariableInitialValueExpression);
        }

        isTheFirstElement = false;
      }

      // TO DO: change this!

      // get the common subdataset...
      if (subdataset != null) {
        ExpressionContext ec = new ExpressionContext();
        ec.setSubDataset(subdataset);
        spParameterDefaultValueExpression.setExpressionContext(ec);
        spVariableExpression.setExpressionContext(ec);
        spVariableInitialValueExpression.setExpressionContext(ec);
      }

      spParameterDefaultValueExpression.setLabelError(null);
      spParameterDefaultValueExpression.updateLabel();

      spVariableExpression.setLabelError(null);
      spVariableExpression.updateLabel();

      spVariableInitialValueExpression.setLabelError(null);
      spVariableInitialValueExpression.updateLabel();

      if (areAllparameters) {

        String commonStr =
            it.businesslogic.ireport.util.I18n.getString(
                "parameterProperties", "Parameter Properties");
        if (getSelection().size() == 1) this.addSheetProperty(commonStr, spParameterName);
        spParameterIsForPrompting.setDefaultValue(new Boolean(true));
        this.addSheetProperty(commonStr, spParameterClassType);
        spParameterClassType.setLabelColor(
            (sameParameterClassType)
                ? mandatoryPropertiesLabelColor
                : sharedDifferentValueLabelColor);

        if (!areBuiltInParameters) {
          spParameterName.setReadOnly(false);
          spParameterClassType.setReadOnly(false);
          spParameterIsForPrompting.setLabelColor(
              (sameParameterIsForPrompting)
                  ? mandatoryPropertiesLabelColor
                  : sharedDifferentValueLabelColor);
          this.addSheetProperty(commonStr, spParameterIsForPrompting);
          spParameterDefaultValueExpression.setLabelColor(
              (sameParameterDefaultValueExpression)
                  ? mandatoryPropertiesLabelColor
                  : sharedDifferentValueLabelColor);
          this.addSheetProperty(commonStr, spParameterDefaultValueExpression);
          if (getSelection().size() == 1) this.addSheetProperty(commonStr, spParameterProperties);
        } else {
          spParameterName.setReadOnly(true);
          spParameterClassType.setReadOnly(true);
        }

      } else if (areAllfields) {
        String commonStr =
            it.businesslogic.ireport.util.I18n.getString("fieldProperties", "Field Properties");
        if (getSelection().size() == 1) this.addSheetProperty(commonStr, spFieldName);
        spFieldClassType.setLabelColor(
            (sameFieldClassType) ? mandatoryPropertiesLabelColor : sharedDifferentValueLabelColor);
        this.addSheetProperty(commonStr, spFieldClassType);
        spFieldDescription.setLabelColor(
            (sameFieldDescription)
                ? mandatoryPropertiesLabelColor
                : sharedDifferentValueLabelColor);
        this.addSheetProperty(commonStr, spFieldDescription);
        if (getSelection().size() == 1) this.addSheetProperty(commonStr, spFieldProperties);

      } else if (areAllvariables) {
        String commonStr =
            it.businesslogic.ireport.util.I18n.getString(
                "variableProperties", "Variable Properties");
        if (getSelection().size() == 1) this.addSheetProperty(commonStr, spVariableName);

        spVariableClassType.setLabelColor(
            (sameVariableClassType)
                ? mandatoryPropertiesLabelColor
                : sharedDifferentValueLabelColor);
        this.addSheetProperty(commonStr, spVariableClassType);

        if (!areBuiltInVariables) {
          spVariableName.setReadOnly(false);
          spVariableClassType.setReadOnly(false);

          spVariableCalculationType.setLabelColor(
              (sameVariableCalculationType)
                  ? mandatoryPropertiesLabelColor
                  : sharedDifferentValueLabelColor);
          this.addSheetProperty(commonStr, spVariableCalculationType);

          if (subdataset != null) {
            spVariableResetType.setLabelColor(
                (sameVariableResetType)
                    ? mandatoryPropertiesLabelColor
                    : sharedDifferentValueLabelColor);
            this.addSheetProperty(commonStr, spVariableResetType);
            spVariableResetGroup.setLabelColor(
                (sameVariableResetGroup)
                    ? mandatoryPropertiesLabelColor
                    : sharedDifferentValueLabelColor);
            this.addSheetProperty(commonStr, spVariableResetGroup);
            spVariableIncrementType.setLabelColor(
                (sameVariableIncrementType)
                    ? mandatoryPropertiesLabelColor
                    : sharedDifferentValueLabelColor);
            this.addSheetProperty(commonStr, spVariableIncrementType);
            spVariableIncrementGroup.setLabelColor(
                (sameVariableIncrementGroup)
                    ? mandatoryPropertiesLabelColor
                    : sharedDifferentValueLabelColor);
            this.addSheetProperty(commonStr, spVariableIncrementGroup);

            if (!sameVariableResetType || !spVariableResetType.getValue().equals("Group")) {
              spVariableResetGroup.setReadOnly(true);
            } else {
              spVariableResetGroup.setReadOnly(false);
            }

            if (!sameVariableIncrementType || !spVariableIncrementType.getValue().equals("Group")) {
              spVariableIncrementGroup.setReadOnly(true);
            } else {
              spVariableIncrementGroup.setReadOnly(false);
            }
          }
          spVariableIncrementerClass.setLabelColor(
              (sameVariableIncrementerClass)
                  ? mandatoryPropertiesLabelColor
                  : sharedDifferentValueLabelColor);
          this.addSheetProperty(commonStr, spVariableIncrementerClass);
          spVariableExpression.setLabelColor(
              (sameVariableExpression)
                  ? mandatoryPropertiesLabelColor
                  : sharedDifferentValueLabelColor);
          this.addSheetProperty(commonStr, spVariableExpression);
          spVariableInitialValueExpression.setLabelColor(
              (sameVariableInitialValueExpression)
                  ? mandatoryPropertiesLabelColor
                  : sharedDifferentValueLabelColor);
          this.addSheetProperty(commonStr, spVariableInitialValueExpression);

        } else {
          spVariableName.setReadOnly(true);
          spVariableClassType.setReadOnly(true);
        }
      }

      this.recreateSheet();

    } catch (Exception ex) {
      ex.printStackTrace();
    } finally {

    }

    setInit(false);
  }
Esempio n. 5
0
  /** ******************************************************** */
  public static Report createColumnarReport(UserChoicesWizardTemplate ucwt) throws Exception {
    Report template = new Report(ucwt.getTemplateFile());

    template.incrementReportChanges();
    template.setFilename(null);

    // 2. Find detail and column header bands...
    Band detail = null;
    Band columns = null;

    Enumeration e = template.getBands().elements();
    while (e.hasMoreElements()) {
      Band band = (Band) e.nextElement();
      if (band.getName().equals("detail")) {
        detail = band;
      } else if (band.getName().equals("columnHeader")) {
        columns = band;
      }
    }

    // 1. Normalize position to band...
    e = template.getElements().elements();
    while (e.hasMoreElements()) {
      ReportElement rElement = (ReportElement) e.nextElement();
      rElement.trasform(
          new java.awt.Point(0, -template.getBandYLocation(rElement.getBand())),
          TransformationType.TRANSFORMATION_MOVE);
    }

    // 1. Adding groups...
    if (ucwt.getGroupExpressions().size() > 0) {
      Group g = template.getGroupByName("Group1");
      Group g1 =
          new Group(
              template,
              Misc.string_replace("_", " ", "" + ucwt.getGroupExpressions().get(0)),
              g.getGroupFooter().getHeight(),
              g.getGroupHeader().getHeight());
      g1.setGroupExpression("$F{" + ucwt.getGroupExpressions().get(0) + "}");
      template.addGroup(g1);

      // Add to g1 all elements attached to g.header and g.footer...
      e = template.getElements().elements();
      while (e.hasMoreElements()) {
        ReportElement rElement = (ReportElement) e.nextElement();
        if (rElement.getBand() == g.getGroupHeader()) rElement.setBand(g1.getGroupHeader());
        else if (rElement.getBand() == g.getGroupFooter()) rElement.setBand(g1.getGroupFooter());
        // Set text to Group1 label...
        if (rElement instanceof StaticTextReportElement) {
          StaticTextReportElement stre = (StaticTextReportElement) rElement;
          if (stre.getText().equals("G1Label"))
            stre.setText("" + ucwt.getGroupExpressions().get(0));
        } else if (rElement instanceof TextFieldReportElement) {
          TextFieldReportElement tfre = (TextFieldReportElement) rElement;
          if (tfre.getText().equals("G1Field")) {
            tfre.setText("$F{" + ucwt.getGroupExpressions().get(0) + "}");
            // find the class type...
            String fname = "" + ucwt.getGroupExpressions().get(0);
            String fclass = "java.lang.String";
            for (int i = 0; i < ucwt.getDisplayFields().size(); ++i) {
              JRField f = (JRField) ucwt.getDisplayFields().get(i);
              if (f.getName().equals(fname)) {
                fclass = f.getClassType();
                break;
              }
            }
            tfre.setMatchingClassExpression(fclass, true);
          }
        }
      }
    }

    if (ucwt.getGroupExpressions().size() > 1) {
      Group g = template.getGroupByName("Group2");
      Group g2 =
          new Group(
              template,
              Misc.string_replace("_", " ", "" + ucwt.getGroupExpressions().get(1)),
              g.getGroupFooter().getHeight(),
              g.getGroupHeader().getHeight());
      g2.setGroupExpression("$F{" + ucwt.getGroupExpressions().get(1) + "}");
      template.addGroup(g2);

      // Add to g2 all elements attached to g.header and g.footer...
      e = template.getElements().elements();
      while (e.hasMoreElements()) {
        ReportElement rElement = (ReportElement) e.nextElement();
        if (rElement.getBand() == g.getGroupHeader()) rElement.setBand(g2.getGroupHeader());
        else if (rElement.getBand() == g.getGroupFooter()) rElement.setBand(g2.getGroupFooter());
        // Set text to Group2 label...
        if (rElement instanceof StaticTextReportElement) {
          StaticTextReportElement stre = (StaticTextReportElement) rElement;
          if (stre.getText().equals("G2Label"))
            stre.setText("" + ucwt.getGroupExpressions().get(1));
        } else if (rElement instanceof TextFieldReportElement) {
          TextFieldReportElement tfre = (TextFieldReportElement) rElement;
          if (tfre.getText().equals("G2Field")) {
            tfre.setText("$F{" + ucwt.getGroupExpressions().get(1) + "}");
            // find the class type...
            String fname = "" + ucwt.getGroupExpressions().get(1);
            String fclass = "java.lang.String";
            for (int i = 0; i < ucwt.getDisplayFields().size(); ++i) {
              JRField f = (JRField) ucwt.getDisplayFields().get(i);
              if (f.getName().equals(fname)) {
                fclass = f.getClassType();
                break;
              }
            }
            tfre.setMatchingClassExpression(fclass, true);
          }
        }
      }
    }

    if (ucwt.getGroupExpressions().size() > 2) {
      Group g = template.getGroupByName("Group3");
      Group g3 =
          new Group(
              template,
              Misc.string_replace("_", " ", "" + ucwt.getGroupExpressions().get(2)),
              g.getGroupFooter().getHeight(),
              g.getGroupHeader().getHeight());
      g3.setGroupExpression("$F{" + ucwt.getGroupExpressions().get(2) + "}");
      template.addGroup(g3);

      // Add to g3 all elements attached to g.header and g.footer...
      e = template.getElements().elements();
      while (e.hasMoreElements()) {
        ReportElement rElement = (ReportElement) e.nextElement();
        if (rElement.getBand() == g.getGroupHeader()) rElement.setBand(g3.getGroupHeader());
        else if (rElement.getBand() == g.getGroupFooter()) rElement.setBand(g3.getGroupFooter());
        // Set text to Group3 label...
        if (rElement instanceof StaticTextReportElement) {
          StaticTextReportElement stre = (StaticTextReportElement) rElement;
          if (stre.getText().equals("G3Label"))
            stre.setText("" + ucwt.getGroupExpressions().get(2));
        } else if (rElement instanceof TextFieldReportElement) {
          TextFieldReportElement tfre = (TextFieldReportElement) rElement;
          if (tfre.getText().equals("G3Field")) {
            tfre.setText("$F{" + ucwt.getGroupExpressions().get(2) + "}");
            // find the class type...
            String fname = "" + ucwt.getGroupExpressions().get(2);
            String fclass = "java.lang.String";
            for (int i = 0; i < ucwt.getDisplayFields().size(); ++i) {
              JRField f = (JRField) ucwt.getDisplayFields().get(i);
              if (f.getName().equals(fname)) {
                fclass = f.getClassType();
                break;
              }
            }
            tfre.setMatchingClassExpression(fclass, true);
          }
        }
      }
    }

    if (ucwt.getGroupExpressions().size() > 3) {
      Group g = template.getGroupByName("Group4");
      Group g4 =
          new Group(
              template,
              Misc.string_replace("_", " ", "" + ucwt.getGroupExpressions().get(3)),
              g.getGroupFooter().getHeight(),
              g.getGroupHeader().getHeight());
      g4.setGroupExpression("$F{" + ucwt.getGroupExpressions().get(3) + "}");
      template.addGroup(g4);

      // Add to g4 all elements attached to g.header and g.footer...
      e = template.getElements().elements();
      while (e.hasMoreElements()) {
        ReportElement rElement = (ReportElement) e.nextElement();
        if (rElement.getBand() == g.getGroupHeader()) rElement.setBand(g4.getGroupHeader());
        else if (rElement.getBand() == g.getGroupFooter()) rElement.setBand(g4.getGroupFooter());
        // Set text to Group4 label...
        if (rElement instanceof StaticTextReportElement) {
          StaticTextReportElement stre = (StaticTextReportElement) rElement;
          if (stre.getText().equals("G4Label"))
            stre.setText("" + ucwt.getGroupExpressions().get(3));
        } else if (rElement instanceof TextFieldReportElement) {
          TextFieldReportElement tfre = (TextFieldReportElement) rElement;
          if (tfre.getText().equals("G4Field")) {
            tfre.setText("$F{" + ucwt.getGroupExpressions().get(3) + "}");
            // find the class type...
            String fname = "" + ucwt.getGroupExpressions().get(3);
            String fclass = "java.lang.String";
            for (int i = 0; i < ucwt.getDisplayFields().size(); ++i) {
              JRField f = (JRField) ucwt.getDisplayFields().get(i);
              if (f.getName().equals(fname)) {
                fclass = f.getClassType();
                break;
              }
            }
            tfre.setMatchingClassExpression(fclass, true);
          }
        }
      }
    }

    // 1. Adding fields...
    int currentx = template.getLeftMargin() + 10;
    int currenty = 10;
    e = template.getElements().elements();
    StaticTextReportElement detailLabel = null;
    TextFieldReportElement detailField = null;
    while (e.hasMoreElements() && (detailLabel == null || detailField == null)) {
      ReportElement rElement = (ReportElement) e.nextElement();
      if (rElement instanceof StaticTextReportElement) {
        StaticTextReportElement stre = (StaticTextReportElement) rElement;
        if (stre.getText().equalsIgnoreCase("DetailLabel")) detailLabel = stre;
      } else if (rElement instanceof TextFieldReportElement) {
        TextFieldReportElement tfre = (TextFieldReportElement) rElement;
        if (tfre.getText().equalsIgnoreCase("DetailField")) detailField = tfre;
      }
    }

    if (detailField != null) template.getElements().removeElement(detailField);
    if (detailLabel != null) template.getElements().removeElement(detailLabel);

    if (detailField == null) detailField = new TextFieldReportElement(0, 0, 100, 20);
    if (detailLabel == null) detailLabel = new StaticTextReportElement(0, 0, 100, 18);

    int nfields = ucwt.getDisplayFields().size();

    if (ucwt.getGroupExpressions().size() > 0) nfields--;
    if (ucwt.getGroupExpressions().size() > 1) nfields--;
    if (ucwt.getGroupExpressions().size() > 2) nfields--;
    if (ucwt.getGroupExpressions().size() > 3) nfields--;

    int fwidth = template.getColumnWidth() / ((nfields <= 0) ? 1 : nfields);
    int ffheight = detailField.getPosition().y;

    for (int i = 0; i < ucwt.getDisplayFields().size(); ++i) {
      // FIELD
      it.businesslogic.ireport.JRField f =
          (it.businesslogic.ireport.JRField) ucwt.getDisplayFields().get(i);
      template.addField(f);

      if (ucwt.getGroupExpressions().size() > 0
          && f.getName().equalsIgnoreCase("" + ucwt.getGroupExpressions().get(0))) continue;
      if (ucwt.getGroupExpressions().size() > 1
          && f.getName().equalsIgnoreCase("" + ucwt.getGroupExpressions().get(1))) continue;
      if (ucwt.getGroupExpressions().size() > 2
          && f.getName().equalsIgnoreCase("" + ucwt.getGroupExpressions().get(2))) continue;
      if (ucwt.getGroupExpressions().size() > 3
          && f.getName().equalsIgnoreCase("" + ucwt.getGroupExpressions().get(3))) continue;

      TextFieldReportElement re = (TextFieldReportElement) detailField.cloneMe();
      re.setPosition(new java.awt.Point(detailField.getPosition().x, ffheight));
      re.updateBounds();
      re.setText("$F{" + f.getName() + "}");

      re.setBand(detail);
      re.setMatchingClassExpression(f.getClassType(), true);

      template.getElements().addElement(re);

      // COLUMN LABEL...
      StaticTextReportElement sre = (StaticTextReportElement) detailLabel.cloneMe();
      sre.setPosition(new java.awt.Point(detailLabel.getPosition().x, ffheight));
      sre.setWidth(fwidth);
      sre.updateBounds();

      // Get field description from datasource
      // added by Felix Firgau on Oct, 11th 2006
      String columnLabelText = f.getName();
      if (ucwt.isSaveFieldDescriptions()) {
        String description = f.getDescription();
        if (description != null && description.length() > 0) columnLabelText = description;
      }

      sre.setText("" + columnLabelText + "");
      sre.setBand(detail);
      template.getElements().addElement(sre);

      ffheight += detailField.position.y + detailField.height - 10;
    }

    detail.setHeight(ffheight);

    // Remove template groups...****************
    e = template.getElements().elements();
    Vector elements_to_delete = new Vector();
    while (e.hasMoreElements()) {
      ReportElement rElement = (ReportElement) e.nextElement();
      Group g = null;
      if ((g = template.getGroupByName("Group1")) != null
          && (rElement.getBand() == g.getGroupHeader() || rElement.getBand() == g.getGroupFooter()))
        elements_to_delete.addElement(rElement);
      else if ((g = template.getGroupByName("Group2")) != null
          && (rElement.getBand() == g.getGroupHeader() || rElement.getBand() == g.getGroupFooter()))
        elements_to_delete.addElement(rElement);
      else if ((g = template.getGroupByName("Group3")) != null
          && (rElement.getBand() == g.getGroupHeader() || rElement.getBand() == g.getGroupFooter()))
        elements_to_delete.addElement(rElement);
      else if ((g = template.getGroupByName("Group4")) != null
          && (rElement.getBand() == g.getGroupHeader() || rElement.getBand() == g.getGroupFooter()))
        elements_to_delete.addElement(rElement);
    }
    e = elements_to_delete.elements();
    while (e.hasMoreElements()) {
      template.getElements().removeElement(e.nextElement());
    }

    Group g;
    if ((g = template.getGroupByName("Group1")) != null) {
      template.getBands().removeElement(g.getGroupFooter());
      template.getBands().removeElement(g.getGroupHeader());
      template.getGroups().removeElement(g);
    }
    if ((g = template.getGroupByName("Group2")) != null) {
      template.getBands().removeElement(g.getGroupFooter());
      template.getBands().removeElement(g.getGroupHeader());
      template.getGroups().removeElement(g);
    }
    if ((g = template.getGroupByName("Group3")) != null) {
      template.getBands().removeElement(g.getGroupFooter());
      template.getBands().removeElement(g.getGroupHeader());
      template.getGroups().removeElement(g);
    }

    if ((g = template.getGroupByName("Group4")) != null) {
      template.getBands().removeElement(g.getGroupFooter());
      template.getBands().removeElement(g.getGroupHeader());
      template.getGroups().removeElement(g);
    }

    // Positioning columns band...
    /*
    if ((g=template.getGroupByName("Columns"))!=null)
    {
            template.getGroups().removeElement(g);
            template.getBands().removeElement(g.getGroupHeader());
            template.getBands().removeElement(g.getGroupFooter());
            template.addGroup(g);
            // we must adjust band position...
    }
     */
    // Adjust vertical position of elements
    e = template.getElements().elements();
    while (e.hasMoreElements()) {
      ReportElement rElement = (ReportElement) e.nextElement();
      rElement.trasform(
          new java.awt.Point(0, +template.getBandYLocation(rElement.getBand())),
          TransformationType.TRANSFORMATION_MOVE);
    }

    template.setQuery(ucwt.getQuery());
    template.setQueryLanguage(ucwt.getQuery_language());

    return template;
  }