/*
* 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.AdaptrexConfig;
import java.io.IOException;
import java.util.HashMap;
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";
}
@SuppressWarnings("unchecked")
@Override
public void encodeBegin(FacesContext context) throws IOException {
try {
Adaptrex adaptrex = Adaptrex.getAdaptrex();
AdaptrexConfig config = adaptrex.getConfig();
Map<String,Object> a = getAttributes();
Map<String,Object> extConfig = new HashMap<String,Object>();
if (adaptrex.getConfig().get("ext") != null) {
extConfig.putAll((Map<String,Object>) config.get("ext"));
}
/*
* If we're configuring Ext, do it here
*/
String extVersion = (String) a.get("ext-version");
if (extVersion == null) extVersion = (String) extConfig.get("version");
if (extVersion != null) {
/*
* Get the context path
*/
String contextPath = context.getExternalContext().getRequestContextPath();
if (contextPath.equals("/")) contextPath = "";
/*
* Get the weblib path inside this context
*/
String weblibPath = (String) a.get("weblibpath");
if (weblibPath == null) weblibPath = "/weblib";
/*
* Get the ext build. Check in this order:
* tag --> app config --> server config --> default (
*/
String extBuildName = "ext";
if (a.get("ext-build") != null && !a.get("ext-build").equals("production")) {
extBuildName += "-" + (String) a.get("ext-build");
} else if (extConfig.containsKey("build") && !extConfig.get("build").equals("production")) {
extBuildName += "-" + extConfig.get("build");
}
/*
* Ext folder
*/
String extFolder = contextPath + weblibPath + "/extjs-" + extVersion;
/*
* Write Ext Javascript Bootstrap File
*/
write("<script type='text/javascript' src='" + extFolder + "/" + extBuildName + ".js'></script>");
/*
* Theme
*/
String theme = "all";
if (a.containsKey("ext-theme")) {
theme = (String) a.get("ext-theme");
} else if (extConfig.containsKey("theme")) {
theme = (String) extConfig.get("theme");
}
if (theme.contains("neptune")) {
write("<script type='text/javascript' src='" + extFolder + "/ext-neptune.js'></script>");
}
if (theme.contains("/")) {
write("<link rel='stylesheet' type='text/css' href='" + contextPath + "/" + theme + "' />");
} else {
write("<link rel='stylesheet' type='text/css' href='" + extFolder + "/resources/css/ext-" + theme + ".css' />");
}
/*
* 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 (extBuildName.contains("debug")) {
write("<script type='text/javascript'>" +
"Ext.Loader.setPath('Adaptrex', '" + adaptrexFolder + "/src')</script>");
}
/*
* Application bootstrap path
*/
if (extBuildName.equals("ext")) {
//write("<script type='text/javascript' src='app-all.js'></script>");
} else {
String bootstrapPath = a.containsKey("ext-bootstrap") ? (String) a.get("ext-bootstrap") : "app.js";
write("<script type='text/javascript' src='" + bootstrapPath + "'></script>");
}
String jpaPU = (String) a.get("jpa-pu");
if (jpaPU != null) {
setAttribute("jpa-pu", jpaPU);
}
if (a.get("touch") != null) {
setAttribute("touch", Boolean.parseBoolean((String) a.get("touch")));
} else {
setAttribute("touch", false);
}
}
/*
* Set up namespace
*/
String ns = (String) a.get("ns");
if (ns != null) {
setAttribute("ns", ns);
write("<script type='text/javascript'>Ext.ns(\"" + ns + "\")</script>");
}
} catch (Exception e) {
log.warn("Error", e);
}
}
}