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.apt.annotation.Preliminary;
23  import org.apache.myfaces.tobago.internal.component.AbstractUISplitLayout;
24  import org.apache.myfaces.tobago.internal.layout.LayoutUtils;
25  import org.apache.myfaces.tobago.renderkit.css.Style;
26  import org.apache.myfaces.tobago.renderkit.html.DataAttributes;
27  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
28  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
29  import org.apache.myfaces.tobago.renderkit.html.HtmlInputTypes;
30  import org.apache.myfaces.tobago.renderkit.html.MarkupLanguageAttributes;
31  import org.apache.myfaces.tobago.internal.renderkit.renderer.GridLayoutRenderer;
32  import org.apache.myfaces.tobago.internal.util.RenderUtils;
33  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  import javax.faces.component.UIComponent;
38  import javax.faces.context.FacesContext;
39  import java.io.IOException;
40  import java.util.List;
41  import java.util.Map;
42  
43  /**
44   * <p>
45   * WARNING: This component is preliminary and may be changed without a major release.
46   * </p>
47   */
48  @Preliminary
49  public class SplitLayoutRenderer extends GridLayoutRenderer {
50  
51    private static final Logger LOG = LoggerFactory.getLogger(SplitLayoutRenderer.class);
52    private static final String POSITION_ID_POSTFIX = "_spLP";
53  
54    // todo: put as enum to DataAttributes, when moved from sandbox to core
55    private static final MarkupLanguageAttributes SPLIT_LAYOUT = DataAttributes.dynamic("tobago-split-layout");
56  
57    // todo: put as enum to DataAttributes, when moved from sandbox to core
58    private static final MarkupLanguageAttributes SPLIT_LAYOUT_CONTAINMENT
59        = DataAttributes.dynamic("data-tobago-split-layout-containment");
60  
61    @Override
62    public void decode(final FacesContext facesContext, final UIComponent component) {
63      final String sourceId = facesContext.getExternalContext().getRequestParameterMap().get("javax.faces.source");
64      final String clientId = component.getClientId();
65      if (clientId.equals(sourceId)) {
66        // only decode and update layout at resize request
67        final Map<String, String> parameterMap = facesContext.getExternalContext().getRequestParameterMap();
68        final String position = parameterMap.get(clientId + POSITION_ID_POSTFIX);
69        ((AbstractUISplitLayout) component).updateLayout(Integer.parseInt(position));
70      }
71    }
72  
73    @Override
74    public void encodeChildren(final FacesContext facesContext, final UIComponent component) throws IOException {
75      final UIComponent parent = component.getParent();
76      if (parent.isRendered()) {
77        final List<UIComponent> components = LayoutUtils.findLayoutChildren(parent);
78        if (components.size() != 2) {
79          LOG.warn("Illegal component count in splitLayout: {}", components.size());
80        }
81        RenderUtils.encode(facesContext, components.get(0));
82        RenderUtils.encode(facesContext, components.get(1));
83        if (components.get(0).isRendered() && components.get(1).isRendered()) {
84          // only when both components are rendered
85          encodeHandle(facesContext, (AbstractUISplitLayout) component);
86        }
87      }
88    }
89  
90    protected void encodeHandle(final FacesContext facesContext, final AbstractUISplitLayout layout) throws IOException {
91      final String id = layout.getClientId(facesContext);
92  
93      final TobagoResponseWriter writer = getResponseWriter(facesContext);
94      writer.startElement(HtmlElements.SPAN);
95      writer.writeIdAttribute(id);
96      writer.writeAttribute(SPLIT_LAYOUT, layout.getOrientation().name(), false);
97      writer.writeAttribute(SPLIT_LAYOUT_CONTAINMENT, createDraggableContainment(layout), true);
98  /* XXX todo not implemented currently
99      final Style style = calculateHandleStyle(layout);
100     writer.writeStyleAttribute(style);
101     writer.writeClassAttribute(Classes.create(layout, layout.getOrientation().name()));
102 
103     final int position;
104     if (layout.getOrientation() == Orientation.horizontal) {
105       position = style.getLeft().getPixel();
106     } else {
107       position = style.getTop().getPixel();
108     }
109 */
110     writer.startElement(HtmlElements.INPUT);
111     writer.writeIdAttribute(id + POSITION_ID_POSTFIX);
112     writer.writeNameAttribute(id + POSITION_ID_POSTFIX);
113     writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.HIDDEN);
114 //    writer.writeAttribute(HtmlAttributes.value, Integer.toString(position), false);
115     writer.endElement(HtmlElements.INPUT);
116     writer.endElement(HtmlElements.SPAN);
117   }
118 
119   private String createDraggableContainment(final AbstractUISplitLayout layout) {
120 /* XXX todo not implemented currently
121     final UIComponent parent = layout.getParent();
122     final LayoutContainer container = (LayoutContainer) parent;
123     final List<UIComponent> components = LayoutUtils.findLayoutChildren(parent);
124     final Style firstComponent = ((Visual) components.get(0)).getStyle();
125     final Style secondComponent = ((Visual) components.get(1)).getStyle();
126 
127     Measure minimum;
128 
129     if (layout.getOrientation() == Orientation.horizontal) {
130       minimum = firstComponent.getMinWidth();
131       final int minimumSize1 = minimum != null ? minimum.getPixel() : 0;
132       minimum = secondComponent.getMinWidth();
133       final int minimumSize2 = minimum != null ? minimum.getPixel() : 0;
134       final int totalSize = container.getWidth().getPixel();
135       return "[" + minimumSize1 + ", 0, " + (totalSize - minimumSize2) + ", 0]";
136     } else {
137       minimum = firstComponent.getMinHeight();
138       final int minimumSize1 = minimum != null ? minimum.getPixel() : 0;
139       minimum = secondComponent.getMinHeight();
140       final int minimumSize2 = minimum != null ? minimum.getPixel() : 0;
141       final int totalSize = container.getHeight().getPixel();
142       return "[0, " + minimumSize1 + ", 0, " + (totalSize - minimumSize2) + "]";
143     }
144 */
145     return "";
146   }
147 
148   private Style calculateHandleStyle(final AbstractUISplitLayout layout) {
149 /* XXX todo not implemented currently
150     final UIComponent parent = layout.getParent();
151     final LayoutContainer container = (LayoutContainer) parent;
152     final LayoutComponent secondComponent = (LayoutComponent) LayoutUtils.findLayoutChildren(parent).get(1);
153     final Style style = new Style();
154 
155     if (layout.getOrientation() == Orientation.horizontal) {
156       style.setWidth(Measure.valueOf(5));
157       style.setHeight(container.getCurrentHeight());
158       style.setLeft(Measure.valueOf(secondComponent.getLeft().subtract(5)));
159       style.setTop(Measure.valueOf(0));
160     } else {
161       style.setWidth(container.getCurrentWidth());
162       style.setHeight(Measure.valueOf(5));
163       style.setLeft(Measure.valueOf(0));
164       style.setTop(Measure.valueOf(Measure.valueOf(secondComponent.getTop().subtract(5))));
165     }
166     // todo: use CSS class
167     style.setDisplay(Display.block);
168     style.setPosition(Position.absolute);
169     return style;
170 */
171     return null;
172   }
173 }