01: package ru.emdev.EmForge.web.converter;
02:
03: import javax.faces.component.StateHolder;
04: import javax.faces.component.UIComponent;
05: import javax.faces.context.FacesContext;
06: import javax.faces.convert.Converter;
07:
08: import org.apache.commons.lang.ObjectUtils;
09:
10: /** This converter used for trimming string by specified length
11: *
12: * @author akakunin
13: *
14: */
15: public class TrimTextConverter implements Converter, StateHolder {
16: private Integer length;
17:
18: public Integer getLength() {
19: return length;
20: }
21:
22: public void setLength(Integer i_length) {
23: length = i_length;
24: }
25:
26: public Object getAsObject(FacesContext i_context,
27: UIComponent i_component, String i_value) {
28: // Not supported
29: return null;
30: }
31:
32: public String getAsString(FacesContext i_context,
33: UIComponent i_component, Object i_value) {
34: String strValue = ObjectUtils.toString(i_value);
35:
36: if (length != null && strValue.length() > length) {
37: strValue = strValue.substring(0, length - 3);
38: strValue += "...";
39: }
40:
41: return strValue;
42: }
43:
44: boolean m_transient;
45:
46: public boolean isTransient() {
47: return m_transient;
48: }
49:
50: public void setTransient(boolean i_newTransientValue) {
51: m_transient = i_newTransientValue;
52: }
53:
54: public void restoreState(FacesContext i_context, Object i_state) {
55: Object values[] = (Object[]) i_state;
56: length = (Integer) values[0];
57:
58: }
59:
60: public Object saveState(FacesContext i_context) {
61: Object values[] = new Object[1];
62: values[0] = length;
63:
64: return (values);
65: }
66:
67: }
|