/** Updates the AttributedCharacterIterator and bitset, if necessary. */
  void updateMaskIfNecessary() {
    if (!getAllowsInvalid() && (getFormat() != null)) {
      if (!isValidMask()) {
        updateMask();
      } else {
        String newString = getFormattedTextField().getText();

        if (!newString.equals(string)) {
          updateMask();
        }
      }
    }
  }
  /**
   * Updates the AttributedCharacterIterator by invoking <code>formatToCharacterIterator</code> on
   * the <code>Format</code>. If this is successful, <code>updateMask(AttributedCharacterIterator)
   * </code> is then invoked to update the internal bitmask.
   */
  void updateMask() {
    if (getFormat() != null) {
      Document doc = getFormattedTextField().getDocument();

      validMask = false;
      if (doc != null) {
        try {
          string = doc.getText(0, doc.getLength());
        } catch (BadLocationException ble) {
          string = null;
        }
        if (string != null) {
          try {
            Object value = stringToValue(string);
            AttributedCharacterIterator iterator = getFormat().formatToCharacterIterator(value);

            updateMask(iterator);
          } catch (ParseException pe) {
          } catch (IllegalArgumentException iae) {
          } catch (NullPointerException npe) {
          }
        }
      }
    }
  }
  /**
   * Returns the <code>Format.Field</code> constants associated with the text at <code>offset</code>
   * . If <code>offset</code> is not a valid location into the current text, this will return an
   * empty array.
   *
   * @param offset offset into text to be examined
   * @return Format.Field constants associated with the text at the given position.
   */
  public Format.Field[] getFields(int offset) {
    if (getAllowsInvalid()) {
      // This will work if the currently edited value is valid.
      updateMask();
    }

    Map attrs = getAttributes(offset);

    if (attrs != null && attrs.size() > 0) {
      ArrayList al = new ArrayList();

      al.addAll(attrs.keySet());
      return (Format.Field[]) al.toArray(EMPTY_FIELD_ARRAY);
    }
    return EMPTY_FIELD_ARRAY;
  }