org.primefaces.component.tabmenu.TabMenuRenderer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of primefaces Show documentation
Show all versions of primefaces Show documentation
PrimeFaces is one of the most popular UI libraries in Java EE Ecosystem and widely used by software companies, world renowned brands, banks, financial institutions, insurance companies, universities and more.
/**
* Copyright 2009-2018 PrimeTek.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.primefaces.component.tabmenu;
import java.io.IOException;
import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import org.primefaces.component.menu.AbstractMenu;
import org.primefaces.component.menu.BaseMenuRenderer;
import org.primefaces.model.menu.MenuElement;
import org.primefaces.model.menu.MenuItem;
import org.primefaces.util.WidgetBuilder;
public class TabMenuRenderer extends BaseMenuRenderer {
@Override
protected void encodeScript(FacesContext context, AbstractMenu abstractMenu) throws IOException {
TabMenu menu = (TabMenu) abstractMenu;
String clientId = menu.getClientId(context);
WidgetBuilder wb = getWidgetBuilder(context);
wb.init("TabMenu", menu.resolveWidgetVar(), clientId);
wb.finish();
}
@Override
protected void encodeMarkup(FacesContext context, AbstractMenu component) throws IOException {
ResponseWriter writer = context.getResponseWriter();
TabMenu menu = (TabMenu) component;
String clientId = menu.getClientId(context);
String styleClass = menu.getStyleClass();
styleClass = styleClass == null ? TabMenu.CONTAINER_CLASS : TabMenu.CONTAINER_CLASS + " " + styleClass;
int activeIndex = menu.getActiveIndex();
List elements = menu.getElements();
writer.startElement("div", menu);
writer.writeAttribute("id", clientId, null);
writer.writeAttribute("class", styleClass, "styleClass");
if (menu.getStyle() != null) {
writer.writeAttribute("style", menu.getStyle(), "style");
}
writer.startElement("ul", null);
writer.writeAttribute("class", TabMenu.NAVIGATOR_CLASS, null);
writer.writeAttribute("role", "tablist", null);
int i = 0;
if (elements != null && !elements.isEmpty()) {
for (Object element : elements) {
if (element instanceof MenuElement) {
if (((MenuElement) element).isRendered() && (element instanceof MenuItem)) {
encodeItem(context, menu, (MenuItem) element, (i == activeIndex));
i++;
}
}
}
}
writer.endElement("ul");
writer.endElement("div");
}
protected void encodeItem(FacesContext context, TabMenu menu, MenuItem item, boolean active) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String containerStyle = item.getContainerStyle();
String containerStyleClass = item.getContainerStyleClass();
String containerClass = active ? TabMenu.ACTIVE_TAB_HEADER_CLASS : TabMenu.INACTIVE_TAB_HEADER_CLASS;
if (item.getIcon() != null) {
containerClass += " ui-tabmenuitem-hasicon";
}
if (containerStyleClass != null) {
containerClass = containerClass + " " + containerStyleClass;
}
//header container
writer.startElement("li", null);
writer.writeAttribute("class", containerClass, null);
writer.writeAttribute("role", "tab", null);
writer.writeAttribute("aria-expanded", String.valueOf(active), null);
writer.writeAttribute("aria-selected", String.valueOf(active), null);
if (containerStyle != null) {
writer.writeAttribute("style", containerStyle, null);
}
encodeMenuItem(context, menu, item);
writer.endElement("li");
}
@Override
public void encodeChildren(FacesContext context, UIComponent component) throws IOException {
// Do nothing
}
@Override
public boolean getRendersChildren() {
return true;
}
}