1 23 package org.enhydra.barracuda.config; 24 25 import java.io.*; 26 import java.util.*; 27 import javax.servlet.*; 28 import javax.servlet.http.*; 29 30 import org.apache.log4j.*; 31 32 import org.enhydra.barracuda.config.events.*; 33 import org.enhydra.barracuda.core.comp.*; 34 import org.enhydra.barracuda.core.event.*; 35 import org.enhydra.barracuda.core.forms.*; 36 import org.enhydra.barracuda.core.forms.validators.*; 37 import org.enhydra.barracuda.core.util.dom.*; 38 import org.enhydra.barracuda.core.util.l10n.Locales; 39 import org.enhydra.barracuda.plankton.data.*; 40 import org.enhydra.barracuda.plankton.http.*; 41 import org.enhydra.barracuda.plankton.l10n.Localize; 42 43 44 48 public class MasterConfig extends DefaultEventGateway { 49 50 protected static final Logger logger = Logger.getLogger(MasterConfig.class.getName()); 51 52 public static final String FORM = MasterConfig.class.getName()+".Form"; 54 55 public static final String MODEL_NAME = "Master"; 57 58 public static final String LOCALE = "Locale"; 60 public static final String LOGGING = "Logging"; 61 62 63 private static final Locale[] locales = new Locale[] { 65 Locale.ENGLISH, 66 Locale.GERMAN, 67 new Locale("es",""), 68 new Locale("fr",""), 69 new Locale("sv",""), 70 new Locale("pt",""), 71 new Locale("fi",""), 72 }; 73 74 private static final String [] langs = new String [] { 75 "English", 76 "Deutsch", 77 "Español", 78 "Français", 79 "Svenska", 80 "Português", 81 "Suomi", 82 }; 83 84 private ListenerFactory updateConfigFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new UpdateConfigHandler();} public String getListenerID() {return getID(UpdateConfigHandler.class);}}; 86 87 public MasterConfig() { 88 specifyLocalEventInterests(updateConfigFactory); 90 } 91 92 93 99 class MasterModel extends AbstractTemplateModel { 100 101 public String getName() {return MODEL_NAME;} 103 104 public Object getItem(String key) { 106 if (logger.isDebugEnabled()) logger.debug("Asking for key:"+key); 107 ViewContext vc = getViewContext(); 108 109 if (key.equals(LOCALE)) { 111 DefaultListModel dlm = new DefaultListModel(); 112 Locale curloc = vc.getViewCapabilities().getClientLocale(); 113 int sel = Locales.findClosestLocale(curloc, locales, 0); 114 for (int i=0, max=locales.length; i<max; i++) { 115 dlm.add(new DefaultItemMap(i, langs[i])); 116 } 117 BSelect bsComp = ScreenUtil.getSelectComp(vc, key, dlm, sel); 118 bsComp.addEventListener(updateConfigFactory, true); return bsComp; 122 123 } else if (key.equals(LOGGING)) { 124 DefaultListModel dlm = new DefaultListModel(); 125 Locale curloc = vc.getViewCapabilities().getClientLocale(); 126 ResourceBundle rb = ResourceBundle.getBundle("org.enhydra.barracuda.config.xmlc.Config", curloc); 127 dlm.add(new DefaultItemMap(1, Localize.getString(rb, "Config.General.Logging.Fatal"))); 128 dlm.add(new DefaultItemMap(2, Localize.getString(rb, "Config.General.Logging.Error"))); 129 dlm.add(new DefaultItemMap(3, Localize.getString(rb, "Config.General.Logging.Warn"))); 130 dlm.add(new DefaultItemMap(4, Localize.getString(rb, "Config.General.Logging.Info"))); 131 dlm.add(new DefaultItemMap(5, Localize.getString(rb, "Config.General.Logging.Debug"))); 132 int selIndex = ScreenUtil.cvtLevelToInt(Logger.getRootLogger().getLevel())-1; 133 BSelect bsComp = ScreenUtil.getSelectComp(vc, key, dlm, selIndex); bsComp.addEventListener(updateConfigFactory, true); return bsComp; 138 } else return super.getItem(key); 139 } 140 } 141 142 148 class MasterForm extends DefaultFormMap { 149 public MasterForm() { 150 if (logger.isDebugEnabled()) logger.debug("Defining form elements"); 155 this.defineElement(new DefaultFormElement(LOCALE, FormType.INTEGER, null, null, false)); 156 this.defineElement(new DefaultFormElement(LOGGING, FormType.INTEGER, new Integer (-1), null, false)); 157 } 158 } 159 160 167 class UpdateConfigHandler extends DefaultBaseEventListener { 168 public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException { 169 if (logger.isDebugEnabled()) ServletUtil.showParams(context.getRequest(), logger); 170 171 ValidationException ve = null; 173 MasterForm formMap = new MasterForm(); 174 int sel = Locales.findClosestLocale(context.getViewCapabilities().getClientLocale(), locales, 0); 175 MasterScreen screen = new MasterScreenFactory().getInstance(MasterConfig.this, context, locales[sel]); 176 int newsel = sel; 177 try { 178 formMap.map(context.getRequest()).validate(true); 180 181 newsel = formMap.getIntegerVal(LOCALE).intValue(); 183 if (newsel!=sel) { 184 Locales.saveClientLocale(context, locales[newsel]); 185 186 screen.bcRoot.validate(); 188 } 189 190 int logPri = formMap.getIntegerVal(LOGGING).intValue(); 192 int curPri = ScreenUtil.cvtLevelToInt(Logger.getRootLogger().getLevel()); 193 if (logPri>-1 && logPri!=curPri) { 194 Logger.getRootLogger().setLevel(ScreenUtil.cvtIntToLevel(logPri)); 195 196 screen.bcRoot.validate(); 198 } 199 200 } catch (ValidationException e) { 201 } 202 203 throw new ClientSideRedirectException(new GetBConfig()); 205 } 206 } 207 208 209 210 public TemplateModel getModel() { 214 return new MasterModel(); 215 } 216 } 217 218 | Popular Tags |