1 package org.apache.myfaces.tobago.example.demo.clientConfig;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
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
70
71 locale = facesContext.getViewRoot().getLocale();
72
73
74
75 contentTypeItems = new SelectItem[]{
76 new SelectItem("html"),
77 new SelectItem("wml", "wml (experimental)"),
78 new SelectItem("fo", "fo (experimental)")
79 };
80
81
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
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 }