001: package abbot.editor.widgets;
002:
003: import abbot.i18n.Strings;
004:
005: /** Provides text formatting utilities. */
006:
007: public class TextFormat {
008: public static final int TOOLTIP_WRAP = 50;
009: public static final int DIALOG_WRAP = 60;
010:
011: /** Turns "SomeRunTogetherWords" into "Some Run Together Words". */
012: public static String wordBreak(String phrase) {
013: StringBuffer words = new StringBuffer(phrase);
014: for (int i = 0; i < words.length() - 1; i++) {
015: char up = words.charAt(i + 1);
016: if (Character.isUpperCase(up)
017: && (i == words.length() - 2 || Character
018: .isLowerCase(words.charAt(i + 2)))) {
019: words.insert(++i, ' ');
020: }
021: }
022: return words.toString();
023: }
024:
025: /** Wrap the given text at the given number of characters per line.
026: Whitespace may be compressed.
027: */
028: public static String wordWrap(String msg, int wrapAt, String lineSep) {
029: if (msg == null)
030: return null;
031:
032: int len = msg.length();
033: StringBuffer sb = new StringBuffer(len * 3 / 2);
034: int pos = 0;
035: while (pos < len) {
036: // Trim leading whitespace
037: char ch;
038: while (pos < len
039: && Character.isWhitespace(ch = msg.charAt(pos))) {
040: if (ch == '\n') {
041: sb.append(lineSep);
042: }
043: ++pos;
044: }
045:
046: // Find the last whitespace prior to the wrap column
047: int lastWhite = -1;
048: boolean nonwhite = false;
049: int col = 0;
050: while (pos + col < len && col <= wrapAt) {
051: if (Character.isWhitespace(ch = msg.charAt(pos + col))) {
052: if (lastWhite == -1 || nonwhite) {
053: lastWhite = pos + col;
054: }
055: if (ch == '\n') {
056: break;
057: }
058: } else {
059: nonwhite = true;
060: }
061: ++col;
062: }
063: if (pos + col == len) {
064: // end of input
065: while (pos < len) {
066: sb.append(msg.charAt(pos));
067: ++pos;
068: }
069: break;
070: } else if (lastWhite != -1) {
071: // found whitespace, wrap there
072: while (pos < lastWhite) {
073: sb.append(msg.charAt(pos));
074: ++pos;
075: }
076: } else {
077: // no whitespace on the line; wrap at next whitespace
078: // or end of input
079: while (pos < len) {
080: ch = msg.charAt(pos);
081: if (Character.isWhitespace(ch))
082: break;
083: sb.append(ch);
084: ++pos;
085: }
086: if (pos == len)
087: break;
088: }
089: sb.append(lineSep);
090: ++pos;
091: }
092:
093: return sb.toString();
094: }
095:
096: /** Emit html, suitably line-wrapped and formatted for a tool tip. */
097: public static String tooltip(String tip) {
098: if (tip.startsWith("<html>")) {
099: tip = tip.substring(6, tip.length() - 7);
100: } else {
101: tip = wordWrap(tip, TOOLTIP_WRAP, "<br>");
102: }
103: return Strings.get("TooltipFormat", new Object[] { tip });
104: }
105:
106: /** Emit html, suitably line-wrapped and formatted for a dialog. */
107: public static String dialog(String msg) {
108: if (msg.startsWith("<html>")) {
109: msg = msg.substring(6, msg.length() - 7);
110: } else {
111: msg = wordWrap(msg, DIALOG_WRAP, "<br>");
112: }
113: return Strings.get("DialogFormat", new Object[] { msg });
114: }
115: }
|