コード例 #1
0
    private static CharSequence recreateView(
        String text,
        int caret,
        int selectionLength,
        boolean hasFocus,
        Highlighter... highlighters) {
      StringBuilder sb = new StringBuilder();
      sb.append("<pre>");

      ColorModel colorModel = new ColorModel(text.length() + 1);

      for (Highlighter highlighter : highlighters) {
        highlighter.highlight(text, caret, selectionLength, hasFocus, colorModel);
      }

      if (hasFocus) {
        if (selectionLength != 0) {
          colorModel.addStyle(caret, caret + selectionLength, "sel");
        } else {
          colorModel.addStyle(caret, "caret");
        }
      }

      for (int i = 0; i <= text.length(); ) {
        int next = colorModel.getNextDifferent(i);

        CharSequence style = colorModel.getStyle(i);
        if (style != null) {
          sb.append("<span class='").append(style).append("'>");
        }

        if (next == text.length() + 1) {
          sb.append(text, i, text.length());
          sb.append("&nbsp;");
        } else {
          sb.append(text, i, next);
        }

        if (style != null) {
          sb.append("</span>");
        }

        i = next;
      }

      sb.append("</pre>");

      return sb;
    }