All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.omnifaces.component.tree.TreeFamily Maven / Gradle / Ivy

There is a newer version: 4.4.1
Show newest version
/*
 * Copyright OmniFaces
 *
 * 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
 *
 *     https://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.omnifaces.component.tree;

import static org.omnifaces.util.FacesLocal.isDevelopment;

import java.io.IOException;

import jakarta.faces.FacesException;
import jakarta.faces.component.UIComponentBase;
import jakarta.faces.context.FacesContext;
import jakarta.faces.event.PhaseId;

/**
 * Base class which is to be shared between all components of the Tree family.
 *
 * @author Bauke Scholtz
 */
public abstract class TreeFamily extends UIComponentBase {

	// Public constants -----------------------------------------------------------------------------------------------

	/** The standard component family. */
	public static final String COMPONENT_FAMILY = "org.omnifaces.component.tree";

	// Constructors ---------------------------------------------------------------------------------------------------

	/**
	 * The base constructor sets the renderer type to null as the components of the Tree family does not
	 * render anything by themselves.
	 */
	protected TreeFamily() {
		setRendererType(null);
	}

	// UIComponent overrides ------------------------------------------------------------------------------------------

	/**
	 * Returns {@link #COMPONENT_FAMILY}.
	 */
	@Override
	public String getFamily() {
		return COMPONENT_FAMILY;
	}

	/**
	 * Returns true.
	 */
	@Override
	public boolean getRendersChildren() {
		return true;
	}

	/**
	 * Calls {@link #process(FacesContext, PhaseId)} with {@link PhaseId#APPLY_REQUEST_VALUES}.
	 */
	@Override
	public void processDecodes(FacesContext context) {
		process(context, PhaseId.APPLY_REQUEST_VALUES);
	}

	/**
	 * Calls {@link #process(FacesContext, PhaseId)} with {@link PhaseId#PROCESS_VALIDATIONS}.
	 */
	@Override
	public void processValidators(FacesContext context) {
		process(context, PhaseId.PROCESS_VALIDATIONS);
	}

	/**
	 * Calls {@link #process(FacesContext, PhaseId)} with {@link PhaseId#UPDATE_MODEL_VALUES}.
	 */
	@Override
	public void processUpdates(FacesContext context) {
		process(context, PhaseId.UPDATE_MODEL_VALUES);
	}

	/**
	 * Calls {@link #validateHierarchy()} when project stage is Development and then
	 * calls {@link #process(FacesContext, PhaseId)} with {@link PhaseId#RENDER_RESPONSE}.
	 */
	@Override
	public void encodeChildren(FacesContext context) throws IOException {
		if (isDevelopment(context)) {
			validateHierarchy();
		}

		process(context, PhaseId.RENDER_RESPONSE);
	}

	// Actions --------------------------------------------------------------------------------------------------------

	/**
	 * Validate the component hierarchy. This should only be called when project stage is Development.
	 * @throws IllegalStateException When component hierarchy is wrong.
	 */
	protected abstract void validateHierarchy();

	/**
	 * Process the component according to the rules of the given phase ID.
	 * @param context The faces context to work with.
	 * @param phaseId The current phase ID.
	 */
	protected abstract void process(FacesContext context, PhaseId phaseId);

	/**
	 * Helper method to delegate the processing further to the {@link UIComponentBase} superclass which will handle
	 * all children.
	 * @param context The faces context to work with.
	 * @param phaseId The current phase ID.
	 */
	protected void processSuper(FacesContext context, PhaseId phaseId) {
		if (phaseId == PhaseId.APPLY_REQUEST_VALUES) {
			super.processDecodes(context);
		}
		else if (phaseId == PhaseId.PROCESS_VALIDATIONS) {
			super.processValidators(context);
		}
		else if (phaseId == PhaseId.UPDATE_MODEL_VALUES) {
			super.processUpdates(context);
		}
		else if (phaseId == PhaseId.RENDER_RESPONSE) {
			try {
				super.encodeChildren(context);
			}
			catch (IOException e) {
				throw new FacesException(e);
			}
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy