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

com.evasion.common.flex.component.FlexComponent Maven / Gradle / Ivy

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package com.evasion.common.flex.component;

import com.evasion.common.Component;
import java.io.IOException;
import javax.faces.application.ResourceDependencies;
import javax.faces.application.ResourceDependency;
import javax.faces.component.FacesComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author glon-56610
 */
@FacesComponent(value = "evasion")
@ResourceDependencies({
    @ResourceDependency(name = "component/flex/swfobject.js", target = "head"),
    @ResourceDependency(name = "component/flex/history/history.css", target = "head"),
    @ResourceDependency(name = "component/flex/history/history.js", target = "head")
})
public class FlexComponent extends Component {

    /**
     * LOGGER.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(
            FlexComponent.class);

    protected static final String FAMILY = "swf";

    public static final String EXTERNAL_URL = "ExternalURL";

    public FlexComponent() {
        super();
    }

    @Override
    public String getFamily() {
        return FAMILY;
    }

    protected enum PropertyKeys {

        id,
        rendered,
        versionPlayer,
        width,
        height,
        quality,
        bgcolor,
        url,
        urlBlazeDs;

        String toString;

        PropertyKeys(String toString) {
            this.toString = toString;
        }

        PropertyKeys() {
        }

        @Override
        public String toString() {
            return ((toString != null) ? toString : super.toString());
        }
    }

    @Override
    public boolean isRendered() {
        return Boolean.valueOf(getStateHelper().eval(PropertyKeys.rendered, Boolean.TRUE).toString());
    }

    public String getUrl() {
        return (java.lang.String) getStateHelper().eval(PropertyKeys.url);
    }

    public void setUrl(String url) {
        getStateHelper().put(PropertyKeys.url, url);
        handleAttribute(PropertyKeys.url.toString(), url);
    }

    public String getVersionPlayer() {
        return (java.lang.String) getStateHelper().eval(PropertyKeys.versionPlayer, "0");

    }

    public void setVersionPlayer(String versionPlayer) {
        getStateHelper().put(PropertyKeys.versionPlayer, versionPlayer);
        handleAttribute(PropertyKeys.versionPlayer.toString(), versionPlayer);
    }

    public String getWidth() {
        return (java.lang.String) getStateHelper().eval(PropertyKeys.width, "100%");
    }

    public void setWidth(String width) {
        getStateHelper().put(PropertyKeys.width, width);
        handleAttribute(PropertyKeys.width.toString(), width);
    }

    public String getHeight() {
        return (java.lang.String) getStateHelper().eval(PropertyKeys.height, "100%");
    }

    public void setHeight(String height) {
        getStateHelper().put(PropertyKeys.height, height);
        handleAttribute(PropertyKeys.height.toString(), height);
    }

    public String getQuality() {
        return (java.lang.String) getStateHelper().eval(PropertyKeys.quality, "high");
    }

    public void setQuality(String quality) {
        getStateHelper().put(PropertyKeys.quality, quality);
        handleAttribute(PropertyKeys.quality.toString(), quality);
    }

    public String getBgcolor() {
        return (java.lang.String) getStateHelper().eval(PropertyKeys.bgcolor, "#ffffff");
    }

    public void setBgcolor(String bgcolor) {
        getStateHelper().put(PropertyKeys.bgcolor, bgcolor);
        handleAttribute(PropertyKeys.bgcolor.toString(), bgcolor);
    }

    public String getUrlBlazeDs() {
        final String extUrl = (java.lang.String) getStateHelper().eval(PropertyKeys.bgcolor, "#ffffff");
        final String fullUrl;
        if (extUrl == null || StringUtils.isEmpty(extUrl)) {
            fullUrl = "/";
        } else {
            fullUrl = extUrl;
        }
        return (java.lang.String) getStateHelper().eval(PropertyKeys.urlBlazeDs, fullUrl);
    }

    public void setUrlBlazeDs(String urlBlazeDs) {
        getStateHelper().put(PropertyKeys.urlBlazeDs, urlBlazeDs);
        handleAttribute(PropertyKeys.urlBlazeDs.toString(), urlBlazeDs);
    }

    @Override
    public void encodeBegin(FacesContext context) throws IOException {
        ResponseWriter responseWriter = context.getResponseWriter();

        responseWriter.startElement("style", null);
        responseWriter.writeAttribute("type", "text/css", null);
        responseWriter.writeAttribute("media", "screen", null);
        responseWriter.writeText("object:focus { outline:none; }\n #flashContent { display:none; }", null);
        responseWriter.endElement("style");

        responseWriter.startElement("script", null);
        responseWriter.writeAttribute("type", "text/javascript", null);
        StringBuilder jsCode = new StringBuilder().append("\n").
                append("var swfVersionStr = \"").
                append(getVersionPlayer()).
                append("\";\n").
                append("\n").
                append("var xiSwfUrlStr = \"playerProductInstall.swf\";\n").
                append("var flashvars = {};\n").
                append("flashvars.urlBlazeDs=\"").
                append(this.getUrlBlazeDs()).
                append("\";\n").
                append("var params = {};\n").
                append("params.quality = \"").
                append(getQuality()).
                append("\";\n").
                append("params.bgcolor = \"").
                append(getBgcolor()).
                append("\";\n").
                append("params.allowscriptaccess = \"sameDomain\";\n").
                append("params.allowfullscreen = \"true\";\n").
                append("var attributes = {};\n").
                append("attributes.id = \"test\";\n").
                append("attributes.name = \"test\";\n").
                append("attributes.align = \"middle\";\n").
                append("swfobject.embedSWF(").
                append("    \"").
                append(getUrl()).
                append("\", \"flashContent\", ").
                append("    \"").
                append(getWidth()).
                append("\", \"").
                append(getHeight()).
                append("\", ").
                append("    swfVersionStr, xiSwfUrlStr, ").
                append("    flashvars, params, attributes);\n").
                append("	\n").
                append("	swfobject.createCSS(\"#flashContent\", \"display:block;text-align:left;\");\n");
        responseWriter.writeText(jsCode, null);
        responseWriter.endElement("script");
    }

    @Override
    public void encodeEnd(FacesContext context) throws IOException {
        ResponseWriter responseWriter = context.getResponseWriter();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        String browserType = request.getHeader("User-Agent");


        responseWriter.startElement("div", null);
        responseWriter.writeAttribute(PropertyKeys.id.toString(), getId(), null);
        {
            responseWriter.startElement("div", null);
            responseWriter.writeAttribute(PropertyKeys.id.toString(), "flashContent", null);

            // Balise P
            responseWriter.startElement("p", null);
            responseWriter.writeText("To view this page ensure that Adobe Flash Player version \n"
                    + getVersionPlayer() + " or greater is installed. ", null);
            responseWriter.endElement("p");
            // Bouton pour install d'adobe FlashPalyer
            responseWriter.startElement("script", null);
            responseWriter.writeAttribute("type", "text/javascript", null);
            responseWriter.writeText("var pageHost = ((document.location.protocol == \"https:\") ? \"https://\" :	\"http://\");"
                    + "document.write(\"Get Adobe Flash player\" );", null);
            responseWriter.endElement("script");

            responseWriter.endElement("div");
        }


        responseWriter.startElement("noscript", null);
        responseWriter.startElement("object", null);
        responseWriter.writeAttribute("classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000", null);
        responseWriter.writeAttribute(PropertyKeys.width.toString(), this.getWidth(), null);
        responseWriter.writeAttribute(PropertyKeys.height.toString(), this.getHeight(), null);
        responseWriter.writeAttribute("id", "test", null);
        wirteParam(responseWriter, "movie", this.getUrl());
        wirteParam(responseWriter, "quality", this.getQuality());
        wirteParam(responseWriter, "bgcolor", this.getBgcolor());
        wirteParam(responseWriter, "allowScriptAccess", "sameDomain");
        wirteParam(responseWriter, "allowFullScreen", "true");
        wirteParam(responseWriter, "flashVars", "urlBlazeDs=" + this.getUrlBlazeDs());

        if (!"MSIE 6.0".contains(browserType)) {
            // responseWriter.writeComment("[if !IE]>");
            responseWriter.startElement("object", null);
            responseWriter.writeAttribute("type", "application/x-shockwave-flash", null);
            responseWriter.writeAttribute("data", this.getUrl(), null);
            responseWriter.writeAttribute(PropertyKeys.width.toString(), this.getWidth(), null);
            responseWriter.writeAttribute(PropertyKeys.height.toString(), this.getHeight(), null);
            wirteParam(responseWriter, "quality", this.getQuality());
            wirteParam(responseWriter, "bgcolor", this.getBgcolor());
            wirteParam(responseWriter, "allowScriptAccess", "sameDomain");
            wirteParam(responseWriter, "allowFullScreen", "true");
            downloadLink(responseWriter);
            responseWriter.endElement("object");

        } else {
            responseWriter.startElement("p", null);
            responseWriter.writeText("Either scripts and active content are not permitted to run or Adobe Flash Player version"
                    + getVersionPlayer() + " or greater is not installed.", null);
            responseWriter.endElement("p");
            downloadLink(responseWriter);
        }
        responseWriter.endElement("object");
        responseWriter.endElement("noscript");
        responseWriter.endElement("div");

    }

    @Override
    public void decode(FacesContext context) {
    }

    private void wirteParam(ResponseWriter responseWriter, String name, String value) throws IOException {
        responseWriter.startElement("param", null);
        responseWriter.writeAttribute("name", name, null);
        responseWriter.writeAttribute("value", value, null);
        responseWriter.endElement("param");

    }

    private void downloadLink(ResponseWriter responseWriter) throws IOException {
        responseWriter.startElement("a", null);
        responseWriter.writeAttribute("href", "http://www.adobe.com/go/getflashplayer", null);
        responseWriter.startElement("img", null);
        responseWriter.writeAttribute("src", "http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif", null);
        responseWriter.writeAttribute("alt", "Get Adobe Flash Player", null);
        responseWriter.endElement("img");
        responseWriter.endElement("a");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy