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