001 package org.apache.myfaces.tobago.example.demo.overview;
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 import org.apache.commons.logging.Log;
021 import org.apache.commons.logging.LogFactory;
022 import org.apache.myfaces.tobago.component.UIData;
023 import org.apache.myfaces.tobago.context.ResourceManager;
024 import org.apache.myfaces.tobago.context.ResourceManagerFactory;
025 import org.apache.myfaces.tobago.event.SortActionEvent;
026 import org.apache.myfaces.tobago.event.TabChangeEvent;
027 import org.apache.myfaces.tobago.example.demo.model.solar.SolarObject;
028 import org.apache.myfaces.tobago.example.demo.model.Salutation;
029 import org.apache.myfaces.tobago.model.SheetState;
030 import org.apache.myfaces.tobago.taglib.component.ToolBarTag;
031
032 import javax.faces.context.FacesContext;
033 import javax.faces.event.ActionEvent;
034 import javax.faces.model.SelectItem;
035 import javax.faces.component.UIComponent;
036 import javax.faces.validator.ValidatorException;
037 import javax.faces.application.FacesMessage;
038 import java.util.ArrayList;
039 import java.util.Collections;
040 import java.util.Comparator;
041 import java.util.Date;
042 import java.util.List;
043
044 /*
045 * Created 19.05.2004 18:47:47.
046 * $Id: OverviewController.java 702627 2008-10-07 20:44:53Z weber $
047 */
048
049 public class OverviewController {
050
051 private static final Log LOG = LogFactory.getLog(OverviewController.class);
052
053 private static final String[] TREE_SELECT_MODE_KEYS = {
054 "none",
055 "single",
056 "singleLeafOnly",
057 "multi",
058 "multiLeafOnly"
059 };
060
061 private static final String[] TREELISTBOX_SELECT_MODE_KEYS = {
062 "single",
063 "singleLeafOnly",
064 "siblingLeafOnly"
065 };
066
067 private Salutation radioValue;
068
069 private Salutation singleValue;
070
071 private Salutation[] multiValue;
072
073 private String basicInput = "";
074
075 private String basicArea = "";
076
077 private Date basicDate = new Date();
078
079 private Date basicTime = new Date();
080
081 private String treeSelectMode;
082
083 private String treeListboxSelectMode;
084
085 private Integer treeTabsState;
086
087 private String lastAction;
088
089 private SheetConfig sheetConfig;
090
091 private String toolbarIconSize;
092
093 private SelectItem[] toolbarIconItems;
094
095 private String toolbarTextPosition;
096
097 private SelectItem[] toolbarTextItems;
098
099
100 public OverviewController() {
101 radioValue = Salutation.UNKNOWN;
102 singleValue = Salutation.UNKNOWN;
103 treeSelectMode = TREE_SELECT_MODE_KEYS[0];
104 treeListboxSelectMode = TREELISTBOX_SELECT_MODE_KEYS[0];
105 multiValue = new Salutation[0];
106 treeTabsState = 0;
107 sheetConfig = new SheetConfig();
108 String[] toolbarIconKeys
109 = {ToolBarTag.ICON_OFF, ToolBarTag.ICON_SMALL, ToolBarTag.ICON_BIG};
110 toolbarIconItems = new SelectItem[toolbarIconKeys.length];
111 for (int i = 0; i < toolbarIconKeys.length; i++) {
112 toolbarIconItems[i] = new SelectItem(toolbarIconKeys[i], toolbarIconKeys[i]);
113 }
114 toolbarIconSize = ToolBarTag.ICON_SMALL;
115
116 String[] toolbarTextKeys =
117 {ToolBarTag.LABEL_OFF, ToolBarTag.LABEL_BOTTOM, ToolBarTag.LABEL_RIGHT};
118 toolbarTextItems = new SelectItem[toolbarTextKeys.length];
119 for (int i = 0; i < toolbarTextKeys.length; i++) {
120 toolbarTextItems[i] = new SelectItem(toolbarTextKeys[i], toolbarTextKeys[i]);
121 }
122 toolbarTextPosition = ToolBarTag.LABEL_BOTTOM;
123 }
124
125 private static SelectItem[] getSalutationSelectItems(ResourceManager resourceManager, String resource) {
126 Salutation[] salutations = Salutation.values();
127 SelectItem[] items = new SelectItem[salutations.length];
128 for (int i = 0; i < items.length; i++) {
129 String label = resourceManager.getProperty(
130 FacesContext.getCurrentInstance().getViewRoot(), resource, salutations[i].getKey());
131 if (LOG.isTraceEnabled()) {
132 LOG.trace("label = " + label + "");
133 }
134 if (label == null) {
135 label = salutations[i].getKey();
136 }
137 items[i] = new SelectItem(salutations[i], label);
138 }
139 return items;
140 }
141
142 private static SelectItem[] getSelectItems(
143 String[] keys, ResourceManager resourceManager, String resource) {
144 SelectItem[] items = new SelectItem[keys.length];
145 for (int i = 0; i < items.length; i++) {
146 String label = resourceManager.getProperty(
147 FacesContext.getCurrentInstance().getViewRoot(), resource, keys[i]);
148 if (LOG.isTraceEnabled()) {
149 LOG.trace("label = " + label + "");
150 }
151 if (label == null) {
152 label = keys[i];
153 }
154 items[i] = new SelectItem(keys[i], label);
155 }
156 return items;
157 }
158
159 public void click(ActionEvent actionEvent) {
160 lastAction = actionEvent.getComponent().getId();
161 }
162
163
164 public void sheetSorter(ActionEvent event) {
165 if (event instanceof SortActionEvent) {
166 SortActionEvent sortEvent = (SortActionEvent) event;
167 UIData sheet = sortEvent.getSheet();
168 SheetState sheetState
169 = sheet.getSheetState(FacesContext.getCurrentInstance());
170 String columnId = sheetState.getSortedColumnId();
171 List<SolarObject> list = (List<SolarObject>) sheet.getValue();
172 SolarObject sun = list.remove(0);
173
174 Comparator<SolarObject> comparator = null;
175
176 if ("name".equals(columnId)) {
177 comparator = new Comparator<SolarObject>() {
178 public int compare(SolarObject o1, SolarObject o2) {
179 return o1.getName().compareToIgnoreCase(o2.getName());
180 }
181 };
182 } else if ("orbit".equals(columnId)) {
183 comparator = new Comparator<SolarObject>() {
184 public int compare(SolarObject o1, SolarObject o2) {
185 return o1.getOrbit().compareToIgnoreCase(o2.getOrbit());
186 }
187 };
188 } else if ("population".equals(columnId)) {
189 comparator = new Comparator<SolarObject>() {
190 public int compare(SolarObject o1, SolarObject o2) {
191 Integer i1 = -1;
192 try {
193 i1 = new Integer(o1.getPopulation().replaceAll("\\D", "").trim());
194 } catch (NumberFormatException e) {
195 // ignore
196 }
197 Integer i2 = -1;
198 try {
199 i2 = new Integer(o2.getPopulation().replaceAll("\\D", "").trim());
200 } catch (NumberFormatException e) {
201 // ignore
202 }
203 return i1.compareTo(i2);
204 }
205 };
206 } else if ("distance".equals(columnId)) {
207 comparator = new Comparator<SolarObject>() {
208 public int compare(SolarObject o1, SolarObject o2) {
209 return o1.getDistance().compareTo(o2.getDistance());
210 }
211 };
212 } else if ("period".equals(columnId)) {
213 comparator = new Comparator<SolarObject>() {
214 public int compare(SolarObject o1, SolarObject o2) {
215 return o1.getPeriod().compareTo(o2.getPeriod());
216 }
217 };
218 }
219
220 Collections.sort(list, comparator);
221 if (!sheetState.isAscending()) {
222 Collections.reverse(list);
223 }
224
225 list.add(0, sun);
226 }
227 }
228
229 public String ping() {
230 LOG.debug("ping invoked");
231 return null;
232 }
233
234 public void customValidator(FacesContext context, UIComponent component, Object value) throws ValidatorException {
235 if (value == null) {
236 return;
237 }
238 if (!"tobago".equalsIgnoreCase(value.toString())) {
239 throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please type in 'Tobago'",
240 "Please type in 'Tobago'"));
241 }
242 }
243
244 public void processTabChange(TabChangeEvent tabChangeEvent) {
245 int oldIndex = tabChangeEvent.getOldTabIndex();
246 int newIndex = tabChangeEvent.getNewTabIndex();
247 LOG.error("Tab index changed from Tab " + oldIndex + " to Tab " + newIndex);
248 }
249
250 public boolean getShowPopup() {
251 return "popupButton".equals(lastAction) || "popupButton2".equals(lastAction);
252 }
253
254 public SelectItem[] getItems() {
255 ResourceManager resourceManager = ResourceManagerFactory
256 .getResourceManager(FacesContext.getCurrentInstance());
257 return getSalutationSelectItems(resourceManager, "overview");
258 }
259
260 public SelectItem[] getItems2() {
261 ResourceManager resourceManager = ResourceManagerFactory
262 .getResourceManager(FacesContext.getCurrentInstance());
263 return getSelectItems(TREE_SELECT_MODE_KEYS, resourceManager, "overview");
264
265 }
266
267
268 public SelectItem[] getTreeSelectModeItems() {
269 ResourceManager resourceManager = ResourceManagerFactory
270 .getResourceManager(FacesContext.getCurrentInstance());
271 return getSelectItems(TREE_SELECT_MODE_KEYS, resourceManager, "overview");
272
273 }
274
275 public SelectItem[] getTreeListboxSelectModeItems() {
276 ResourceManager resourceManager = ResourceManagerFactory
277 .getResourceManager(FacesContext.getCurrentInstance());
278 return getSelectItems(TREELISTBOX_SELECT_MODE_KEYS, resourceManager, "overview");
279
280 }
281
282 public Salutation getRadioValue() {
283 return radioValue;
284 }
285
286 public void setRadioValue(Salutation radioValue) {
287 this.radioValue = radioValue;
288 }
289
290 public Salutation getSingleValue() {
291 return singleValue;
292 }
293
294 public void setSingleValue(Salutation singleValue) {
295 this.singleValue = singleValue;
296 }
297
298 public Salutation[] getMultiValue() {
299 return multiValue;
300 }
301
302 public void setMultiValue(Salutation[] multiValue) {
303 this.multiValue = multiValue;
304 }
305
306 public Date getBasicDate() {
307 return basicDate;
308 }
309
310 public void setBasicDate(Date basicDate) {
311 this.basicDate = basicDate;
312 }
313
314 public Date getBasicTime() {
315 return basicTime;
316 }
317
318 public void setBasicTime(Date basicTime) {
319 this.basicTime = basicTime;
320 }
321
322 public String getTreeSelectMode() {
323 return treeSelectMode;
324 }
325
326 public void setTreeSelectMode(String treeSelectMode) {
327 this.treeSelectMode = treeSelectMode;
328 }
329
330 public String getTreeListboxSelectMode() {
331 return treeListboxSelectMode;
332 }
333
334 public void setTreeListboxSelectMode(String treeListboxSelectMode) {
335 this.treeListboxSelectMode = treeListboxSelectMode;
336 }
337
338 public String getBasicInput() {
339 return basicInput;
340 }
341
342 public void setBasicInput(String basicInput) {
343 this.basicInput = basicInput;
344 }
345
346 public String getBasicArea() {
347 return basicArea;
348 }
349
350 public void setBasicArea(String basicArea) {
351 this.basicArea = basicArea;
352 }
353
354 public String getLastAction() {
355 return lastAction;
356 }
357
358 public Integer getTreeTabsState() {
359 return treeTabsState;
360 }
361
362 public void setTreeTabsState(Integer treeTabsState) {
363 this.treeTabsState = treeTabsState;
364 }
365
366 public SheetConfig getSheetConfig() {
367 return sheetConfig;
368 }
369
370 public void setSheetConfig(SheetConfig sheetConfig) {
371 this.sheetConfig = sheetConfig;
372 }
373
374 public String getToolbarIconSize() {
375 return toolbarIconSize;
376 }
377
378 public void setToolbarIconSize(String toolbarIconSize) {
379 this.toolbarIconSize = toolbarIconSize;
380 }
381
382 public SelectItem[] getToolbarIconItems() {
383 return toolbarIconItems;
384 }
385
386 public void setToolbarIconItems(SelectItem[] toolbarIconItems) {
387 this.toolbarIconItems = toolbarIconItems;
388 }
389
390 public String getToolbarTextPosition() {
391 return toolbarTextPosition;
392 }
393
394 public void setToolbarTextPosition(String toolbarTextPosition) {
395 this.toolbarTextPosition = toolbarTextPosition;
396 }
397
398 public SelectItem[] getToolbarTextItems() {
399 return toolbarTextItems;
400 }
401
402 public void setToolbarTextItems(SelectItem[] toolbarTextItems) {
403 this.toolbarTextItems = toolbarTextItems;
404 }
405
406 public List<String> getInputSuggestItems(String prefix) {
407 LOG.info("createing items for prefix :\"" + prefix + "\"");
408 List<String> li = new ArrayList<String>();
409 li.add(prefix+1);
410 li.add(prefix+2);
411 li.add(prefix+3);
412 li.add(prefix+4);
413 li.add(prefix+5);
414 li.add(prefix+6);
415 return li;
416 }
417 }