001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site (http://www.enhydra.org/).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: MasterConfig.java,v 1.1 2006-09-11 12:30:02 sinisa Exp $
022: */
023: package org.enhydra.barracuda.config;
024:
025: import java.io.*;
026: import java.util.*;
027: import javax.servlet.*;
028: import javax.servlet.http.*;
029:
030: import org.apache.log4j.*;
031:
032: import org.enhydra.barracuda.config.events.*;
033: import org.barracudamvc.core.comp.*;
034: import org.barracudamvc.core.event.*;
035: import org.barracudamvc.core.forms.*;
036: import org.barracudamvc.core.forms.validators.*;
037: import org.barracudamvc.core.util.dom.*;
038: import org.barracudamvc.core.util.l10n.Locales;
039: import org.barracudamvc.plankton.data.*;
040: import org.barracudamvc.plankton.http.*;
041: import org.barracudamvc.plankton.l10n.Localize;
042:
043: /**
044: * This class provides the methods needed to configure the util
045: * screen
046: */
047: public class MasterConfig extends DefaultEventGateway {
048:
049: protected static final Logger logger = Logger
050: .getLogger(MasterConfig.class.getName());
051:
052: //form id (must be unique)
053: public static final String FORM = MasterConfig.class.getName()
054: + ".Form";
055:
056: //model name
057: public static final String MODEL_NAME = "Master";
058:
059: //model elements (these are the data items supported)
060: public static final String LOCALE = "Locale";
061: public static final String LOGGING = "Logging";
062:
063: //supported locales
064: private static final Locale[] locales = new Locale[] {
065: Locale.ENGLISH, Locale.GERMAN, new Locale("es", ""),
066: new Locale("fr", ""), new Locale("sv", ""),
067: new Locale("pt", ""), new Locale("fi", ""), };
068:
069: private static final String[] langs = new String[] { "English",
070: "Deutsch", "Español", "Français", "Svenska", "Português",
071: "Suomi", };
072:
073: //private vars
074: private ListenerFactory updateConfigFactory = new DefaultListenerFactory() {
075: public BaseEventListener getInstance() {
076: return new UpdateConfigHandler();
077: }
078:
079: public String getListenerID() {
080: return getID(UpdateConfigHandler.class);
081: }
082: };
083:
084: public MasterConfig() {
085: //specify generic interest
086: specifyLocalEventInterests(updateConfigFactory);
087: }
088:
089: //------------------------------------------------------------
090: // Data Models
091: //------------------------------------------------------------
092: /**
093: * define the template model that backs this screen
094: */
095: class MasterModel extends AbstractTemplateModel {
096:
097: //register the model by name
098: public String getName() {
099: return MODEL_NAME;
100: }
101:
102: //provide items by key
103: public Object getItem(String key) {
104: if (logger.isDebugEnabled())
105: logger.debug("Asking for key:" + key);
106: ViewContext vc = getViewContext();
107:
108: //Handle requests for data
109: if (key.equals(LOCALE)) {
110: DefaultListModel dlm = new DefaultListModel();
111: Locale curloc = vc.getViewCapabilities()
112: .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,
118: sel);
119: //csc_041403.2 bsComp.addEventListener(updateConfigFactory);
120: //csc_041403.2 bsComp.setDisableBackButton(true);
121: bsComp.addEventListener(updateConfigFactory, true); //csc_041403.2
122: return bsComp;
123:
124: } else if (key.equals(LOGGING)) {
125: DefaultListModel dlm = new DefaultListModel();
126: Locale curloc = vc.getViewCapabilities()
127: .getClientLocale();
128: ResourceBundle rb = ResourceBundle.getBundle(
129: "org.enhydra.barracuda.config.xmlc.Config",
130: curloc);
131: dlm.add(new DefaultItemMap(1, Localize.getString(rb,
132: "Config.General.Logging.Fatal")));
133: dlm.add(new DefaultItemMap(2, Localize.getString(rb,
134: "Config.General.Logging.Error")));
135: dlm.add(new DefaultItemMap(3, Localize.getString(rb,
136: "Config.General.Logging.Warn")));
137: dlm.add(new DefaultItemMap(4, Localize.getString(rb,
138: "Config.General.Logging.Info")));
139: dlm.add(new DefaultItemMap(5, Localize.getString(rb,
140: "Config.General.Logging.Debug")));
141: int selIndex = ScreenUtil.cvtLevelToInt(Logger
142: .getRootLogger().getLevel()) - 1;
143: BSelect bsComp = ScreenUtil.getSelectComp(vc, key, dlm,
144: selIndex); //offset by 1 since option 0 ('System') is not a valid option here
145: //csc_041403.2 bsComp.addEventListener(updateConfigFactory);
146: //csc_041403.2 bsComp.setDisableBackButton(true);
147: bsComp.addEventListener(updateConfigFactory, true); //csc_041403.2
148: return bsComp;
149: } else
150: return super .getItem(key);
151: }
152: }
153:
154: //------------------------------------------------------------
155: // Form Mappings, Validators
156: //------------------------------------------------------------
157: /**
158: * define the form map that backs the model
159: */
160: class MasterForm extends DefaultFormMap {
161: public MasterForm() {
162: //define the elements (note: these don't need any validators). Note
163: //also that we set the defaults to the current values. This allows
164: //us just to reset all current values down below without checking to
165: //see if the elements actually got passed in from the form.
166: if (logger.isDebugEnabled())
167: logger.debug("Defining form elements");
168: this .defineElement(new DefaultFormElement(LOCALE,
169: FormType.INTEGER, null, null, false));
170: this .defineElement(new DefaultFormElement(LOGGING,
171: FormType.INTEGER, new Integer(-1), null, false));
172: }
173: }
174:
175: //------------------------------------------------------------
176: // Model 2 - Controller Event Handlers
177: //------------------------------------------------------------
178: /**
179: * UpdateConfigHandler - handle the request to update the config
180: * screen.
181: */
182: class UpdateConfigHandler extends DefaultBaseEventListener {
183: public void handleControlEvent(ControlEventContext context)
184: throws EventException, ServletException, IOException {
185: if (logger.isDebugEnabled())
186: ServletUtil.showParams(context.getRequest(), logger);
187:
188: //create the login form
189: ValidationException ve = null;
190: MasterForm formMap = new MasterForm();
191: int sel = Locales.findClosestLocale(context
192: .getViewCapabilities().getClientLocale(), locales,
193: 0);
194: MasterScreen screen = new MasterScreenFactory()
195: .getInstance(MasterConfig.this , context,
196: locales[sel]);
197: int newsel = sel;
198: try {
199: //map/validate the form
200: formMap.map(context.getRequest()).validate(true);
201:
202: //update the language value if it changed
203: newsel = ((Integer) formMap.getVal(LOCALE)).intValue();
204: if (newsel != sel) {
205: Locales.saveClientLocale(context, locales[newsel]);
206:
207: //fire an update so that the screen will redraw
208: screen.bcRoot.validate();
209: }
210:
211: //update the logging value if it changed
212: int logPri = ((Integer) formMap.getVal(LOGGING))
213: .intValue();
214: int curPri = ScreenUtil.cvtLevelToInt(Logger
215: .getRootLogger().getLevel());
216: if (logPri > -1 && logPri != curPri) {
217: Logger.getRootLogger().setLevel(
218: ScreenUtil.cvtIntToLevel(logPri));
219:
220: //fire an update so that the screen will redraw
221: screen.bcRoot.validate();
222: }
223:
224: } catch (ValidationException e) {
225: }
226:
227: //redirect to the get screen again
228: throw new ClientSideRedirectException(new GetBConfig());
229: }
230: }
231:
232: //------------------------------------------------------------
233: // Utility Methods
234: //------------------------------------------------------------
235: public TemplateModel getModel() {
236: return new MasterModel();
237: }
238: }
|