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 | 0 | { |
44 | 0 | } |
45 | |
|
46 | |
public Object getState(String key) |
47 | |
{ |
48 | 0 | if(stateMap == null) |
49 | |
{ |
50 | 0 | return null; |
51 | |
} |
52 | 0 | return stateMap.get(key); |
53 | |
} |
54 | |
|
55 | |
public Object putState(String key, Object value) |
56 | |
{ |
57 | 0 | if (stateMap == null) |
58 | |
{ |
59 | 0 | stateMap = new HashMap<String, Object>(); |
60 | |
} |
61 | 0 | return stateMap.put(key, value); |
62 | |
} |
63 | |
|
64 | |
|
65 | |
public Object saveState(FacesContext context) |
66 | |
{ |
67 | 0 | UIViewRoot root = context.getViewRoot(); |
68 | 0 | if (root != null && root.initialStateMarked()) |
69 | |
{ |
70 | 0 | if (stateMap != null) |
71 | |
{ |
72 | 0 | Object values[] = new Object[1]; |
73 | 0 | values[0] = UIComponentBase.saveAttachedState(context, stateMap); |
74 | 0 | return values; |
75 | |
} |
76 | 0 | return null; |
77 | |
} |
78 | |
else |
79 | |
{ |
80 | 0 | Object values[] = new Object[2]; |
81 | 0 | values[0] = UIComponentBase.saveAttachedState(context, stateMap); |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | 0 | values[1] = UIComponentBase.saveAttachedState(context, bindingsMap); |
87 | 0 | return values; |
88 | |
} |
89 | |
} |
90 | |
|
91 | |
@SuppressWarnings("unchecked") |
92 | |
public void restoreState(FacesContext context, Object state) |
93 | |
{ |
94 | 0 | if (state == null) |
95 | |
{ |
96 | 0 | stateMap = null; |
97 | 0 | bindingsMap = null; |
98 | 0 | return; |
99 | |
} |
100 | 0 | Object values[] = (Object[])state; |
101 | 0 | if (values.length == 2) |
102 | |
{ |
103 | |
|
104 | 0 | stateMap = (Map<String,Object>) UIComponentBase.restoreAttachedState(context, values[0]); |
105 | 0 | bindingsMap = (Map<String,Map<String, ValueExpression>>) |
106 | |
UIComponentBase.restoreAttachedState(context, values[1]); |
107 | |
} |
108 | |
else |
109 | |
{ |
110 | 0 | if (values[0] == null) |
111 | |
{ |
112 | 0 | stateMap = null; |
113 | |
} |
114 | |
else |
115 | |
{ |
116 | 0 | stateMap = (Map<String,Object>) UIComponentBase.restoreAttachedState(context, values[0]); |
117 | |
} |
118 | |
} |
119 | 0 | } |
120 | |
|
121 | |
public boolean isTransient() |
122 | |
{ |
123 | 0 | return false; |
124 | |
} |
125 | |
|
126 | |
public void setTransient(boolean newTransientValue) |
127 | |
{ |
128 | 0 | } |
129 | |
|
130 | |
public boolean isDynamic() |
131 | |
{ |
132 | 0 | return stateMap == null ? false : !stateMap.isEmpty(); |
133 | |
} |
134 | |
|
135 | |
public void putBinding(String uniqueId, String key, ValueExpression expr) |
136 | |
{ |
137 | 0 | if (bindingsMap == null) |
138 | |
{ |
139 | 0 | bindingsMap = new HashMap<String, Map<String, ValueExpression>>(); |
140 | |
} |
141 | 0 | Map<String, ValueExpression> bindings = bindingsMap.get(uniqueId); |
142 | 0 | if (bindings == null) |
143 | |
{ |
144 | 0 | bindings = new HashMap<String, ValueExpression>(); |
145 | 0 | bindingsMap.put(uniqueId, bindings); |
146 | |
} |
147 | 0 | bindings.put(key, expr); |
148 | 0 | } |
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 | 0 | if (bindingsMap == null) |
168 | |
{ |
169 | 0 | return null; |
170 | |
} |
171 | 0 | Map<String, ValueExpression> bindings = bindingsMap.get(uniqueId); |
172 | 0 | if (bindings == null) |
173 | |
{ |
174 | 0 | return null; |
175 | |
} |
176 | 0 | return bindings.get(key); |
177 | |
} |
178 | |
|
179 | |
public Map<String, Map<String, ValueExpression>> getBindings() |
180 | |
{ |
181 | 0 | return bindingsMap; |
182 | |
} |
183 | |
|
184 | |
@Override |
185 | |
public int hashCode() |
186 | |
{ |
187 | 0 | int hash = 7; |
188 | 0 | hash = 79 * hash + (this.stateMap != null ? this.stateMap.hashCode() : 0); |
189 | 0 | return hash; |
190 | |
} |
191 | |
|
192 | |
@Override |
193 | |
public boolean equals(Object obj) |
194 | |
{ |
195 | 0 | if (obj == null) |
196 | |
{ |
197 | 0 | return false; |
198 | |
} |
199 | 0 | if (getClass() != obj.getClass()) |
200 | |
{ |
201 | 0 | return false; |
202 | |
} |
203 | 0 | final FaceletState other = (FaceletState) obj; |
204 | 0 | if (this.stateMap != other.stateMap && (this.stateMap == null || !this.stateMap.equals(other.stateMap))) |
205 | |
{ |
206 | 0 | return false; |
207 | |
} |
208 | 0 | return true; |
209 | |
} |
210 | |
|
211 | |
} |