KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > faces > component > UIViewRoot


1 /*
2  * Copyright 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 package javax.faces.component;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.ConcurrentModificationException JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.ListIterator JavaDoc;
22 import java.util.Locale JavaDoc;
23
24 import javax.faces.context.FacesContext;
25 import javax.faces.el.ValueBinding;
26 import javax.faces.event.AbortProcessingException;
27 import javax.faces.event.FacesEvent;
28 import javax.faces.event.PhaseId;
29 import javax.faces.render.RenderKitFactory;
30
31 /**
32  * see Javadoc of JSF Specification
33  *
34  * @author Manfred Geiler (latest modification by $Author: matzew $)
35  * @version $Revision: 1.15 $ $Date: 2004/12/07 10:49:27 $
36  * $Log: UIViewRoot.java,v $
37  * Revision 1.15 2004/12/07 10:49:27 matzew
38  * closed MAVEN-10, thanks to Christian Rueed for supporting this
39  *
40  * Revision 1.14 2004/09/03 12:32:06 tinytoony
41  * file upload
42  *
43  * Revision 1.13 2004/08/23 05:13:37 dave0000
44  * Externalize String-to-Locale conversion
45  *
46  * Revision 1.12 2004/08/22 10:37:29 mwessendorf
47  * bug #1007065
48  *
49  * Revision 1.11 2004/07/01 22:00:48 mwessendorf
50  * ASF switch
51  *
52  * Revision 1.10 2004/06/08 02:37:50 o_rossmueller
53  * fix #967991: remove event from queue after broadcase
54  * abort event procession on AbortProcessingException
55  *
56  * Revision 1.9 2004/05/12 07:57:40 manolito
57  * Log in javadoc header
58  *
59  */

