Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Formatter |
|
| 1.5555555555555556;1.556 | ||||
Formatter$StringLengthFormat |
|
| 1.5555555555555556;1.556 |
1 | /* | |
2 | * Created on May 9, 2006 by wyatt | |
3 | */ | |
4 | package org.homeunix.thecave.buddi.util; | |
5 | ||
6 | import java.awt.FontMetrics; | |
7 | import java.text.DateFormat; | |
8 | import java.text.DecimalFormat; | |
9 | import java.text.NumberFormat; | |
10 | import java.text.SimpleDateFormat; | |
11 | ||
12 | ||
13 | /** | |
14 | * A combination of many Formatting functions. | |
15 | * | |
16 | * @author wyatt | |
17 | * | |
18 | */ | |
19 | 0 | public class Formatter { |
20 | ||
21 | 2986 | private static StringLengthFormat lengthFormat = new StringLengthFormat(); |
22 | ||
23 | public static NumberFormat getDecimalFormat(){ | |
24 | 28547 | return getDecimalFormat(2); |
25 | } | |
26 | ||
27 | public static NumberFormat getDecimalFormat(int decimalPlaces){ | |
28 | 28571 | NumberFormat f = DecimalFormat.getInstance(); |
29 | 28571 | f.setMaximumFractionDigits(decimalPlaces); |
30 | 28571 | f.setMinimumFractionDigits(decimalPlaces); |
31 | ||
32 | 28571 | return f; |
33 | } | |
34 | ||
35 | public static DateFormat getDateFormat(){ | |
36 | 0 | return SimpleDateFormat.getInstance(); |
37 | } | |
38 | ||
39 | public static DateFormat getDateFormat(String format){ | |
40 | 0 | return new SimpleDateFormat(format); |
41 | } | |
42 | ||
43 | public static StringLengthFormat getStringLengthFormat(int length){ | |
44 | 4988 | lengthFormat.setLength(length); |
45 | 4988 | return lengthFormat; |
46 | } | |
47 | ||
48 | /** | |
49 | * Returns a string which will be at most maxPixelWidth pixels long as displayed by | |
50 | * the given FontMetrics object. If the string is any longer than that, it will | |
51 | * be cut off, and show '...' at the end. | |
52 | * @param value | |
53 | * @param maxPixelWidth | |
54 | * @param fm | |
55 | * @return | |
56 | */ | |
57 | public static String getStringToLength(String value, int maxPixelWidth, FontMetrics fm){ | |
58 | 4988 | if (fm == null) |
59 | 0 | return value; |
60 | int width; | |
61 | 4988 | for(width = 5; width <= value.length() && fm.stringWidth(value.substring(0, width)) < maxPixelWidth; width++); |
62 | 4988 | return getStringLengthFormat(width).format(value); |
63 | } | |
64 | ||
65 | // public static String getDecimalFormattedToLength(long value, int decimalPlaces, int maxPixelWidth, FontMetrics fm){ | |
66 | // int width; | |
67 | // String temp = getDecimalFormat(decimalPlaces).format(value); | |
68 | // for(width = 1; width <= temp.length() && fm.stringWidth(temp.substring(0, width)) < maxPixelWidth; width++); | |
69 | // temp = temp.substring(0, width).replaceAll("\\D", ""); | |
70 | // int sigDigs = temp.length(); | |
71 | // int totalDigs = (value + "").length(); | |
72 | // long newValue = value; | |
73 | // String unit = ""; | |
74 | // if ((totalDigs - 3) <= sigDigs){ | |
75 | // newValue = Math.round(value / (Math.pow(10, 3))); | |
76 | // unit = "k"; | |
77 | // } | |
78 | // else if ((totalDigs - 6) <= sigDigs){ | |
79 | // newValue = Math.round(value / (Math.pow(10, 6))); | |
80 | // unit = "m"; | |
81 | // } | |
82 | // else if ((totalDigs - 9) <= sigDigs){ | |
83 | // newValue = Math.round(value / (Math.pow(10, 9))); | |
84 | // unit = "b"; | |
85 | // } | |
86 | // else if ((totalDigs - 12) <= sigDigs){ | |
87 | // newValue = Math.round(value / (Math.pow(10, 12))); | |
88 | // unit = "t"; | |
89 | // } | |
90 | // | |
91 | // return getDecimalFormat(decimalPlaces).format(newValue) + unit; | |
92 | // } | |
93 | ||
94 | // /** | |
95 | // * Returns a Singleton instance of Formatter | |
96 | // * @return | |
97 | // */ | |
98 | // public static Formatter getInstance() { | |
99 | // return SingletonHolder.instance; | |
100 | // } | |
101 | // | |
102 | // /** | |
103 | // * You should call this the first time to correctly set the | |
104 | // * date format string. After, you can just call getInstance(). | |
105 | // * @param dateFormatString | |
106 | // * @return | |
107 | // */ | |
108 | // public static Formatter getInstance(String dateFormatString) { | |
109 | // SingletonHolder.dateFormatString = dateFormatString; | |
110 | // SingletonHolder.instance.setDateFormat(dateFormatString); | |
111 | // | |
112 | // return SingletonHolder.instance; | |
113 | // } | |
114 | // | |
115 | // private static class SingletonHolder { | |
116 | // private static String dateFormatString = "yyyy-MM-dd"; | |
117 | // private static Formatter instance = new Formatter(dateFormatString); | |
118 | // } | |
119 | // | |
120 | // private final NumberFormat decimalFormat; | |
121 | // private SimpleDateFormat dateFormat; | |
122 | // private final SimpleDateFormat shortDateFormat; | |
123 | // private final LengthFormat lengthFormat; | |
124 | // | |
125 | // private Formatter(String dateFormatString){ | |
126 | // decimalFormat = DecimalFormat.getInstance(); | |
127 | // decimalFormat.setMaximumFractionDigits(2); | |
128 | // decimalFormat.setMinimumFractionDigits(2); | |
129 | // | |
130 | // shortDateFormat = new SimpleDateFormat("MM/dd"); | |
131 | // | |
132 | // setDateFormat(dateFormatString); | |
133 | // | |
134 | // lengthFormat = new LengthFormat(); | |
135 | // } | |
136 | // | |
137 | // /** | |
138 | // * Forces a reload of the DateFormat object | |
139 | // * @param dateFormatString | |
140 | // */ | |
141 | // public void setDateFormat(String dateFormatString){ | |
142 | // dateFormat = new SimpleDateFormat(dateFormatString); | |
143 | // } | |
144 | // | |
145 | // /** | |
146 | // * Returns the DecimalFormat object | |
147 | // * @return | |
148 | // */ | |
149 | // public NumberFormat getDecimalFormat(){ | |
150 | // return decimalFormat; | |
151 | // } | |
152 | // | |
153 | // /** | |
154 | // * Returns the DateFormat object | |
155 | // * @return | |
156 | // */ | |
157 | // public SimpleDateFormat getDateFormat(){ | |
158 | // return dateFormat; | |
159 | // } | |
160 | // | |
161 | // /** | |
162 | // * Returns the LengthFormat object, using specified length | |
163 | // * @param length | |
164 | // * @return | |
165 | // */ | |
166 | // public LengthFormat getLengthFormat(int length){ | |
167 | // lengthFormat.setLength(length); | |
168 | // return lengthFormat; | |
169 | // } | |
170 | // | |
171 | // /** | |
172 | // * Returns the ShortDateFormat object (for formatting dates when | |
173 | // * space is at a premium) | |
174 | // * @return | |
175 | // */ | |
176 | // public SimpleDateFormat getShortDateFormat(){ | |
177 | // return shortDateFormat; | |
178 | // } | |
179 | // | |
180 | /** | |
181 | * A formatter-style class which cuts strings off after a given length. | |
182 | * @author wyatt | |
183 | * | |
184 | */ | |
185 | 0 | public static class StringLengthFormat{ |
186 | private int length; | |
187 | ||
188 | public String format(Object o){ | |
189 | 4988 | StringBuffer sb = new StringBuffer(); |
190 | 4988 | sb.append(o.toString()); |
191 | ||
192 | 4988 | if (length < sb.toString().length()){ |
193 | 0 | sb.delete(length, sb.length()); |
194 | 0 | sb.append("..."); |
195 | } | |
196 | ||
197 | 4988 | return sb.toString(); |
198 | } | |
199 | ||
200 | public int getLength() { | |
201 | 0 | return length; |
202 | } | |
203 | ||
204 | public void setLength(int length) { | |
205 | 4988 | this.length = length; |
206 | 4988 | } |
207 | } | |
208 | } |