blob: c67e093982567ed763fee7862f11b335ee6c44a5 [file] [log] [blame]
/*
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
/*
* $Id: PaneComponent.java,v 1.1 2005/11/03 02:59:50 SherryShen Exp $
*/
package components.components;
import com.sun.org.apache.commons.logging.Log;
import com.sun.org.apache.commons.logging.LogFactory;
import jakarta.faces.component.StateHolder;
import jakarta.faces.component.UIComponent;
import jakarta.faces.component.UIComponentBase;
import jakarta.faces.context.FacesContext;
import jakarta.faces.event.FacesEvent;
import jakarta.faces.event.FacesListener;
import java.util.Iterator;
/**
* <p>Component designed to contain child components (and possibly other
* layout in a JSP environment) for things like a tabbed pane control.
*/
public class PaneComponent extends UIComponentBase {
private static Log log = LogFactory.getLog(PaneComponent.class);
// creates and adds a listener;
public PaneComponent() {
PaneSelectedListener listener = new PaneSelectedListener();
addFacesListener(listener);
}
/**
* <p>Return the component family for this component.
*/
public String getFamily() {
return ("Pane");
}
// Does this component render its own children?
public boolean getRendersChildren() {
return (true);
}
public void processDecodes(FacesContext context) {
// Process all facets and children of this component
Iterator kids = getFacetsAndChildren();
while (kids.hasNext()) {
UIComponent kid = (UIComponent) kids.next();
kid.processDecodes(context);
}
// Process this component itself
try {
decode(context);
} catch (RuntimeException e) {
context.renderResponse();
throw e;
}
}
// Ignore update model requests
public void updateModel(FacesContext context) {
}
/**
* <p>Faces Listener implementation which sets the selected tab
* component
*/
public class PaneSelectedListener implements FacesListener, StateHolder {
public PaneSelectedListener() {
}
// process the event..
public void processPaneSelectedEvent(FacesEvent event) {
UIComponent source = event.getComponent();
PaneSelectedEvent pevent = (PaneSelectedEvent) event;
String id = pevent.getId();
boolean paneSelected = false;
// Find the parent tab control so we can set all tabs
// to "unselected";
UIComponent tabControl = findParentForRendererType(source,
"Tabbed");
int n = tabControl.getChildCount();
for (int i = 0; i < n; i++) {
PaneComponent pane = (PaneComponent) tabControl.getChildren()
.get(i);
if (pane.getId().equals(id)) {
pane.setRendered(true);
paneSelected = true;
} else {
pane.setRendered(false);
}
}
if (!paneSelected) {
log.warn("Cannot select pane for id=" + id + "," +
", selecting first pane");
((PaneComponent) tabControl.getChildren().get(0)).setRendered(
true);
}
}
// methods from StateHolder
public Object saveState(FacesContext context) {
return null;
}
public void restoreState(FacesContext context, Object state) {
}
public void setTransient(boolean newTransientValue) {
}
public boolean isTransient() {
return true;
}
}
private UIComponent findParentForRendererType(UIComponent component, String rendererType) {
Object facetParent = null;
UIComponent currentComponent = component;
// Search for an ancestor that is the specified renderer type;
// search includes the facets.
while (null != (currentComponent = currentComponent.getParent())) {
if (currentComponent.getRendererType().equals(rendererType)) {
break;
}
}
return currentComponent;
}
}