예제 #1
0
  public void check(String name, String in, String[] out, BreakIterator bi, TestHarness harness) {
    harness.checkPoint(name);
    bi.setText(in);

    int index = 0;
    int from = bi.current();
    harness.check(from, 0);

    while (true) {
      int to = bi.next();
      if (to == BreakIterator.DONE) break;
      harness.check(in.substring(from, to), out[index]);
      ++index;
      from = to;
    }

    harness.check(index, out.length);

    harness.checkPoint("backwards " + name);
    bi.last();
    index = out.length - 1;
    from = bi.current();
    harness.check(from, in.length());

    while (true) {
      int to = bi.previous();
      if (to == BreakIterator.DONE) break;
      harness.check(in.substring(to, from), out[index]);
      --index;
      from = to;
    }

    harness.check(index, -1);
  }
예제 #2
0
 /**
  * set / update the text of the displayLabels. these are the Week column headers above the days on
  * the Calendar part of the <code>CDateTime</code>.
  */
 private void updateDaysOfWeek() {
   if (dayPanel != null) {
     Calendar tmpcal = cdt.getCalendarInstance();
     tmpcal.set(Calendar.DAY_OF_WEEK, tmpcal.getFirstDayOfWeek());
     Locale locale = cdt.getLocale();
     boolean ltr =
         (ComponentOrientation.getOrientation(locale).isLeftToRight()
             && !locale.getLanguage().equals("zh")); // $NON-NLS-1$
     BreakIterator iterator = BreakIterator.getCharacterInstance(locale);
     for (int x = 0; x < dayLabels.length; x++) {
       String str = getFormattedDate("E", tmpcal.getTime()); // $NON-NLS-1$
       if (dayLabels[x].getData(CDT.Key.Compact, Boolean.class)) {
         iterator.setText(str);
         int start, end;
         if (ltr) {
           start = iterator.first();
           end = iterator.next();
         } else {
           end = iterator.last();
           start = iterator.previous();
         }
         dayLabels[x].setText(str.substring(start, end));
       } else {
         dayLabels[x].setText(str);
       }
       tmpcal.add(Calendar.DAY_OF_WEEK, 1);
     }
   }
 }
예제 #3
0
  public static String wordWrap(String input, int width, Locale locale) {
    if (input == null) return "";
    if (width < 5) return input;
    if (width >= input.length()) return input;
    StringBuffer buf = new StringBuffer(input);
    boolean endOfLine = false;
    int lineStart = 0;
    for (int i = 0; i < buf.length(); i++) {
      if (buf.charAt(i) == '\n') {
        lineStart = i + 1;
        endOfLine = true;
      }
      if (i <= (lineStart + width) - 1) continue;
      if (!endOfLine) {
        int limit = i - lineStart - 1;
        BreakIterator breaks = BreakIterator.getLineInstance(locale);
        breaks.setText(buf.substring(lineStart, i));
        int end = breaks.last();
        if (end == limit + 1 && !Character.isWhitespace(buf.charAt(lineStart + end)))
          end = breaks.preceding(end - 1);
        if (end != -1 && end == limit + 1) {
          buf.replace(lineStart + end, lineStart + end + 1, "\n");
          lineStart += end;
          continue;
        }
        if (end != -1 && end != 0) {
          buf.insert(lineStart + end, '\n');
          lineStart = lineStart + end + 1;
        } else {
          buf.insert(i, '\n');
          lineStart = i + 1;
        }
      } else {
        buf.insert(i, '\n');
        lineStart = i + 1;
        endOfLine = false;
      }
    }

    return buf.toString();
  }
