001    package org.apache.myfaces.tobago.renderkit.html.speyside.standard.tag;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    /*
021     * Created 07.02.2003 16:00:00.
022     * $Id: BoxRenderer.java 655629 2008-05-12 20:17:45Z bommel $
023     */
024    
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_ICON_SIZE;
028    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_LABEL;
029    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_LABEL_POSITION;
030    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_STYLE;
031    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_SUPPPRESS_TOOLBAR_CONTAINER;
032    import static org.apache.myfaces.tobago.TobagoConstants.FACET_LABEL;
033    import static org.apache.myfaces.tobago.TobagoConstants.FACET_TOOL_BAR;
034    import static org.apache.myfaces.tobago.TobagoConstants.SUBCOMPONENT_SEP;
035    import org.apache.myfaces.tobago.ajax.api.AjaxRenderer;
036    import org.apache.myfaces.tobago.ajax.api.AjaxUtils;
037    import org.apache.myfaces.tobago.component.SupportsMarkup;
038    import org.apache.myfaces.tobago.component.UIToolBar;
039    import org.apache.myfaces.tobago.renderkit.BoxRendererBase;
040    import org.apache.myfaces.tobago.renderkit.util.RenderUtil;
041    import org.apache.myfaces.tobago.renderkit.html.HtmlConstants;
042    import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtil;
043    import org.apache.myfaces.tobago.renderkit.html.HtmlStyleMap;
044    import org.apache.myfaces.tobago.renderkit.html.StyleClasses;
045    import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
046    
047    import javax.faces.component.UIComponent;
048    import javax.faces.component.UIPanel;
049    import javax.faces.context.FacesContext;
050    import java.io.IOException;
051    import java.util.Map;
052    
053    public class BoxRenderer extends BoxRendererBase implements AjaxRenderer {
054    
055      private static final Log LOG = LogFactory.getLog(BoxRenderer.class);
056      public static final String CONTENT_INNER = SUBCOMPONENT_SEP + "content-inner";
057      public static final String HEADER = SUBCOMPONENT_SEP + "header";
058    
059      public void prepareRender(FacesContext facesContext, UIComponent component) throws IOException {
060        super.prepareRender(facesContext, component);
061        HtmlRendererUtil.renderDojoDndSource(facesContext, component);
062      }
063    
064      public int getFixedHeight(FacesContext facesContext, UIComponent component) {
065        return super.getFixedHeight(facesContext, component);
066      }
067    
068      public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException {
069    
070        TobagoResponseWriter writer = HtmlRendererUtil.getTobagoResponseWriter(facesContext);
071        HtmlStyleMap style = (HtmlStyleMap) component.getAttributes().get(ATTR_STYLE);
072    
073        if (style != null) {
074          Integer styleHeight = style.getInt("height");
075          if (styleHeight != null) {
076            style.put("height", styleHeight-1);
077          }
078        }
079    
080        String clientId = component.getClientId(facesContext);
081        writer.startElement(HtmlConstants.DIV, component);
082        HtmlRendererUtil.renderDojoDndItem(component, writer, true);
083        writer.writeClassAttribute();
084        writer.writeIdAttribute(clientId);
085        if (style != null) {
086          writer.writeStyleAttribute(style);
087        }
088        writer.writeJavascript("Tobago.addAjaxComponent(\"" + clientId + "\");");
089        
090        encodeBeginInner(facesContext, writer, component);
091      }
092    
093      private void encodeBeginInner(FacesContext facesContext,
094          TobagoResponseWriter writer, UIComponent component) throws IOException {
095        HtmlStyleMap innerStyle = HtmlRendererUtil.prepareInnerStyle(component);
096    
097        renderBoxHeader(facesContext, writer, component);
098    
099    
100        writer.startElement(HtmlConstants.DIV, component);
101        StyleClasses contentClasses = new StyleClasses();
102        contentClasses.addClass("box", "content");
103        if (component instanceof SupportsMarkup) {
104          contentClasses.addMarkupClass((SupportsMarkup) component, "box", "content");
105        }
106        writer.writeClassAttribute(contentClasses);
107    
108        writer.startElement(HtmlConstants.DIV, component);
109        String id = component.getClientId(facesContext) + CONTENT_INNER; 
110        writer.writeIdAttribute(id);
111        StyleClasses contentInnerClasses = new StyleClasses();
112        contentInnerClasses.addClass("box", "content-inner");
113        contentInnerClasses.addFullQualifiedClass("dojoDndItem");
114        if (component instanceof SupportsMarkup) {
115          contentInnerClasses.addMarkupClass((SupportsMarkup) component, "box", "content-inner");
116        }
117        writer.writeClassAttribute(contentInnerClasses);
118        writer.writeStyleAttribute(innerStyle);
119      }
120    
121    
122      protected void renderBoxHeader(FacesContext facesContext,
123          TobagoResponseWriter writer, UIComponent component) throws IOException {
124    
125        writer.startElement(HtmlConstants.DIV, component);
126        StyleClasses headerClasses = new StyleClasses();
127        headerClasses.addClass("box", "header");
128        if (component instanceof SupportsMarkup) {
129          headerClasses.addMarkupClass((SupportsMarkup) component, "box", "header");
130        }
131        writer.writeClassAttribute(headerClasses);
132        String id = component.getClientId(facesContext) + HEADER;
133        writer.writeIdAttribute(id);
134        UIComponent label = component.getFacet(FACET_LABEL);
135        writer.startElement(HtmlConstants.SPAN, null);
136        writer.writeClassAttribute("tobago-box-header-label");
137        String labelString
138            = (String) component.getAttributes().get(ATTR_LABEL);
139        if (label != null) {
140          RenderUtil.encode(facesContext, label);
141        } else if (labelString != null) {
142          writer.writeText(labelString);
143        }
144        writer.endElement(HtmlConstants.SPAN);
145    
146        UIPanel toolbar = (UIPanel) component.getFacet(FACET_TOOL_BAR);
147        if (toolbar != null) {
148          renderToolbar(facesContext, writer, toolbar);
149        }
150        writer.endElement(HtmlConstants.DIV);
151      }
152    
153      public void encodeEnd(FacesContext facesContext,
154          UIComponent component) throws IOException {
155        TobagoResponseWriter writer = HtmlRendererUtil.getTobagoResponseWriter(facesContext);
156        encodeEndInner(writer);
157        writer.endElement(HtmlConstants.DIV);
158      }
159    
160      private void encodeEndInner(TobagoResponseWriter writer) throws IOException {
161        writer.endElement(HtmlConstants.DIV);
162        writer.endElement(HtmlConstants.DIV);
163      }
164    
165      protected void renderToolbar(
166          FacesContext facesContext, TobagoResponseWriter writer, UIPanel toolbar) throws IOException {
167        final Map attributes = toolbar.getAttributes();
168        String className = "tobago-box-header-toolbar-div";
169        if (UIToolBar.LABEL_OFF.equals(attributes.get(ATTR_LABEL_POSITION))) {
170          className += " tobago-box-header-toolbar-label_off";
171        }
172        writer.startElement(HtmlConstants.DIV, null);
173        writer.writeClassAttribute(className);
174        attributes.put(ATTR_SUPPPRESS_TOOLBAR_CONTAINER, Boolean.TRUE);
175        if (UIToolBar.LABEL_BOTTOM.equals(attributes.get(ATTR_LABEL_POSITION))) {
176          attributes.put(ATTR_LABEL_POSITION, UIToolBar.LABEL_RIGHT);
177        }
178        if (UIToolBar.ICON_BIG.equals(attributes.get(ATTR_ICON_SIZE))) {
179          attributes.put(ATTR_ICON_SIZE, UIToolBar.ICON_SMALL);
180        }
181        RenderUtil.encode(facesContext, toolbar);
182        writer.endElement(HtmlConstants.DIV);
183      }
184    
185      public void encodeAjax(FacesContext facesContext, UIComponent component) throws IOException {
186        AjaxUtils.checkParamValidity(facesContext, component, UIPanel.class);
187        TobagoResponseWriter writer = HtmlRendererUtil.getTobagoResponseWriter(facesContext);
188    
189        encodeBeginInner(facesContext, writer, component);
190        component.encodeChildren(facesContext);
191        encodeEndInner(writer);
192        facesContext.responseComplete();
193      }
194    }
195