コード例 #1
0
  /**
   * Helper method to set attributes on a graphic. Only sets the attributes on the newGraphic
   * variable if the value has changed. Returns true if the value has changed and has been set on
   * the graphic.
   *
   * @return boolean hasValueChanged
   */
  public static boolean setAttribute(
      Map<String, Object> attrs,
      Graphic oldGraphic,
      Field field,
      String value,
      DateFormat formatter) {

    boolean hasValueChanged = false;

    // if its a string, and it has changed from the oldGraphic value
    if (FieldType.determineFieldType(field) == FieldType.STRING) {

      if (!value.equals(oldGraphic.getAttributeValue(field.getName()))) {

        // set the value as it is
        attrs.put(field.getName(), value);
        hasValueChanged = true;
      }
    } else if (FieldType.determineFieldType(field) == FieldType.NUMBER) {

      // if its an empty string, its a 0 number value (nulls not
      // supported), check this is a
      // change before making it a 0
      if (value.equals("") && oldGraphic.getAttributeValue(field.getName()) != Integer.valueOf(0)) {

        // set a null value on the new graphic
        attrs.put(field.getName(), new Integer(0));
        hasValueChanged = true;

      } else {

        // parse as an int and check this is a change
        int intValue = Integer.parseInt(value);
        if (intValue
            != Integer.parseInt(oldGraphic.getAttributeValue(field.getName()).toString())) {

          attrs.put(field.getName(), Integer.valueOf(intValue));
          hasValueChanged = true;
        }
      }
    } else if (FieldType.determineFieldType(field) == FieldType.DECIMAL) {

      // if its an empty string, its a 0 double value (nulls not
      // supported), check this is a
      // change before making it a 0
      if ((value.equals("")
          && oldGraphic.getAttributeValue(field.getName()) != Double.valueOf(0))) {

        // set a null value on the new graphic
        attrs.put(field.getName(), new Double(0));
        hasValueChanged = true;

      } else {

        // parse as an double and check this is a change
        double dValue = Double.parseDouble(value);
        if (dValue
            != Double.parseDouble(oldGraphic.getAttributeValue(field.getName()).toString())) {

          attrs.put(field.getName(), Double.valueOf(dValue));
          hasValueChanged = true;
        }
      }
    } else if (FieldType.determineFieldType(field) == FieldType.DATE) {

      // if its a date, get the milliseconds value
      Calendar c = Calendar.getInstance();
      long dateInMillis = 0;

      try {

        // parse to a double and check this is a change
        c.setTime(formatter.parse(value));
        dateInMillis = c.getTimeInMillis();

        if (dateInMillis
            != Long.parseLong(oldGraphic.getAttributeValue(field.getName()).toString())) {

          attrs.put(field.getName(), Long.valueOf(dateInMillis));
          hasValueChanged = true;
        }
      } catch (ParseException e) {
        // do nothing
      }
    }
    // }

    return hasValueChanged;
  }