1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.view.facelets.tag.jsf;
20
21 import java.io.Serializable;
22 import java.util.HashMap;
23 import java.util.Map;
24 import javax.el.ValueExpression;
25
26 import javax.faces.component.StateHolder;
27 import javax.faces.component.UIComponentBase;
28 import javax.faces.component.UIViewRoot;
29 import javax.faces.context.FacesContext;
30
31 public class FaceletState implements StateHolder, Serializable
32 {
33
34
35
36 private static final long serialVersionUID = -7823771271935942737L;
37
38 private Map<String, Object> stateMap;
39
40 private Map<String, Map<String, ValueExpression>> bindingsMap;
41
42 public FaceletState()
43 {
44 }
45
46 public Object getState(String key)
47 {
48 if(stateMap == null)
49 {
50 return null;
51 }
52 return stateMap.get(key);
53 }
54
55 public Object putState(String key, Object value)
56 {
57 if (stateMap == null)
58 {
59 stateMap = new HashMap<String, Object>();
60 }
61 return stateMap.put(key, value);
62 }
63
64
65 public Object saveState(FacesContext context)
66 {
67 UIViewRoot root = context.getViewRoot();
68 if (root != null && root.initialStateMarked())
69 {
70 if (stateMap != null)
71 {
72 Object values[] = new Object[1];
73 values[0] = UIComponentBase.saveAttachedState(context, stateMap);
74 return values;
75 }
76 return null;
77 }
78 else
79 {
80 Object values[] = new Object[2];
81 values[0] = UIComponentBase.saveAttachedState(context, stateMap);
82
83
84
85
86 values[1] = UIComponentBase.saveAttachedState(context, bindingsMap);
87 return values;
88 }
89 }
90
91 @SuppressWarnings("unchecked")
92 public void restoreState(FacesContext context, Object state)
93 {
94 if (state == null)
95 {
96 stateMap = null;
97 bindingsMap = null;
98 return;
99 }
100 Object values[] = (Object[])state;
101 if (values.length == 2)
102 {
103
104 stateMap = (Map<String,Object>) UIComponentBase.restoreAttachedState(context, values[0]);
105 bindingsMap = (Map<String,Map<String, ValueExpression>>)
106 UIComponentBase.restoreAttachedState(context, values[1]);
107 }
108 else
109 {
110 if (values[0] == null)
111 {
112 stateMap = null;
113 }
114 else
115 {
116 stateMap = (Map<String,Object>) UIComponentBase.restoreAttachedState(context, values[0]);
117 }
118 }
119 }
120
121 public boolean isTransient()
122 {
123 return false;
124 }
125
126 public void setTransient(boolean newTransientValue)
127 {
128 }
129
130 public boolean isDynamic()
131 {
132 return stateMap == null ? false : !stateMap.isEmpty();
133 }
134
135 public void putBinding(String uniqueId, String key, ValueExpression expr)
136 {
137 if (bindingsMap == null)
138 {
139 bindingsMap = new HashMap<String, Map<String, ValueExpression>>();
140 }
141 Map<String, ValueExpression> bindings = bindingsMap.get(uniqueId);
142 if (bindings == null)
143 {
144 bindings = new HashMap<String, ValueExpression>();
145 bindingsMap.put(uniqueId, bindings);
146 }
147 bindings.put(key, expr);
148 }
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 public ValueExpression getBinding(String uniqueId, String key)
166 {
167 if (bindingsMap == null)
168 {
169 return null;
170 }
171 Map<String, ValueExpression> bindings = bindingsMap.get(uniqueId);
172 if (bindings == null)
173 {
174 return null;
175 }
176 return bindings.get(key);
177 }
178
179 public Map<String, Map<String, ValueExpression>> getBindings()
180 {
181 return bindingsMap;
182 }
183
184 @Override
185 public int hashCode()
186 {
187 int hash = 7;
188 hash = 79 * hash + (this.stateMap != null ? this.stateMap.hashCode() : 0);
189 return hash;
190 }
191
192 @Override
193 public boolean equals(Object obj)
194 {
195 if (obj == null)
196 {
197 return false;
198 }
199 if (getClass() != obj.getClass())
200 {
201 return false;
202 }
203 final FaceletState other = (FaceletState) obj;
204 if (this.stateMap != other.stateMap && (this.stateMap == null || !this.stateMap.equals(other.stateMap)))
205 {
206 return false;
207 }
208 return true;
209 }
210
211 }