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

org.xlcloud.console.utils.DynamicDialogComponentBean Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012 AMG.lab, a Bull Group Company
 * 
 * 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.xlcloud.console.utils;

import java.util.HashSet;
import java.util.Set;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseId;
import javax.inject.Named;

import org.apache.commons.lang3.StringUtils;
import org.primefaces.component.dialog.Dialog;
import org.xlcloud.console.extensions.scope.ViewScoped;

/**
 * This bean allows to verify whether the primefaces Dialog content is rendered or not.
 * 
 * This is useful when you have dialog with "dynamic" attribute set to "true". All primefaces component embedded into dialog component
 *  will not be rendered until dialog is shown. Unfortunately ui:include, ui:decorate instructions are invoked earlier and do not follow this rule.
 *  The only way to stop rendering content inside ui instructions is to wrap the content with:
 *      {@code ...}. 
 *  
 * @author Konrad Król, AMG.net
 */
@Named
@ViewScoped
public class DynamicDialogComponentBean {

    private Set activeDynamicDialogsIds = new HashSet();
    
    /**
     * Indicates whether the content of the dynamic dialog is rendered or not.
     * 
     * @param dynamicDialogId
     *  id of the component
     * @return {@code true} if dialog content is rendered, {@code false} otherwise
     */
    public boolean isContentRendered(String dynamicDialogId) {
        if(!getFacesContext().getCurrentPhaseId().equals(PhaseId.RENDER_RESPONSE)) {
            return true; // react only for "RENDER RESPONSE" phase
        }
        
        Dialog dialog = findDialogByComponentId(dynamicDialogId);
        
        if(activeDynamicDialogsIds.contains(dynamicDialogId)) {
            return true; // return true if dialog has been already opened
        }
        
        if(dialog == null) {
            return false; // if there is no such dialog return false
        } else if(!dialog.isDynamic()) {
            return true; // if dialog is not dynamic return true
        } else {
            boolean isContentLoadRequest = dialog.isContentLoadRequest(getFacesContext());
            if(isContentLoadRequest) {
                activeDynamicDialogsIds.add(dynamicDialogId);
            }
            return isContentLoadRequest;     // otherwise return true only if this request is a request that opens the dialog
        }
    }

    private Dialog findDialogByComponentId(String componentId) {
        if(StringUtils.isNotEmpty(componentId)) {
            UIComponent component = getFacesContext().getViewRoot().findComponent(componentId);
            return ((component != null && component instanceof Dialog) ? (Dialog) component : null );
        } else {
            return null;
        }
    }
    
    private FacesContext getFacesContext() {
        return FacesContext.getCurrentInstance();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy