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.renderkit.html.standard.standard.tag.GridLayoutRenderer;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import org.apache.myfaces.tobago.internal.component.AbstractUISplitLayout;
27  import org.apache.myfaces.tobago.layout.Display;
28  import org.apache.myfaces.tobago.layout.LayoutComponent;
29  import org.apache.myfaces.tobago.layout.LayoutContainer;
30  import org.apache.myfaces.tobago.layout.Measure;
31  import org.apache.myfaces.tobago.renderkit.css.Classes;
32  import org.apache.myfaces.tobago.renderkit.css.Position;
33  import org.apache.myfaces.tobago.renderkit.css.Style;
34  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
35  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
36  import org.apache.myfaces.tobago.renderkit.html.HtmlInputTypes;
37  import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
38  import org.apache.myfaces.tobago.renderkit.util.RenderUtils;
39  import org.apache.myfaces.tobago.util.ComponentUtils;
40  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
41  
42  import javax.faces.component.UIComponent;
43  import javax.faces.context.FacesContext;
44  import java.io.IOException;
45  import java.util.List;
46  import java.util.Map;
47  
48  public class SplitLayoutRenderer extends GridLayoutRenderer {
49    
50    private static final Logger LOG = LoggerFactory.getLogger(SplitLayoutRenderer.class);
51    private static final String POSITION_ID_POSTFIX = "_spLP";
52  
53    @Override
54    public void decode(final FacesContext facesContext, final UIComponent component) {
55      final String clientId = component.getClientId();
56      if (clientId.equals(ComponentUtils.findPage(facesContext).getActionId())) {
57        // only decode and update layout at resize request
58        final Map<String, String> parameterMap = facesContext.getExternalContext().getRequestParameterMap();
59        final String position = parameterMap.get(clientId + POSITION_ID_POSTFIX);
60        ((AbstractUISplitLayout) component).updateLayout(Integer.parseInt(position));
61      }
62    }
63  
64    @Override
65    public void encodeChildren(final FacesContext facesContext, final UIComponent component) throws IOException {
66      final LayoutContainer container = (LayoutContainer) ((AbstractUISplitLayout) component).getParent();
67      if (!((LayoutContainer) container).isLayoutChildren()) {
68        return;
69      } else {
70        final List<LayoutComponent> components = container.getComponents();
71        if (components.size() != 2) {
72          LOG.warn("Illegal component count in splitLayout: {}", components.size());
73        }
74        RenderUtils.encode(facesContext, (UIComponent) components.get(0));
75        RenderUtils.encode(facesContext, (UIComponent) components.get(1));
76        if (((UIComponent) components.get(0)).isRendered() && ((UIComponent) components.get(1)).isRendered()) {
77          // only when both components are rendered
78          encodeHandle(facesContext, (AbstractUISplitLayout) component);
79        }
80      }
81    }
82  
83    protected void encodeHandle(final FacesContext facesContext, final AbstractUISplitLayout layout) throws IOException {
84      final String id = layout.getClientId(facesContext);
85      
86      final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
87      writer.startElement(HtmlElements.SPAN, layout);
88      writer.writeIdAttribute(id);
89      writer.writeAttribute("data-tobago-split-layout", layout.getOrientation().toLowerCase(), true);
90      writer.writeAttribute("data-tobago-split-layout-containment", createDraggableContainment(layout), true);
91      final Style style = calculateHandleStyle(layout);
92      writer.writeStyleAttribute(style);
93      writer.writeClassAttribute(Classes.create(layout, layout.getOrientation().toLowerCase()));
94  
95      final int position;
96      if (AbstractUISplitLayout.HORIZONTAL.equals(layout.getOrientation())) {
97        position = style.getLeft().getPixel();
98      } else {
99        position = style.getTop().getPixel();
100     }
101     writer.startElement(HtmlElements.INPUT, null);
102     writer.writeIdAttribute(id + POSITION_ID_POSTFIX);
103     writer.writeNameAttribute(id + POSITION_ID_POSTFIX);
104     writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.HIDDEN, false);
105     writer.writeAttribute(HtmlAttributes.VALUE, Integer.toString(position), false);
106     writer.endElement(HtmlElements.INPUT);
107 
108 
109     writer.endElement(HtmlElements.SPAN);
110 
111   }
112 
113   private String createDraggableContainment(final AbstractUISplitLayout layout) {
114     final LayoutContainer container = (LayoutContainer) ((AbstractUISplitLayout) layout).getParent();
115     final LayoutComponent firstComponent = container.getComponents().get(0);
116     final LayoutComponent secondComponent = container.getComponents().get(1);
117 
118     if (AbstractUISplitLayout.HORIZONTAL.equals(layout.getOrientation())) {
119       final int minimumSize1 = firstComponent.getMinimumWidth().getPixel();
120       final int minimumSize2 = secondComponent.getMinimumWidth().getPixel();
121       final int totalSize = container.getCurrentWidth().getPixel();
122       return new StringBuilder("[").append(minimumSize1).append(", 0, ").append(totalSize-minimumSize2).append(", 0]")
123           .toString();
124     } else {
125       final int minimumSize1 = firstComponent.getMinimumHeight().getPixel();
126       final int minimumSize2 = secondComponent.getMinimumHeight().getPixel();
127       final int totalSize = container.getCurrentHeight().getPixel();
128       return new StringBuilder("[0, ").append(minimumSize1).append(", 0, ").append(totalSize-minimumSize2).append("]")
129           .toString();
130     }
131   }
132 
133   private Style calculateHandleStyle(final AbstractUISplitLayout layout) {
134     final LayoutContainer container = (LayoutContainer) ((AbstractUISplitLayout) layout).getParent();
135     final LayoutComponent secondComponent = container.getComponents().get(1);
136     final Style style = new Style();
137     if (AbstractUISplitLayout.HORIZONTAL.equals(layout.getOrientation())) {
138       style.setWidth(Measure.valueOf(5));
139       style.setHeight(container.getCurrentHeight());
140       style.setLeft(Measure.valueOf(secondComponent.getLeft().subtract(5)));
141       style.setTop(Measure.valueOf(0));
142     } else {
143       style.setWidth(container.getCurrentWidth());
144       style.setHeight(Measure.valueOf(5));
145       style.setLeft(Measure.valueOf(0));
146       style.setTop(Measure.valueOf(Measure.valueOf(secondComponent.getTop().subtract(5))));
147     }
148     style.setDisplay(Display.BLOCK);
149     style.setPosition(Position.ABSOLUTE);
150     return style;
151   }
152 }