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

org.icefaces.ace.component.graphicimage.GraphicImageRenderer Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2004-2014 ICEsoft Technologies Canada Corp.
 *
 * 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.icefaces.ace.component.graphicimage;

import org.icefaces.ace.util.ComponentUtils;
import org.icefaces.component.PassthroughAttributes;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.Renderer;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;


public class GraphicImageRenderer extends Renderer {
    private final static String[] PASSTHROUGH_ATTRIBUTES = ((PassthroughAttributes) GraphicImage.class.getAnnotation(PassthroughAttributes.class)).value();

    private static final Logger logger =
            Logger.getLogger(GraphicImageRenderer.class.toString());


    public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException {
        //
        ResponseWriter writer = facesContext.getResponseWriter();
        assert (null != facesContext);
        assert (null != writer);
        assert (null != uiComponent);
        String clientId = uiComponent.getClientId(facesContext);
        GraphicImage uiGraphic = (GraphicImage) uiComponent;
        if (logger.isLoggable(Level.FINER)) {
            checkAttributes(uiGraphic);
        }
        writer.startElement("img", uiComponent);
        writer.writeAttribute("id", clientId, null);

        ComponentUtils.renderPassThroughAttributes(writer, uiGraphic, PASSTHROUGH_ATTRIBUTES);
        ComponentUtils.renderExternalPassThroughAttributes(writer, uiGraphic);

        String styleClass;
        if (null != (styleClass = (String)
                uiComponent.getAttributes().get("styleClass"))) {
            writer.writeAttribute("class", styleClass, "styleClass");
        }
        String mimeType = uiGraphic.getMimeType();
        if (null == mimeType) mimeType = "image/jpeg";
        String scope = uiGraphic.getScope().toLowerCase().trim();
        if (!scope.equals("flash") && !(scope.equals("window")) && !(scope.equals("application"))
                && (!scope.equals("request")) && (!scope.equals("view"))) {
            scope = "session";
            if (logger.isLoggable(Level.FINE)) {
                logger.fine("setting scope to session for addition of resrouce");
            }
        }
        String name = uiGraphic.getName();
        String library = uiGraphic.getLibrary();
        if (null == name || name.equals("")) name = "image" + clientId;
        Object value = uiGraphic.getValue();
        String srcAttribute = uiGraphic.processSrcAttribute(facesContext, value, name, library, scope, mimeType);
        writer.writeAttribute("src", srcAttribute, null);
        writer.endElement("img");
    }

    /*
      * just a diagnostic method that isn't currently being used.
      */
    private void checkAttributes(GraphicImage uiGraphic) {
        Map tempMap = uiGraphic.getAttributes();
        for (Map.Entry entry : tempMap.entrySet()) {
            logger.finer(" attribute entry is " + entry.getKey() + " value is " + entry.getValue());
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy