001 package org.apache.myfaces.tobago.example.demo.clientConfig;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements. See the NOTICE file distributed with
006 * this work for additional information regarding copyright ownership.
007 * The ASF licenses this file to You under the Apache License, Version 2.0
008 * (the "License"); you may not use this file except in compliance with
009 * the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020 /*
021 * Created 21.08.2002 at 18:00:22.
022 * $Id: ClientConfigController.java 578592 2007-09-23 18:51:32Z bommel $
023 */
024
025 import org.apache.myfaces.tobago.config.TobagoConfig;
026 import org.apache.myfaces.tobago.context.ClientProperties;
027 import org.apache.myfaces.tobago.context.Theme;
028
029 import org.apache.commons.logging.Log;
030 import org.apache.commons.logging.LogFactory;
031
032 import javax.faces.application.Application;
033 import javax.faces.context.FacesContext;
034 import javax.faces.model.SelectItem;
035 import java.util.ArrayList;
036 import java.util.Iterator;
037 import java.util.List;
038 import java.util.Locale;
039
040 public class ClientConfigController {
041
042 private static final Log LOG = LogFactory.getLog(ClientConfigController.class);
043
044 private boolean debugMode;
045
046 private Theme theme;
047 private SelectItem[] themeItems;
048
049 private Locale locale;
050
051 private String contentType;
052 private SelectItem[] contentTypeItems;
053
054 public ClientConfigController() {
055
056 // theme
057
058 FacesContext facesContext = FacesContext.getCurrentInstance();
059 TobagoConfig tobagoConfig = TobagoConfig.getInstance(facesContext);
060
061 List<Theme> themes = new ArrayList<Theme>(tobagoConfig.getSupportedThemes());
062 themes.add(0, tobagoConfig.getDefaultTheme());
063 themeItems = new SelectItem[themes.size()];
064 for (int i = 0; i < themeItems.length; i++) {
065 Theme themeItem = themes.get(i);
066 themeItems[i] = new SelectItem(themeItem, themeItem.getDisplayName());
067 }
068
069 // locale
070
071 locale = facesContext.getViewRoot().getLocale();
072
073 // contentType
074
075 contentTypeItems = new SelectItem[]{
076 new SelectItem("html"),
077 new SelectItem("wml", "wml (experimental)"),
078 new SelectItem("fo", "fo (experimental)")
079 };
080
081 // load
082
083 loadFromClientProperties();
084 }
085
086 public String submit() {
087 if (LOG.isDebugEnabled()) {
088 LOG.debug("invoke!!!");
089 }
090 FacesContext facesContext = FacesContext.getCurrentInstance();
091
092 storeInClientProperties();
093
094 for (int i = 0; i < ClientConfigPhaseListener.BEAN_NAMES.length; i++) {
095 String beanName = ClientConfigPhaseListener.BEAN_NAMES[i];
096 ClientConfigController controller
097 = getCurrentInstance(facesContext, beanName);
098 if (controller != null) {
099 controller.setLocale(locale);
100 }
101 }
102
103 return "view";
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 }