001: /*
002: * GWT-Ext Widget Library
003: * Copyright(c) 2007-2008, GWT-Ext.
004: * licensing@gwt-ext.com
005: *
006: * http://www.gwt-ext.com/license
007: */
008:
009: package com.gwtext.client.util;
010:
011: /**
012: * Reusable data formatting functions.
013: */
014: public class Format {
015:
016: /**
017: * Parse a value into a formatted date using the specified format pattern. Format defaults to
018: * 'm/d/Y'.
019: *
020: * @param value the date string
021: * @return the formatted date string
022: */
023: public static native String date(String value)/*-{
024: return $wnd.Ext.util.Format.date(value);
025: }-*/;
026:
027: /**
028: * Parse a value into a formatted date using the specified format pattern.
029: *
030: * @param value the value to format
031: * @param format Any valid date format string (defaults to 'm/d/Y')
032: * @return the formatted date string
033: */
034: public static native String date(String value, String format)/*-{
035: return $wnd.Ext.util.Format.date(value, format);
036: }-*/;
037:
038: /**
039: * Truncate a string and add an ellipsis ('...') to the end if it exceeds the specified length.
040: *
041: * @param value the string to truncate
042: * @param length the maximum length to allow before truncating
043: * @return the converted text
044: */
045: public static native String ellipsis(String value, int length)/*-{
046: return $wnd.Ext.util.Format.ellipsis(value, length);
047: }-*/;
048:
049: /**
050: * Convert certain characters (&, <, >, and ') from their HTML character equivalents.
051: *
052: * @param value the string to decode
053: * @return the decoded text
054: */
055: public static native String htmlDecode(String value)/*-{
056: return $wnd.Ext.util.Format.htmlDecode(value);
057: }-*/;
058:
059: /**
060: * Convert certain characters (&, <, >, and ') to their HTML character equivalents for literal display in web pages.
061: *
062: * @param value the string to encode
063: * @return the encoded text
064: */
065: public static native String htmlEncode(String value)/*-{
066: return $wnd.Ext.util.Format.htmlEncode(value);
067: }-*/;
068:
069: /**
070: * Strips all HTML tags.
071: *
072: * @param value the text from which to strip tags
073: * @return the stripped text
074: */
075: public static native String stripTags(String value)/*-{
076: return $wnd.Ext.util.Format.stripTags(value);
077: }-*/;
078:
079: /**
080: * Strips all script tags.
081: *
082: * @param text the text from which to strip script tags
083: * @return the stripped text
084: */
085: public static native String stripScripts(String text)/*-{
086: return $wnd.Ext.util.Format.stripScripts(text);
087: }-*/;
088:
089: /**
090: * Simple format for a file size (xxx bytes, xxx KB, xxx MB).
091: *
092: * @param size the numeric value to format
093: * @return the formatted file size
094: */
095: public static native String fileSize(long size)/*-{
096: return $wnd.Ext.util.Format.fileSize(size);
097: }-*/;
098:
099: /**
100: * Format a number as US currency.
101: *
102: * @param value the value value to format
103: * @return the formatted currency string
104: */
105: public static native String usMoney(String value)/*-{
106: return $wnd.Ext.util.Format.usMoney(value);
107: }-*/;
108:
109: /**
110: * Format a number as US currency.
111: *
112: * @param value the numeric value to format
113: * @return the formatted currency string
114: */
115: public static native String usMoney(double value)/*-{
116: return $wnd.Ext.util.Format.usMoney(value);
117: }-*/;
118:
119: /**
120: * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each token must be unique, and must increment in the format {0}, {1}, etc.
121: *
122: * @param format the tokenized string to be formatted
123: * @param value the value to replace token {0}
124: * @return the formatted string
125: */
126: public static String format(String format, int value) {
127: return format(format, value + "");
128: }
129:
130: /**
131: * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each token must be unique, and must increment in the format {0}, {1}, etc.
132: *
133: * @param format the tokenized string to be formatted
134: * @param value the value to replace token {0}
135: * @return the formatted string
136: */
137: public static native String format(String format, String value) /*-{
138: return $wnd.String.format(format, value);
139: }-*/;
140:
141: //ext should be taking array of strings as secord argument like Template instead of varargs
142: //need hack becuase of varargs
143: /**
144: * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each token must be unique, and must increment in the format {0}, {1}, etc.
145: *
146: * @param format the tokenized string to be formatted
147: * @param values the value to replace token {0}, {1}, ...
148: * @return the formatted string
149: */
150: public static String format(String format, String[] values) {
151: switch (values.length) {
152: case 1:
153: return format(format, values[0]);
154: case 2:
155: return format(format, values[0], values[1]);
156: case 3:
157: return format(format, values[0], values[1], values[2]);
158: case 4:
159: return format(format, values[0], values[1], values[2],
160: values[3]);
161: case 5:
162: return format(format, values[0], values[1], values[2],
163: values[3], values[4]);
164: case 6:
165: return format(format, values[0], values[1], values[2],
166: values[3], values[4], values[5]);
167: case 7:
168: return format(format, values[0], values[1], values[2],
169: values[3], values[4], values[5], values[6]);
170: default:
171: return format(format, values[0], values[1], values[2],
172: values[3], values[4]);
173: }
174: }
175:
176: /**
177: * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each token must be unique, and must increment in the format {0}, {1}, etc.
178: *
179: * @param format the tokenized string to be formatted
180: * @param value1 the value to replace token {0}
181: * @param value2 the value to replace token {1}
182: * @return the formatted string
183: */
184: public static String format(String format, int value1, int value2) {
185: return format(format, value1 + "", value2 + "");
186: }
187:
188: /**
189: * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each token must be unique, and must increment in the format {0}, {1}, etc.
190: *
191: * @param format the tokenized string to be formatted
192: * @param value1 the value to replace token {0}
193: * @param value2 the value to replace token {1}
194: * @return the formatted string
195: */
196: public static native String format(String format, String value1,
197: String value2) /*-{
198: return $wnd.String.format(format, value1, value2);
199: }-*/;
200:
201: /**
202: * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each token must be unique, and must increment in the format {0}, {1}, etc.
203: *
204: * @param format the tokenized string to be formatted
205: * @param value1 the value to replace token {0}
206: * @param value2 the value to replace token {1}
207: * @param value3 the value to replace token {2}
208: * @return the formatted string
209: */
210: public static String format(String format, int value1, int value2,
211: int value3) {
212: return format(format, value1 + "", value2 + "", value3 + "");
213: }
214:
215: /**
216: * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each token must be unique, and must increment in the format {0}, {1}, etc.
217: *
218: * @param format the tokenized string to be formatted
219: * @param value1 the value to replace token {0}
220: * @param value2 the value to replace token {1}
221: * @param value3 the value to replace token {2}
222: * @return the formatted string
223: */
224: public static native String format(String format, String value1,
225: String value2, String value3) /*-{
226: return $wnd.String.format(format, value1, value2, value3);
227: }-*/;
228:
229: /**
230: * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each token must be unique, and must increment in the format {0}, {1}, etc.
231: *
232: * @param format the tokenized string to be formatted
233: * @param value1 the value to replace token {0}
234: * @param value2 the value to replace token {1}
235: * @param value3 the value to replace token {2}
236: * @param value4 the value to replace token {3}
237: * @return the formatted string
238: */
239: public static native String format(String format, String value1,
240: String value2, String value3, String value4) /*-{
241: return $wnd.String.format(format, value1, value2, value3, value4);
242: }-*/;
243:
244: /**
245: * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each token must be unique, and must increment in the format {0}, {1}, etc.
246: *
247: * @param format the tokenized string to be formatted
248: * @param value1 the value to replace token {0}
249: * @param value2 the value to replace token {1}
250: * @param value3 the value to replace token {2}
251: * @param value4 the value to replace token {3}
252: * @param value5 the value to replace token {4}
253: * @return the formatted string
254: */
255: public static native String format(String format, String value1,
256: String value2, String value3, String value4, String value5) /*-{
257: return $wnd.String.format(format, value1, value2, value3, value4, value5);
258: }-*/;
259:
260: /**
261: * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each token must be unique, and must increment in the format {0}, {1}, etc.
262: *
263: * @param format the tokenized string to be formatted
264: * @param value1 the value to replace token {0}
265: * @param value2 the value to replace token {1}
266: * @param value3 the value to replace token {2}
267: * @param value4 the value to replace token {3}
268: * @param value5 the value to replace token {4}
269: * @param value6 the value to replace token {5}
270: * @return the formatted string
271: */
272: public static native String format(String format, String value1,
273: String value2, String value3, String value4, String value5,
274: String value6) /*-{
275: return $wnd.String.format(format, value1, value2, value3, value4, value5, value6);
276: }-*/;
277:
278: /**
279: * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each token must be unique, and must increment in the format {0}, {1}, etc.
280: *
281: * @param format the tokenized string to be formatted
282: * @param value1 the value to replace token {0}
283: * @param value2 the value to replace token {1}
284: * @param value3 the value to replace token {2}
285: * @param value4 the value to replace token {3}
286: * @param value5 the value to replace token {4}
287: * @param value6 the value to replace token {5}
288: * @param value7 the value to replace token {6}
289: * @return the formatted string
290: */
291: public static native String format(String format, String value1,
292: String value2, String value3, String value4, String value5,
293: String value6, String value7) /*-{
294: return $wnd.String.format(format, value1, value2, value3, value4, value5, value6, value7);
295: }-*/;
296:
297: /**
298: * Pads the left side of a string with a specified character. This is especially useful for normalizing number and date strings.
299: * <p/>
300: * <pre>
301: * String val = Format.leftPad("123", 5, "0");
302: * //val now containts the String "00123"
303: * </pre>
304: *
305: * @param string the original string
306: * @param size the total length of the output string
307: * @return the padded string
308: */
309: public static native String leftPad(String string, int size) /*-{
310: return $wnd.String.leftPad(string, size);
311: }-*/;
312:
313: /**
314: * Pads the left side of a string with a specified character. This is especially useful for normalizing number and date strings.
315: * <p/>
316: * <pre>
317: * String val = Format.leftPad("123", 5, "0");
318: * //val now containts the String "00123"
319: * </pre>
320: *
321: * @param string the original string
322: * @param size the total length of the output string
323: * @param character he character with which to pad the original string (defaults to empty string " ")
324: * @return the padded string
325: */
326: public static native String leftPad(String string, int size,
327: String character) /*-{
328: return $wnd.String.leftPad(string, size, character);
329: }-*/;
330: }
|