1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package javax.faces.component; |
17 | |
|
18 | |
import java.io.IOException; |
19 | |
import java.util.ArrayList; |
20 | |
import java.util.Iterator; |
21 | |
import java.util.List; |
22 | |
import java.util.Locale; |
23 | |
|
24 | |
import javax.faces.context.FacesContext; |
25 | |
import javax.faces.event.AbortProcessingException; |
26 | |
import javax.faces.event.FacesEvent; |
27 | |
import javax.faces.event.PhaseId; |
28 | |
|
29 | |
import org.seasar.framework.util.AssertionUtil; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class UIViewRoot extends UIComponentBase { |
35 | |
|
36 | |
public static final String COMPONENT_FAMILY = "javax.faces.ViewRoot"; |
37 | |
|
38 | |
public static final String COMPONENT_TYPE = "javax.faces.ViewRoot"; |
39 | |
|
40 | |
public static final String UNIQUE_ID_PREFIX = "_id"; |
41 | |
|
42 | 1817 | private String renderKitId = null; |
43 | |
|
44 | 1817 | private String viewId = null; |
45 | |
|
46 | 1817 | private List events = null; |
47 | |
|
48 | 1817 | private int lastId = 0; |
49 | |
|
50 | 1817 | private Locale locale = null; |
51 | |
|
52 | 1817 | public UIViewRoot() { |
53 | 1817 | } |
54 | |
|
55 | |
public String getFamily() { |
56 | 5 | return COMPONENT_FAMILY; |
57 | |
} |
58 | |
|
59 | |
public String getRenderKitId() { |
60 | 522 | if (renderKitId != null) { |
61 | 516 | return renderKitId; |
62 | |
} |
63 | 6 | return (String) ComponentUtil_ |
64 | |
.getValueBindingValue(this, "renderKitId"); |
65 | |
} |
66 | |
|
67 | |
public void setRenderKitId(String renderKitId) { |
68 | 1408 | this.renderKitId = renderKitId; |
69 | 1408 | } |
70 | |
|
71 | |
public String getViewId() { |
72 | 155 | return this.viewId; |
73 | |
} |
74 | |
|
75 | |
public void setViewId(String viewId) { |
76 | 54 | this.viewId = viewId; |
77 | 54 | } |
78 | |
|
79 | |
public void queueEvent(FacesEvent event) { |
80 | 32 | AssertionUtil.assertNotNull("event", event); |
81 | 31 | getEvents().add(event); |
82 | 31 | } |
83 | |
|
84 | |
private List getEvents() { |
85 | 73 | if (events == null) { |
86 | 27 | events = new ArrayList(); |
87 | |
} |
88 | 73 | return events; |
89 | |
} |
90 | |
|
91 | |
public void processDecodes(FacesContext context) { |
92 | 4 | super.processDecodes(context); |
93 | 3 | broadcastEvents(context, PhaseId.APPLY_REQUEST_VALUES); |
94 | 3 | clearEventsIfResponseRendered(context); |
95 | 3 | } |
96 | |
|
97 | |
public void encodeBegin(FacesContext context) throws IOException { |
98 | 9 | lastId = 0; |
99 | 9 | clearEvents(); |
100 | 9 | super.encodeBegin(context); |
101 | 8 | } |
102 | |
|
103 | |
public void processValidators(FacesContext context) { |
104 | 4 | super.processValidators(context); |
105 | 3 | broadcastEvents(context, PhaseId.PROCESS_VALIDATIONS); |
106 | 3 | clearEventsIfResponseRendered(context); |
107 | 3 | } |
108 | |
|
109 | |
public void processUpdates(FacesContext context) { |
110 | 4 | super.processUpdates(context); |
111 | 3 | broadcastEvents(context, PhaseId.UPDATE_MODEL_VALUES); |
112 | 3 | clearEventsIfResponseRendered(context); |
113 | 3 | } |
114 | |
|
115 | |
public void processApplication(FacesContext context) { |
116 | 4 | AssertionUtil.assertNotNull("context", context); |
117 | 3 | broadcastEvents(context, PhaseId.INVOKE_APPLICATION); |
118 | 3 | clearEventsIfResponseRendered(context); |
119 | 3 | } |
120 | |
|
121 | |
public String createUniqueId() { |
122 | 364 | return UNIQUE_ID_PREFIX + (lastId++); |
123 | |
} |
124 | |
|
125 | |
public Locale getLocale() { |
126 | 194 | if (locale != null) { |
127 | 190 | return locale; |
128 | |
} |
129 | 4 | Locale locale = null; |
130 | 4 | FacesContext context = getFacesContext(); |
131 | 4 | if (getValueBinding("locale") != null) { |
132 | 2 | Object obj = ComponentUtil_.getValueBindingValue(this, "locale"); |
133 | 2 | if (obj == null) { |
134 | 0 | locale = ComponentUtil_.calculateLocale(context); |
135 | 2 | } else if (obj instanceof Locale) { |
136 | 1 | locale = (Locale) obj; |
137 | 1 | } else if (obj instanceof String) { |
138 | 1 | locale = getLocaleFromString((String) obj); |
139 | |
} |
140 | |
} else { |
141 | 2 | locale = ComponentUtil_.calculateLocale(context); |
142 | |
} |
143 | 4 | return locale; |
144 | |
} |
145 | |
|
146 | |
public void setLocale(Locale locale) { |
147 | 1631 | this.locale = locale; |
148 | 1631 | } |
149 | |
|
150 | |
public void restoreState(FacesContext context, Object state) { |
151 | 4 | Object values[] = (Object[]) state; |
152 | 4 | super.restoreState(context, values[0]); |
153 | 4 | renderKitId = (String) values[1]; |
154 | 4 | viewId = (String) values[2]; |
155 | 4 | locale = (Locale) values[3]; |
156 | 4 | } |
157 | |
|
158 | |
public Object saveState(FacesContext context) { |
159 | 8 | Object values[] = new Object[4]; |
160 | 8 | values[0] = super.saveState(context); |
161 | 8 | values[1] = renderKitId; |
162 | 8 | values[2] = viewId; |
163 | 8 | values[3] = locale; |
164 | 8 | return values; |
165 | |
} |
166 | |
|
167 | |
private Locale getLocaleFromString(String localeStr) { |
168 | 1 | Locale locale = Locale.getDefault(); |
169 | 1 | if (ComponentUtil_.isLocaleShort(localeStr)) { |
170 | 1 | locale = new Locale(localeStr); |
171 | 0 | } else if (ComponentUtil_.isLocaleLong(localeStr)) { |
172 | 0 | String language = localeStr.substring(0, 2); |
173 | 0 | String country = localeStr.substring(3, 5); |
174 | 0 | locale = new Locale(language, country); |
175 | |
} |
176 | 1 | return locale; |
177 | |
} |
178 | |
|
179 | |
protected void broadcastEvents(FacesContext context, PhaseId phaseId) { |
180 | 15 | if (getEvents().isEmpty()) { |
181 | 0 | return; |
182 | |
} |
183 | 15 | int phaseIdOrdinal = phaseId.getOrdinal(); |
184 | 15 | for (Iterator itr = events.iterator(); itr.hasNext();) { |
185 | 23 | FacesEvent event = (FacesEvent) itr.next(); |
186 | 23 | int ordinal = event.getPhaseId().getOrdinal(); |
187 | 23 | if (ordinal == PhaseId.ANY_PHASE.getOrdinal() |
188 | |
|| ordinal == phaseIdOrdinal) { |
189 | 11 | UIComponent source = event.getComponent(); |
190 | |
try { |
191 | |
try { |
192 | 11 | source.broadcast(event); |
193 | |
} finally { |
194 | 11 | itr.remove(); |
195 | 10 | } |
196 | 1 | } catch (AbortProcessingException e) { |
197 | 1 | clearEvents(); |
198 | 1 | break; |
199 | 32 | } |
200 | |
} |
201 | |
} |
202 | 15 | } |
203 | |
|
204 | |
protected void clearEventsIfResponseRendered(FacesContext context) { |
205 | 12 | if (context.getRenderResponse() || context.getResponseComplete()) { |
206 | 4 | clearEvents(); |
207 | |
} |
208 | 12 | } |
209 | |
|
210 | |
private void clearEvents() { |
211 | 14 | events = null; |
212 | 14 | } |
213 | |
|
214 | |
public int getEventSize() { |
215 | 27 | return getEvents().size(); |
216 | |
} |
217 | |
|
218 | |
} |