Author: jwaldman
Date: Mon Apr 16 18:38:30 2007
New Revision: 529464

URL: http://svn.apache.org/viewvc?view=rev&rev=529464
Log:
 ADFFACES-448 portal+skinning: suppress stylesheet if requested skin-id+skin's 
stylesheetid are found

If a portal is running Trinidad it may want its portlets running Trinidad to 
use the same skin as it is running instead of the default portal skin. To tell 
the portlet what skin to use, the portal sends some information via the 
RequestMap. The information is the skin-id and the skin's stylesheetdocument's 
id.
We will check to see if we have this skin and if its stylesheet document 
matches exactly and we will use this skin. Otherwise we will use the default 
portal skin.

The portal may want to tell the portlet to not render out the css url (see 
StyleSheetRenderer) since it will render out its own url to help performance. 
We will check another RequestMap parameter for this.

I propose adding this code and will check for these RequestMap parameters:

oracle.apache.myfaces.trinidad.skin.suppressStylesheet
oracle.apache.myfaces.trinidad.skin.id
oracle.apache.myfaces.trinidad.skin.stylesheet.id

Added this Skin API:
  /**
   * Returns the id of the Skin's stylesheet document.
   */
  abstract public String getStyleSheetDocumentId(RenderingContext arc); 

Modified:
    
incubator/adffaces/trunk/trinidad/trinidad-api/src/main/java/org/apache/myfaces/trinidad/skin/Skin.java
    
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/CoreRenderingContext.java
    
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/StyleContextImpl.java
    
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/StyleSheetRenderer.java
    
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/skin/SkinImpl.java

Modified: 
incubator/adffaces/trunk/trinidad/trinidad-api/src/main/java/org/apache/myfaces/trinidad/skin/Skin.java
URL: 
http://svn.apache.org/viewvc/incubator/adffaces/trunk/trinidad/trinidad-api/src/main/java/org/apache/myfaces/trinidad/skin/Skin.java?view=diff&rev=529464&r1=529463&r2=529464
==============================================================================
--- 
incubator/adffaces/trunk/trinidad/trinidad-api/src/main/java/org/apache/myfaces/trinidad/skin/Skin.java
 (original)
+++ 
incubator/adffaces/trunk/trinidad/trinidad-api/src/main/java/org/apache/myfaces/trinidad/skin/Skin.java
 Mon Apr 16 18:38:30 2007
@@ -59,6 +59,11 @@
    * Returns the renderKitId for the Skin.
    */
   abstract public String getRenderKitId();
+  
+  /**
+   * Returns the id of the Skin's stylesheet document.
+   */
+  abstract public String getStyleSheetDocumentId(RenderingContext arc);
 
   /**
    * Returns the style class map, or null if there is no map.
@@ -66,8 +71,7 @@
    * the key, and the value could be a shortened style class name,
    * or a portlet style class name, etc.
    */
