View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.trinidad.component;
20  
21  import java.io.IOException;
22  
23  import java.util.Collection;
24  
25  import javax.faces.FacesException;
26  import javax.faces.component.UIComponent;
27  import javax.faces.context.FacesContext;
28  import javax.faces.event.AbortProcessingException;
29  
30  /**
31   */
32  abstract class ChildLoop implements ComponentProcessor<Object>
33  {
34    public ChildLoop()
35    {
36      this(null);
37    }
38  
39    public ChildLoop(ComponentProcessingContext cpContext)
40    {
41      _cpContext = cpContext;
42    }
43    
44    public final void run(FacesContext context, UIXCollection comp)
45    {
46      run(context, comp, comp);
47    }
48  
49    public final void run(FacesContext context, 
50                          UIXCollection comp, UIComponent child)
51    {
52      if (shouldRun(comp))
53        runAlways(context, child);
54    }
55    
56    @SuppressWarnings("unchecked")
57    public final void runAlways(FacesContext context, UIComponent comp)
58    {
59      runAlways(context, comp.getChildren());
60    }
61  
62    public final void runAlways(FacesContext context, Collection<UIComponent> kids)
63    {
64      try
65      {
66        if (_cpContext != null)
67          UIXComponent.processFlattenedChildren(context, _cpContext, this, kids, null);
68        else
69          UIXComponent.processFlattenedChildren(context, this, kids, null);
70      }
71      catch (IOException ioe)
72      {
73        throw new FacesException(ioe);
74      }
75    }
76    
77    /**
78     * Sets up the context for the child and processes it
79     */
80    public void processComponent(
81      FacesContext context,
82      ComponentProcessingContext cpContext,
83      UIComponent component,
84      Object callbackContext) throws IOException
85    {
86      process(context, component, cpContext);
87    }
88    
89    /**
90     * See if we need to run at all (which is usually only
91     * if there is available data on this row)
92     */
93    protected boolean shouldRun(UIXCollection comp)
94    {
95      return comp.isRowAvailable();
96    }
97  
98    protected final ComponentProcessingContext getComponentProcessingContext()
99    {
100     return _cpContext;
101   }
102     
103   protected abstract void process(FacesContext context, UIComponent comp, ComponentProcessingContext cpContext);
104 
105   private final ComponentProcessingContext _cpContext;                            
106   
107   static final class Update extends ChildLoop
108   {
109     @Override
110     protected void process(FacesContext context, UIComponent comp, ComponentProcessingContext cpContext)
111     {
112       comp.processUpdates(context);
113     }
114   }
115 
116   static final class Validate extends ChildLoop
117   {
118     @Override
119     protected void process(FacesContext context, UIComponent comp, ComponentProcessingContext cpContext)
120     {
121       comp.processValidators(context);
122     }
123   }
124 
125   static final class Decode extends ChildLoop
126   {
127     @Override
128     protected void process(FacesContext context, UIComponent comp, ComponentProcessingContext cpContext)
129     {
130       comp.processDecodes(context);
131     }
132   }  
133 }