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