View Javadoc

1   package org.apache.myfaces.tobago.example.demo.clientConfig;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  /*
21   * Created 21.08.2002 at 18:00:22.
22   * $Id: ClientConfigController.java 719606 2008-11-21 15:50:48Z lofwyr $
23   */
24  
25  import org.apache.myfaces.tobago.config.TobagoConfig;
26  import org.apache.myfaces.tobago.context.ClientProperties;
27  import org.apache.myfaces.tobago.context.Theme;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  import javax.faces.application.Application;
33  import javax.faces.context.FacesContext;
34  import javax.faces.model.SelectItem;
35  import java.util.ArrayList;
36  import java.util.Iterator;
37  import java.util.List;
38  import java.util.Locale;
39  
40  public class ClientConfigController {
41  
42    private static final Log LOG = LogFactory.getLog(ClientConfigController.class);
43  
44    private boolean debugMode;
45  
46    private Theme theme;
47    private SelectItem[] themeItems;
48  
49    private Locale locale;
50  
51    private String contentType;
52    private SelectItem[] contentTypeItems;
53  
54    public ClientConfigController() {
55  
56      // theme
57  
58      FacesContext facesContext = FacesContext.getCurrentInstance();
59      TobagoConfig tobagoConfig = TobagoConfig.getInstance(facesContext);
60  
61      List<Theme> themes = new ArrayList<Theme>(tobagoConfig.getSupportedThemes());
62      themes.add(0, tobagoConfig.getDefaultTheme());
63      themeItems = new SelectItem[themes.size()];
64      for (int i = 0; i < themeItems.length; i++) {
65        Theme themeItem = themes.get(i);
66        themeItems[i] = new SelectItem(themeItem, themeItem.getDisplayName());
67      }
68  
69      // locale
70  
71      locale = facesContext.getViewRoot().getLocale();
72  
73      // contentType
74  
75      contentTypeItems = new SelectItem[]{
76        new SelectItem("html"),
77        new SelectItem("wml", "wml (experimental)"),
78        new SelectItem("fo", "fo (experimental)")
79      };
80  
81      // load
82  
83      loadFromClientProperties();
84    }
85  
86    public String submit() {
87      if (LOG.isDebugEnabled()) {
88        LOG.debug("invoke!!!");
89      }
90      FacesContext facesContext = FacesContext.getCurrentInstance();
91  
92      storeInClientProperties();
93  
94      for (int i = 0; i < ClientConfigPhaseListener.BEAN_NAMES.length; i++) {
95        String beanName = ClientConfigPhaseListener.BEAN_NAMES[i];
96        ClientConfigController controller
97            = getCurrentInstance(facesContext, beanName);
98        if (controller != null) {
99          controller.setLocale(locale);
100       }
101     }
102 
103     return FacesContext.getCurrentInstance().getViewRoot().getViewId();
104   }
105 
106 // ///////////////////////////////////////////// logic
107 
108   public void storeInClientProperties() {
109     ClientProperties client
110         = ClientProperties.getInstance(FacesContext.getCurrentInstance());
111 
112     client.setDebugMode(debugMode);
113     client.setTheme(theme);
114     client.setContentType(contentType);
115   }
116 
117   public void loadFromClientProperties() {
118     ClientProperties client
119         = ClientProperties.getInstance(FacesContext.getCurrentInstance());
120 
121     debugMode = client.isDebugMode();
122     theme = client.getTheme();
123     contentType = client.getContentType();
124   }
125 
126   public List<SelectItem> getLocaleItems() {
127     FacesContext facesContext = FacesContext.getCurrentInstance();
128     Application application = facesContext.getApplication();
129     Locale defaultLocale = application.getDefaultLocale();
130     Iterator supportedLocales = application.getSupportedLocales();
131 
132     List<SelectItem> localeItems = new ArrayList<SelectItem>();
133     localeItems.add(createLocaleItem(defaultLocale));
134     while (supportedLocales.hasNext()) {
135       Locale locale = (Locale) supportedLocales.next();
136       localeItems.add(createLocaleItem(locale));
137     }
138     return localeItems;
139   }
140 
141   private SelectItem createLocaleItem(Locale localeItem) {
142     if (locale != null) {
143       return new SelectItem(localeItem, localeItem.getDisplayName(locale));
144     } else {
145       return new SelectItem(localeItem, localeItem.getDisplayName(localeItem));
146     }
147   }
148 
149   public static ClientConfigController getCurrentInstance(
150       FacesContext facesContext, String beanName) {
151     return (ClientConfigController) facesContext.getApplication()
152         .getVariableResolver().resolveVariable(facesContext, beanName);
153   }
154 
155   public boolean isDebugMode() {
156     return debugMode;
157   }
158 
159   public void setDebugMode(boolean debugMode) {
160     this.debugMode = debugMode;
161   }
162 
163   public Theme getTheme() {
164     return theme;
165   }
166 
167   public String getLocalizedTheme() {
168     for (int i = 0; i < themeItems.length; i++) {
169       SelectItem themeItem = themeItems[i];
170       if (themeItem.getValue().equals(theme)) {
171         return themeItem.getLabel();
172       }
173     }
174     return "???";
175   }
176 
177   public void setTheme(Theme theme) {
178     this.theme = theme;
179   }
180 
181   public SelectItem[] getThemeItems() {
182     return themeItems;
183   }
184 
185   public void setThemeItems(SelectItem[] themeItems) {
186     this.themeItems = themeItems;
187   }
188 
189   public Locale getLocale() {
190     return locale;
191   }
192 
193   public String getLocalizedLocale() {
194     if (locale != null) {
195       return locale.getDisplayName(locale);
196     } else{
197       return null;
198     }
199   }
200 
201   public void setLocale(Locale locale) {
202     this.locale = locale;
203   }
204 
205   public String getContentType() {
206     return contentType;
207   }
208 
209   public void setContentType(String contentType) {
210     this.contentType = contentType;
211   }
212 
213   public SelectItem[] getContentTypeItems() {
214     return contentTypeItems;
215   }
216 
217   public void setContentTypeItems(SelectItem[] contentTypeItems) {
218     this.contentTypeItems = contentTypeItems;
219   }
220 }