View Javadoc
1   /*
2    * $Id$
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts.faces.component;
23  
24  import jakarta.el.ValueExpression;
25  import jakarta.faces.component.UIOutput;
26  import jakarta.faces.context.FacesContext;
27  
28  /**
29   * Custom component to load a {@code MessagesMap}.
30   */
31  public class LoadMessagesComponent extends UIOutput {
32  
33  
34      // ------------------------------------------------------------ Constructors
35  
36  
37      /**
38       * Create a new {@link LoadMessagesComponent} with default properties.
39       */
40      public LoadMessagesComponent() {
41          super();
42          setRendererType("org.apache.struts.faces.LoadMessages");
43      }
44  
45  
46      // ------------------------------------------------------ Instance Variables
47  
48  
49      /**
50       * The name of the {@code MessageResources} to expose, or
51       * {@code null} for the default {@code MessageResources}
52       * for this application module.
53       */
54      private String messages = null;
55  
56      /**
57       * The request attribute key under which the
58       * {@code MessagesMap} will be exposed.
59       */
60      private String var = null;
61  
62  
63      // ---------------------------------------------------- Component Properties
64  
65  
66      /**
67       * Return the component family to which this component belongs.
68       */
69      public String getFamily() {
70          return "org.apache.struts.faces.LoadMessages";
71      }
72  
73      /**
74       * Gets the name of the {@code MessageResources} to expose,
75       * or {@code null} for the default {@code MessageResources}
76       * for this application module.
77       *
78       * @return the name of the {@code MessageResources}
79       */
80      public String getMessages() {
81          if (messages != null) {
82              return messages;
83          }
84          ValueExpression vb = getValueExpression("messages");
85          if (vb != null) {
86              return (String) vb.getValue(getFacesContext().getELContext());
87          } else {
88              return null;
89          }
90      }
91  
92      /**
93       * Sets the name of the {@code MessageResources} to expose,
94       * or {@code null} for the default {@code MessageResources}
95       * for this application module.
96       *
97       * @param messages the name of the {@code MessageResources}
98       */
99      public void setMessages(String messages) {
100         this.messages = messages;
101     }
102 
103     /**
104      * Gets the request attribute key under which the
105      * {@code MessagesMap} will be exposed.
106      *
107      * @return the request attribute key
108      */
109     public String getVar() {
110         if (var != null) {
111             return var;
112         }
113         ValueExpression vb = getValueExpression("var");
114         if (vb != null) {
115             return (String) vb.getValue(getFacesContext().getELContext());
116         } else {
117             return null;
118         }
119     }
120 
121     /**
122      * Sets the request attribute key under which the
123      * {@code MessagesMap} will be exposed.
124      *
125      * @param var the request attribute key
126      */
127     public void setVar(String var) {
128         this.var = var;
129     }
130 
131 
132     // ---------------------------------------------------- StateManager Methods
133 
134 
135     /**
136      * Restore the state of this component.
137      *
138      * @param context {@code FacesContext} for the current request
139      * @param state State object from which to restore our state
140      */
141     public void restoreState(FacesContext context, Object state) {
142         Object values[] = (Object[]) state;
143         super.restoreState(context, values[0]);
144         messages = (String) values[1];
145         var = (String) values[2];
146     }
147 
148     /**
149      * Save the state of this component.
150      *
151      * @param context {@code FacesContext} for the current request
152      */
153     public Object saveState(FacesContext context) {
154         Object values[] = new Object[3];
155         values[0] = super.saveState(context);
156         values[1] = messages;
157         values[2] = var;
158         return values;
159     }
160 }