001: package nl.knowlogy.validation.jsf.component.html;
002:
003: import java.util.List;
004:
005: import javax.faces.application.FacesMessage;
006: import javax.faces.component.UIMessages;
007: import javax.faces.context.FacesContext;
008: import javax.faces.el.ValueBinding;
009:
010: import nl.knowlogy.validation.Message;
011: import nl.knowlogy.validation.MessageType;
012: import nl.knowlogy.validation.Messages;
013: import nl.knowlogy.validation.jsf.MessagesUtil;
014: import nl.knowlogy.validation.jsf.utils.JsfUtils;
015:
016: import org.apache.log4j.Logger;
017:
018: public class ValidationHtmlMessages extends UIMessages {
019:
020: private static final Logger log = Logger
021: .getLogger(ValidationHtmlMessages.class);
022:
023: public static final String COMPONENT_TYPE = "nl.knowlogy.validation.ValidationHtmlMessages";
024: public static final String DEFAULT_RENDERER_TYPE = "nl.knowlogy.validation.ValidationHtmlMessages";
025: private static final String DEFAULT_LAYOUT = "list";
026: private static final boolean DEFAULT_TOOLTIP = false;
027:
028: private String _errorClass = null;
029: private String _errorStyle = null;
030: private String _fatalClass = null;
031: private String _fatalStyle = null;
032: private String _infoClass = null;
033: private String _infoStyle = null;
034: private String _layout = null;
035: private String _style = null;
036: private String _styleClass = null;
037: private String _title = null;
038: private Boolean _tooltip = null;
039: private String _warnClass = null;
040: private String _warnStyle = null;
041: private Object forObject = null;
042:
043: public ValidationHtmlMessages() {
044: setRendererType(DEFAULT_RENDERER_TYPE);
045: }
046:
047: public void setForObject(Object forObject) {
048: this .forObject = forObject;
049: }
050:
051: public Object getForObject() {
052: if (forObject != null)
053: return forObject;
054: ValueBinding vb = getValueBinding("forObject");
055: return vb != null ? vb.getValue(getFacesContext()) : null;
056: }
057:
058: public void setErrorClass(String errorClass) {
059: _errorClass = errorClass;
060: }
061:
062: public String getErrorClass() {
063: if (_errorClass != null)
064: return _errorClass;
065: ValueBinding vb = getValueBinding("errorClass");
066: return vb != null ? JsfUtils.getStringValue(getFacesContext(),
067: vb) : null;
068: }
069:
070: public void setErrorStyle(String errorStyle) {
071: _errorStyle = errorStyle;
072: }
073:
074: public String getErrorStyle() {
075: if (_errorStyle != null)
076: return _errorStyle;
077: ValueBinding vb = getValueBinding("errorStyle");
078: return vb != null ? JsfUtils.getStringValue(getFacesContext(),
079: vb) : null;
080: }
081:
082: public void setFatalClass(String fatalClass) {
083: _fatalClass = fatalClass;
084: }
085:
086: public String getFatalClass() {
087: if (_fatalClass != null)
088: return _fatalClass;
089: ValueBinding vb = getValueBinding("fatalClass");
090: return vb != null ? JsfUtils.getStringValue(getFacesContext(),
091: vb) : null;
092: }
093:
094: public void setFatalStyle(String fatalStyle) {
095: _fatalStyle = fatalStyle;
096: }
097:
098: public String getFatalStyle() {
099: if (_fatalStyle != null)
100: return _fatalStyle;
101: ValueBinding vb = getValueBinding("fatalStyle");
102: return vb != null ? JsfUtils.getStringValue(getFacesContext(),
103: vb) : null;
104: }
105:
106: public void setInfoClass(String infoClass) {
107: _infoClass = infoClass;
108: }
109:
110: public String getInfoClass() {
111: if (_infoClass != null)
112: return _infoClass;
113: ValueBinding vb = getValueBinding("infoClass");
114: return vb != null ? JsfUtils.getStringValue(getFacesContext(),
115: vb) : null;
116: }
117:
118: public void setInfoStyle(String infoStyle) {
119: _infoStyle = infoStyle;
120: }
121:
122: public String getInfoStyle() {
123: if (_infoStyle != null)
124: return _infoStyle;
125: ValueBinding vb = getValueBinding("infoStyle");
126: return vb != null ? JsfUtils.getStringValue(getFacesContext(),
127: vb) : null;
128: }
129:
130: public void setLayout(String layout) {
131: _layout = layout;
132: }
133:
134: public String getLayout() {
135: if (_layout != null)
136: return _layout;
137: ValueBinding vb = getValueBinding("layout");
138: return vb != null ? JsfUtils.getStringValue(getFacesContext(),
139: vb) : DEFAULT_LAYOUT;
140: }
141:
142: public void setStyle(String style) {
143: _style = style;
144: }
145:
146: public String getStyle() {
147: if (_style != null)
148: return _style;
149: ValueBinding vb = getValueBinding("style");
150: return vb != null ? JsfUtils.getStringValue(getFacesContext(),
151: vb) : null;
152: }
153:
154: public void setStyleClass(String styleClass) {
155: _styleClass = styleClass;
156: }
157:
158: public String getStyleClass() {
159: if (_styleClass != null)
160: return _styleClass;
161: ValueBinding vb = getValueBinding("styleClass");
162: return vb != null ? JsfUtils.getStringValue(getFacesContext(),
163: vb) : null;
164: }
165:
166: public void setTitle(String title) {
167: _title = title;
168: }
169:
170: public String getTitle() {
171: if (_title != null)
172: return _title;
173: ValueBinding vb = getValueBinding("title");
174: return vb != null ? JsfUtils.getStringValue(getFacesContext(),
175: vb) : null;
176: }
177:
178: public void setTooltip(boolean tooltip) {
179: _tooltip = Boolean.valueOf(tooltip);
180: }
181:
182: public boolean isTooltip() {
183: if (_tooltip != null)
184: return _tooltip.booleanValue();
185: ValueBinding vb = getValueBinding("tooltip");
186: Boolean v = vb != null ? (Boolean) vb
187: .getValue(getFacesContext()) : null;
188: return v != null ? v.booleanValue() : DEFAULT_TOOLTIP;
189: }
190:
191: public void setWarnClass(String warnClass) {
192: _warnClass = warnClass;
193: }
194:
195: public String getWarnClass() {
196: if (_warnClass != null)
197: return _warnClass;
198: ValueBinding vb = getValueBinding("warnClass");
199: return vb != null ? JsfUtils.getStringValue(getFacesContext(),
200: vb) : null;
201: }
202:
203: public void setWarnStyle(String warnStyle) {
204: _warnStyle = warnStyle;
205: }
206:
207: public String getWarnStyle() {
208:
209: if (_warnStyle != null)
210: return _warnStyle;
211: ValueBinding vb = getValueBinding("warnStyle");
212: return vb != null ? JsfUtils.getStringValue(getFacesContext(),
213: vb) : null;
214: }
215:
216: public Object saveState(FacesContext context) {
217: Object values[] = new Object[15];
218: values[0] = super .saveState(context);
219: values[1] = _errorClass;
220: values[2] = _errorStyle;
221: values[3] = _fatalClass;
222: values[4] = _fatalStyle;
223: values[5] = _infoClass;
224: values[6] = _infoStyle;
225: values[7] = _layout;
226: values[8] = _style;
227: values[9] = _styleClass;
228: values[10] = _title;
229: values[11] = _tooltip;
230: values[12] = _warnClass;
231: values[13] = _warnStyle;
232: values[14] = forObject;
233: return ((Object) (values));
234: }
235:
236: public void restoreState(FacesContext context, Object state) {
237: Object values[] = (Object[]) state;
238: super .restoreState(context, values[0]);
239: _errorClass = (String) values[1];
240: _errorStyle = (String) values[2];
241: _fatalClass = (String) values[3];
242: _fatalStyle = (String) values[4];
243: _infoClass = (String) values[5];
244: _infoStyle = (String) values[6];
245: _layout = (String) values[7];
246: _style = (String) values[8];
247: _styleClass = (String) values[9];
248: _title = (String) values[10];
249: _tooltip = (Boolean) values[11];
250: _warnClass = (String) values[12];
251: _warnStyle = (String) values[13];
252: forObject = values[14];
253: }
254:
255: public List getFacesMessages(FacesContext context) {
256: List messages = getValidationMessages(context);
257:
258: for (int i = 0; i < messages.size(); i++) {
259: Message message = (Message) messages.get(i);
260: FacesMessage facesMessage = convertMessage(message);
261: messages.set(i, facesMessage);
262: }
263: return messages;
264: }
265:
266: private FacesMessage convertMessage(Message message) {
267: FacesMessage.Severity serverity;
268: if (MessageType.ERROR.equals(message.getMessageType())) {
269: serverity = FacesMessage.SEVERITY_ERROR;
270: } else if (MessageType.INFO.equals(message.getMessageType())) {
271: serverity = FacesMessage.SEVERITY_INFO;
272: } else {
273: serverity = FacesMessage.SEVERITY_WARN;
274: }
275:
276: return new FacesMessage(serverity, message.getMessage(), null);
277: }
278:
279: public List getValidationMessages(FacesContext context) {
280:
281: ValueBinding valueBinding = context.getApplication()
282: .createValueBinding(MessagesUtil.VALUE_BINDING_NAME);
283:
284: Messages messages = (Messages) valueBinding.getValue(context);
285:
286: Object forObject = getForObject();
287: if (forObject != null) {
288: if (log.isDebugEnabled()) {
289: log.debug("Retrieved object " + forObject);
290: }
291:
292: return messages.getMessages(forObject, isGlobalOnly());
293:
294: } else {
295: return messages.getMessages();
296: }
297: }
298: }
|