Coverage Report - org.homeunix.thecave.buddi.plugin.api.util.TextFormatter
 
Classes in this File Line Coverage Branch Coverage Complexity
TextFormatter
37%
17/45
46%
15/32
1.682
 
 1  
 /*
 2  
  * Created on Apr 11, 2007 by wyatt
 3  
  */
 4  
 package org.homeunix.thecave.buddi.plugin.api.util;
 5  
 
 6  
 import java.text.DateFormat;
 7  
 import java.util.Date;
 8  
 
 9  
 import org.homeunix.thecave.buddi.model.Account;
 10  
 import org.homeunix.thecave.buddi.model.AccountType;
 11  
 import org.homeunix.thecave.buddi.model.BudgetCategory;
 12  
 import org.homeunix.thecave.buddi.model.Source;
 13  
 import org.homeunix.thecave.buddi.model.prefs.PrefsModel;
 14  
 import org.homeunix.thecave.buddi.plugin.api.model.ImmutableAccount;
 15  
 import org.homeunix.thecave.buddi.plugin.api.model.ImmutableAccountType;
 16  
 import org.homeunix.thecave.buddi.plugin.api.model.ImmutableBudgetCategory;
 17  
 import org.homeunix.thecave.buddi.plugin.api.model.ImmutableSource;
 18  
 import org.homeunix.thecave.buddi.plugin.api.model.ImmutableTransaction;
 19  
 import org.homeunix.thecave.buddi.util.Formatter;
 20  
 import org.homeunix.thecave.buddi.util.InternalFormatter;
 21  
 
 22  
 /**
 23  
  * Gives a common framework for formatting text.  It is recommended that all plugins which
 24  
  * display amounts, accounts, transactions, etc. use this class to ensure common formatting
 25  
  * is used for all areas of Buddi.
 26  
  * 
 27  
  * @author wyatt
 28  
  *
 29  
  */
 30  0
 public class TextFormatter {
 31  
         /**
 32  
          * Get the date format which the user has specified.  You can format dates
 33  
          * by using the getDateFormat().format(Date d) method.
 34  
          * @return
 35  
          */
 36  
         public static DateFormat getDateFormat(){
 37  0
                 return Formatter.getDateFormat(PrefsModel.getInstance().getDateFormat());
 38  
         }
 39  
 
 40  
         /**
 41  
          * Returns a formatted version of the given date.  A wrapper method 
 42  
          * for getDateFormat().format(date).
 43  
          * @param date
 44  
          * @return
 45  
          */
 46  
         public static String getFormattedDate(Date date){
 47  0
                 return getDateFormat().format(date);
 48  
         }
 49  
 
 50  
         /**
 51  
          * Return the translation for the given key.
 52  
          * @param key
 53  
          * @return
 54  
          */
 55  
         public static String getTranslation(String key){
 56  1211092
                 return PrefsModel.getInstance().getTranslator().get(key);
 57  
         }
 58  
 
 59  
         /**
 60  
          * Return the translation for the given key.
 61  
          * @param key
 62  
          * @return
 63  
          */
 64  
         public static String getTranslation(Enum<?> key){
 65  524536
                 return PrefsModel.getInstance().getTranslator().get(key);
 66  
         }
 67  
 
 68  
         public static String getHtmlWrapper(String htmlToWrap){
 69  33077
                 return "<html>" + htmlToWrap + "</html>";
 70  
         }
 71  
 
 72  
         /**
 73  
          * Converts a long value (in cents: 10000 == $100.00) to a string
 74  
          * with proper decimal values, with the user's desired currency
 75  
          * sign in the user's specified position (whether behind or in front
 76  
          * of the amount).  It is highly recommended that you use this method
 77  
          * to output monetary values, as it presents the user with a constant
 78  
          * look for currency.
 79  
          * 
 80  
          * @param value The currency amount, in cents (as per Buddi's internal 
 81  
          * representation of currency).  For instance, to represent the value
 82  
          * $123.45, you would pass in 12345.
 83  
          * @param red Wrap the return string in HTML font tags to make it red.
 84  
          * @param negate Multiply the value by *1 before rendering.  
 85  
          * @return A string with proper decimal places, plus the user's defined 
 86  
          * currency symbol in the correct position (whether before or after the
 87  
          * amount).  Optionally it will be wrapped in red font tags.
 88  
          */
 89  
         public static String getFormattedCurrency(long value, boolean isRed){
 90  28547
                 return getFormattedCurrency(value, isRed, false);
 91  
         }
 92  
 
 93  
         /**
 94  
          * Returns the given value formatted as a currency.
 95  
          * @param value
 96  
          * @return
 97  
          */
 98  
         public static String getFormattedCurrency(long value){
 99  28547
                 return getFormattedCurrency(value, value < 0);
 100  
         }
 101  
 
 102  
         /**
 103  
          * Converts a long value (in cents: 10000 == $100.00) to a string
 104  
          * with proper decimal values, with the user's desired currency
 105  
          * sign in the user's specified position (whether behind or in front
 106  
          * of the amount).  It is highly recommended that you use this method
 107  
          * to output monetary values, as it presents the user with a constant
 108  
          * look for currency.
 109  
          * 
 110  
          * Note that this method uses HTML font tags to set the color.  This
 111  
          * means that you must wrap all code which uses this with HTML start 
 112  
          * and end tags - you can use the getHtmlWrapper() method to do this.
 113  
          * 
 114  
          * @param value The currency amount, in cents (as per Buddi's internal 
 115  
          * representation of currency).  For instance, to represent the value
 116  
          * $123.45, you would pass in 12345.
 117  
          * @param isRed Wrap the return string in HTML font tags to make it red.
 118  
          * @param negate Multiply the value by *1 before rendering.  
 119  
          * @return A string with proper decimal places, plus the user's defined 
 120  
          * currency symbol in the correct position (whether before or after the
 121  
          * amount).  Optionally it will be wrapped in red font tags.
 122  
          */
 123  
         public static String getFormattedCurrency(long value, boolean isRed, boolean negate){
 124  28547
                 if (negate)
 125  0
                         value *= -1;
 126  
 
 127  28547
                 boolean symbolAfterAmount = PrefsModel.getInstance().isShowCurrencyAfterAmount();
 128  28547
                 String symbol = PrefsModel.getInstance().getCurrencySign();
 129  
 
 130  28547
                 if (PrefsModel.getInstance().isDontShowNegativeSign())
 131  4
                         value = Math.abs(value);
 132  
 
 133  28547
                 String formatted = 
 134  
                         (isRed ? "<font color='red'>" : "")
 135  
                         + (symbolAfterAmount ? "" : symbol)
 136  
                         + Formatter.getDecimalFormat().format((double) value / 100.0)  
 137  
                         + (symbolAfterAmount ? " " + symbol : "")
 138  
                         + (isRed ? "</font>" : "");
 139  
 
 140  28547
                 return formatted;
 141  
         }
 142  
 
 143  
         /**
 144  
          * Just like getFormattedCurrency, but uses a string builder (passed
 145  
          * as an argument) instead of creating a new string.  Should be faster.
 146  
          * 
 147  
          * Converts a long value (in cents: 10000 == $100.00) to a string
 148  
          * with proper decimal values, with the user's desired currency
 149  
          * sign in the user's specified position (whether behind or in front
 150  
          * of the amount).  It is highly recommended that you use this method
 151  
          * to output monetary values, as it presents the user with a constant
 152  
          * look for currency.
 153  
          * 
 154  
          * Note that this method uses HTML font tags to set the color.  This
 155  
          * means that you must wrap all code which uses this with HTML start 
 156  
          * and end tags - you can use the getHtmlWrapper() method to do this.
 157  
          * 
 158  
          * @param value The currency amount, in cents (as per Buddi's internal 
 159  
          * representation of currency).  For instance, to represent the value
 160  
          * $123.45, you would pass in 12345.
 161  
          * @param isRed Wrap the return string in HTML font tags to make it red.
 162  
          * @param negate Multiply the value by *1 before rendering.  
 163  
          * @return A string with proper decimal places, plus the user's defined 
 164  
          * currency symbol in the correct position (whether before or after the
 165  
          * amount).  Optionally it will be wrapped in red font tags.
 166  
          */
 167  
         public static void appendFormattedCurrency(StringBuilder sb, long value, boolean isRed, boolean negate){
 168  0
                 if (negate)
 169  0
                         value *= -1;
 170  
 
 171  0
                 boolean symbolAfterAmount = PrefsModel.getInstance().isShowCurrencyAfterAmount();
 172  
 
 173  0
                 if (isRed)
 174  0
                         sb.append("<font color='red'>");
 175  0
                 if (!symbolAfterAmount)
 176  0
                         sb.append(PrefsModel.getInstance().getCurrencySign());
 177  0
                 sb.append(Formatter.getDecimalFormat().format((double) value / 100.0));  
 178  0
                 if (symbolAfterAmount)
 179  0
                         sb.append(" ").append(PrefsModel.getInstance().getCurrencySign());
 180  0
                 if (isRed)
 181  0
                         sb.append("</font>");
 182  0
         }
 183  
 
 184  
         /**
 185  
          * Returns the name, formatted as a String.  This method complies
 186  
          * with the Buddi Formatting Standard, and should be used whenever
 187  
          * possible.
 188  
          * @param a
 189  
          * @return
 190  
          */
 191  
         public static String getFormattedNameForAccount(Account a){
 192  0
                 return getFormattedNameGeneric(a.getName(), a.getAccountType().isCredit());
 193  
         }
 194  
 
 195  
         /**
 196  
          * Returns the name, formatted as a String.  This method complies
 197  
          * with the Buddi Formatting Standard, and should be used whenever
 198  
          * possible.
 199  
          * @param c
 200  
          * @return
 201  
          */
 202  
         public static String getFormattedNameForCategory(BudgetCategory c){
 203  4530
                 return getFormattedNameGeneric(c.getName(), !c.isIncome());
 204  
         }
 205  
 
 206  
         /**
 207  
          * Returns the name, formatted as a String.  This method complies
 208  
          * with the Buddi Formatting Standard, and should be used whenever
 209  
          * possible.
 210  
          * @param t
 211  
          * @return
 212  
          */
 213  
         public static String getFormattedNameForType(AccountType t){
 214  0
                 return getFormattedNameGeneric(t.getName(), t.isCredit());
 215  
         }
 216  
 
 217  
         /**
 218  
          * Returns the given string, optionally wrapped in red font tags.
 219  
          * @param name
 220  
          * @param isRed
 221  
          * @return
 222  
          */
 223  
         public static String getFormattedNameGeneric(String name, boolean isRed){
 224  4530
                 String formatted = 
 225  
                         (isRed ? "<font color='red'>" : "")
 226  
                         + PrefsModel.getInstance().getTranslator().get(name)  
 227  
                         + (isRed ? "</font>" : "");
 228  
 
 229  4530
                 return formatted;
 230  
         }
 231  
 
 232  
         public static String getDeletedWrapper(String s, Source source){
 233  4530
                 if (source.isDeleted())
 234  0
                         return "<strike>" + s + "</strike>";
 235  4530
                 return s;
 236  
         }
 237  
 
 238  
         /**
 239  
          * Should the arguments be colored in red text when displayed?
 240  
          * @param s
 241  
          * @return
 242  
          */
 243  
         public static boolean isRed(ImmutableSource s){
 244  0
                 return InternalFormatter.isRed(s.getSource());
 245  
         }
 246  
 
 247  
         /**
 248  
          * Should the arguments be colored in red text when displayed?
 249  
          * @param s
 250  
          * @return
 251  
          */
 252  
         public static boolean isRed(ImmutableAccount a, long value){
 253  0
                 return InternalFormatter.isRed(a.getAccount(), value);
 254  
         }
 255  
 
 256  
         /**
 257  
          * Should the arguments be colored in red text when displayed?
 258  
          * @param s
 259  
          * @return
 260  
          */
 261  
         public static boolean isRed(ImmutableBudgetCategory c, long value){
 262  0
                 return InternalFormatter.isRed(c.getBudgetCategory(), value);
 263  
         }
 264  
 
 265  
         /**
 266  
          * Should the arguments be colored in red text when displayed?
 267  
          * @param s
 268  
          * @return
 269  
          */
 270  
         public static boolean isRed(ImmutableAccountType t){
 271  0
                 return InternalFormatter.isRed(t.getType());
 272  
         }
 273  
 
 274  
         /**
 275  
          * Should the arguments be colored in red text when displayed?
 276  
          * @param s
 277  
          * @return
 278  
          */
 279  
         public static boolean isRed(ImmutableAccountType t, long value){
 280  0
                 return InternalFormatter.isRed(t.getType(), value);
 281  
         }
 282  
 
 283  
         /**
 284  
          * Should the arguments be colored in red text when displayed?
 285  
          * @param s
 286  
          * @return
 287  
          */
 288  
         public static boolean isRed(ImmutableTransaction t){
 289  0
                 return InternalFormatter.isRed(t.getTransaction());
 290  
         }
 291  
 
 292  
         /**
 293  
          * Should the arguments be colored in red text when displayed?
 294  
          * @param s
 295  
          * @return
 296  
          */
 297  
         public static boolean isRed(ImmutableTransaction t, boolean toSelectedAccount){
 298  0
                 return InternalFormatter.isRed(t.getTransaction(), toSelectedAccount);
 299  
         }
 300  
 
 301  
         /**
 302  
          * Should the arguments be colored in red text when displayed?
 303  
          * @param s
 304  
          * @return
 305  
          */
 306  
         public static boolean isRed(long value){
 307  0
                 return InternalFormatter.isRed(value);
 308  
         }
 309  
 }