You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by mm...@apache.org on 2005/09/27 11:08:05 UTC

svn commit: r291865 - in /myfaces/tomahawk/trunk/src/java/org/apache/myfaces: component/html/util/AddResource.java custom/navmenu/jscookmenu/HtmlJSCookMenuRenderer.java

Author: mmarinschek
Date: Tue Sep 27 02:07:57 2005
New Revision: 291865

URL: http://svn.apache.org/viewcvs?rev=291865&view=rev
Log:
JSCookMenu now allows setting javascript, style and image Location

Modified:
    myfaces/tomahawk/trunk/src/java/org/apache/myfaces/component/html/util/AddResource.java
    myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/navmenu/jscookmenu/HtmlJSCookMenuRenderer.java

Modified: myfaces/tomahawk/trunk/src/java/org/apache/myfaces/component/html/util/AddResource.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/src/java/org/apache/myfaces/component/html/util/AddResource.java?rev=291865&r1=291864&r2=291865&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/src/java/org/apache/myfaces/component/html/util/AddResource.java (original)
+++ myfaces/tomahawk/trunk/src/java/org/apache/myfaces/component/html/util/AddResource.java Tue Sep 27 02:07:57 2005
@@ -64,13 +64,33 @@
      * Adds the given Javascript resource to the document body.
      */
     public static void addJavaScriptHere(Class componentClass, String resourceFileName, FacesContext context, UIComponent component) throws IOException{
+
+        addJavaScriptHere(componentClass, null, resourceFileName, context, component);
+
+    }
+
+    private static void addJavaScriptHere(
+            Class componentClass, String baseDirectory, String resourceFileName, FacesContext context, UIComponent component)
+            throws IOException
+    {
+
         ResponseWriter writer = context.getResponseWriter();
 
         writer.startElement(HTML.SCRIPT_ELEM, component);
         writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR,HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT,null);
-        writer.writeURIAttribute(HTML.SRC_ATTR,
-                getResourceMappedPath(componentClass, resourceFileName, context),
-                null);
+
+        if(baseDirectory != null)
+        {
+            writer.writeURIAttribute(HTML.SRC_ATTR,
+                    getResourceBasePath(baseDirectory)+resourceFileName, null);
+        }
+        else
+        {
+            writer.writeURIAttribute(HTML.SRC_ATTR,
+                    getResourceMappedPath(componentClass, resourceFileName, context),
+                    null);
+        }
+
         writer.endElement(HTML.SCRIPT_ELEM);
     }
 
@@ -82,6 +102,13 @@
     }
 
     /**
+     * Adds the given Javascript resource to the document body, without passing UIComponent.
+     */
+    public static void addJavaScriptHere(String baseDirectory, String resourceFileName, FacesContext context) throws IOException{
+        addJavaScriptHere(null, baseDirectory, resourceFileName,context,null);
+    }
+
+    /**
      * Adds the given Javascript resource to the document Header.
      * If the script is already has already been referenced, it's added only once.
      */
@@ -100,6 +127,24 @@
     }
 
     /**
+     * Adds the given Javascript resource to the document Header.
+     * If the script is already has already been referenced, it's added only once.
+     */
+    public static void addJavaScriptToHeader(String baseDirectory, String resourceFileName, FacesContext context){
+        addJavaScriptToHeader(baseDirectory, resourceFileName, false, context);
+    }
+
+    /**
+     * Adds the given Javascript resource to the document Header.
+     * If the script is already has already been referenced, it's added only once.
+     */
+    public static void addJavaScriptToHeader(String baseDirectory, String resourceFileName, boolean defer, FacesContext context){
+        AdditionalHeaderInfoToRender jsInfo =
+            new AdditionalHeaderInfoToRender(AdditionalHeaderInfoToRender.TYPE_JS, baseDirectory, resourceFileName, defer);
+        addAdditionalHeaderInfoToRender(context, jsInfo );
+    }
+
+    /**
      * Adds the given Style Sheet to the document Header.
      * If the style sheet is already has already been referenced, it's added only once.
      */
@@ -113,6 +158,16 @@
      * Adds the given Style Sheet to the document Header.
      * If the style sheet is already has already been referenced, it's added only once.
      */
