/*
   * 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;
  }
  /** 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);
    }
  }