/*
* Copyright 2012 Adaptrex, LLC
*
* 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 com.adaptrex.core.ext.jsf;
import com.adaptrex.core.Adaptrex;
import com.adaptrex.core.config.AdaptrexConfig;
import java.io.IOException;
import java.util.Map;
import javax.faces.component.FacesComponent;
import javax.faces.context.FacesContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@FacesComponent("ext.data.Config")
public class ConfigComponent extends ExtComponentBase {
private static Logger log = LoggerFactory.getLogger(ConfigComponent.class);
@Override
public String getFamily() {
return "ext.data.config";
}
@Override
public void encodeBegin(FacesContext context) throws IOException {
try {
Adaptrex adaptrex = Adaptrex.getAdaptrex();
AdaptrexConfig config = adaptrex.getConfig();
Map<String,Object> tagAttr = getAttributes();
String contextPath = context.getExternalContext().getRequestContextPath();
if (contextPath.isEmpty()) contextPath = "/";
/*
* Weblib path
*/
String weblibPath = (String) tagAttr.get(AdaptrexConfig.WEBLIB);
if (weblibPath == null) weblibPath = config.get(AdaptrexConfig.WEBLIB);
/*
* Read and configure ext path and build information
*/
String extVersion = (String) tagAttr.get(AdaptrexConfig.EXT_VERSION);
if (extVersion == null) extVersion = config.get(AdaptrexConfig.EXT_VERSION);
String extBuild = (String) tagAttr.get(AdaptrexConfig.EXT_BUILD);
if (extBuild == null) extBuild = config.get(AdaptrexConfig.EXT_BUILD, "production");
String extPath = (String) tagAttr.get(AdaptrexConfig.EXT_PATH);
if (extPath == null) extPath = config.get(AdaptrexConfig.EXT_PATH);
if (extPath == null) {
if (extVersion != null) {
extPath = weblibPath + "/extjs-" + extVersion + "/ext" + (extBuild.equals("production") ? "" : "-" + extBuild) + ".js";
}
}
/*
* Write ext bootstrap code
*/
if (extPath != null) {
write("<script type='text/javascript' src='" + contextPath + extPath + "'></script>");
String extFolder = extPath.substring(0, extPath.lastIndexOf("/"));
String theme = (String) tagAttr.get(AdaptrexConfig.EXT_THEME);
if (theme == null) theme = config.get(AdaptrexConfig.EXT_THEME, "all");
if (!theme.contains("/")) {
theme = extFolder + "/resources/css/ext-" + theme + ".css";
}
write("<link rel='stylesheet' type='text/css' href='" + contextPath + theme + "' />");
if (theme.contains("neptune")) {
write("<script type='text/javascript' src='" + contextPath + extFolder + "/ext-neptune.js'></script>");
}
/*
* Bootstrap Ext classes we need for adaptrex stores and models
*/
write ("<script type='text/javascript' src='" + contextPath + weblibPath + "/adaptrex-bootstrap-ext-" + extVersion + ".js'></script>");
}
/*
* Set up adaptrex loader if we're in debug mode
*/
String adaptrexFolder = contextPath + weblibPath + "/adaptrexjs";
write("<script type='text/javascript' src='" + adaptrexFolder + "/adaptrex.js'></script>");
write("<script type='text/javascript'>Adaptrex.Context={path:'" + contextPath + "'}</script>");
if (config.get(AdaptrexConfig.DEBUG).equals("true")) {
write("<script type='text/javascript'>" +
"Ext.Loader.setPath('Adaptrex', '" + adaptrexFolder + "/src')</script>");
}
/*
* Set up namespace
*/
String namespace = (String) tagAttr.get(AdaptrexConfig.EXT_NAMESPACE);
if (namespace == null) namespace = (String) tagAttr.get("ns");
if (namespace == null) namespace = (String) tagAttr.get("namespace");
if (namespace == null) namespace = config.get(AdaptrexConfig.EXT_NAMESPACE);
if (namespace != null) {
setAttribute("namespace", namespace);
write("<script type='text/javascript'>Ext.ns('" + namespace + "')</script>");
}
} catch (Exception e) {
log.warn("Error", e);
}
}
}