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  
20  package org.apache.myfaces.tobago.internal.component;
21  
22  import org.apache.myfaces.tobago.component.Attributes;
23  import org.apache.myfaces.tobago.component.Visual;
24  import org.apache.myfaces.tobago.internal.util.StringUtils;
25  import org.apache.myfaces.tobago.util.ComponentUtils;
26  
27  import javax.el.ValueExpression;
28  import javax.faces.component.NamingContainer;
29  import javax.faces.component.UICommand;
30  import javax.faces.component.UIComponent;
31  import javax.faces.component.UINamingContainer;
32  import javax.faces.context.FacesContext;
33  import java.io.IOException;
34  import java.util.Iterator;
35  
36  public abstract class AbstractUIPopup extends AbstractUIPanel
37      implements NamingContainer, Visual {
38  
39    private boolean activated;
40  
41    public void setActivated(final boolean activated) {
42      this.activated = activated;
43    }
44  
45    @Override
46    public void processDecodes(final FacesContext facesContext) {
47      if (isSubmitted()) {
48        for (final Iterator it = getFacetsAndChildren(); it.hasNext();) {
49          final UIComponent childOrFacet = (UIComponent) it.next();
50          childOrFacet.processDecodes(facesContext);
51        }
52        try {
53          decode(facesContext);
54        } catch (final RuntimeException e) {
55          facesContext.renderResponse();
56          throw e;
57        }
58        if (facesContext.getRenderResponse()) {
59          setActivated(true);
60        }
61      }
62    }
63  
64    @Override
65    public boolean isRendered() {
66      final ValueExpression expression = getValueExpression("rendered");
67      if (expression != null) {
68        final FacesContext context = FacesContext.getCurrentInstance();
69        return (Boolean) expression.getValue(context.getELContext());
70      } else {
71        return isActivated() || isRedisplay();
72      }
73    }
74  
75    private boolean isSubmitted() {
76      final FacesContext facesContext = getFacesContext();
77      final String sourceId = facesContext.getExternalContext().getRequestParameterMap().get("javax.faces.source");
78      return StringUtils.startsWith(
79          sourceId, getClientId(facesContext) + UINamingContainer.getSeparatorChar(facesContext));
80    }
81  
82    private boolean isRedisplay() {
83      if (isSubmitted()) {
84        final String sourceId = getFacesContext().getExternalContext().getRequestParameterMap().get("javax.faces.source");
85        if (sourceId != null) {
86          final UIComponent command = getFacesContext().getViewRoot()
87              .findComponent(UINamingContainer.getSeparatorChar(getFacesContext()) + sourceId);
88          if (command != null && command instanceof UICommand) {
89            return ComponentUtils.getAttribute(command, Attributes.popupClose) == null;
90          }
91        }
92      }
93      return false;
94    }
95  
96    private boolean isActivated() {
97      return activated;
98    }
99  
100 
101   @Override
102   public void processValidators(final FacesContext context) {
103     if (isSubmitted()) {
104       for (final Iterator it = getFacetsAndChildren(); it.hasNext();) {
105         final UIComponent childOrFacet = (UIComponent) it.next();
106         childOrFacet.processValidators(context);
107       }
108       //TODO: check if validation has failed and reset rendered if needed
109       if (context.getRenderResponse()) {
110         setActivated(true);
111       }
112     }
113   }
114 
115   @Override
116   public void processUpdates(final FacesContext context) {
117     if (isSubmitted()) {
118       for (final Iterator it = getFacetsAndChildren(); it.hasNext();) {
119         final UIComponent childOrFacet = (UIComponent) it.next();
120         childOrFacet.processUpdates(context);
121       }
122     }
123   }
124 
125   @Override
126   public Object saveState(final FacesContext context) {
127     final Object[] saveState = new Object[2];
128     saveState[0] = super.saveState(context);
129     saveState[1] = activated;
130     return saveState;
131   }
132 
133   @Override
134   public void restoreState(final FacesContext context, final Object savedState) {
135     final Object[] values = (Object[]) savedState;
136     super.restoreState(context, values[0]);
137     activated = (Boolean) values[1];
138   }
139 
140   @Override
141   public void encodeEnd(final FacesContext context) throws IOException {
142     super.encodeEnd(context);
143     activated = false;
144   }
145 }