60 public class UIViewRoot
61         extends UIComponentBase
62 {
63     public static final String JavaDoc UNIQUE_ID_PREFIX = "_id";
64     public static final int ANY_PHASE_ORDINAL = PhaseId.ANY_PHASE.getOrdinal();
65
66     private int _uniqueIdCounter = 0;
67
68     private String JavaDoc _viewId = null;
69     private Locale JavaDoc _locale = null;
70     private List JavaDoc _events = null;
71
72     public String JavaDoc getViewId()
73     {
74         return _viewId;
75     }
76
77     public void setViewId(String JavaDoc viewId)
78     {
79         if (viewId == null) throw new NullPointerException JavaDoc("viewId");
80         _viewId = viewId;
81     }
82
83     public void queueEvent(FacesEvent event)
84     {
85         if (event == null) throw new NullPointerException JavaDoc("event");
86         if (_events == null)
87         {
88             _events = new ArrayList JavaDoc();
89         }
90         _events.add(event);
91     }
92
93     private void _broadcastForPhase(PhaseId phaseId)
94     {
95         if (_events == null) return;
96
97         boolean abort = false;
98
99         int phaseIdOrdinal = phaseId.getOrdinal();
100         for (ListIterator JavaDoc listiterator = _events.listIterator(); listiterator.hasNext();)
101         {
102             FacesEvent event = (FacesEvent) listiterator.next();
103             int ordinal = event.getPhaseId().getOrdinal();
104             if (ordinal == ANY_PHASE_ORDINAL ||
105                 ordinal == phaseIdOrdinal)
106             {
107                 UIComponent source = event.getComponent();
108                 try
109                 {
110                     source.broadcast(event);
111                 }
112                 catch (AbortProcessingException e)
113                 {
114                     // abort event processing
115
// Page 3-30 of JSF 1.1 spec: "Throw an AbortProcessingException, to tell the JSF implementation
116
// that no further broadcast of this event, or any further events, should take place."
117
abort = true;
118                     break;
119                 } finally {
120
121                     try
122                     {
123                         listiterator.remove();
124                     }
125                     catch(ConcurrentModificationException JavaDoc cme)
126                     {
127                         int eventIndex = listiterator.previousIndex();
128                         _events.remove(eventIndex);
129                         listiterator = _events.listIterator();
130                     }
131                 }
132             }
133         }
134
135         if (abort) {
136             // TODO: abort processing of any event of any phase or just of any event of the current phase???
137
clearEvents();
138         }
139     }
140
141
142     private void clearEvents()
143     {
144         _events = null;
145     }
146
147
148     public void processDecodes(FacesContext context)
149     {
150         if (context == null) throw new NullPointerException JavaDoc("context");
151         super.processDecodes(context);
152         _broadcastForPhase(PhaseId.APPLY_REQUEST_VALUES);
153         if (context.getRenderResponse() || context.getResponseComplete())
154         {
155             clearEvents();
156         }
157     }
158
159     public void processValidators(FacesContext context)
160     {
161         if (context == null) throw new NullPointerException JavaDoc("context");
162         super.processValidators(context);
163         _broadcastForPhase(PhaseId.PROCESS_VALIDATIONS);
164         if (context.getRenderResponse() || context.getResponseComplete())
165         {
166             clearEvents();
167         }
168     }
169
170     public void processUpdates(FacesContext context)
171     {
172         if (context == null) throw new NullPointerException JavaDoc("context");
173         super.processUpdates(context);
174         _broadcastForPhase(PhaseId.UPDATE_MODEL_VALUES);
175         if (context.getRenderResponse() || context.getResponseComplete())
176         {
177             clearEvents();
178         }
179     }
180
181     public void processApplication(FacesContext context)
182     {
183         if (context == null) throw new NullPointerException JavaDoc("context");
184         _broadcastForPhase(PhaseId.INVOKE_APPLICATION);
185         if (context.getRenderResponse() || context.getResponseComplete())
186         {
187             clearEvents();
188         }
189     }
190
191     public void encodeBegin(FacesContext context)
192             throws java.io.IOException JavaDoc
193     {
194         _uniqueIdCounter = 0;
195         clearEvents();
196         super.encodeBegin(context);
197     }
198
199     public String JavaDoc createUniqueId()
200     {
201         return UNIQUE_ID_PREFIX + _uniqueIdCounter++;
202     }
203
204     public Locale JavaDoc getLocale()
205     {
206         if (_locale != null) return _locale;
207         ValueBinding vb = getValueBinding("locale");
208         FacesContext facesContext = getFacesContext();
209         if (vb == null)
210         {
211             return facesContext.getApplication().getViewHandler().calculateLocale(facesContext);
212         }
213         Object JavaDoc locale = vb.getValue(facesContext);
214         if (locale == null)
215         {
216             return facesContext.getApplication().getViewHandler().calculateLocale(facesContext);
217         }
218         if (locale instanceof Locale JavaDoc)
219         {
220             return (Locale JavaDoc)locale;
221         }
222         else if (locale instanceof String JavaDoc)
223         {
224             return getLocale((String JavaDoc)locale);
225         }
226         else
227         {
228             throw new IllegalArgumentException JavaDoc("locale binding"); //TODO: not specified!?
229
}
230     }
231
232     /**
233      * Create Locale from String representation.
234      *
235      * @see http://java.sun.com/j2se/1.4.2/docs/api/java/util/Locale.html
236      * @param locale locale representation in String.
237      * @return Locale instance
238      */

239     private static Locale JavaDoc getLocale(String JavaDoc locale){
240         int cnt = 0;
241         int pos = 0;
242         int prev = 0;
243
244         // store locale variation.
245
// ex. "ja_JP_POSIX"
246
// lv[0] : language(ja)
247
// lv[1] : country(JP)
248
// lv[2] : variant(POSIX)
249
String JavaDoc[] lv = new String JavaDoc[3];
250         Locale JavaDoc l=null;
251
252         while((pos=locale.indexOf('_',prev))!=-1){
253              lv[cnt++] = locale.substring(prev,pos);
254              prev = pos + 1;
255         }
256
257         lv[cnt++] = locale.substring(prev,locale.length());
258
259         switch(cnt){
260             case 1:
261                 // create Locale from language.
262
l = new Locale JavaDoc(lv[0]);
263                 break;
264             case 2:
265                 // create Locale from language and country.
266
l = new Locale JavaDoc(lv[0],lv[1]);
267                 break;
268             case 3:
269                 // create Locale from language, country and variant.
270
l = new Locale JavaDoc(lv[0], lv[1], lv[2]);
271                 break;
272         }
273         return l;
274     }
275
276
277     public void setLocale(Locale JavaDoc locale)
278     {
279         _locale = locale;
280     }
281
282     //------------------ GENERATED CODE BEGIN (do not modify!) --------------------
283

284     public static final String JavaDoc COMPONENT_TYPE = "javax.faces.ViewRoot";
285     public static final String JavaDoc COMPONENT_FAMILY = "javax.faces.ViewRoot";
286     private static final String JavaDoc DEFAULT_RENDERKITID = RenderKitFactory.HTML_BASIC_RENDER_KIT;
287
288     private String JavaDoc _renderKitId = null;
289
290     public String JavaDoc getFamily()
291     {
292         return COMPONENT_FAMILY;
293     }
294
295
296     public void setRenderKitId(String JavaDoc renderKitId)
297     {
298         _renderKitId = renderKitId;
299     }
300
301     public String JavaDoc getRenderKitId()
302     {
303         if (_renderKitId != null) return _renderKitId;
304         ValueBinding vb = getValueBinding("renderKitId");
305         return vb != null ? (String JavaDoc)vb.getValue(getFacesContext()) : DEFAULT_RENDERKITID;
306     }
307
308
309
310     public Object JavaDoc saveState(FacesContext context)
311     {
312         Object JavaDoc values[] = new Object JavaDoc[4];
313         values[0] = super.saveState(context);
314         values[1] = _locale;
315         values[2] = _renderKitId;
316         values[3] = _viewId;
317         return values;
318     }
319
320     public void restoreState(FacesContext context, Object JavaDoc state)
321     {
322         Object JavaDoc values[] = (Object JavaDoc[])state;
323         super.restoreState(context, values[0]);
324         _locale = (Locale JavaDoc)values[1];
325         _renderKitId = (String JavaDoc)values[2];
326         _viewId = (String JavaDoc)values[3];
327     }
328     //------------------ GENERATED CODE END ---------------------------------------
329
}
Popular Tags