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 13.07.2004 at 10:48:28.
022 * $Id: ThemeConfigViewController.java 578592 2007-09-23 18:51:32Z bommel $
023 */
024
025 import org.apache.commons.collections.KeyValue;
026 import org.apache.commons.collections.keyvalue.DefaultKeyValue;
027 import org.apache.commons.io.IOUtils;
028 import org.apache.commons.logging.Log;
029 import org.apache.commons.logging.LogFactory;
030 import static org.apache.myfaces.tobago.TobagoConstants.RENDERER_TYPE_BUTTON;
031 import static org.apache.myfaces.tobago.TobagoConstants.RENDERER_TYPE_IN;
032 import static org.apache.myfaces.tobago.TobagoConstants.RENDERER_TYPE_LABEL;
033 import static org.apache.myfaces.tobago.TobagoConstants.RENDERER_TYPE_LINK;
034 import static org.apache.myfaces.tobago.TobagoConstants.RENDERER_TYPE_SELECT_ONE_RADIO;
035 import org.apache.myfaces.tobago.config.ThemeConfig;
036
037 import javax.faces.component.UIComponent;
038 import javax.faces.component.UIInput;
039 import javax.faces.context.ExternalContext;
040 import javax.faces.context.FacesContext;
041 import javax.faces.model.SelectItem;
042 import java.io.InputStream;
043 import java.util.ArrayList;
044 import java.util.Arrays;
045 import java.util.HashSet;
046 import java.util.Iterator;
047 import java.util.Properties;
048 import java.util.Set;
049
050 public class ThemeConfigViewController {
051
052
053 private static final Log LOG = LogFactory.getLog(ThemeConfigViewController.class);
054
055 private static final String PROPERTY_FILE_PREFIX = "tobago/html/";
056
057 private static final String PROPERTY_FILE_POSTFIX =
058 "/standard/property/tobago-theme-config.properties";
059
060 public static final String[] DEFAULT_THEMES = {
061 "charlotteville",
062 "inexso",
063 "sap",
064 "scarborough",
065 "tui"
066 };
067
068 public static final String[] DEFAULT_RENDERER_TYPES = {
069 "Action",
070 RENDERER_TYPE_BUTTON,
071 "Calendar",
072 "Date",
073 "File",
074 "Form",
075 "GridLayout",
076 "Box",
077 "Hidden",
078 "Image",
079 "Items",
080 RENDERER_TYPE_LABEL,
081 RENDERER_TYPE_LINK,
082 "Message",
083 "Messages",
084 "MultiSelect",
085 "Page",
086 "Panel",
087 "Progress",
088 "RichTextEditor",
089 "SelectBooleanCheckbox",
090 "SelectItems",
091 "SelectManyCheckbox",
092 "SelectOneChoice",
093 RENDERER_TYPE_SELECT_ONE_RADIO,
094 "Sheet",
095 "Subview",
096 "TabGroup",
097 "Tab",
098 "TextArea",
099 RENDERER_TYPE_IN,
100 "Out",
101 "Tree",
102 "TreeNode",
103 // "",
104 // "",
105 // "",
106 // "",
107 "Verbatim"
108 };
109
110 private String[] propertyNames;
111
112 private KeyValue[] entrys;
113
114 private String[] themeNames;
115
116 private String[] rendererTypes;
117
118 private SelectItem[] selectItems;
119
120 private String rendererType;
121
122 private UIComponent component;
123
124 public ThemeConfigViewController() {
125 component = new UIInput();
126 init();
127 }
128
129 private void init() {
130 String[] themes;
131 if (themeNames != null) {
132 themes = themeNames;
133 } else {
134 themes = DEFAULT_THEMES;
135 }
136
137 final Set<String> names = new HashSet<String>();
138 for (int j = 0; j < DEFAULT_THEMES.length; j++) {
139 Properties properties = new Properties();
140 String file = PROPERTY_FILE_PREFIX + themes[j] + PROPERTY_FILE_POSTFIX;
141 InputStream inputStream = null;
142 try {
143 final ExternalContext externalContext
144 = FacesContext.getCurrentInstance().getExternalContext();
145 inputStream = externalContext.getResourceAsStream(file);
146 properties.load(inputStream);
147 } catch (Exception e) {
148 if (LOG.isDebugEnabled()) {
149 LOG.debug("Exception when loading file \"" + file + "\"");
150 }
151 } finally {
152 IOUtils.closeQuietly(inputStream);
153 }
154
155 for (Iterator i = properties.keySet().iterator(); i.hasNext();) {
156 String name = (String) i.next();
157 if (name.indexOf('.') != -1) {
158 names.add(name.substring(name.indexOf('.') + 1));
159 if (LOG.isDebugEnabled()) {
160 LOG.debug("add \"" + name.substring(name.indexOf('.') + 1) + "\" "
161 + "from file \"" + file + "\"");
162 }
163 }
164 }
165 }
166 propertyNames = names.toArray(new String[names.size()]);
167 Arrays.sort(propertyNames);
168 }
169
170 public String doRequest() {
171 final FacesContext facesContext = FacesContext.getCurrentInstance();
172 component.setRendererType(rendererType);
173
174 ArrayList<KeyValue> found = new ArrayList<KeyValue>();
175 for (int i = 0; i < propertyNames.length; i++) {
176 String propertyName = propertyNames[i];
177 try {
178 int value = ThemeConfig.getValue(facesContext, component, propertyName);
179 found.add(new DefaultKeyValue(propertyName, Integer.toString(value)));
180 } catch (Exception e) {
181 if (LOG.isDebugEnabled()) {
182 LOG.debug("No value found for \"" + propertyName
183 + "\" in \"" + rendererType + "\"");
184 }
185 }
186 }
187 entrys = found.toArray(new KeyValue[found.size()]);
188
189 return null;
190 }
191
192 public String changeTheme() {
193 final FacesContext facesContext = FacesContext.getCurrentInstance();
194 ClientConfigController controller = (ClientConfigController)
195 facesContext.getExternalContext().getSessionMap().get("clientConfigController");
196 controller.submit();
197
198
199 return doRequest();
200 }
201
202 public SelectItem[] createSelectItems() {
203 String[] renderer = null;
204 if (renderer != null) {
205 renderer = rendererTypes;
206 } else {
207 renderer = DEFAULT_RENDERER_TYPES;
208 }
209 SelectItem[] items = new SelectItem[renderer.length];
210 for (int i = 0; i < items.length; i++) {
211 items[i] = new SelectItem(renderer[i]);
212 }
213 return items;
214 }
215
216 // ///////////////////////////////////////////// bean getter + setter
217
218 public String[] getThemeNames() {
219 return themeNames;
220 }
221
222 public void setThemeNames(String[] themeNames) {
223 this.themeNames = themeNames;
224 init();
225 }
226
227 public SelectItem[] getSelectItems() {
228 if (selectItems == null) {
229 selectItems = createSelectItems();
230 }
231 return selectItems;
232 }
233
234 public KeyValue[] getEntrys() {
235 return entrys;
236 }
237
238 public String[] getRendererTypes() {
239 return rendererTypes;
240 }
241
242 public void setRendererTypes(String[] rendererTypes) {
243 this.rendererTypes = rendererTypes;
244 selectItems = null;
245 }
246
247 public String getRendererType() {
248 return rendererType;
249 }
250
251 public void setRendererType(String rendererType) {
252 this.rendererType = rendererType;
253 }
254 }