001: package org.araneaframework.example.main.web.testing;
002:
003: import java.lang.reflect.Field;
004: import java.util.ArrayList;
005: import java.util.Iterator;
006: import java.util.List;
007: import java.util.Map;
008: import org.apache.commons.collections.Transformer;
009: import org.apache.commons.lang.ClassUtils;
010: import org.araneaframework.Widget;
011: import org.araneaframework.example.main.TemplateBaseWidget;
012: import org.araneaframework.example.main.web.OverlayRootWidget;
013: import org.araneaframework.example.main.web.menu.MenuWidget;
014: import org.araneaframework.framework.container.StandardFlowContainerWidget;
015: import org.araneaframework.uilib.core.MenuContext;
016: import org.araneaframework.uilib.core.MenuItem;
017: import org.araneaframework.uilib.event.OnChangeEventListener;
018: import org.araneaframework.uilib.form.FormWidget;
019: import org.araneaframework.uilib.form.control.SelectControl;
020: import org.araneaframework.uilib.form.data.StringData;
021: import org.araneaframework.uilib.support.DisplayItem;
022: import org.araneaframework.uilib.util.DisplayItemUtil;
023:
024: public class ModalDialogTestWidget extends TemplateBaseWidget {
025: private FormWidget form;
026: private List menuitems = new ArrayList();
027:
028: protected void init() throws Exception {
029: setViewSelector("testing/modalDialogTest");
030: MenuWidget menuWidget = (MenuWidget) getEnvironment().getEntry(
031: MenuContext.class);
032: new MenuItemCollector().visit(menuWidget.getMenu(), menuitems);
033:
034: form = new FormWidget();
035: SelectControl select = new SelectControl();
036: select.addItem(new DisplayItem(null, t("select.choose")));
037: select.addOnChangeEventListener(new OnChangeEventListener() {
038: public void onChange() throws Exception {
039: if (form.getElement("classSelect").convertAndValidate()) {
040: String className = (String) form
041: .getValueByFullName("classSelect");
042:
043: Widget newInstance = (Widget) org.springframework.util.ClassUtils
044: .forName(className).newInstance();
045: Widget testWidget = new OverlayRootWidget(
046: new StandardFlowContainerWidget(
047: new WrapperWidget(newInstance)));
048:
049: getOverlayCtx().start(testWidget);
050: }
051: }
052: });
053:
054: DisplayItemUtil.addItemsFromBeanCollection(select, menuitems,
055: new ValueTransformer(), new DSTransformer());
056:
057: form.addElement("classSelect", "#Select your poison", select,
058: new StringData(), false);
059: addWidget("form", form);
060: }
061:
062: private void handleEventReturn() {
063: getFlowCtx().cancel();
064: }
065:
066: private static class DSTransformer implements Transformer {
067: public Object transform(Object o) {
068: return ClassUtils.getShortClassName(((Class) o).getName());
069: }
070: }
071:
072: private static class ValueTransformer implements Transformer {
073: public Object transform(Object o) {
074: return ((Class) o).getName();
075: }
076: }
077:
078: private static class WrapperWidget extends TemplateBaseWidget {
079: private Widget wrapped;
080:
081: public WrapperWidget(Widget wrapped) {
082: this .wrapped = wrapped;
083: }
084:
085: protected void init() throws Exception {
086: setViewSelector("testing/renderChild");
087: addWidget("wrapped", wrapped);
088: }
089:
090: private void handleEventGobacknow() {
091: getFlowCtx().cancel();
092: }
093: }
094:
095: private class MenuItemCollector {
096: public void visit(MenuItem menu, List itemList)
097: throws SecurityException, NoSuchFieldException,
098: IllegalArgumentException, IllegalAccessException {
099: Class c = menu.getClass();
100: Field classfld = menu.getClass().getDeclaredField(
101: "flowClass");
102:
103: if (classfld != null) {
104: classfld.setAccessible(true);
105: Class clazz = (Class) classfld.get(menu);
106: if (clazz != null)
107: itemList.add(clazz);
108: }
109:
110: Map subMenu = menu.getSubMenu();
111:
112: if (subMenu == null)
113: return;
114:
115: for (Iterator i = subMenu.entrySet().iterator(); i
116: .hasNext();) {
117: Map.Entry entry = (Map.Entry) i.next();
118: MenuItem menuItem = ((MenuItem) entry.getValue());
119: new MenuItemCollector().visit(menuItem, itemList);
120: }
121: }
122: }
123: }
|