View Javadoc

1   package org.apache.myfaces.tobago.example.test;
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  import org.apache.myfaces.tobago.config.TobagoConfig;
21  import org.apache.myfaces.tobago.context.ClientProperties;
22  import org.apache.myfaces.tobago.context.Theme;
23  
24  import javax.faces.application.Application;
25  import javax.faces.context.FacesContext;
26  import javax.faces.model.SelectItem;
27  import java.util.ArrayList;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Locale;
31  
32  public class ClientConfigController {
33  
34    private boolean debugMode;
35  
36    private Theme theme;
37    private SelectItem[] themeItems;
38  
39    private Locale locale;
40  
41    private String contentType;
42    private SelectItem[] contentTypeItems;
43  
44    public ClientConfigController() {
45  
46      // theme
47  
48      FacesContext facesContext = FacesContext.getCurrentInstance();
49      TobagoConfig tobagoConfig = TobagoConfig.getInstance(facesContext);
50  
51      List<Theme> themes = new ArrayList<Theme>(tobagoConfig.getSupportedThemes());
52      themes.add(0, tobagoConfig.getDefaultTheme());
53      themeItems = new SelectItem[themes.size()];
54      for (int i = 0; i < themeItems.length; i++) {
55        Theme themeItem = themes.get(i);
56        themeItems[i] = new SelectItem(themeItem, themeItem.getDisplayName());
57      }
58  
59      // locale
60  
61      locale = facesContext.getViewRoot().getLocale();
62  
63      // contentType
64  
65      contentTypeItems = new SelectItem[]{
66        new SelectItem("html"),
67        new SelectItem("wml", "wml (experimental)"),
68        new SelectItem("fo", "fo (experimental)")
69      };
70  
71      // load
72  
73      loadFromClientProperties();
74    }
75  
76    public String submit() {
77      storeInClientProperties();
78      return "navigation";
79    }
80  
81  // ///////////////////////////////////////////// logic
82  
83    public void storeInClientProperties() {
84      ClientProperties client = ClientProperties.getInstance(FacesContext.getCurrentInstance());
85  
86      client.setDebugMode(debugMode);
87      client.setTheme(theme);
88      client.setContentType(contentType);
89    }
90  
91    public void loadFromClientProperties() {
92      ClientProperties client
93          = ClientProperties.getInstance(FacesContext.getCurrentInstance());
94  
95      debugMode = client.isDebugMode();
96      theme = client.getTheme();
97      contentType = client.getContentType();
98    }
99  
100   public List<SelectItem> getLocaleItems() {
101     FacesContext facesContext = FacesContext.getCurrentInstance();
102     Application application = facesContext.getApplication();
103     Locale defaultLocale = application.getDefaultLocale();
104     Iterator supportedLocales = application.getSupportedLocales();
105 
106     List<SelectItem> localeItems = new ArrayList<SelectItem>();
107     localeItems.add(createLocaleItem(defaultLocale));
108     while (supportedLocales.hasNext()) {
109       Locale locale = (Locale) supportedLocales.next();
110       localeItems.add(createLocaleItem(locale));
111     }
112     return localeItems;
113   }
114 
115   private SelectItem createLocaleItem(Locale localeItem) {
116     if (locale != null) {
117       return new SelectItem(localeItem, localeItem.getDisplayName(locale));
118     } else {
119       return new SelectItem(localeItem, localeItem.getDisplayName(localeItem));
120     }
121   }
122 
123   public static ClientConfigController getCurrentInstance(
124       FacesContext facesContext, String beanName) {
125     return (ClientConfigController) facesContext.getApplication()
126         .getVariableResolver().resolveVariable(facesContext, beanName);
127   }
128 
129   public boolean isDebugMode() {
130     return debugMode;
131   }
132 
133   public void setDebugMode(boolean debugMode) {
134     this.debugMode = debugMode;
135   }
136 
137   public Theme getTheme() {
138     return theme;
139   }
140 
141   public String getLocalizedTheme() {
142     for (int i = 0; i < themeItems.length; i++) {
143       SelectItem themeItem = themeItems[i];
144       if (themeItem.getValue().equals(theme)) {
145         return themeItem.getLabel();
146       }
147     }
148     return "???";
149   }
150 
151   public void setTheme(Theme theme) {
152     this.theme = theme;
153   }
154 
155   public SelectItem[] getThemeItems() {
156     return themeItems;
157   }
158 
159   public void setThemeItems(SelectItem[] themeItems) {
160     this.themeItems = themeItems;
161   }
162 
163   public Locale getLocale() {
164     return locale;
165   }
166 
167   public String getLocalizedLocale() {
168     if (locale != null) {
169       return locale.getDisplayName(locale);
170     } else{
171       return null;
172     }
173   }
174 
175   public void setLocale(Locale locale) {
176     this.locale = locale;
177   }
178 
179   public String getContentType() {
180     return contentType;
181   }
182 
183   public void setContentType(String contentType) {
184     this.contentType = contentType;
185   }
186 
187   public SelectItem[] getContentTypeItems() {
188     return contentTypeItems;
189   }
190 
191   public void setContentTypeItems(SelectItem[] contentTypeItems) {
192     this.contentTypeItems = contentTypeItems;
193   }
194 }