/**
   * Updates the interal bitset from <code>iterator</code>. This will set <code>validMask</code> to
   * true if <code>iterator</code> is non-null.
   */
  private void updateMask(AttributedCharacterIterator iterator) {
    if (iterator != null) {
      validMask = true;
      this.iterator = iterator;

      // Update the literal mask
      if (literalMask == null) {
        literalMask = new BitSet();
      } else {
        for (int counter = literalMask.length() - 1; counter >= 0; counter--) {
          literalMask.clear(counter);
        }
      }

      iterator.first();
      while (iterator.current() != CharacterIterator.DONE) {
        Map attributes = iterator.getAttributes();
        boolean set = isLiteral(attributes);
        int start = iterator.getIndex();
        int end = iterator.getRunLimit();

        while (start < end) {
          if (set) {
            literalMask.set(start);
          } else {
            literalMask.clear(start);
          }
          start++;
        }
        iterator.setIndex(start);
      }
    }
  }
  /** Returns a Set of the attribute identifiers at <code>index</code>. */
  Map getAttributes(int index) {
    if (isValidMask()) {
      AttributedCharacterIterator iterator = getIterator();

      if (index >= 0 && index <= iterator.getEndIndex()) {
        iterator.setIndex(index);
        return iterator.getAttributes();
      }
    }
    return null;
  }
    public void actionPerformed(ActionEvent ae) {

      if (getFormattedTextField().isEditable()) {
        if (getAllowsInvalid()) {
          // This will work if the currently edited value is valid.
          updateMask();
        }

        boolean validEdit = false;

        if (isValidMask()) {
          int start = getFormattedTextField().getSelectionStart();

          if (start != -1) {
            AttributedCharacterIterator iterator = getIterator();

            iterator.setIndex(start);

            Map attributes = iterator.getAttributes();
            Object field = getAdjustField(start, attributes);

            if (canIncrement(field, start)) {
              try {
                Object value = stringToValue(getFormattedTextField().getText());
                int fieldTypeCount = getFieldTypeCountTo(field, start);

                value = adjustValue(value, attributes, field, direction);
                if (value != null && isValidValue(value, false)) {
                  resetValue(value);
                  updateMask();

                  if (isValidMask()) {
                    selectField(field, fieldTypeCount);
                  }
                  validEdit = true;
                }
              } catch (ParseException pe) {
              } catch (BadLocationException ble) {
              }
            }
          }
        }
        if (!validEdit) {
          invalidEdit();
        }
      }
    }