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.renderkit.html.scarborough.standard.tag;
21  
22  import org.apache.myfaces.tobago.component.UIPopup;
23  import org.apache.myfaces.tobago.context.Markup;
24  import org.apache.myfaces.tobago.internal.component.AbstractUIPage;
25  import org.apache.myfaces.tobago.internal.layout.LayoutContext;
26  import org.apache.myfaces.tobago.internal.util.FacesContextUtils;
27  import org.apache.myfaces.tobago.layout.Measure;
28  import org.apache.myfaces.tobago.renderkit.LayoutComponentRendererBase;
29  import org.apache.myfaces.tobago.renderkit.css.Classes;
30  import org.apache.myfaces.tobago.renderkit.css.Style;
31  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
32  import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
33  import org.apache.myfaces.tobago.util.ComponentUtils;
34  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  import javax.faces.component.UIComponent;
39  import javax.faces.context.FacesContext;
40  import java.io.IOException;
41  
42  public class PopupRenderer extends LayoutComponentRendererBase {
43  
44    private static final Logger LOG = LoggerFactory.getLogger(PopupRenderer.class);
45  
46    @Override
47    public boolean getRendersChildren() {
48      return true;
49    }
50  
51    @Override
52    public void prepareRender(FacesContext facesContext, UIComponent component) throws IOException {
53  
54      final UIPopup popup = (UIPopup) component;
55      FacesContextUtils.addPopup(facesContext, popup);
56  
57      super.prepareRender(facesContext, popup);
58  
59      if (popup.isModal()) {
60        ComponentUtils.addCurrentMarkup(popup, Markup.MODAL);
61      }
62    }
63  
64    @Override
65    public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException {
66  
67      final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
68      final UIPopup popup = (UIPopup) component;
69  
70      final LayoutContext layoutContext = new LayoutContext(popup);
71      layoutContext.layout();
72  
73      // XXX fixing invisible popups
74      if (popup.getCurrentWidth() == null || popup.getCurrentWidth().equals(Measure.ZERO)) {
75        LOG.warn("Undefined width of popup with id='" + popup.getClientId(facesContext) + "'");
76        popup.setCurrentWidth(getPreferredWidth(facesContext, popup));
77      }
78      if (popup.getCurrentHeight() == null || popup.getCurrentHeight().equals(Measure.ZERO)) {
79        LOG.warn("Undefined height of popup with id='" + popup.getClientId(facesContext) + "'");
80        popup.setCurrentHeight(getPreferredHeight(facesContext, popup));
81      }
82  
83      final String clientId = popup.getClientId(facesContext);
84  
85      // XXX May be computed in the "Layout Manager Phase"
86      AbstractUIPage page = ComponentUtils.findPage(facesContext);
87      if (popup.getLeft() == null) {
88        popup.setLeft(page.getCurrentWidth().subtract(popup.getCurrentWidth()).divide(2));
89      }
90      if (popup.getTop() == null) {
91        popup.setTop(page.getCurrentHeight().subtract(popup.getCurrentHeight()).divide(2));
92      }
93  
94      writer.startElement(HtmlElements.DIV, popup);
95      writer.writeIdAttribute(clientId);
96      HtmlRendererUtils.writeDataAttributes(facesContext, writer, popup);
97      Style style = new Style(facesContext, popup);
98      Integer zIndex = popup.getZIndex();
99      if (zIndex == null) {
100       zIndex = 100;
101       LOG.warn("No z-index found for UIPopup. Set to " + zIndex);
102     }
103     style.setZIndex(zIndex);
104     writer.writeStyleAttribute(style);
105     writer.writeClassAttribute(Classes.create(popup));
106   }
107 
108   @Override
109   public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
110     TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
111     writer.endElement(HtmlElements.DIV);
112   }
113 }