KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portal > faces > component > CssStyleBuilderVisitor


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.portal.faces.component;
6
7 import java.util.HashMap JavaDoc ;
8 import java.util.Iterator JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Map JavaDoc;
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 /**
18  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
19  * @since Nov 17, 2004
20  * @version $Id$
21  */

22 public class CssStyleBuilderVisitor extends ComponentVisitor {
23   private SkinConfigService service_ ;
24   private Map JavaDoc cssURLMap_ ;
25   
26   public CssStyleBuilderVisitor(SkinConfigService service) {
27     service_ = service ;
28     cssURLMap_ = new HashMap JavaDoc(15) ;
29   }
30   
31   public String JavaDoc getCSS() {
32     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc() ;
33     Iterator JavaDoc styleIterator = cssURLMap_.values().iterator();
34     while (styleIterator.hasNext()) {
35       String JavaDoc cssLink = (String JavaDoc) 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 JavaDoc 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 JavaDoc 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 JavaDoc portletStyle = comp.getPortletModel().getPortletStyle() ;
74     if (portletStyle != null && portletStyle.length() > 0 && !"default".equals(portletStyle)) {
75       ExoWindowID windowId = comp.getWindowId() ;
76       String JavaDoc 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 JavaDoc cssLink = "expect an style url here for the , portlet : " +
84                           portletId + " style " + portletStyle + '\n';
85         String JavaDoc key = portletId + "/" + portletStyle ;
86         cssURLMap_.put(key , cssLink) ;
87       } else {
88         String JavaDoc url = styleConfig.getUrl() ;
89         String JavaDoc 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 JavaDoc decorator = comp.getDecorator() ;
97     if (decorator != null && decorator.length() > 0 && !"default".equals(decorator)) {
98       String JavaDoc rendererRef = comp.getRendererType() ;
99       Style styleConfig = comp.getDecoratorStyle(service_, rendererRef, decorator) ;
100      
101       if (styleConfig == null) {
102         String JavaDoc cssLink = "expect an style url here for the , renderer: " +
103                          rendererRef + " decorator " + decorator + '\n';
104         String JavaDoc key = comp.getIdPrefix() + "/" + rendererRef + "/" + decorator ;
105         cssURLMap_.put(key , cssLink) ;
106       } else {
107         String JavaDoc url = styleConfig.getUrl() ;
108         String JavaDoc cssLink = "<link rel='stylesheet' type='text/css' HREF='" + url + "'/>\n" ;
109         cssURLMap_.put(url, cssLink) ;
110       }
111     }
112   }
113   
114   static public String JavaDoc 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