/**
   * @tests java.text.AttributedString#getIterator(AttributedCharacterIterator.Attribute[], int,
   *     int) Test of method
   *     java.text.AttributedString#getIterator(AttributedCharacterIterator.Attribute[], int, int).
   */
  public void test_getIterator$Ljava_text_AttributedCharacterIterator$AttributeII() {
    String test = "Test string";
    try {
      Map<AttributedCharacterIterator.Attribute, String> hm =
          new HashMap<AttributedCharacterIterator.Attribute, String>();
      AttributedCharacterIterator.Attribute[] aci = new AttributedCharacterIterator.Attribute[3];
      aci[0] = new TestAttributedCharacterIteratorAttribute("att1");
      aci[1] = new TestAttributedCharacterIteratorAttribute("att2");
      aci[2] = new TestAttributedCharacterIteratorAttribute("att3");
      hm.put(aci[0], "value1");
      hm.put(aci[1], "value2");

      AttributedString attrString = new AttributedString(test);
      attrString.addAttributes(hm, 2, 4);
      AttributedCharacterIterator it = attrString.getIterator(aci, 1, 5);

      assertTrue("Incorrect iteration on AttributedString", it.getAttribute(aci[0]) == null);
      assertTrue("Incorrect iteration on AttributedString", it.getAttribute(aci[1]) == null);
      assertTrue("Incorrect iteration on AttributedString", it.getAttribute(aci[2]) == null);

      it.next();

      assertTrue(
          "Incorrect iteration on AttributedString", it.getAttribute(aci[0]).equals("value1"));
      assertTrue(
          "Incorrect iteration on AttributedString", it.getAttribute(aci[1]).equals("value2"));
      assertTrue("Incorrect iteration on AttributedString", it.getAttribute(aci[2]) == null);
    } catch (Exception e) {
      fail("Unexpected exceptiption " + e.toString());
    }
  }
  public List getTextRuns(TextNode node, AttributedCharacterIterator aci) {
    List textRuns = node.getTextRuns();
    if (textRuns != null) {
      return textRuns;
    }

    AttributedCharacterIterator[] chunkACIs = getTextChunkACIs(aci);
    textRuns = computeTextRuns(node, aci, chunkACIs);

    aci.first();
    List rgns = (List) aci.getAttribute(FLOW_REGIONS);

    if (rgns != null) {
      Iterator i = textRuns.iterator();
      List chunkLayouts = new ArrayList();
      TextRun tr = (TextRun) i.next();
      List layouts = new ArrayList();
      chunkLayouts.add(layouts);
      layouts.add(tr.getLayout());
      while (i.hasNext()) {
        tr = (TextRun) i.next();
        if (tr.isFirstRunInChunk()) {
          layouts = new ArrayList();
          chunkLayouts.add(layouts);
        }
        layouts.add(tr.getLayout());
      }

      FlowExtGlyphLayout.textWrapTextChunk(chunkACIs, chunkLayouts, rgns);
    }

    node.setTextRuns(textRuns);
    return textRuns;
  }
Beispiel #3
0
 protected void clicked(CharPos pos) {
   AttributedCharacterIterator inf = pos.part.ti();
   inf.setIndex(pos.ch.getCharIndex());
   FuckMeGentlyWithAChainsaw url =
       (FuckMeGentlyWithAChainsaw) inf.getAttribute(ChatAttribute.HYPERLINK);
   if ((url != null) && (WebBrowser.self != null)) WebBrowser.self.show(url.url);
 }
  /**
   * 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;
  }
        public int findMaxFontSize(AttributedCharacterIterator line, int defaultFontSize) {
          line.setIndex(0);
          Float maxFontSize = ZERO;
          int runLimit = 0;

          while (runLimit < line.getEndIndex()
              && (runLimit = line.getRunLimit(TextAttribute.SIZE)) <= line.getEndIndex()) {
            Float size = (Float) line.getAttribute(TextAttribute.SIZE);
            if (maxFontSize.compareTo(size) < 0) {
              maxFontSize = size;
            }
            line.setIndex(runLimit);
          }

          return maxFontSize.intValue();
        }
  /**
   * 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;
  }
  protected void dumpACIWord(AttributedString as) {
    if (as == null) return;

    StringBuffer chars = new StringBuffer();
    StringBuffer brkStr = new StringBuffer();
    AttributedCharacterIterator aci = as.getIterator();
    AttributedCharacterIterator.Attribute WORD_LIMIT = TextLineBreaks.WORD_LIMIT;

    for (char ch = aci.current(); ch != AttributedCharacterIterator.DONE; ch = aci.next()) {

      chars.append(ch).append(' ').append(' ');
      int w = ((Integer) aci.getAttribute(WORD_LIMIT)).intValue();
      brkStr.append(w).append(' ');
      if (w < 10) {
        // for small values append another ' '
        brkStr.append(' ');
      }
    }
    System.out.println(chars.toString());
    System.out.println(brkStr.toString());
  }
Beispiel #8
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
  /**
   * 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();
        }
      }
    }
  }
Beispiel #11
0
  /**
   * Updates the <code>TextMeasurer</code> after a single character has been inserted into the
   * paragraph currently represented by this <code>TextMeasurer</code>. After this call, this <code>
   * TextMeasurer</code> is equivalent to a new <code>TextMeasurer</code> created from the text;
   * however, it will usually be more efficient to update an existing <code>TextMeasurer</code> than
   * to create a new one from scratch.
   *
   * @param newParagraph the text of the paragraph after performing the insertion. Cannot be null.
   * @param insertPos the position in the text where the character was inserted. Must not be less
   *     than the start of <code>newParagraph</code>, and must be less than the end of <code>
   *     newParagraph</code>.
   * @throws IndexOutOfBoundsException if <code>insertPos</code> is less than the start of <code>
   *     newParagraph</code> or greater than or equal to the end of <code>newParagraph</code>
   * @throws NullPointerException if <code>newParagraph</code> is <code>null</code>
   */
  public void insertChar(AttributedCharacterIterator newParagraph, int insertPos) {

    if (collectStats) {
      printStats();
    }
    if (wantStats) {
      collectStats = true;
    }

    fStart = newParagraph.getBeginIndex();
    int end = newParagraph.getEndIndex();
    if (end - fStart != fChars.length + 1) {
      initAll(newParagraph);
    }

    char[] newChars = new char[end - fStart];
    int newCharIndex = insertPos - fStart;
    System.arraycopy(fChars, 0, newChars, 0, newCharIndex);

    char newChar = newParagraph.setIndex(insertPos);
    newChars[newCharIndex] = newChar;
    System.arraycopy(fChars, newCharIndex, newChars, newCharIndex + 1, end - insertPos - 1);
    fChars = newChars;

    if (fBidi != null
        || Bidi.requiresBidi(newChars, newCharIndex, newCharIndex + 1)
        || newParagraph.getAttribute(TextAttribute.BIDI_EMBEDDING) != null) {

      fBidi = new Bidi(newParagraph);
      if (fBidi.isLeftToRight()) {
        fBidi = null;
      }
    }

    fParagraph = StyledParagraph.insertChar(newParagraph, fChars, insertPos, fParagraph);
    invalidateComponents();
  }