From 8a5e20d6ecaae5730f01d587eb1b5fcad5e81f98 Mon Sep 17 00:00:00 2001 From: stephan Date: Fri, 5 Oct 2018 22:09:32 +0200 Subject: [PATCH] #853 performance optimization (added Kyle's improvements) --- .../listeners/AddResourcesListener.java | 100 ++++++++---------- 1 file changed, 42 insertions(+), 58 deletions(-) diff --git a/src/main/java/net/bootsfaces/listeners/AddResourcesListener.java b/src/main/java/net/bootsfaces/listeners/AddResourcesListener.java index 9b4cbb08f..5fe9713c3 100644 --- a/src/main/java/net/bootsfaces/listeners/AddResourcesListener.java +++ b/src/main/java/net/bootsfaces/listeners/AddResourcesListener.java @@ -148,10 +148,8 @@ public void processEvent(SystemEvent event) throws AbortProcessingException { * If it found nothing, it check for components that has as a resource lib, the * "bsf" lib. * - * @param root - * <- the UIViewRoot - * @param context - * <- the faces context + * @param root the UIViewRoot + * @param context the faces context * @return */ private boolean ensureExistBootsfacesComponent(UIViewRoot root, FacesContext context) { @@ -178,10 +176,8 @@ private boolean ensureExistBootsfacesComponent(UIViewRoot root, FacesContext con * target library. I use this method to check existence of a BsF component * because, at this level, the getComponentResource returns always null * - * @param parent - * the parent component - * @param targetLib - * the lib to search + * @param parent the parent component + * @param targetLib the lib to search * @return */ public static UIComponent findBsfComponent(UIComponent parent, String targetLib) { @@ -232,10 +228,8 @@ private void addMetaTags(UIViewRoot root, FacesContext context) { /** * Add the required CSS files and the FontAwesome CDN link. * - * @param root - * The UIViewRoot of the JSF tree. - * @param context - * The current FacesContext + * @param root The UIViewRoot of the JSF tree. + * @param context The current FacesContext */ private void addCSS(UIViewRoot root, FacesContext context) { // The following code is needed to diagnose the warning "Unable to save dynamic @@ -386,10 +380,8 @@ private String getTheme(FacesContext context) { /** * Add the required Javascript files and the FontAwesome CDN link. * - * @param root - * The UIViewRoot of the JSF tree. - * @param context - * The current FacesContext + * @param root The UIViewRoot of the JSF tree. + * @param context The current FacesContext */ private void addJavascript(UIViewRoot root, FacesContext context) { // The following code is needed to diagnose the warning "Unable to save dynamic @@ -583,7 +575,7 @@ public static void setNeedsFontsAwesome(Object uiComponent) { } list.add(uiComponent); } - + private boolean needsFontAwesome4() { FacesContext context = FacesContext.getCurrentInstance(); UIViewRoot root = context.getViewRoot(); @@ -602,9 +594,9 @@ private boolean needsFontAwesome4() { return true; } boolean v4 = false; - for (Object o: all) { + for (Object o : all) { if (!v5.containsKey(o)) { - v4=true; + v4 = true; break; } } @@ -621,10 +613,8 @@ private boolean needsFontAwesome4() { * TODO: Verify if the duplicate resource files are a bug of BootsFaces or of * the Mojarra library itself. * - * @param root - * The current UIViewRoot - * @param context - * The current FacesContext + * @param root The current UIViewRoot + * @param context The current FacesContext */ private void removeDuplicateResources(UIViewRoot root, FacesContext context) { List resourcesToRemove = new ArrayList(); @@ -669,10 +659,8 @@ public int compare(UIComponent o1, UIComponent o2) { * Theme (Bootstrap) and custom themes, other themes would get default theme * look and feel otherwise. * - * @param root - * The current UIViewRoot - * @param context - * The current FacesContext + * @param root The current UIViewRoot + * @param context The current FacesContext */ private void removeBootstrapResources(UIViewRoot root, FacesContext context) { String theme = getTheme(context); @@ -708,10 +696,8 @@ private void removeBootstrapResources(UIViewRoot root, FacesContext context) { * prior to other resource files, giving the developer the opportunity to * overwrite a CSS or JS file. * - * @param root - * The current UIViewRoot - * @param context - * The current FacesContext + * @param root The current UIViewRoot + * @param context The current FacesContext */ private void enforceCorrectLoadOrder(UIViewRoot root, FacesContext context) { // // first, handle the CSS files. @@ -820,8 +806,7 @@ private boolean isFontAwesomeComponentUsedAndRemoveIt() { /** * Looks for the header in the JSF tree. * - * @param root - * The root of the JSF tree. + * @param root The root of the JSF tree. * @return null, if the head couldn't be found. */ private UIComponent findHeader(UIViewRoot root) { @@ -843,10 +828,8 @@ private UIComponent findHeader(UIViewRoot root) { * Registers a JS file that needs to be included in the header of the HTML file, * but after jQuery and AngularJS. * - * @param library - * The name of the sub-folder of the resources folder. - * @param resource - * The name of the resource file within the library folder. + * @param library The name of the sub-folder of the resources folder. + * @param resource The name of the resource file within the library folder. */ public static void addResourceToHeadButAfterJQuery(String library, String resource) { addResource(resource, library, library + "#" + resource, RESOURCE_KEY); @@ -856,10 +839,8 @@ public static void addResourceToHeadButAfterJQuery(String library, String resour * Registers a core JS file that needs to be included in the header of the HTML * file, but after jQuery and AngularJS. * - * @param library - * The name of the sub-folder of the resources folder. - * @param resource - * The name of the resource file within the library folder. + * @param library The name of the sub-folder of the resources folder. + * @param resource The name of the resource file within the library folder. */ public static void addBasicJSResource(String library, String resource) { addResource(resource, library, resource, BASIC_JS_RESOURCE_KEY); @@ -869,8 +850,7 @@ public static void addBasicJSResource(String library, String resource) { * Registers a themed CSS file that needs to be included in the header of the * HTML file. * - * @param resource - * The name of the resource file within the library folder. + * @param resource The name of the resource file within the library folder. */ public static void addThemedCSSResource(String resource) { Map viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap(); @@ -890,8 +870,7 @@ public static void addThemedCSSResource(String resource) { * Registers a Extension CSS file that needs to be included in the header of the * HTML file. * - * @param resource - * The name of the resource file within the library folder. + * @param resource The name of the resource file within the library folder. */ public static void addExtCSSResource(String resource) { Map viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap(); @@ -964,10 +943,12 @@ private boolean isFalseOrNo(String param) { } /** - * Add the default datatables.net resource if and only if the user doesn't bring their own copy, and if they didn't disallow it in the web.xml - * by setting the context paramter net.bootsfaces.get_datatable_from_cdn to true. + * Add the default datatables.net resource if and only if the user doesn't bring + * their own copy, and if they didn't disallow it in the web.xml by setting the + * context paramter net.bootsfaces.get_datatable_from_cdn to true. + * * @param defaultFilename The URL of the file to be loaded - * @param type either "js" or "css" + * @param type either "js" or "css" */ public static void addDatatablesResourceIfNecessary(String defaultFilename, String type) { boolean loadDatatables = shouldLibraryBeLoaded(P_GET_DATATABLE_FROM_CDN, true); @@ -975,16 +956,19 @@ public static void addDatatablesResourceIfNecessary(String defaultFilename, Stri FacesContext context = FacesContext.getCurrentInstance(); UIViewRoot root = context.getViewRoot(); - String[] positions = {"head", "body", "form"}; - for (String position: positions) { - List availableResources = root.getComponentResources(context, position); - for (UIComponent ava : availableResources) { - String name = (String) ava.getAttributes().get("name"); - if (null != name) { - System.out.println(name); - name = name.toLowerCase(); - if (name.contains("datatables") && name.endsWith("."+type)) { - loadDatatables = false; + String[] positions = { "head", "body", "form" }; + for (String position : positions) { + if (loadDatatables) { + List availableResources = root.getComponentResources(context, position); + for (UIComponent ava : availableResources) { + String name = (String) ava.getAttributes().get("name"); + if (null != name) { + System.out.println(name); + name = name.toLowerCase(); + if (name.contains("datatables") && name.endsWith("." + type)) { + loadDatatables = false; + break; + } } } }