-  abstract public Map<String, String> getStyleClassMap(
-    RenderingContext arc);
+  abstract public Map<String, String> getStyleClassMap(RenderingContext arc);
 
   /**
    * Returns the name of the XSS style sheet for this Skin.

Modified: 
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/CoreRenderingContext.java
URL: 
http://svn.apache.org/viewvc/incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/CoreRenderingContext.java?view=diff&rev=529464&r1=529463&r2=529464
==============================================================================
--- 
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/CoreRenderingContext.java
 (original)
+++ 
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/CoreRenderingContext.java
 Mon Apr 16 18:38:30 2007
@@ -208,6 +208,13 @@
   @Override
   public Skin getSkin()
   {
+    if(!_checkedRequestMapSkin)
+    {
+      Skin requestedSkin = getRequestMapSkin();
+      _checkedRequestMapSkin = true;
+      if (requestedSkin != null)
+        _skin = requestedSkin;
+    }
     return _skin;
   }
 
@@ -372,6 +379,104 @@
   {
     return "minimal";
   }
+  
+ 
+  /**
+   * Returns the skin that is requested on the request map if the exact skin 
exists.
+   * <p>
+   * If we are in a portlet, then we might need to recalculate the skin.
+   * The portal container might have its own skin that it wants us to use 
instead
+   * of what we picked based on the skin-family and render-kit-id.
+   * If it does, it will send the skin-id and the skin's styleSheetDocument id
+   * in the request map.
+   * </p>
+   * <p>
+   * If we have the skin with that id and the stylesheetdocument's id match,
+   * then we return that skin; else we return null, indicating that there is no
+   * requestMap skin.
+   * </p>
+   * @return null if there is no local skin that matches the requestMap skin, 
if any.
+   *         skin that is requested to be used on the requestMap if we can 
find that
+   *         exact skin with the same stylesheetdocument id locally.
+   */
+  public Skin getRequestMapSkin()
+  {
+    // protect against rechecking this more than once.
+    // if we already checked for the _requestMapSkin and it's null,
+    // then we'll return it anyway because that means we have no request map 
skin. 
+    if (_checkedRequestMapSkin)
+      return _requestMapSkin;
+    _checkedRequestMapSkin = true;
+    
+    if (CoreRenderKit.OUTPUT_MODE_PORTLET.equals(getOutputMode()))
+    {
+      FacesContext context = FacesContext.getCurrentInstance();
+      Map<String, Object> requestMap = 
context.getExternalContext().getRequestMap();
+      
+      // Get the requested Skin Id from the request Map
+      Object requestedSkinId = requestMap.get(_SKIN_ID_PARAM);
+      if (requestedSkinId != null)
+      {
+        SkinFactory factory = SkinFactory.getFactory();
+        if (factory == null)
+        {
+          _LOG.warning("There is no SkinFactory");
+          return null;
+        }
+        
+        Skin requestedSkin = factory.getSkin(context, 
requestedSkinId.toString());
+        if (requestedSkin != null)
+        {
+          // Get the skin's stylesheet id from the request Map and then 
compare it 
+          // to the local skin's stylesheet id to make sure they match.
+          Object requestMapStyleSheetId = 
requestMap.get(_SKIN_STYLESHEET_ID_PARAM);
+          if (requestMapStyleSheetId != null)
+          {
+            String skinForPortalStyleSheetId = 
requestedSkin.getStyleSheetDocumentId(this);
+            if (skinForPortalStyleSheetId != null && 
+                skinForPortalStyleSheetId.equals(requestMapStyleSheetId))
+            {
+              // it is ok to use this skin
+              // Switch the _skin here to be the tmpRequestedSkin
+              if (_LOG.isFine())
+                _LOG.fine("The skin " +requestedSkinId+ 
+                  " specified on the requestMap will be used.");
+              _requestMapSkin = requestedSkin;
+              return requestedSkin;
+            }
+            else
+            {
+              if (_LOG.isWarning())
+                _LOG.warning("The skin " +requestedSkinId+ 
+                            " specified on the requestMap will not be used 
because" + 
+                             " the styleSheetDocument id on the requestMap" +
+                             " does not match the local skin's 
styleSheetDocument's id.");
+            }                
+          }
+          else
+          {
+            if (_LOG.isSevere())
+              _LOG.severe("The skin " +requestedSkinId+ 
+                          " specified on the requestMap will not be used 
because" + 
+                           " its styleSheetDocument id was not in the 
requestMap" +
+                           " and it is needed to compare with the local" +
+                           " skin's styleSheetDocument's id to make sure the 
skins are the same.");              
+          }
+        }// end requestedSkin != null
+        else
+        {
+          if (_LOG.isWarning())
+          {
+            _LOG.warning("The skin " +requestedSkinId+ 
+                        " specified on the requestMap will not be used 
because" + 
+                         " it does not exist.");
+          }
+        }     
+      }
+      
+    } // end outputMode == portlet
+    return null;
+  }  
 
   /**
    * Set the local variable _skin to be the Skin from the
@@ -407,29 +512,11 @@
     SkinFactory factory = SkinFactory.getFactory();
     if (factory == null)
     {
-      if (_LOG.isWarning())
-        _LOG.warning("There is no SkinFactory");
+      _LOG.warning("There is no SkinFactory");
       return;
     }
 
-    Skin skin = null;
-
-    // see if there is a skinID on the requestParameterMap. If there is, then
-    // we want to use that skin. Otherwise, use find the skin as usual, using 
the portlet
-    // renderKitId.
-    if (CoreRenderKit.OUTPUT_MODE_PORTLET.equals(getOutputMode()))
-    {
-      Map<String, Object> requestMap = 
context.getExternalContext().getRequestMap();
-      Object skinId = requestMap.get(_SKIN_ID_PARAM);
-      if (skinId != null)
-      {
-        skin = factory.getSkin(context, skinId.toString());
-      }
-
-    }
-
-    if (skin == null)
-      skin = factory.getSkin(null, skinFamily, renderKitId);
+    Skin skin = factory.getSkin(null, skinFamily, renderKitId);
 
     if (skin == null)
     {
@@ -443,6 +530,8 @@
 
     _skin = skin;
   }
+  
+
 
   private TrinidadAgent _initializeAgent(
     FacesContext context,
@@ -576,6 +665,8 @@
 
 
   private Skin                _skin;
+  private boolean             _checkedRequestMapSkin = false;
+  private Skin                _requestMapSkin;
   private FormData            _formData;
   private TrinidadAgent       _agent;
   private Map<String, String> _styleMap;
@@ -589,7 +680,11 @@
   private int                 _linkStyleDisabledCount = 0;
   private boolean             _isLinkDisabled = false;
 
-  static private final String _SKIN_ID_PARAM = 
"oracle.apache.myfaces.trinidad.skin.id";
+  static private final String _SKIN_ID_PARAM = 
+    "oracle.apache.myfaces.trinidad.skin.id";
+  static private final String _SKIN_STYLESHEET_ID_PARAM = 
+    "oracle.apache.myfaces.trinidad.skin.stylesheet.id";
+
   // Maps describing the capabilities of our output modes
   // -= Simon Lessard =-
   // FIXME: Cannot use CapabilityKey in the generic definition because

Modified: 
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/StyleContextImpl.java
URL: 
http://svn.apache.org/viewvc/incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/StyleContextImpl.java?view=diff&rev=529464&r1=529463&r2=529464
==============================================================================
--- 
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/StyleContextImpl.java
 (original)
+++ 
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/StyleContextImpl.java
 Mon Apr 16 18:38:30 2007
@@ -42,18 +42,20 @@
   {
     _arc = arc;
     _generatedFilesPath = generatedFilesPath;
-    _styleProvider = _getDefaultStyleProvider(arc.getSkin());
-    _styleMap = _styleProvider.getStyleMap(this);
   }
 
 
   public StyleProvider getStyleProvider()
   {
+    if (_styleProvider == null)
+      _styleProvider = _getDefaultStyleProvider(((CoreRenderingContext) 
_arc).getSkin());
     return _styleProvider;
   }
 
   public StyleMap getStyleMap()
   {
+    if (_styleMap == null)
+      _styleMap = getStyleProvider().getStyleMap(this);
     return _styleMap;
   }
 

Modified: 
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/StyleSheetRenderer.java
URL: 
http://svn.apache.org/viewvc/incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/StyleSheetRenderer.java?view=diff&rev=529464&r1=529463&r2=529464
==============================================================================
--- 
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/StyleSheetRenderer.java
 (original)
+++ 
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/StyleSheetRenderer.java
 Mon Apr 16 18:38:30 2007
@@ -31,6 +31,7 @@
 import org.apache.myfaces.trinidad.component.core.CoreStyleSheet;
 
 import org.apache.myfaces.trinidad.context.RenderingContext;
+import org.apache.myfaces.trinidad.skin.Skin;
 import org.apache.myfaces.trinidadinternal.renderkit.core.CoreRenderingContext;
 
 import org.apache.myfaces.trinidadinternal.style.StyleContext;
@@ -88,51 +89,60 @@
     if (provider != null)
     {
       String href = provider.getStyleSheetURI(sContext);
-      if (href != null)
+      
+      // If the requestMap has a skin-id, a skin's stylesheet's id and 
suppressStylesheet
+      // is true, and the skin information matches our current skin, then it 
is safe
+      // to not write out the css. This means that it will be written out by 
the external
+      // source, like the portal container.
+      boolean suppressStylesheet = _isSuppressStylesheet(context, arc);
+      if (!suppressStylesheet)
       {
-        ExternalContext externalContext = context.getExternalContext();
-        String contextUri = externalContext.getRequestContextPath();
-        String baseURL = contextUri + XhtmlConstants.STYLES_CACHE_DIRECTORY;
-        
-        String outputMode = arc.getOutputMode();
-        // =-=AEW Don't like hardcoding facet names...
-        if (XhtmlConstants.OUTPUT_MODE_PORTLET.equals(outputMode) &&
-            supportsScripting(arc))
+        if (href != null)
         {
-          writer.startElement("script", null);
-          writer.writeText("var _adfSS;if(!_adfSS){_adfSS=1;document.write(\"" 
+
-                        "<link rel=\\\"stylesheet\\\" "+
-                        "charset=\\\"UTF-8\\\" type=\\\"text/css\\\" " +
-                        "href=\\\"",
-                                               null);
-          String uri = 
context.getExternalContext().encodeResourceURL(baseURL+href);
-          writer.writeText(uri, null);
-          writer.writeText("\\\">\")}", null);
-          writer.endElement("script");
+          ExternalContext externalContext = context.getExternalContext();
+          String contextUri = externalContext.getRequestContextPath();
+          String baseURL = contextUri + XhtmlConstants.STYLES_CACHE_DIRECTORY;
+          
+          String outputMode = arc.getOutputMode();
+          // =-=AEW Don't like hardcoding facet names...
+          if (XhtmlConstants.OUTPUT_MODE_PORTLET.equals(outputMode) &&
+              supportsScripting(arc))
+          {
+            writer.startElement("script", null);
+            writer.writeText("var 
_adfSS;if(!_adfSS){_adfSS=1;document.write(\"" +
+                          "<link rel=\\\"stylesheet\\\" "+
+                          "charset=\\\"UTF-8\\\" type=\\\"text/css\\\" " +
+                          "href=\\\"",
+              null);
+            String uri = 
context.getExternalContext().encodeResourceURL(baseURL+href);
+            writer.writeText(uri, null);
+            writer.writeText("\\\">\")}", null);
+            writer.endElement("script");
+          }
+          else
+          {
+            writer.startElement("link", null);
+            renderId(context, comp);
+            writer.writeAttribute("rel", "stylesheet", null);
+            writer.writeAttribute("charset", "UTF-8", null);
+            
+            String type = provider.getContentStyleType(sContext);
+            writer.writeAttribute("type", type, null);
+            
+            renderEncodedResourceURI(context, "href", baseURL + href);
+            writer.endElement("link");
+          }
         }
         else
         {
-          writer.startElement("link", null);
-          renderId(context, comp);
-          writer.writeAttribute("rel", "stylesheet", null);
-          writer.writeAttribute("charset", "UTF-8", null);
-          
-          String type = provider.getContentStyleType(sContext);
-          writer.writeAttribute("type", type, null);
-          
-          renderEncodedResourceURI(context, "href", baseURL + href);
-          writer.endElement("link");
+          if (arc.getSkin() == null)
+            writer.writeComment("ERROR: Could not create stylesheet, because " 
+
+                                "no skin is available");
+          else
+            writer.writeComment("ERROR: could not create stylesheet for " +
+                                arc.getSkin().getStyleSheetName());
         }
       }
-      else
-      {
-        if (arc.getSkin() == null)
-          writer.writeComment("ERROR: Could not create stylesheet, because " +
-                              "no skin is available");
-        else
-          writer.writeComment("ERROR: could not create stylesheet for " +
-                              arc.getSkin().getStyleSheetName());
-      }
 
 
       // Hand the Faces-major renderers the style Map for compressing.
@@ -146,4 +156,22 @@
     }
   }
 
+  // returns true if we want to suppress the stylesheet.
+  private boolean _isSuppressStylesheet(FacesContext context,  
RenderingContext arc)
+  {
+
+    Map<String, Object> requestMap = 
context.getExternalContext().getRequestMap();   
+     
+    boolean suppressStylesheet = 
"true".equals(requestMap.get(_SUPPRESS_STYLESHEET_ID_PARAM));
+    if (suppressStylesheet)
+    {
+      Skin requestMapSkin = ((CoreRenderingContext) arc).getRequestMapSkin();
+      return (requestMapSkin != null) ? true : false;
+    }
+    return false;
+  }
+  
+  static private final String _SUPPRESS_STYLESHEET_ID_PARAM = 
+    "oracle.apache.myfaces.trinidad.skin.suppressStylesheet";
+  
 }

Modified: 
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/skin/SkinImpl.java
URL: 
http://svn.apache.org/viewvc/incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/skin/SkinImpl.java?view=diff&rev=529464&r1=529463&r2=529464
==============================================================================
--- 
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/skin/SkinImpl.java
 (original)
+++ 
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/skin/SkinImpl.java
 Mon Apr 16 18:38:30 2007
@@ -99,6 +99,17 @@
   {
     return null;
   }
+  
+  /**
+   * Returns the id of the Skin's stylesheet document. This is the 
StyleSheetDocument's 
+   * id for the StyleContext.
+   */
+   @Override
+  public String getStyleSheetDocumentId(RenderingContext arc)
+  {
+    StyleContext sContext = ((CoreRenderingContext)arc).getStyleContext();
+    return getStyleSheetDocument(sContext).getDocumentId(sContext);
+  }
 
   /**
    * Returns the name of the XSS style sheet for this Skin.


Reply via email to