public void afterInit() {
    // this.setSize(620, 320);

    this.pack();
    // Open in center...
    it.businesslogic.ireport.util.Misc.centerFrame(this);

    this.jComboBoxWhenResourceMissingType.addItem(
        new Tag(
            "Null",
            it.businesslogic.ireport.util.I18n.getString("whenResourceMissingType.Null", "Null")));
    this.jComboBoxWhenResourceMissingType.addItem(
        new Tag(
            "Empty",
            it.businesslogic.ireport.util.I18n.getString(
                "whenResourceMissingType.Empty", "Empty")));
    this.jComboBoxWhenResourceMissingType.addItem(
        new Tag(
            "Key",
            it.businesslogic.ireport.util.I18n.getString("whenResourceMissingType.Key", "Key")));
    this.jComboBoxWhenResourceMissingType.addItem(
        new Tag(
            "Error",
            it.businesslogic.ireport.util.I18n.getString(
                "whenResourceMissingType.Error", "Error")));

    applyI18n();

    it.businesslogic.ireport.util.Misc.setComboboxSelectedTagValue(
        jComboBoxWhenResourceMissingType, "Null");

    boolean found = true;
    for (int i = 1; found; ++i) {
      found = false;
      Enumeration e =
          it.businesslogic.ireport.gui.MainFrame.getMainInstance()
              .getActiveReportFrame()
              .getReport()
              .getSubDatasets()
              .elements();
      while (e.hasMoreElements() && !found) {
        SubDataset dataset = (SubDataset) e.nextElement();
        if (dataset.getName().equals("SubDataset" + i)) {
          found = true;
        }
      }
      if (!found) {
        jTextFieldName.setText("SubDataset" + i);
      }
    }

    jTextFieldResourceBoundle.setText(
        it.businesslogic.ireport.gui.MainFrame.getMainInstance()
            .getActiveReportFrame()
            .getReport()
            .getResourceBundleBaseName());
  }
  /*
   * 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 applyNewParameterProperty(
      JRParameter param, String propertyName, Object oldValue, Object newValue) {
    if (propertyName == null) return false;

    boolean objectModified = true;

    if (propertyName.equals("parameterName")) {
      SubDataset paramSubdataset = Misc.getObjectSubDataset(getJrf().getReport(), param);
      if (paramSubdataset != null && newValue != null) {
        for (int i = 0; i < paramSubdataset.getParameters().size(); ++i) {
          JRParameter f = (JRParameter) paramSubdataset.getParameters().get(i);
          if (f.getName().equals(newValue)) {
            ((SheetProperty) this.getSheetProperty(propertyName))
                .setLabelError(
                    I18n.getString(
                        "messages.jRParameterDialog.DuplicatedParameterName",
                        "A parameter with this name already exists!"));
            ((SheetProperty) this.getSheetProperty(propertyName)).updateLabel();
            return false;
          }
        }

        param.setName("" + newValue);
        ((SheetProperty) this.getSheetProperty(propertyName)).setLabelError(null);
        ((SheetProperty) this.getSheetProperty(propertyName)).updateLabel();
      }
    } else if (propertyName.equals("parameterIsForPrompting")) {
      param.setIsForPrompting(((Boolean) newValue).booleanValue());
    } else if (propertyName.equals("parameterDescription")) {
      if (newValue != null) {
        param.setDescription("" + newValue);
      }
    } else if (propertyName.equals("parameterProperties")) {
      if (newValue != null && newValue instanceof List) {
        param.setProperties((List) newValue);
      }
    } else if (propertyName.equals("parameterClassType")) {
      if (newValue != null) {
        param.setClassType("" + newValue);
      }
    } else if (propertyName.equals("parameterDefaultValueExpression")) {
      if (newValue != null) {
        param.setDefaultValueExpression("" + newValue);
      }
      ((SheetProperty) this.getSheetProperty(propertyName)).setLabelError(null);
      ((SheetProperty) this.getSheetProperty(propertyName)).updateLabel();
    }

    return objectModified;
  }
  /*
   * 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;
  }
  public void setSubDataset(SubDataset subDataset) {
    this.subDataset = subDataset;
    this.jTextFieldName.setText(subDataset.getName());
    this.jTextFieldScriptletClass.setText(subDataset.getScriptletClass());
    this.jTextFieldResourceBoundle.setText(subDataset.getResourceBundleBaseName());
    this.jRTextExpressionAreaFilterExpression.setText(subDataset.getFilterExpression());
    if (subDataset != null) {
      jRTextExpressionAreaFilterExpression.setSubDataset(subDataset);
    }

    Misc.setComboboxSelectedTagValue(
        jComboBoxWhenResourceMissingType, subDataset.getWhenResourceMissingType());
    jButtonFields.setEnabled(true);
    jButtonVariables.setEnabled(true);
    jButtonParameters.setEnabled(true);
    jButtonQuery.setEnabled(true);
    jButtonGroups.setEnabled(true);
    jButtonProperties.setEnabled(true);
    this.jButtonOk.setText(I18n.getString("subDatasetDialog.buttonOk", "Ok"));
  }
  /** Update groups */
  protected void updateAllComboBoxes(SubDataset subDataset) {
    if (jrf == null) return;
    // Use the name of the group and not the group object....
    Vector group_names = new Vector();

    if (subDataset != null) {
      Enumeration e = subDataset.getGroups().elements();
      while (e.hasMoreElements()) {
        group_names.addElement("" + e.nextElement());
      }
    }
    spVariableIncrementGroup.updateValues(group_names, false);
    spVariableResetGroup.updateValues(group_names, false);
  }
  /*
   * 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 applyNewVariableProperty(
      JRVariable variable, String propertyName, Object oldValue, Object newValue) {
    if (propertyName == null) return false;

    boolean objectModified = true;

    if (propertyName.equals("variableName")) {
      SubDataset paramSubdataset = Misc.getObjectSubDataset(getJrf().getReport(), variable);
      if (paramSubdataset != null && newValue != null) {
        for (int i = 0; i < paramSubdataset.getVariables().size(); ++i) {
          JRVariable f = (JRVariable) paramSubdataset.getVariables().get(i);
          if (f.getName().equals(newValue)) {
            ((SheetProperty) this.getSheetProperty(propertyName))
                .setLabelError(
                    I18n.getString(
                        "messages.JRVariableDialog.DuplicatedVariableName",
                        "A variable with this name already exists!"));
            ((SheetProperty) this.getSheetProperty(propertyName)).updateLabel();
            return false;
          }
        }

        variable.setName("" + newValue);
        ((SheetProperty) this.getSheetProperty(propertyName)).setLabelError(null);
        ((SheetProperty) this.getSheetProperty(propertyName)).updateLabel();
      }
    } else if (propertyName.equals("variableClassType")) {
      if (newValue != null) {
        variable.setClassType("" + newValue);
      }
    } else if (propertyName.equals("variableCalculationType")) {
      if (newValue != null) {
        variable.setCalculation("" + newValue);
      }
    } else if (propertyName.equals("variableResetType")) {
      if (newValue != null) {
        variable.setResetType("" + newValue);
      }
      if (newValue != null && newValue.equals("Group")) {
        spVariableResetGroup.setReadOnly(false);
        variable.setResetGroup(spVariableResetGroup.getValue() + "");
      } else {
        spVariableResetGroup.setReadOnly(true);
        variable.setResetGroup("");
      }
      spVariableResetGroup.updateLabel();
    } else if (propertyName.equals("variableResetGroup")) {
      variable.setResetGroup((newValue == null) ? "" : "" + newValue);
    } else if (propertyName.equals("variableIncrementType")) {
      if (newValue != null) {
        variable.setIncrementType("" + newValue);
      }
      if (newValue != null && newValue.equals("Group")) {
        spVariableIncrementGroup.setReadOnly(false);
        variable.setResetGroup(spVariableIncrementGroup.getValue() + "");
      } else {
        spVariableIncrementGroup.setReadOnly(true);
        variable.setIncrementGroup("");
      }
      spVariableIncrementGroup.updateLabel();
    } else if (propertyName.equals("variableIncrementGroup")) {
      variable.setIncrementGroup((newValue == null) ? "" : "" + newValue);
    } else if (propertyName.equals("variableIncrementerClass")) {
      variable.setIncrementerFactoryClass((newValue == null) ? "" : "" + newValue);
    } else if (propertyName.equals("variableExpression")) {
      if (newValue != null) {
        variable.setExpression("" + newValue);
      }
      ((SheetProperty) this.getSheetProperty(propertyName)).setLabelError(null);
      ((SheetProperty) this.getSheetProperty(propertyName)).updateLabel();
    } else if (propertyName.equals("variableInitialValueExpression")) {
      if (newValue != null) {
        variable.setInitialValueExpression("" + newValue);
      }
      ((SheetProperty) this.getSheetProperty(propertyName)).setLabelError(null);
      ((SheetProperty) this.getSheetProperty(propertyName)).updateLabel();
    }

    return objectModified;
  }
  /** This methos is called when a property changes... */
  public void sheetPropertyValueChanged(SheetPropertyValueChangedEvent evt) {
    if (isInit()) return;

    try {
      setInit(true);
      // System.out.println("Changed: " + evt.getPropertyName());
      // if (isNullItem((SheetProperty)evt.getSource())) return;

      // removeNullItem( (SheetProperty)evt.getSource() );

      Vector selectedElements = getSelection();

      Vector modified_parameters = new Vector();
      Vector original_parameters = new Vector();

      Vector modified_fields = new Vector();
      Vector original_fields = new Vector();

      Vector modified_variables = new Vector();
      Vector original_variables = new Vector();

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

        Object object = selectedElements.elementAt(i);

        if (object instanceof JRParameter) {
          JRParameter param = (JRParameter) object;
          if (param.isBuiltin()) continue;
          JRParameter originalParam = param.cloneMe();
          if (applyNewParameterProperty(
              param, evt.getPropertyName(), evt.getOldValue(), evt.getNewValue())) {
            modified_parameters.add(object);
            original_parameters.add(originalParam);
          }
        } else if (object instanceof JRField) {
          JRField field = (JRField) object;
          JRField originalField = field.cloneMe();
          if (applyNewFieldProperty(
              field, evt.getPropertyName(), evt.getOldValue(), evt.getNewValue())) {
            modified_fields.add(object);
            original_fields.add(originalField);
          }
        } else if (object instanceof JRVariable) {
          JRVariable variable = (JRVariable) object;
          JRVariable originalVariable = variable.cloneMe();
          if (applyNewVariableProperty(
              variable, evt.getPropertyName(), evt.getOldValue(), evt.getNewValue())) {
            modified_variables.add(object);
            original_variables.add(originalVariable);
          }
        }

        // don't listen to these events...

        for (int k = 0; k < modified_parameters.size(); ++k) {
          JRParameter param = (JRParameter) modified_parameters.get(k);
          JRParameter oldParam = (JRParameter) original_parameters.get(k);
          SubDataset sd = Misc.getObjectSubDataset(getJrf().getReport(), param);
          sd.fireSubDatasetObjectChangedListenerSubDatasetObjectChanged(
              new it.businesslogic.ireport.gui.event.SubDatasetObjectChangedEvent(
                  sd,
                  it.businesslogic.ireport.gui.event.SubDatasetObjectChangedEvent.PARAMETER,
                  it.businesslogic.ireport.gui.event.SubDatasetObjectChangedEvent.MODIFIED,
                  oldParam,
                  param));
        }

        for (int k = 0; k < modified_fields.size(); ++k) {
          JRField field = (JRField) modified_fields.get(k);
          JRField oldField = (JRField) original_fields.get(k);
          SubDataset sd = Misc.getObjectSubDataset(getJrf().getReport(), field);
          sd.fireSubDatasetObjectChangedListenerSubDatasetObjectChanged(
              new it.businesslogic.ireport.gui.event.SubDatasetObjectChangedEvent(
                  sd,
                  it.businesslogic.ireport.gui.event.SubDatasetObjectChangedEvent.FIELD,
                  it.businesslogic.ireport.gui.event.SubDatasetObjectChangedEvent.MODIFIED,
                  oldField,
                  field));
        }

        for (int k = 0; k < modified_variables.size(); ++k) {
          JRVariable variable = (JRVariable) modified_variables.get(k);
          JRVariable oldVariable = (JRVariable) original_variables.get(k);
          SubDataset sd = Misc.getObjectSubDataset(getJrf().getReport(), variable);
          sd.fireSubDatasetObjectChangedListenerSubDatasetObjectChanged(
              new it.businesslogic.ireport.gui.event.SubDatasetObjectChangedEvent(
                  sd,
                  it.businesslogic.ireport.gui.event.SubDatasetObjectChangedEvent.VARIABLE,
                  it.businesslogic.ireport.gui.event.SubDatasetObjectChangedEvent.MODIFIED,
                  oldVariable,
                  variable));
        }
      }

      getJrf().getReport().incrementReportChanges();

    } finally {
      setInit(false);
    }
  }
  private void jButtonOkActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_jButtonOkActionPerformed

    if (this.jTextFieldName.getText().trim().length() <= 0) {
      javax.swing.JOptionPane.showMessageDialog(
          this,
          I18n.getString(
              "messages.subDatasetDialog.notValidName", "Please insert a valid SubDataset name!"),
          I18n.getString("messages.subDatasetDialog.notValidNameCaption", "Invalid name!"),
          javax.swing.JOptionPane.WARNING_MESSAGE);
      return;
    }

    String newName = this.jTextFieldName.getText().trim();
    // check for duplicate name....
    Enumeration e =
        it.businesslogic.ireport.gui.MainFrame.getMainInstance()
            .getActiveReportFrame()
            .getReport()
            .getSubDatasets()
            .elements();
    while (e.hasMoreElements()) {
      SubDataset dataset = (SubDataset) e.nextElement();
      if (dataset.getName().equals(newName) && dataset != getSubDataset()) {
        javax.swing.JOptionPane.showMessageDialog(
            this,
            I18n.getFormattedString(
                "messages.subDatasetDialog.nameAlreadyInUse",
                "The name \"{0}\" is already in use!\nPlease change it.",
                new Object[] {newName}),
            I18n.getString("messages.subDatasetDialog.notValidNameCaption", "Invalid name!"),
            javax.swing.JOptionPane.WARNING_MESSAGE);
        return;
      }
    }

    if (getSubDataset() == null) {
      this.subDataset = new SubDataset();
    }

    SubDataset subdataset = getSubDataset();
    subdataset.setName(jTextFieldName.getText());
    subdataset.setScriptletClass(jTextFieldScriptletClass.getText());
    subdataset.setResourceBundleBaseName(jTextFieldResourceBoundle.getText());
    subdataset.setFilterExpression(jRTextExpressionAreaFilterExpression.getText());

    if (jComboBoxWhenResourceMissingType.getSelectedItem() != null) {
      Tag val = (Tag) jComboBoxWhenResourceMissingType.getSelectedItem();
      subdataset.setWhenResourceMissingType(val.getValue() + "");
    }

    if (this.jButtonOk
        .getText()
        .equals(I18n.getString("subDatasetDialog.buttonCreate", "Create"))) {
      if (this.getParent() instanceof SubDatasetsDialog) {
        ((SubDatasetsDialog) this.getParent()).addSubDataset(this.getSubDataset());
      }
      this.setSubDataset(subdataset);
      MainFrame.getMainInstance().getActiveReportFrame().getReport().addSubDataset(subdataset);
      return;
    } else {
      MainFrame.getMainInstance()
          .getActiveReportFrame()
          .getReport()
          .fireReportSubDatasetChangedListenerReportSubDatasetChanged(
              new ReportSubDatasetChangedEvent(
                  MainFrame.getMainInstance().getActiveReportFrame().getReport(),
                  getSubDataset(),
                  ReportSubDatasetChangedEvent.CHANGED,
                  ReportSubDatasetChangedEvent.OBJECT_SUBDATASET));
    }
    MainFrame.getMainInstance().getActiveReportFrame().getReport().incrementReportChanges();
    this.setDialogResult(javax.swing.JOptionPane.OK_OPTION);
    setVisible(false);
    dispose();
  } // GEN-LAST:event_jButtonOkActionPerformed