Clover coverage report - clover
Coverage timestamp: Sat Oct 8 2005 22:54:17 EDT
file stats: LOC: 121   Methods: 4
NCLOC: 97   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
TextFormat.java 68.4% 85.7% 75% 78.6%
coverage coverage
 1   
 package abbot.editor.widgets;
 2   
 
 3   
 import abbot.i18n.Strings;
 4   
 
 5   
 /** Provides text formatting utilities. */
 6   
 
 7   
 public class TextFormat {
 8   
     public static final int TOOLTIP_WRAP = 50;
 9   
     public static final int DIALOG_WRAP = 60;
 10   
 
 11   
     /** Turns "SomeRunTogetherWords" into "Some Run Together Words". */
 12  62
     public static String wordBreak(String phrase) {
 13  62
         StringBuffer words = new StringBuffer(phrase);
 14  62
         for (int i=0;i < words.length()-1;i++) {
 15  564
             char up = words.charAt(i+1);
 16  564
             if (Character.isUpperCase(up)
 17   
                 && (i == words.length()-2
 18   
                     || Character.isLowerCase(words.charAt(i+2)))) {
 19  64
                 words.insert(++i, ' ');
 20   
             }
 21   
         }
 22  62
         return words.toString();
 23   
     }
 24   
 
 25   
     /** Wrap the given text at the given number of characters per line.
 26   
         Whitespace may be compressed.
 27   
      */
 28  15
     public static String wordWrap(String msg, int wrapAt, String lineSep) {
 29  15
         if (msg == null)
 30  0
             return null;
 31   
         
 32  15
         int len = msg.length();
 33  15
         StringBuffer sb = new StringBuffer(len * 3 / 2);
 34  15
         int pos = 0;
 35  19
         while (pos < len) {
 36   
             // Trim leading whitespace
 37  19
             char ch;
 38  ?
             while (pos < len
 39   
                    && Character.isWhitespace(ch = msg.charAt(pos))) {
 40  7
                 if (ch == '\n') {
 41  0
                     sb.append(lineSep);
 42   
                 }
 43  7
                 ++pos;
 44   
             }
 45   
                    
 46   
             // Find the last whitespace prior to the wrap column
 47  19
             int lastWhite = -1;
 48  19
             boolean nonwhite = false;
 49  19
             int col = 0;
 50  19
             while (pos + col < len && col <= wrapAt) {
 51  ?
                 if (Character.isWhitespace(ch = msg.charAt(pos + col))) {
 52  30
                     if (lastWhite == -1 || nonwhite) {
 53  30
                         lastWhite = pos + col;
 54   
                     }
 55  30
                     if (ch == '\n') {
 56  0
                         break;
 57   
                     }
 58   
                 }
 59   
                 else {
 60  450
                     nonwhite = true;
 61   
                 }
 62  480
                 ++col;
 63   
             }
 64  19
             if (pos + col == len) {
 65   
                 // end of input
 66  14
                 while (pos < len) {
 67  335
                     sb.append(msg.charAt(pos));
 68  335
                     ++pos;
 69   
                 }
 70  14
                 break;
 71   
             }
 72  5
             else if (lastWhite != -1) {
 73   
                 // found whitespace, wrap there
 74  3
                 while (pos < lastWhite) {
 75  126
                     sb.append(msg.charAt(pos));
 76  126
                     ++pos;
 77   
                 }
 78   
             }
 79   
             else {
 80   
                 // no whitespace on the line; wrap at next whitespace
 81   
                 // or end of input
 82  2
                 while (pos < len) {
 83  21
                     ch = msg.charAt(pos);
 84  21
                     if (Character.isWhitespace(ch))
 85  1
                         break;
 86  20
                     sb.append(ch);                    
 87  20
                     ++pos;
 88   
                 }
 89  2
                 if (pos == len)
 90  1
                     break;
 91   
             }
 92  4
             sb.append(lineSep);
 93  4
             ++pos;
 94   
         }
 95   
 
 96  15
         return sb.toString();
 97   
     }
 98   
 
 99   
     /** Emit html, suitably line-wrapped and formatted for a tool tip. */
 100  12
     public static String tooltip(String tip) {
 101  12
         if (tip.startsWith("<html>")) {
 102  0
             tip = tip.substring(6, tip.length() - 7);
 103   
         }
 104   
         else {
 105  12
             tip = wordWrap(tip, TOOLTIP_WRAP, "<br>");
 106   
         }
 107  12
         return Strings.get("TooltipFormat", new Object[] { tip });
 108   
     }
 109   
 
 110   
     /** Emit html, suitably line-wrapped and formatted for a dialog. */
 111  0
     public static String dialog(String msg) {
 112  0
         if (msg.startsWith("<html>")) {
 113  0
             msg = msg.substring(6, msg.length() - 7);
 114   
         }
 115   
         else {
 116  0
             msg = wordWrap(msg, DIALOG_WRAP, "<br>");
 117   
         }
 118  0
         return Strings.get("DialogFormat", new Object[] { msg });
 119   
     }
 120   
 }
 121