1 5 package org.exoplatform.portal.faces.component; 6 7 import java.util.HashMap ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 import java.util.Map ; 11 import org.exoplatform.container.PortalContainer; 12 import org.exoplatform.faces.core.component.ComponentVisitor; 13 import org.exoplatform.faces.core.component.UIExoComponent ; 14 import org.exoplatform.services.portal.skin.SkinConfigService; 15 import org.exoplatform.services.portal.skin.model.Style; 16 import org.exoplatform.services.portletcontainer.pci.ExoWindowID; 17 22 public class CssStyleBuilderVisitor extends ComponentVisitor { 23 private SkinConfigService service_ ; 24 private Map cssURLMap_ ; 25 26 public CssStyleBuilderVisitor(SkinConfigService service) { 27 service_ = service ; 28 cssURLMap_ = new HashMap (15) ; 29 } 30 31 public String getCSS() { 32 StringBuffer sbuf = new StringBuffer () ; 33 Iterator styleIterator = cssURLMap_.values().iterator(); 34 while (styleIterator.hasNext()) { 35 String cssLink = (String ) styleIterator.next() ; 36 sbuf.append(cssLink); 37 } 38 return sbuf.toString() ; 39 } 40 41 public void traverse(UIExoComponent component) { 42 visit(component) ; 43 if(component instanceof UIContainer || 44 component instanceof UISinglePage || 45 component instanceof UIPortal) { 46 List children = component.getChildren() ; 47 for(int i = 0; i < children.size() ; i++) { 48 UIExoComponent child = (UIExoComponent) children.get(i); 49 traverse(child) ; 50 } 51 } 52 } 53 54 protected void visit(UIExoComponent comp) { 55 if(comp instanceof UIPortlet) { 56 UIPortlet uiPortlet = (UIPortlet)comp ; 57 addBasicComponentStyle(uiPortlet) ; 58 addPortletComponentStyle(uiPortlet) ; 59 } else if(comp instanceof UIBody) { 60 Object bodyComponent = ((UIBody)comp).getBodyComponent() ; 61 if(bodyComponent instanceof UIPage) { 62 traverse((UIPage)bodyComponent) ; 63 } else if(bodyComponent instanceof UIPagePreview) { 64 UIPagePreview uiPreview = (UIPagePreview) bodyComponent ; 65 traverse(uiPreview.getUIPage()) ; 66 } 67 } else if(comp instanceof UIBasicComponent) { 68 addBasicComponentStyle((UIBasicComponent)comp) ; 69 } 70 } 71 72 private void addPortletComponentStyle(UIPortlet comp) { 73 String portletStyle = comp.getPortletModel().getPortletStyle() ; 74 if (portletStyle != null && portletStyle.length() > 0 && !"default".equals(portletStyle)) { 75 ExoWindowID windowId = comp.getWindowId() ; 76 String portletId = windowId.getPortletApplicationName() + "/" +windowId.getPortletName() ; 77 Style styleConfig = service_.getPortletStyle(portletId, portletStyle) ; 78 if(styleConfig == null) { 79 portletId = "default" ; 80 styleConfig = service_.getPortletStyle(portletId, portletStyle) ; 81 } 82 if (styleConfig == null) { 83 String cssLink = "expect an style url here for the , portlet : " + 84 portletId + " style " + portletStyle + '\n'; 85 String key = portletId + "/" + portletStyle ; 86 cssURLMap_.put(key , cssLink) ; 87 } else { 88 String url = styleConfig.getUrl() ; 89 String cssLink = "<link rel='stylesheet' type='text/css' HREF='" + url + "'/>\n" ; 90 cssURLMap_.put(url, cssLink) ; 91 } 92 } 93 } 94 95 private void addBasicComponentStyle(UIBasicComponent comp) { 96 String decorator = comp.getDecorator() ; 97 if (decorator != null && decorator.length() > 0 && !"default".equals(decorator)) { 98 String rendererRef = comp.getRendererType() ; 99 Style styleConfig = comp.getDecoratorStyle(service_, rendererRef, decorator) ; 100 101 if (styleConfig == null) { 102 String cssLink = "expect an style url here for the , renderer: " + 103 rendererRef + " decorator " + decorator + '\n'; 104 String key = comp.getIdPrefix() + "/" + rendererRef + "/" + decorator ; 105 cssURLMap_.put(key , cssLink) ; 106 } else { 107 String url = styleConfig.getUrl() ; 108 String cssLink = "<link rel='stylesheet' type='text/css' HREF='" + url + "'/>\n" ; 109 cssURLMap_.put(url, cssLink) ; 110 } 111 } 112 } 113 114 static public String getCSS(UIExoComponent uiPortal) { 115 PortalContainer manager = PortalContainer.getInstance(); 116 SkinConfigService service = 117 (SkinConfigService) manager.getComponentInstanceOfType(SkinConfigService.class) ; 118 CssStyleBuilderVisitor visitor = new CssStyleBuilderVisitor(service) ; 119 visitor.traverse(uiPortal) ; 120 return visitor.getCSS() ; 121 } 122 } 123 | Popular Tags |