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.RendererTypes;
23  import org.apache.myfaces.tobago.component.UIMenu;
24  import org.apache.myfaces.tobago.context.Markup;
25  import org.apache.myfaces.tobago.context.ResourceManagerUtils;
26  import org.apache.myfaces.tobago.internal.util.AccessKeyMap;
27  import org.apache.myfaces.tobago.renderkit.LabelWithAccessKey;
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.HtmlAttributes;
32  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
33  import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
34  import org.apache.myfaces.tobago.util.ComponentUtils;
35  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  import javax.faces.component.UIComponent;
40  import javax.faces.context.FacesContext;
41  import java.io.IOException;
42  
43  public class MenuRenderer extends LayoutComponentRendererBase {
44  
45    private static final Logger LOG = LoggerFactory.getLogger(MenuRenderer.class);
46  
47    @Override
48    public void prepareRender(FacesContext facesContext, UIComponent component) throws IOException {
49      super.prepareRender(facesContext, component);
50  
51      final UIMenu menu = (UIMenu) component;
52      final boolean firstLevel = !RendererTypes.MENU.equals(menu.getParent().getRendererType());
53      if (firstLevel) {
54        ComponentUtils.addCurrentMarkup(menu, Markup.TOP);
55      }
56    }
57  
58    @Override
59    public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException {
60  
61      final UIMenu menu = (UIMenu) component;
62      TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
63  
64      final boolean disabled = menu.isDisabled();
65      final boolean firstLevel = !RendererTypes.MENU.equals(menu.getParent().getRendererType());
66      final boolean isParentMenu = menu.getChildCount() > 0; // todo: may be not correct
67  
68      writer.startElement(HtmlElements.LI, menu);
69      writer.writeClassAttribute(Classes.create(menu));
70      if (menu.getImage() != null) {
71        Style style = new Style();
72        style.setBackgroundImage("url('"
73            + ResourceManagerUtils.getImageOrDisabledImageWithPath(facesContext, menu.getImage(), menu.isDisabled())
74            + "')");
75        writer.writeStyleAttribute(style);
76      }
77      writer.startElement(HtmlElements.A, menu);
78      writer.writeAttribute(HtmlAttributes.HREF, "#", false);
79      if (isParentMenu && !firstLevel) {
80        writer.writeClassAttribute(Classes.create(menu, "subitem-ancor"));
81      }
82      if (component != null && !component.isTransient()) {
83        writer.writeIdAttribute(component.getClientId(facesContext));
84      }
85  
86      LabelWithAccessKey label = new LabelWithAccessKey(menu);
87      if (label.getText() != null) {
88        if (label.getAccessKey() != null) {
89          if (LOG.isInfoEnabled()
90              && !AccessKeyMap.addAccessKey(facesContext, label.getAccessKey())) {
91            LOG.info("duplicated accessKey : " + label.getAccessKey());
92          }
93          if (!disabled && label.getAccessKey() != null) {
94            writer.writeAttribute(HtmlAttributes.ACCESSKEY, Character.toString(label.getAccessKey()), false);
95          }
96        }
97        HtmlRendererUtils.writeLabelWithAccessKey(writer, label);
98      }
99      writer.endElement(HtmlElements.A);
100     if (isParentMenu) {
101       if (!firstLevel) {
102         writer.startElement(HtmlElements.IMG, menu);
103         String arrow = ResourceManagerUtils
104             .getImageOrDisabledImageWithPath(facesContext, "image/MenuArrow.gif", menu.isDisabled());
105         writer.writeAttribute(HtmlAttributes.SRC, arrow, false);
106         writer.writeClassAttribute(Classes.create(menu, "subitem-arrow"));
107         writer.endElement(HtmlElements.IMG);
108       }
109       writer.startElement(HtmlElements.OL, menu);
110     }
111   }
112 
113   @Override
114   public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
115 
116     UIMenu menu = (UIMenu) component;
117     TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
118 
119     boolean isParentMenu = menu.getChildCount() > 0; // todo: may be not correct
120     if (isParentMenu) {
121       writer.endElement(HtmlElements.OL);
122     }
123     writer.endElement(HtmlElements.LI);
124 
125   }
126 
127 }