/**
   * 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);
      }
    }
  }
Exemplo n.º 2
0
  private AttributedString(
      AttributedCharacterIterator iterator, int start, int end, Set<Attribute> attributes) {
    if (start < iterator.getBeginIndex() || end > iterator.getEndIndex() || start > end) {
      throw new IllegalArgumentException();
    }

    if (attributes == null) {
      return;
    }

    StringBuilder buffer = new StringBuilder();
    iterator.setIndex(start);
    while (iterator.getIndex() < end) {
      buffer.append(iterator.current());
      iterator.next();
    }
    text = buffer.toString();
    attributeMap = new HashMap<Attribute, List<Range>>((attributes.size() * 4 / 3) + 1);

    Iterator<Attribute> it = attributes.iterator();
    while (it.hasNext()) {
      AttributedCharacterIterator.Attribute attribute = it.next();
      iterator.setIndex(start);
      while (iterator.getIndex() < end) {
        Object value = iterator.getAttribute(attribute);
        int runStart = iterator.getRunStart(attribute);
        int limit = iterator.getRunLimit(attribute);
        if ((value instanceof Annotation && runStart >= start && limit <= end)
            || (value != null && !(value instanceof Annotation))) {
          addAttribute(
              attribute,
              value,
              (runStart < start ? start : runStart) - start,
              (limit > end ? end : limit) - start);
        }
        iterator.setIndex(limit);
      }
    }
  }
  /**
   * Returns the start of the first run that contains the attribute <code>id</code>. This will
   * return <code>-1</code> if the attribute can not be found.
   */
  int getAttributeStart(AttributedCharacterIterator.Attribute id) {
    if (isValidMask()) {
      AttributedCharacterIterator iterator = getIterator();

      iterator.first();
      while (iterator.current() != CharacterIterator.DONE) {
        if (iterator.getAttribute(id) != null) {
          return iterator.getIndex();
        }
        iterator.next();
      }
    }
    return -1;
  }
  /**
   * Returns the number of occurences of <code>f</code> before the location <code>start</code> in
   * the current <code>AttributedCharacterIterator</code>.
   */
  private int getFieldTypeCountTo(Object f, int start) {
    AttributedCharacterIterator iterator = getIterator();
    int count = 0;

    if (iterator != null && (f instanceof AttributedCharacterIterator.Attribute)) {
      AttributedCharacterIterator.Attribute field = (AttributedCharacterIterator.Attribute) f;
      int index = 0;

      iterator.first();
      while (iterator.getIndex() < start) {
        while (iterator.getAttribute(field) == null && iterator.next() != CharacterIterator.DONE) ;
        if (iterator.current() != CharacterIterator.DONE) {
          iterator.setIndex(iterator.getRunLimit(field));
          iterator.next();
          count++;
        } else {
          break;
        }
      }
    }
    return count;
  }
  /** Selects the fields identified by <code>attributes</code>. */
  void selectField(Object f, int count) {
    AttributedCharacterIterator iterator = getIterator();

    if (iterator != null && (f instanceof AttributedCharacterIterator.Attribute)) {
      AttributedCharacterIterator.Attribute field = (AttributedCharacterIterator.Attribute) f;

      iterator.first();
      while (iterator.current() != CharacterIterator.DONE) {
        while (iterator.getAttribute(field) == null && iterator.next() != CharacterIterator.DONE) ;
        if (iterator.current() != CharacterIterator.DONE) {
          int limit = iterator.getRunLimit(field);

          if (--count <= 0) {
            getFormattedTextField().select(iterator.getIndex(), limit);
            break;
          }
          iterator.setIndex(limit);
          iterator.next();
        }
      }
    }
  }