001: /*
002: * $Id: MessageComponent.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.faces.component;
023:
024: import javax.faces.component.UIOutput;
025: import javax.faces.context.FacesContext;
026: import javax.faces.el.ValueBinding;
027:
028: /**
029: * <p>Custom component that replaces the Struts
030: * <code><html:message></code> tag.</p>
031: */
032:
033: public class MessageComponent extends UIOutput {
034:
035: // ------------------------------------------------------------ Constructors
036:
037: /**
038: * <p>Create a new {@link MessageComponent} with default properties.</p>
039: */
040: public MessageComponent() {
041:
042: super ();
043: setRendererType("org.apache.struts.faces.Message");
044:
045: }
046:
047: // ------------------------------------------------------ Instance Variables
048:
049: /**
050: * <p>MessageResources attribute key to use for message lookup.</p>
051: */
052: private String bundle = null;
053:
054: /**
055: * <p>Flag indicating whether output should be filtered.</p>
056: */
057: private boolean filter = true;
058: private boolean filterSet = false;
059:
060: /**
061: * <p>Message key to use for message lookup.</p>
062: */
063: private String key = null;
064:
065: /**
066: * <p>CSS style(s) to be rendered for this component.</p>
067: */
068: private String style = null;
069:
070: /**
071: * <p>CSS style class(es) to be rendered for this component.</p>
072: */
073: private String styleClass = null;
074:
075: // ---------------------------------------------------- Component Properties
076:
077: /**
078: * <p>Return the MessageResources key.</p>
079: */
080: public String getBundle() {
081:
082: ValueBinding vb = getValueBinding("bundle");
083: if (vb != null) {
084: return (String) vb.getValue(getFacesContext());
085: } else {
086: return bundle;
087: }
088:
089: }
090:
091: /**
092: * <p>Set the MessageResources key.</p>
093: *
094: * @param bundle The new key
095: */
096: public void setBundle(String bundle) {
097:
098: this .bundle = bundle;
099:
100: }
101:
102: /**
103: * <p>Return the component family to which this component belongs.</p>
104: */
105: public String getFamily() {
106:
107: return "org.apache.struts.faces.Message";
108:
109: }
110:
111: /**
112: * <p>Return a flag indicating whether filtering should take place.</p>
113: */
114: public boolean isFilter() {
115:
116: if (filterSet) {
117: return filter;
118: }
119: ValueBinding vb = getValueBinding("filter");
120: if (vb != null) {
121: Boolean value = (Boolean) vb.getValue(getFacesContext());
122: if (null == value) {
123: return filter;
124: }
125: return value.booleanValue();
126: } else {
127: return filter;
128: }
129:
130: }
131:
132: /**
133: * <p>Set the flag indicating that the output value should be filtered.</p>
134: *
135: * @param filter The new filter flag
136: */
137: public void setFilter(boolean filter) {
138:
139: this .filter = filter;
140: this .filterSet = true;
141:
142: }
143:
144: /**
145: * <p>Return the message key.</p>
146: */
147: public String getKey() {
148:
149: ValueBinding vb = getValueBinding("key");
150: if (vb != null) {
151: return (String) vb.getValue(getFacesContext());
152: } else {
153: return key;
154: }
155:
156: }
157:
158: /**
159: * <p>Set the message key.</p>
160: *
161: * @param key The new key
162: */
163: public void setKey(String key) {
164:
165: this .key = key;
166:
167: }
168:
169: /**
170: * <p>Return the CSS style(s) to be rendered for this component.</p>
171: */
172: public String getStyle() {
173:
174: ValueBinding vb = getValueBinding("style");
175: if (vb != null) {
176: return (String) vb.getValue(getFacesContext());
177: } else {
178: return style;
179: }
180:
181: }
182:
183: /**
184: * <p>Set the CSS style(s) to be rendered for this component.</p>
185: *
186: * @param style The new CSS style(s)
187: */
188: public void setStyle(String style) {
189:
190: this .style = style;
191:
192: }
193:
194: /**
195: * <p>Return the CSS style class(es) to be rendered for this component.</p>
196: */
197: public String getStyleClass() {
198:
199: ValueBinding vb = getValueBinding("styleClass");
200: if (vb != null) {
201: return (String) vb.getValue(getFacesContext());
202: } else {
203: return styleClass;
204: }
205:
206: }
207:
208: /**
209: * <p>Set the CSS style class(es) to be rendered for this component.</p>
210: *
211: * @param styleClass The new CSS style class(es)
212: */
213: public void setStyleClass(String styleClass) {
214:
215: this .styleClass = styleClass;
216:
217: }
218:
219: // ---------------------------------------------------- StateManager Methods
220:
221: /**
222: * <p>Restore the state of this component.</p>
223: *
224: * @param context <code>FacesContext</code> for the current request
225: * @param state State object from which to restore our state
226: */
227: public void restoreState(FacesContext context, Object state) {
228:
229: Object values[] = (Object[]) state;
230: super .restoreState(context, values[0]);
231: bundle = (String) values[1];
232: filter = ((Boolean) values[2]).booleanValue();
233: filterSet = ((Boolean) values[3]).booleanValue();
234: key = (String) values[4];
235: style = (String) values[5];
236: styleClass = (String) values[6];
237:
238: }
239:
240: /**
241: * <p>Save the state of this component.</p>
242: *
243: * @param context <code>FacesContext</code> for the current request
244: */
245: public Object saveState(FacesContext context) {
246:
247: Object values[] = new Object[7];
248: values[0] = super .saveState(context);
249: values[1] = bundle;
250: values[2] = filter ? Boolean.TRUE : Boolean.FALSE;
251: values[3] = filterSet ? Boolean.TRUE : Boolean.FALSE;
252: values[4] = key;
253: values[5] = style;
254: values[6] = styleClass;
255: return values;
256:
257: }
258:
259: }
|