예제 #4
0
  public static void main(String[] args) {
    BreakIterator bi = BreakIterator.getWordInstance();
    bi.setText(text);
    MirroredBreakIterator mirror = new MirroredBreakIterator(bi);
    final int first = bi.first();
    if (first != 0) {
      throw new RuntimeException("first != 0: " + first);
    }
    final int last = bi.last();
    bi = BreakIterator.getWordInstance();
    bi.setText(text);
    int length = text.length();

    /*
     * following(int)
     */
    for (int i = 0; i <= length; i++) {
      if (i == length) {
        check(bi.following(i), DONE);
      }
      check(bi.following(i), mirror.following(i));
      check(bi.current(), mirror.current());
    }
    for (int i = -length; i < 0; i++) {
      checkFollowingException(bi, i);
      checkFollowingException(mirror, i);
      check(bi.current(), mirror.current());
    }
    for (int i = 1; i < length; i++) {
      checkFollowingException(bi, length + i);
      checkFollowingException(mirror, length + i);
      check(bi.current(), mirror.current());
    }

    /*
     * preceding(int)
     */
    for (int i = length; i >= 0; i--) {
      if (i == 0) {
        check(bi.preceding(i), DONE);
      }
      check(bi.preceding(i), mirror.preceding(i));
      check(bi.current(), mirror.current());
    }
    for (int i = -length; i < 0; i++) {
      checkPrecedingException(bi, i);
      checkPrecedingException(mirror, i);
      check(bi.current(), mirror.current());
    }
    for (int i = 1; i < length; i++) {
      checkPrecedingException(bi, length + i);
      checkPrecedingException(mirror, length + i);
      check(bi.current(), mirror.current());
    }

    /*
     * isBoundary(int)
     */
    for (int i = 0; i <= length; i++) {
      check(bi.isBoundary(i), mirror.isBoundary(i));
      check(bi.current(), mirror.current());
    }
    for (int i = -length; i < 0; i++) {
      checkIsBoundaryException(bi, i);
      checkIsBoundaryException(mirror, i);
    }
    for (int i = 1; i < length; i++) {
      checkIsBoundaryException(bi, length + i);
      checkIsBoundaryException(mirror, length + i);
    }
  }
예제 #5
0
  /**
   * Reformats a string where lines that are longer than <tt>width</tt> are split apart at the
   * earliest wordbreak or at maxLength, whichever is sooner. If the width specified is less than 5
   * or greater than the input Strings length the string will be returned as is.
   *
   * <p>Please note that this method can be lossy - trailing spaces on wrapped lines may be trimmed.
   *
   * @param input the String to reformat.
   * @param width the maximum length of any one line.
   * @return a new String with reformatted as needed.
   */
  public static String wordWrap(String input, int width, Locale locale) {
    // protect ourselves
    if (input == null) {
      return "";
    } else if (width < 5) {
      return input;
    } else if (width >= input.length()) {
      return input;
    }

    // default locale
    if (locale == null) {
      locale = JiveGlobals.getLocale();
    }

    StringBuilder buf = new StringBuilder(input);
    boolean endOfLine = false;
    int lineStart = 0;

    for (int i = 0; i < buf.length(); i++) {
      if (buf.charAt(i) == '\n') {
        lineStart = i + 1;
        endOfLine = true;
      }

      // handle splitting at width character
      if (i > lineStart + width - 1) {
        if (!endOfLine) {
          int limit = i - lineStart - 1;
          BreakIterator breaks = BreakIterator.getLineInstance(locale);
          breaks.setText(buf.substring(lineStart, i));
          int end = breaks.last();

          // if the last character in the search string isn't a space,
          // we can't split on it (looks bad). Search for a previous
          // break character
          if (end == limit + 1) {
            if (!Character.isWhitespace(buf.charAt(lineStart + end))) {
              end = breaks.preceding(end - 1);
            }
          }

          // if the last character is a space, replace it with a \n
          if (end != BreakIterator.DONE && end == limit + 1) {
            buf.replace(lineStart + end, lineStart + end + 1, "\n");
            lineStart = lineStart + end;
          }
          // otherwise, just insert a \n
          else if (end != BreakIterator.DONE && end != 0) {
            buf.insert(lineStart + end, '\n');
            lineStart = lineStart + end + 1;
          } else {
            buf.insert(i, '\n');
            lineStart = i + 1;
          }
        } else {
          buf.insert(i, '\n');
          lineStart = i + 1;
          endOfLine = false;
        }
      }
    }

    return buf.toString();
  }