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.ComponentTypes;
24  import org.apache.myfaces.tobago.component.DeprecatedDimension;
25  import org.apache.myfaces.tobago.component.Facets;
26  import org.apache.myfaces.tobago.component.OnComponentCreated;
27  import org.apache.myfaces.tobago.component.OnComponentPopulated;
28  import org.apache.myfaces.tobago.component.Position;
29  import org.apache.myfaces.tobago.component.RendererTypes;
30  import org.apache.myfaces.tobago.internal.layout.LayoutUtils;
31  import org.apache.myfaces.tobago.internal.util.FacesContextUtils;
32  import org.apache.myfaces.tobago.layout.LayoutComponent;
33  import org.apache.myfaces.tobago.layout.LayoutContainer;
34  import org.apache.myfaces.tobago.layout.LayoutManager;
35  import org.apache.myfaces.tobago.layout.Measure;
36  import org.apache.myfaces.tobago.util.CreateComponentUtils;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  import javax.el.ValueExpression;
41  import javax.faces.component.NamingContainer;
42  import javax.faces.component.UICommand;
43  import javax.faces.component.UIComponent;
44  import javax.faces.component.UINamingContainer;
45  import javax.faces.context.FacesContext;
46  import java.io.IOException;
47  import java.util.Iterator;
48  import java.util.List;
49  
50  public abstract class AbstractUIPopup extends AbstractUIPanelBase
51      implements OnComponentCreated, OnComponentPopulated, NamingContainer,
52      DeprecatedDimension, Position, LayoutContainer {
53  
54    private static final Logger LOG = LoggerFactory.getLogger(AbstractUIPopup.class);
55  
56    private static final String Z_INDEX = AbstractUIPopup.class.getName() + ".Z_INDEX";
57  
58    private boolean activated;
59  
60    public void onComponentCreated(FacesContext facesContext, UIComponent parent) {
61      Integer zIndex = (Integer) facesContext.getExternalContext().getRequestMap().get(Z_INDEX);
62      if (zIndex == null) {
63        zIndex = 1;
64      } else {
65        zIndex++;
66      }
67      setZIndex(zIndex);
68      facesContext.getExternalContext().getRequestMap().put(Z_INDEX, zIndex);
69    }
70  
71    public void onComponentPopulated(FacesContext facesContext, UIComponent parent) {
72      if (getLayoutManager() == null) {
73        final AbstractUIGridLayout layoutManager = (AbstractUIGridLayout) CreateComponentUtils.createAndInitLayout(
74            facesContext, ComponentTypes.GRID_LAYOUT, RendererTypes.GRID_LAYOUT, parent);
75        setLayoutManager(layoutManager);
76      }
77    }
78  
79    public List<LayoutComponent> getComponents() {
80      return LayoutUtils.findLayoutChildren(this);
81    }
82  
83    public void setActivated(boolean activated) {
84      this.activated = activated;
85    }
86  
87    @Override
88    public void processDecodes(FacesContext facesContext) {
89      if (isSubmitted()) {
90        for (Iterator it = getFacetsAndChildren(); it.hasNext();) {
91          UIComponent childOrFacet = (UIComponent) it.next();
92          childOrFacet.processDecodes(facesContext);
93        }
94        try {
95          decode(facesContext);
96        } catch (RuntimeException e) {
97          facesContext.renderResponse();
98          throw e;
99        }
100       if (facesContext.getRenderResponse()) {
101         setActivated(true);
102       }
103     }
104   }
105 
106   @Override
107   public boolean isRendered() {
108     final ValueExpression expression = getValueExpression("rendered");
109     if (expression != null) {
110       FacesContext context = FacesContext.getCurrentInstance();
111       return (Boolean) expression.getValue(context.getELContext());
112     } else {
113       return isActivated() || isRedisplay();
114     }
115   }
116 
117   private boolean isSubmitted() {
118     final FacesContext facesContext = getFacesContext();
119     final String action = FacesContextUtils.getActionId(facesContext);
120     return action != null && action.startsWith(
121         getClientId(facesContext) + UINamingContainer.getSeparatorChar(facesContext));
122   }
123 
124   private boolean isRedisplay() {
125     if (isSubmitted()) {
126       String action = FacesContextUtils.getActionId(getFacesContext());
127       if (action != null) {
128         final UIComponent command = getFacesContext().getViewRoot().findComponent(
129             UINamingContainer.getSeparatorChar(getFacesContext()) + action);
130         if (command != null && command instanceof UICommand) {
131           return command.getAttributes().get(Attributes.POPUP_CLOSE) == null;
132         }
133       }
134     }
135     return false;
136   }
137 
138   private boolean isActivated() {
139     return activated;
140   }
141 
142 
143   @Override
144   public void processValidators(FacesContext context) {
145     if (isSubmitted()) {
146       for (Iterator it = getFacetsAndChildren(); it.hasNext();) {
147         UIComponent childOrFacet = (UIComponent) it.next();
148         childOrFacet.processValidators(context);
149       }
150       //TODO: check if validation has failed and reset rendered if needed
151       if (context.getRenderResponse()) {
152         setActivated(true);
153       }
154     }
155   }
156 
157   @Override
158   public void processUpdates(FacesContext context) {
159     if (isSubmitted()) {
160       for (Iterator it = getFacetsAndChildren(); it.hasNext();) {
161         UIComponent childOrFacet = (UIComponent) it.next();
162         childOrFacet.processUpdates(context);
163       }
164     }
165   }
166 
167   @Override
168   public Object saveState(FacesContext context) {
169     Object[] saveState = new Object[2];
170     saveState[0] = super.saveState(context);
171     saveState[1] = activated;
172     return saveState;
173   }
174 
175   @Override
176   public void restoreState(FacesContext context, Object savedState) {
177     Object[] values = (Object[]) savedState;
178     super.restoreState(context, values[0]);
179     activated = (Boolean) values[1];
180   }
181 
182   @Override
183   public void encodeEnd(FacesContext context) throws IOException {
184     super.encodeEnd(context);
185     activated = false;
186   }
187 
188   public LayoutManager getLayoutManager() {
189     return (LayoutManager) getFacet(Facets.LAYOUT);
190   }
191 
192   public void setLayoutManager(LayoutManager layoutManager) {
193     getFacets().put(Facets.LAYOUT, (AbstractUILayoutBase) layoutManager);
194   }
195 
196   public boolean isLayoutChildren() {
197     return isRendered();
198   }
199   
200   public abstract Measure getWidth();
201 
202   public abstract void setWidth(Measure width);
203 
204   public abstract Measure getHeight();
205 
206   public abstract void setHeight(Measure height);
207 
208   public abstract Measure getTop();
209 
210   public abstract void setTop(Measure top);
211 
212   public abstract Measure getLeft();
213 
214   public abstract void setLeft(Measure left);
215 
216   public abstract void setZIndex(Integer zIndex);
217 }