KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > config > MasterConfig


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site (http://www.enhydra.org/).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: MasterConfig.java,v 1.1 2004/05/28 19:39:27 slobodan Exp $
22  */

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 /**
45  * This class provides the methods needed to configure the util
46  * screen
47  */

48 public class MasterConfig extends DefaultEventGateway {
49
50     protected static final Logger logger = Logger.getLogger(MasterConfig.class.getName());
51
52     //form id (must be unique)
53
public static final String JavaDoc FORM = MasterConfig.class.getName()+".Form";
54
55     //model name
56
public static final String JavaDoc MODEL_NAME = "Master";
57
58     //model elements (these are the data items supported)
59
public static final String JavaDoc LOCALE = "Locale";
60     public static final String JavaDoc LOGGING = "Logging";
61
62
63     //supported locales
64
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 JavaDoc[] langs = new String JavaDoc[] {
75                                         "English",
76                                         "Deutsch",
77                                         "Español",
78                                         "Français",
79                                         "Svenska",
80                                         "Português",
81                                         "Suomi",
82                                         };
83
84     //private vars
85
private ListenerFactory updateConfigFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new UpdateConfigHandler();} public String JavaDoc getListenerID() {return getID(UpdateConfigHandler.class);}};
86
87     public MasterConfig() {
88         //specify generic interest
89
specifyLocalEventInterests(updateConfigFactory);
90     }
91
92
93     //------------------------------------------------------------
94
// Data Models
95
//------------------------------------------------------------
96
/**
97      * define the template model that backs this screen
98      */

99     class MasterModel extends AbstractTemplateModel {
100
101         //register the model by name
102
public String JavaDoc getName() {return MODEL_NAME;}
103         
104         //provide items by key
105
public Object JavaDoc getItem(String JavaDoc key) {
106             if (logger.isDebugEnabled()) logger.debug("Asking for key:"+key);
107             ViewContext vc = getViewContext();
108
109             //Handle requests for data
110
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 //csc_041403.2 bsComp.addEventListener(updateConfigFactory);
119
//csc_041403.2 bsComp.setDisableBackButton(true);
120
bsComp.addEventListener(updateConfigFactory, true); //csc_041403.2
121
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); //offset by 1 since option 0 ('System') is not a valid option here
134
//csc_041403.2 bsComp.addEventListener(updateConfigFactory);
135
//csc_041403.2 bsComp.setDisableBackButton(true);
136
bsComp.addEventListener(updateConfigFactory, true); //csc_041403.2
137
return bsComp;
138             } else return super.getItem(key);
139         }
140     }
141
142     //------------------------------------------------------------
143
// Form Mappings, Validators
144
//------------------------------------------------------------
145
/**
146      * define the form map that backs the model
147      */

148     class MasterForm extends DefaultFormMap {
149         public MasterForm() {
150             //define the elements (note: these don't need any validators). Note
151
//also that we set the defaults to the current values. This allows
152
//us just to reset all current values down below without checking to
153
//see if the elements actually got passed in from the form.
154
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 JavaDoc(-1), null, false));
157         }
158     }
159
160     //------------------------------------------------------------
161
// Model 2 - Controller Event Handlers
162
//------------------------------------------------------------
163
/**
164      * UpdateConfigHandler - handle the request to update the config
165      * screen.
166      */

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             //create the login form
172
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                 //map/validate the form
179
formMap.map(context.getRequest()).validate(true);
180                 
181                 //update the language value if it changed
182
newsel = formMap.getIntegerVal(LOCALE).intValue();
183                 if (newsel!=sel) {
184                     Locales.saveClientLocale(context, locales[newsel]);
185                     
186                     //fire an update so that the screen will redraw
187
screen.bcRoot.validate();
188                 }
189                 
190                 //update the logging value if it changed
191
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                     //fire an update so that the screen will redraw
197
screen.bcRoot.validate();
198                 }
199
200             } catch (ValidationException e) {
201             }
202             
203             //redirect to the get screen again
204
throw new ClientSideRedirectException(new GetBConfig());
205         }
206     }
207
208
209
210     //------------------------------------------------------------
211
// Utility Methods
212
//------------------------------------------------------------
213
public TemplateModel getModel() {
214         return new MasterModel();
215     }
216 }
217
218
Popular Tags