Beispiel #1
0
  /** @see Graphics2D#drawString(AttributedCharacterIterator, float, float) */
  public void drawString(AttributedCharacterIterator iter, float x, float y) {
    /*
            StringBuffer sb = new StringBuffer();
            for(char c = iter.first(); c != AttributedCharacterIterator.DONE; c = iter.next()) {
                sb.append(c);
            }
            drawString(sb.toString(),x,y);
    */
    StringBuffer stringbuffer = new StringBuffer(iter.getEndIndex());
    for (char c = iter.first(); c != '\uFFFF'; c = iter.next()) {
      if (iter.getIndex() == iter.getRunStart()) {
        if (stringbuffer.length() > 0) {
          drawString(stringbuffer.toString(), x, y);
          FontMetrics fontmetrics = getFontMetrics();
          x = (float) (x + fontmetrics.getStringBounds(stringbuffer.toString(), this).getWidth());
          stringbuffer.delete(0, stringbuffer.length());
        }
        doAttributes(iter);
      }
      stringbuffer.append(c);
    }

    drawString(stringbuffer.toString(), x, y);
    underline = false;
  }
  /**
   * finds attributes with regards to char index in this AttributedCharacterIterator, and puts them
   * in a vector
   *
   * @param iterator
   * @return a vector, each entry in this vector are of type FieldContainer , which stores start and
   *     end indexes and an attribute this range has
   */
  private static List<FieldContainer> findFields(AttributedCharacterIterator iterator) {
    List<FieldContainer> result = new ArrayList<FieldContainer>();
    while (iterator.getIndex() != iterator.getEndIndex()) {
      int start = iterator.getRunStart();
      int end = iterator.getRunLimit();

      Iterator it = iterator.getAttributes().keySet().iterator();
      while (it.hasNext()) {
        AttributedCharacterIterator.Attribute attribute =
            (AttributedCharacterIterator.Attribute) it.next();
        Object value = iterator.getAttribute(attribute);
        result.add(new FieldContainer(start, end, attribute, value));
        // System.out.println(start + " " + end + ": " + attribute + ",
        // " + value );
        // System.out.println("v.add(new FieldContainer(" + start +"," +
        // end +"," + attribute+ "," + value+ "));");
      }
      iterator.setIndex(end);
    }
    return result;
  }
Beispiel #3
0
  /**
   * ************************************************************************ Return a Iterator with
   * only the relevant attributes. Fixes implementation in AttributedString, which returns
   * everything
   *
   * @param aString attributed string
   * @param relevantAttributes relevant attributes
   * @return iterator
   */
  public static AttributedCharacterIterator getIterator(
      AttributedString aString, AttributedCharacterIterator.Attribute[] relevantAttributes) {
    AttributedCharacterIterator iter = aString.getIterator();
    Set set = iter.getAllAttributeKeys();
    //	System.out.println("AllAttributeKeys=" + set);
    if (set.size() == 0) return iter;
    //	Check, if there are unwanted attributes
    Set<AttributedCharacterIterator.Attribute> unwanted =
        new HashSet<AttributedCharacterIterator.Attribute>(iter.getAllAttributeKeys());
    for (int i = 0; i < relevantAttributes.length; i++) unwanted.remove(relevantAttributes[i]);
    if (unwanted.size() == 0) return iter;

    //	Create new String
    StringBuffer sb = new StringBuffer();
    for (char c = iter.first(); c != AttributedCharacterIterator.DONE; c = iter.next())
      sb.append(c);
    aString = new AttributedString(sb.toString());

    //	copy relevant attributes
    Iterator it = iter.getAllAttributeKeys().iterator();
    while (it.hasNext()) {
      AttributedCharacterIterator.Attribute att = (AttributedCharacterIterator.Attribute) it.next();
      if (!unwanted.contains(att)) {
        for (char c = iter.first(); c != AttributedCharacterIterator.DONE; c = iter.next()) {
          Object value = iter.getAttribute(att);
          if (value != null) {
            int start = iter.getRunStart(att);
            int limit = iter.getRunLimit(att);
            //	System.out.println("Attribute=" + att + " Value=" + value + " Start=" + start + "
            // Limit=" + limit);
            aString.addAttribute(att, value, start, limit);
            iter.setIndex(limit);
          }
        }
      }
      //	else
      //		System.out.println("Unwanted: " + att);
    }
    return aString.getIterator();
  } //	getIterator