KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
8 import java.util.Locale JavaDoc;
9 import java.util.ResourceBundle JavaDoc;
10 import javax.faces.context.FacesContext;
11 import javax.servlet.http.HttpServletRequest JavaDoc;
12 import org.exoplatform.container.SessionContainer;
13 import org.exoplatform.faces.core.component.ComponentVisitor;
14 import org.exoplatform.faces.core.component.UIExoComponent;
15 import org.exoplatform.portal.PortalConstants;
16 import org.exoplatform.portal.session.PortalResources;
17 /**
18  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
19  * @since Nov 17, 2004
20  * @version $Id$
21  */

22 public class ChangeLocaleVisitor extends ComponentVisitor {
23   private ResourceBundle JavaDoc res_ ;
24   
25   public ChangeLocaleVisitor() {
26     
27   }
28   
29   /* set default locale */
30   public void changeLocale(UIPortal uiPortal) {
31     String JavaDoc lang = uiPortal.getPortalConfigModel().getLocale() ;
32     FacesContext context = FacesContext.getCurrentInstance() ;
33     HttpServletRequest JavaDoc request =
34       (HttpServletRequest JavaDoc) context.getExternalContext().getRequest();
35     String JavaDoc langParam = request.getParameter(PortalConstants.LANGUAGE_PARAMETER) ;
36     if(langParam != null) lang = langParam ;
37     if (lang == null || lang.length() == 0) {
38       Locale JavaDoc requestLocale = request.getLocale() ;
39       lang = requestLocale.getLanguage() ;
40     }
41     changeLocale(uiPortal, lang) ;
42   }
43   
44   public void changeLocale(UIPortal uiPortal, String JavaDoc locale) {
45     PortalResources presources =
46       (PortalResources) SessionContainer.getComponent(PortalResources.class) ;
47     presources.changeLocaleConfig(locale, uiPortal.getOwner()) ;
48     res_ = presources.getApplicationOwnerResource() ;
49     visit(uiPortal) ;
50     traverse(uiPortal.getRootContainer());
51   }
52   
53   public void changeLocale(UIPage uiPage) {
54     PortalResources presources =
55       (PortalResources) SessionContainer.getComponent(PortalResources.class) ;
56     res_ = presources.getLocaleConfig().getOwnerResourceBundle(uiPage.getOwner()) ;
57     traverse(uiPage) ;
58   }
59   
60   public void changeLocale(UISinglePage comp, String JavaDoc locale) {
61     PortalResources presources =
62       (PortalResources) SessionContainer.getComponent(PortalResources.class) ;
63     presources.changeLocaleConfig(locale, "anonymous") ;
64     res_ = presources.getApplicationOwnerResource() ;
65     List JavaDoc children = comp.getChildren() ;
66     for(int i = 0; i < children.size() ; i++) {
67       UIExoComponent child = (UIExoComponent) children.get(i);
68       visit(child) ;
69     }
70   }
71   
72   public void traverse(UIExoComponent component) {
73     visit(component) ;
74     if(component instanceof UIContainer) {
75       List JavaDoc children = component.getChildren() ;
76       for(int i = 0; i < children.size() ; i++) {
77         UIExoComponent child = (UIExoComponent) children.get(i);
78         traverse(child) ;
79       }
80     }
81   }
82   
83   public void visit(UIExoComponent comp) {
84     if(comp instanceof UIBasicComponent) {
85       UIBasicComponent uiComp = (UIBasicComponent) comp ;
86       uiComp.changeLocale(res_) ;
87     }
88   }
89 }
Popular Tags