+    public static void addStyleSheet(String baseDirectory, String resourceFileName, FacesContext context){
+        AdditionalHeaderInfoToRender cssInfo =
+            new AdditionalHeaderInfoToRender(AdditionalHeaderInfoToRender.TYPE_CSS, baseDirectory, resourceFileName);
+        addAdditionalHeaderInfoToRender(context, cssInfo );
+    }
+
+    /**
+     * Adds the given Style Sheet to the document Header.
+     * If the style sheet is already has already been referenced, it's added only once.
+     */
     public static void addInlineStyleToHeader(String inlineStyle, FacesContext context){
         AdditionalHeaderInfoToRender cssInfo =
             new AdditionalHeaderInfoToRender(AdditionalHeaderInfoToRender.TYPE_CSS_INLINE, inlineStyle);
@@ -152,6 +207,11 @@
                 contextPath);
     }
 
+    public static String getResourceBasePath(String baseDirectory)
+    {
+        return baseDirectory+(baseDirectory.endsWith("/")?"":"/");
+    }
+
     public static String getResourceBasePath(Class componentClass, FacesContext context){
         String contextPath = null;
         if( context != null ){
@@ -400,9 +460,25 @@
         public int type;
         public boolean deferJS = false;
         public String componentName;
+        public String baseDirectory;
         public String resourceFileName;
         public String inlineText;
 
+        public AdditionalHeaderInfoToRender(int infoType, String baseDirectory, String resourceFileName) {
+            this.type = infoType;
+            this.baseDirectory = baseDirectory;
+            this.resourceFileName = resourceFileName;
+        }
+
+        public AdditionalHeaderInfoToRender(int infoType, String baseDirectory, String resourceFileName, boolean defer) {
+            if( defer && infoType != TYPE_JS )
+                log.error("Defer can only be used for scripts.");
+            this.type = infoType;
+            this.baseDirectory = baseDirectory;
+            this.resourceFileName = resourceFileName;
+            this.deferJS = defer;
+        }
+
         public AdditionalHeaderInfoToRender(int infoType, Class componentClass, String resourceFileName) {
             this.type = infoType;
             this.componentName = getComponentName(componentClass);
@@ -471,8 +547,18 @@
             switch (type) {
                 case TYPE_JS:
                     responseWriter.startElement(HTML.SCRIPT_ELEM,null);
-                    responseWriter.writeAttribute(HTML.SRC_ATTR,
+
+                    if(baseDirectory != null)
+                    {
+                        responseWriter.writeAttribute(HTML.SRC_ATTR,
+                            getResourceBasePath(baseDirectory)+resourceFileName,
+                            null);
+                    }
+                    else
+                    {
+                        responseWriter.writeAttribute(HTML.SRC_ATTR,
                             getResourceMappedPath(componentName, resourceFileName, contextPath),null);
+                    }
 
                     if(deferJS)
                         responseWriter.writeAttribute("defer","true",null);
@@ -482,8 +568,19 @@
                 case TYPE_CSS:
                     responseWriter.startElement("link",null);
                     responseWriter.writeAttribute("rel","stylesheet",null);
-                    responseWriter.writeAttribute(HTML.HREF_ATTR,
+
+                    if(baseDirectory != null )
+                    {
+                        responseWriter.writeAttribute(
+                                HTML.HREF_ATTR,
+                                baseDirectory+(baseDirectory.endsWith("/")?"":"/")+resourceFileName,
+                                null);
+                    }
+                    else
+                    {
+                        responseWriter.writeAttribute(HTML.HREF_ATTR,
                             getResourceMappedPath(componentName, resourceFileName, contextPath),null);
+                    }
 
                     responseWriter.writeAttribute(HTML.TYPE_ATTR,"text/css",null);
                     responseWriter.endElement("link");

Modified: myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/navmenu/jscookmenu/HtmlJSCookMenuRenderer.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/navmenu/jscookmenu/HtmlJSCookMenuRenderer.java?rev=291865&r1=291864&r2=291865&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/navmenu/jscookmenu/HtmlJSCookMenuRenderer.java (original)
+++ myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/navmenu/jscookmenu/HtmlJSCookMenuRenderer.java Tue Sep 27 02:07:57 2005
@@ -21,6 +21,7 @@
 import org.apache.myfaces.custom.navmenu.UINavigationMenuItem;
 import org.apache.myfaces.el.SimpleActionMethodBinding;
 import org.apache.myfaces.renderkit.RendererUtils;
+import org.apache.myfaces.renderkit.JSFAttr;
 import org.apache.myfaces.renderkit.html.HtmlRenderer;
 import org.apache.myfaces.renderkit.html.HTML;
 import org.apache.myfaces.renderkit.html.util.DummyFormResponseWriter;
@@ -85,30 +86,30 @@
         }
     }
 
-    private String decodeValueBinding(String actionParam, FacesContext context) 
+    private String decodeValueBinding(String actionParam, FacesContext context)
     {
-        int idx = actionParam.indexOf(";#{"); 
+        int idx = actionParam.indexOf(";#{");
         if (idx == -1) {
             return actionParam;
         }
-        
+
         String newActionParam = actionParam.substring(0, idx);
         String vbParam = actionParam.substring(idx + 1);
-        
+
         idx = vbParam.indexOf('=');
         if (idx == -1) {
             return newActionParam;
         }
         String vbExpressionString = vbParam.substring(0, idx);
         String vbValue = vbParam.substring(idx + 1);
-        
-        ValueBinding vb = 
-            context.getApplication().createValueBinding(vbExpressionString);        
+
+        ValueBinding vb =
+            context.getApplication().createValueBinding(vbExpressionString);
         vb.setValue(context, vbValue);
-        
+
         return newActionParam;
     }
-    
+
     public boolean getRendersChildren()
     {
         return true;
@@ -127,7 +128,7 @@
             dummyFormResponseWriter.setWriteDummyForm(true);
 
             String myId = getMenuId(context, component);
-            
+
             ResponseWriter writer = context.getResponseWriter();
 
             writer.startElement(HTML.SCRIPT_ELEM,component);
@@ -222,12 +223,12 @@
                     writer.append(",");
                     if (uiNavMenuItem != null)
                     {
-                        encodeNavigationMenuItems(context, writer, menuItems, 
+                        encodeNavigationMenuItems(context, writer, menuItems,
                                 uiNavMenuItem.getChildren(), menuId);
                     } else {
                         encodeNavigationMenuItems(context, writer, menuItems,
                                 new ArrayList(1), menuId);
-                    } 
+                    }
                 }
             }
             writer.append("]");
@@ -262,49 +263,9 @@
         HtmlCommandJSCookMenu menu = (HtmlCommandJSCookMenu)component;
         
         String theme = menu.getTheme();
-        
-        AddResource.addJavaScriptToHeader(HtmlJSCookMenuRenderer.class, "JSCookMenu.js", context);
-        AddResource.addJavaScriptToHeader(HtmlJSCookMenuRenderer.class, "MyFacesHack.js", context);
-
-        if( theme.equals( "ThemeOffice" ) ){
-            StringBuffer buf = new StringBuffer();
-            buf.append("myThemeOfficeBase='");
-            buf.append(AddResource.getResourceBasePath(HtmlJSCookMenuRenderer.class,context));
-            buf.append("/ThemeOffice/';");
-
-        	AddResource.addInlineScriptToHeader(buf.toString(), context);
-
-            AddResource.addJavaScriptToHeader(HtmlJSCookMenuRenderer.class, "ThemeOffice/theme.js", context);
-        	AddResource.addStyleSheet(HtmlJSCookMenuRenderer.class, "ThemeOffice/theme.css", context);
-        }else if( theme.equals( "ThemeMiniBlack" ) ){
-            StringBuffer buf = new StringBuffer();
-            buf.append("myThemeMiniBlackBase='");
-            buf.append(AddResource.getResourceBasePath(HtmlJSCookMenuRenderer.class,context));
-            buf.append("/ThemeMiniBlack/';");
 
-        	AddResource.addInlineScriptToHeader(buf.toString(), context);
-            AddResource.addJavaScriptToHeader(HtmlJSCookMenuRenderer.class, "ThemeMiniBlack/theme.js", context);
-        	AddResource.addStyleSheet(HtmlJSCookMenuRenderer.class, "ThemeMiniBlack/theme.css", context);
-        }else if( theme.equals( "ThemeIE" ) ){
-            StringBuffer buf = new StringBuffer();
-            buf.append("myThemeIEBase='");
-            buf.append(AddResource.getResourceBasePath(HtmlJSCookMenuRenderer.class,context));
-            buf.append("/ThemeIE/';");
-
-        	AddResource.addInlineScriptToHeader(buf.toString(), context);
-            AddResource.addJavaScriptToHeader(HtmlJSCookMenuRenderer.class, "ThemeIE/theme.js", context);
-        	AddResource.addStyleSheet(HtmlJSCookMenuRenderer.class, "ThemeIE/theme.css", context);
-        }else if( theme.equals( "ThemePanel" ) ){
-            StringBuffer buf = new StringBuffer();
-            buf.append("myThemePanelBase='");
-            buf.append(AddResource.getResourceBasePath(HtmlJSCookMenuRenderer.class,context));
-            buf.append("/ThemePanel/';");
+        addResourcesToHeader(theme,menu,context);
 
-        	AddResource.addInlineScriptToHeader(buf.toString(), context);
-            AddResource.addJavaScriptToHeader(HtmlJSCookMenuRenderer.class, "ThemePanel/theme.js", context);
-        	AddResource.addStyleSheet(HtmlJSCookMenuRenderer.class, "ThemePanel/theme.css", context);
-        }
-        	// Otherwise ?? bug ??
         ResponseWriter writer = context.getResponseWriter();
 
         String menuId = getMenuId(context, component);
@@ -330,6 +291,78 @@
 
         writer.writeText(buf.toString(),null);
         writer.endElement(HTML.SCRIPT_ELEM);
+    }
+
+    private void addResourcesToHeader(String themeName, HtmlCommandJSCookMenu menu, FacesContext context)
+    {
+        String javascriptLocation = (String) menu.getAttributes().get(JSFAttr.JAVASCRIPT_LOCATION);
+        String imageLocation = (String) menu.getAttributes().get(JSFAttr.IMAGE_LOCATION);
+        String styleLocation = (String) menu.getAttributes().get(JSFAttr.STYLE_LOCATION);
+
+        if(javascriptLocation != null)
+        {
+            AddResource.addJavaScriptToHeader(javascriptLocation, "JSCookMenu.js", context);
+            AddResource.addJavaScriptToHeader(javascriptLocation, "MyFacesHack.js", context);
+        }
+        else
+        {
+            AddResource.addJavaScriptToHeader(HtmlJSCookMenuRenderer.class, "JSCookMenu.js", context);
+            AddResource.addJavaScriptToHeader(HtmlJSCookMenuRenderer.class, "MyFacesHack.js", context);
+        }
+
+        addThemeSpecificResources(themeName, styleLocation, javascriptLocation, imageLocation, context);
+    }
+
+    private void addThemeSpecificResources(String themeName, String styleLocation, String javascriptLocation, String imageLocation, FacesContext context)
+    {
+        if(themeName != null)
+        {
+            if(!(themeName.equals("ThemeOffice") || themeName.equals("ThemeMiniBlack")
+                || themeName.equals("ThemeIE") || themeName.equals("ThemePanel")))
+            {
+                throw new IllegalArgumentException("You provided a wrong themeName. Must be one of ThemeOffice, ThemeMiniBlack, ThemeIE or ThemePanel");
+            }
+        }
+        else
+        {
+            if(styleLocation == null || javascriptLocation == null || imageLocation == null)
+                throw new IllegalArgumentException("If you don't provide a theme, "+
+                        "you need to provide all resource-paths.");
+        }
+
+        StringBuffer buf = new StringBuffer();
+        buf.append("my");
+        buf.append(themeName);
+        buf.append("Base='");
+        if(imageLocation!=null)
+        {
+            buf.append(AddResource.getResourceBasePath(imageLocation));
+        }
+        else
+        {
+            buf.append(AddResource.getResourceBasePath(HtmlJSCookMenuRenderer.class,context));
+            buf.append("/ThemeOffice/';");
+        }
+
+        AddResource.addInlineScriptToHeader(buf.toString(), context);
+
+        if(javascriptLocation != null)
+        {
+            AddResource.addJavaScriptToHeader(javascriptLocation, "theme.js", context);
+        }
+        else
+        {
+            AddResource.addJavaScriptToHeader(HtmlJSCookMenuRenderer.class, themeName+"/theme.js", context);
+        }
+
+        if(styleLocation != null)
+        {
+            AddResource.addJavaScriptToHeader(javascriptLocation, "theme.css", context);
+        }
+        else
+        {
+            AddResource.addJavaScriptToHeader(HtmlJSCookMenuRenderer.class, themeName+"/theme.css", context);
+        }
     }
 
     /**