Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: javax/faces/component/UIViewRoot.java


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;
19  import java.util.ConcurrentModificationException;
20  import java.util.List;
21  import java.util.ListIterator;
22  import java.util.Locale;
23  import javax.faces.context.ExternalContext;
24  
25  import javax.faces.context.FacesContext;
26  import javax.faces.el.ValueBinding;
27  import javax.faces.event.AbortProcessingException;
28  import javax.faces.event.FacesEvent;
29  import javax.faces.event.PhaseId;
30  import javax.faces.render.RenderKitFactory;
31  
32  /**
33   * see Javadoc of JSF Specification
34   *
35   * @author Manfred Geiler (latest modification by $Author: oros $)
36   * @version $Revision: 265611 $ $Date: 2005-08-31 21:05:16 -0400 (Wed, 31 Aug 2005) $
37   */
38  public class UIViewRoot
39          extends UIComponentBase
40  {
41      public static final String UNIQUE_ID_PREFIX = "_id";
42  
43      private static final int ANY_PHASE_ORDINAL = PhaseId.ANY_PHASE.getOrdinal();
44  
45      private int _uniqueIdCounter = 0;
46  
47      private String _viewId = null;
48      private Locale _locale = null;
49      private List _events = null;
50  
51      public String getViewId()
52      {
53          return _viewId;
54      }
55  
56      public void setViewId(String viewId)
57      {
58  //      The spec does not require to check this, so we don't check it      
59  //        if (viewId == null) throw new NullPointerException("viewId");
60          _viewId = viewId;
61      }
62  
63      public void queueEvent(FacesEvent event)
64      {
65          if (event == null) throw new NullPointerException("event");
66          if (_events == null)
67          {
68              _events = new ArrayList();
69          }
70          _events.add(event);
71      }
72  
73      private void _broadcastForPhase(PhaseId phaseId)
74      {
75          if (_events == null) return;
76  
77          boolean abort = false;
78  
79          int phaseIdOrdinal = phaseId.getOrdinal();
80          for (ListIterator listiterator = _events.listIterator(); listiterator.hasNext();)
81          {
82              FacesEvent event = (FacesEvent) listiterator.next();
83              int ordinal = event.getPhaseId().getOrdinal();
84              if (ordinal == ANY_PHASE_ORDINAL ||
85                  ordinal == phaseIdOrdinal)
86              {
87                  UIComponent source = event.getComponent();
88                  try
89                  {
90                      source.broadcast(event);
91                  }
92                  catch (AbortProcessingException e)
93                  {
94                      // abort event processing
95                      // Page 3-30 of JSF 1.1 spec: "Throw an AbortProcessingException, to tell the JSF implementation
96                      //  that no further broadcast of this event, or any further events, should take place."
97                      abort = true;
98                      break;
99                  } finally {
100 
101                   try
102                   {
103                         listiterator.remove();
104                   }
105                   catch(ConcurrentModificationException cme)
106                   {
107                     int eventIndex = listiterator.previousIndex();
108                     _events.remove(eventIndex);
109                     listiterator = _events.listIterator();
110                   }
111                 }
112             }
113         }
114 
115         if (abort) {
116             // TODO: abort processing of any event of any phase or just of any event of the current phase???
117             clearEvents();
118         }
119     }
120 
121 
122     private void clearEvents()
123     {
124         _events = null;
125     }
126 
127 
128     public void processDecodes(FacesContext context)
129     {
130         if (context == null) throw new NullPointerException("context");
131         super.processDecodes(context);
132         _broadcastForPhase(PhaseId.APPLY_REQUEST_VALUES);
133         if (context.getRenderResponse() || context.getResponseComplete())
134         {
135             clearEvents();
136         }
137     }
138 
139     public void processValidators(FacesContext context)
140     {
141         if (context == null) throw new NullPointerException("context");
142         super.processValidators(context);
143         _broadcastForPhase(PhaseId.PROCESS_VALIDATIONS);
144         if (context.getRenderResponse() || context.getResponseComplete())
145         {
146             clearEvents();
147         }
148     }
149 
150     public void processUpdates(FacesContext context)
151     {
152         if (context == null) throw new NullPointerException("context");
153         super.processUpdates(context);
154         _broadcastForPhase(PhaseId.UPDATE_MODEL_VALUES);
155         if (context.getRenderResponse() || context.getResponseComplete())
156         {
157             clearEvents();
158         }
159     }
160 
161     public void processApplication(FacesContext context)
162     {
163         if (context == null) throw new NullPointerException("context");
164         _broadcastForPhase(PhaseId.INVOKE_APPLICATION);
165         if (context.getRenderResponse() || context.getResponseComplete())
166         {
167             clearEvents();
168         }
169     }
170 
171     public void encodeBegin(FacesContext context)
172             throws java.io.IOException
173     {
174         _uniqueIdCounter = 0;
175         clearEvents();
176         super.encodeBegin(context);
177     }
178 
179     public String createUniqueId()
180     {
181         ExternalContext extCtx = FacesContext.getCurrentInstance().getExternalContext();
182         return extCtx.encodeNamespace(UNIQUE_ID_PREFIX + _uniqueIdCounter++);
183     }
184 
185     public Locale getLocale()
186     {
187         if (_locale != null) return _locale;
188         ValueBinding vb = getValueBinding("locale");
189         FacesContext facesContext = getFacesContext();
190         if (vb == null)
191         {
192             return facesContext.getApplication().getViewHandler().calculateLocale(facesContext);
193         }
194         Object locale = vb.getValue(facesContext);
195         if (locale == null)
196         {
197             return facesContext.getApplication().getViewHandler().calculateLocale(facesContext);
198         }
199         if (locale instanceof Locale)
200         {
201             return (Locale)locale;
202         }
203         else if (locale instanceof String)
204         {
205             return getLocale((String)locale);
206         }
207         else
208         {
209             throw new IllegalArgumentException("locale binding"); //TODO: not specified!?
210         }
211     }
212 
213     /**
214      * Create Locale from String representation.
215      *
216      * http://java.sun.com/j2se/1.4.2/docs/api/java/util/Locale.html
217      *
218      * @param locale locale representation in String.
219      * @return Locale instance
220      */
221     private static Locale getLocale(String locale){
222         int cnt = 0;
223         int pos = 0;
224         int prev = 0;
225 
226         // store locale variation.
227         // ex. "ja_JP_POSIX"
228         //  lv[0] : language(ja)
229         //  lv[1] : country(JP)
230         //  lv[2] : variant(POSIX)
231         String[] lv = new String[3];
232         Locale l=null;
233 
234         while((pos=locale.indexOf('_',prev))!=-1){
235              lv[cnt++] = locale.substring(prev,pos);
236              prev = pos + 1;
237         }
238 
239         lv[cnt++] = locale.substring(prev,locale.length());
240 
241         switch(cnt){
242             case 1:
243                 // create Locale from language.
244                 l = new Locale(lv[0]);
245                 break;
246             case 2:
247                 // create Locale from language and country.
248                 l = new Locale(lv[0],lv[1]);
249                 break;
250             case 3:
251                 // create Locale from language, country and variant.
252                 l = new Locale(lv[0], lv[1], lv[2]);
253                 break;
254         }
255         return l;
256     }
257 
258 
259     public void setLocale(Locale locale)
260     {
261         _locale = locale;
262     }
263 
264     //------------------ GENERATED CODE BEGIN (do not modify!) --------------------
265 
266     public static final String COMPONENT_TYPE = "javax.faces.ViewRoot";
267     public static final String COMPONENT_FAMILY = "javax.faces.ViewRoot";
268     private static final String DEFAULT_RENDERKITID = RenderKitFactory.HTML_BASIC_RENDER_KIT;
269 
270     private String _renderKitId = null;
271 
272     public String getFamily()
273     {
274         return COMPONENT_FAMILY;
275     }
276 
277 
278     public void setRenderKitId(String renderKitId)
279     {
280         _renderKitId = renderKitId;
281     }
282 
283     public String getRenderKitId()
284     {
285         if (_renderKitId != null) return _renderKitId;
286         ValueBinding vb = getValueBinding("renderKitId");
287         return vb != null ? (String)vb.getValue(getFacesContext()) : DEFAULT_RENDERKITID;
288     }
289 
290 
291 
292     public Object saveState(FacesContext context)
293     {
294         Object values[] = new Object[4];
295         values[0] = super.saveState(context);
296         values[1] = _locale;
297         values[2] = _renderKitId;
298         values[3] = _viewId;
299         return values;
300     }
301 
302     public void restoreState(FacesContext context, Object state)
303     {
304         Object values[] = (Object[])state;
305         super.restoreState(context, values[0]);
306         _locale = (Locale)values[1];
307         _renderKitId = (String)values[2];
308         _viewId = (String)values[3];
309     }
310     //------------------ GENERATED CODE END ---------------------------------------
311 }