View Javadoc

1   /*
2    * Copyright 2002-2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.struts.faces.component;
18  
19  
20  import javax.faces.component.UIOutput;
21  import javax.faces.context.FacesContext;
22  import javax.faces.el.ValueBinding;
23  
24  
25  /***
26   * <p>Custom component that replaces the Struts
27   * <code>&lt;html:message&gt;</code> tag.</p>
28   */
29  
30  public class MessageComponent extends UIOutput {
31  
32  
33      // ------------------------------------------------------------ Constructors
34  
35  
36      /***
37       * <p>Create a new {@link MessageComponent} with default properties.</p>
38       */
39      public MessageComponent() {
40  
41          super();
42          setRendererType("org.apache.struts.faces.Message");
43  
44      }
45  
46  
47      // ------------------------------------------------------ Instance Variables
48  
49  
50      /***
51       * <p>MessageResources attribute key to use for message lookup.</p>
52       */
53      private String bundle = null;
54  
55  
56      /***
57       * <p>Flag indicating whether output should be filtered.</p>
58       */
59      private boolean filter = true;
60      private boolean filterSet = false;
61  
62  
63      /***
64       * <p>Message key to use for message lookup.</p>
65       */
66      private String key = null;
67  
68  
69      /***
70       * <p>CSS style(s) to be rendered for this component.</p>
71       */
72      private String style = null;
73  
74  
75      /***
76       * <p>CSS style class(es) to be rendered for this component.</p>
77       */
78      private String styleClass = null;
79  
80  
81      // ---------------------------------------------------- Component Properties
82  
83  
84      /***
85       * <p>Return the MessageResources key.</p>
86       */
87      public String getBundle() {
88  
89          ValueBinding vb = getValueBinding("bundle");
90          if (vb != null) {
91              return (String) vb.getValue(getFacesContext());
92          } else {
93              return bundle;
94          }
95  
96      }
97  
98  
99      /***
100      * <p>Set the MessageResources key.</p>
101      *
102      * @param bundle The new key
103      */
104     public void setBundle(String bundle) {
105 
106         this.bundle = bundle;
107 
108     }
109 
110 
111     /***
112      * <p>Return the component family to which this component belongs.</p>
113      */
114     public String getFamily() {
115 
116         return "org.apache.struts.faces.Message";
117 
118     }
119 
120 
121     /***
122      * <p>Return a flag indicating whether filtering should take place.</p>
123      */
124     public boolean isFilter() {
125 
126         if (filterSet) {
127             return filter;
128         }
129         ValueBinding vb = getValueBinding("filter");
130         if (vb != null) {
131             Boolean value = (Boolean) vb.getValue(getFacesContext());
132             if (null == value) {
133                 return filter;
134             }
135             return value.booleanValue();
136         } else {
137             return filter;
138         }
139 
140     }
141 
142 
143     /***
144      * <p>Set the flag indicating that the output value should be filtered.</p>
145      *
146      * @param filter The new filter flag
147      */
148     public void setFilter(boolean filter) {
149 
150         this.filter = filter;
151         this.filterSet = true;
152 
153     }
154 
155 
156     /***
157      * <p>Return the message key.</p>
158      */
159     public String getKey() {
160 
161         ValueBinding vb = getValueBinding("key");
162         if (vb != null) {
163             return (String) vb.getValue(getFacesContext());
164         } else {
165             return key;
166         }
167 
168     }
169 
170 
171     /***
172      * <p>Set the message key.</p>
173      *
174      * @param key The new key
175      */
176     public void setKey(String key) {
177 
178         this.key = key;
179 
180     }
181 
182 
183     /***
184      * <p>Return the CSS style(s) to be rendered for this component.</p>
185      */
186     public String getStyle() {
187 
188         ValueBinding vb = getValueBinding("style");
189         if (vb != null) {
190             return (String) vb.getValue(getFacesContext());
191         } else {
192             return style;
193         }
194 
195     }
196 
197 
198     /***
199      * <p>Set the CSS style(s) to be rendered for this component.</p>
200      *
201      * @param style The new CSS style(s)
202      */
203     public void setStyle(String style) {
204 
205         this.style = style;
206 
207     }
208 
209 
210     /***
211      * <p>Return the CSS style class(es) to be rendered for this component.</p>
212      */
213     public String getStyleClass() {
214 
215         ValueBinding vb = getValueBinding("styleClass");
216         if (vb != null) {
217             return (String) vb.getValue(getFacesContext());
218         } else {
219             return styleClass;
220         }
221 
222     }
223 
224 
225     /***
226      * <p>Set the CSS style class(es) to be rendered for this component.</p>
227      *
228      * @param styleClass The new CSS style class(es)
229      */
230     public void setStyleClass(String styleClass) {
231 
232         this.styleClass = styleClass;
233 
234     }
235 
236 
237     // ---------------------------------------------------- StateManager Methods
238 
239 
240     /***
241      * <p>Restore the state of this component.</p>
242      *
243      * @param context <code>FacesContext</code> for the current request
244      * @param state State object from which to restore our state
245      */
246     public void restoreState(FacesContext context, Object state) {
247 
248         Object values[] = (Object[]) state;
249         super.restoreState(context, values[0]);
250         bundle = (String) values[1];
251         filter = ((Boolean) values[2]).booleanValue();
252         filterSet = ((Boolean) values[3]).booleanValue();
253         key = (String) values[4];
254         style = (String) values[5];
255         styleClass = (String) values[6];
256 
257     }
258 
259 
260     /***
261      * <p>Save the state of this component.</p>
262      *
263      * @param context <code>FacesContext</code> for the current request
264      */
265     public Object saveState(FacesContext context) {
266 
267         Object values[] = new Object[7];
268         values[0] = super.saveState(context);
269         values[1] = bundle;
270         values[2] = filter ? Boolean.TRUE : Boolean.FALSE;
271         values[3] = filterSet ? Boolean.TRUE : Boolean.FALSE;
272         values[4] = key;
273         values[5] = style;
274         values[6] = styleClass;
275         return values;
276 
277     }
278 
279 
280 }