KickJava   Java API By Example, From Geeks To Geeks.

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


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: ScreenUtil.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 java.lang.ref.*;
28 import java.net.*;
29 import javax.servlet.*;
30 import javax.servlet.http.*;
31
32 import org.apache.log4j.*;
33 import org.w3c.dom.*;
34 import org.w3c.dom.html.*;
35
36 import org.enhydra.barracuda.core.comp.*;
37 import org.enhydra.barracuda.core.event.*;
38 import org.enhydra.barracuda.core.forms.*;
39 import org.enhydra.barracuda.plankton.l10n.*;
40
41
42 /**
43  * This class represents the master Barracuda config screen. It is stored
44  * in the users session on a per-locale basis, and is used kind of as a
45  * master repository for objects that make up the system.
46  */

47 public class ScreenUtil {
48
49     //public constants
50
protected static final Logger logger = Logger.getLogger(ScreenUtil.class.getName());
51
52     /**
53      * This method greats a typical debug level BSelect component
54      *
55      * @param vc the view context
56      * @param compName the component name
57      * @param selectedIndex the index to be selected
58      * @return the resulting BSelect component
59      */

60     public static BSelect getDebugLevelComp(ViewContext vc, String JavaDoc compName, int selectedIndex) {
61         DefaultListModel dlm = new DefaultListModel();
62
63 /*
64         dlm.add(new DefaultItemMap(0, "Debug Off"));
65         dlm.add(new DefaultItemMap(1, "Debug = 1"));
66         dlm.add(new DefaultItemMap(2, "Debug = 2"));
67         dlm.add(new DefaultItemMap(3, "Debug = 3"));
68         dlm.add(new DefaultItemMap(4, "Debug = 4"));
69         dlm.add(new DefaultItemMap(5, "Debug = 5"));
70 */

71
72         Locale curloc = vc.getViewCapabilities().getClientLocale();
73         ResourceBundle rb = ResourceBundle.getBundle("org.enhydra.barracuda.config.xmlc.Config", curloc);
74         String JavaDoc debugOffStr = Localize.getString(rb, "Config.General.Debug_Off");
75         String JavaDoc debugStr = Localize.getString(rb, "Config.General.Debug");
76         
77         dlm.add(new DefaultItemMap(0, debugOffStr));
78         dlm.add(new DefaultItemMap(1, debugStr+" = 1"));
79         dlm.add(new DefaultItemMap(2, debugStr+" = 2"));
80         dlm.add(new DefaultItemMap(3, debugStr+" = 3"));
81         dlm.add(new DefaultItemMap(4, debugStr+" = 4"));
82         dlm.add(new DefaultItemMap(5, debugStr+" = 5"));
83
84         return getSelectComp(vc, compName, dlm, selectedIndex);
85     }
86
87     /**
88      * This method greats a typical debug level BSelect component, using the new
89      * Log4J options (Debug, Info, Warn, Error, Fatal)
90      *
91      * @param vc the view context
92      * @param compName the component name
93      * @param selectedIndex the index to be selected
94      * @return the resulting BSelect component
95      */

96     public static BSelect getDebugLevelComp2(ViewContext vc, String JavaDoc compName, Class JavaDoc cl) {
97         DefaultListModel dlm = new DefaultListModel();
98
99         //build the list
100
Locale curloc = vc.getViewCapabilities().getClientLocale();
101         ResourceBundle rb = ResourceBundle.getBundle("org.enhydra.barracuda.config.xmlc.Config", curloc);
102         String JavaDoc logShort = Localize.getString(rb, "Config.General.Logging.Short");
103         dlm.add(new DefaultItemMap(0, logShort+" "+Localize.getString(rb, "Config.General.Logging.Default")));
104         dlm.add(new DefaultItemMap(1, logShort+" "+Localize.getString(rb, "Config.General.Logging.Fatal")));
105         dlm.add(new DefaultItemMap(2, logShort+" "+Localize.getString(rb, "Config.General.Logging.Error")));
106         dlm.add(new DefaultItemMap(3, logShort+" "+Localize.getString(rb, "Config.General.Logging.Warn")));
107         dlm.add(new DefaultItemMap(4, logShort+" "+Localize.getString(rb, "Config.General.Logging.Info")));
108         dlm.add(new DefaultItemMap(5, logShort+" "+Localize.getString(rb, "Config.General.Logging.Debug")));
109
110         return getSelectComp(vc, compName, dlm, cvtLevelToInt(cl));
111     }
112     
113     public static int cvtLevelToInt(Class JavaDoc cl) {
114         Level l = null;
115         if (cl==null) l = Logger.getRootLogger().getLevel();
116         else l = Logger.getLogger(cl).getLevel();
117 //System.out.println ("Checking level for class "+cl+":"+cvtLevelToInt(l));
118
return cvtLevelToInt(l);
119     }
120     
121     public static int cvtLevelToInt(Level l) {
122         if (l==null) return 0;
123         if (l.equals(Level.DEBUG)) return 5;
124         else if (l.equals(Level.INFO)) return 4;
125         else if (l.equals(Level.WARN)) return 3;
126         else if (l.equals(Level.ERROR)) return 2;
127         else if (l.equals(Level.FATAL)) return 1;
128         else return 0;
129     }
130     
131     public static Level cvtIntToLevel(int i) {
132         if (i==5) return Level.DEBUG;
133         else if (i==4) return Level.INFO;
134         else if (i==3) return Level.WARN;
135         else if (i==2) return Level.ERROR;
136         else if (i==1) return Level.FATAL;
137         else return null;
138     }
139     
140     public static void setLevel(Class JavaDoc cl, int i) {
141         Logger logr = Logger.getLogger(cl);
142         logr.setLevel(cvtIntToLevel(i));
143     }
144
145
146     /**
147      * This method greats a typical debug level BSelect component
148      *
149      * @param vc the view context
150      * @param compName the component name
151      * @param selectedIndex the index to be selected
152      * @return the resulting BSelect component
153      */

154     public static BSelect getSelectComp(ViewContext vc, String JavaDoc compName, ListModel lm, int selectedIndex) {
155 // DefaultView dlv = new DefaultView(vc.getTemplateNode().cloneNode(true));
156
// BSelect bsComp = new BSelect(lm, dlv, null);
157
BSelect bsComp = new BSelect(lm);
158         bsComp.setName(compName);
159         bsComp.setSelectedIndex(selectedIndex);
160         if (logger.isDebugEnabled()) logger.debug("created BSelect component: "+bsComp);
161         return bsComp;
162     }
163     
164     /**
165      * This method greats a typical debug level BSelect component
166      *
167      * @param vc the view context
168      * @param lf the listener factory to be notified when the action occurs
169      * @return the resulting BAction component
170      */

171     public static BToggleButton getToggleButton(ViewContext vc, String JavaDoc compName, String JavaDoc value, boolean selected) {
172 // DefaultView dv = new DefaultView(vc.getTemplateNode().cloneNode(true));
173
// BToggleButton btbComp = new BToggleButton(null, name, value, selected, dv, null);
174
BToggleButton btbComp = new BToggleButton(null, compName, value, selected);
175         if (logger.isDebugEnabled()) logger.debug("created BToggleButton component: "+btbComp);
176         return btbComp;
177     }
178
179
180     /**
181      * This method creates a BAction component and adds the specified
182      * listener to it.
183      *
184      * @param vc the view context
185      * @param lf the listener factory to be notified when the action occurs
186      * @return the resulting BAction component
187      */

188     public static BAction getActionLink(ViewContext vc, ListenerFactory lf) {
189 // DefaultView dv = new DefaultView(vc.getTemplateNode().cloneNode(true));
190
// BAction baComp = new BAction(dv);
191
BAction baComp = new BAction();
192         baComp.addEventListener(lf);
193         baComp.setDisableBackButton(true);
194         if (logger.isDebugEnabled()) logger.debug("created BAction component: "+baComp);
195         return baComp;
196     }
197
198     /**
199      * This method greats a typical debug level BSelect component
200      *
201      * @param vc the view context
202      * @param lf the listener factory to be notified when the action occurs
203      * @return the resulting BAction component
204      */

205     public static BAction getUpdateButtonComp(ViewContext vc, ListenerFactory lf) {
206 // DefaultView dlv = new DefaultView(vc.getTemplateNode().cloneNode(true));
207
// BAction baComp = new BAction(dlv);
208
BAction baComp = new BAction();
209         baComp.addEventListener(lf);
210         //baComp.setDisableBackButton(true);
211
if (logger.isDebugEnabled()) logger.debug("created BAction component: "+baComp);
212         return baComp;
213     }
214
215     /**
216      * This method extracts a ValidationException error message
217      * (if it exists) from a ViewContext object. This method expects
218      * the ViewContext to contain an EventContext, which in turn must
219      * contain a FormMap (specified by formName), which in turn must
220      * contain a ValidationException (specified by keyName).
221      *
222      * @param vc the view context
223      * @param keyName the key name which was used to store the
224      * ValidationException
225      * @return the resulting error message (blank is it doesn't exist)
226      */

227     public static String JavaDoc getErrMsg(ViewContext vc, String JavaDoc formName, String JavaDoc keyName) {
228         String JavaDoc errmsg = " ";
229         EventContext ec = vc.getEventContext();
230         if (ec!=null) {
231             FormMap formMap = (FormMap) ec.getState(formName);
232             if (formMap!=null) {
233                 ValidationException ve = (ValidationException) formMap.getState(keyName);
234                 if (ve!=null) {
235                     List errlist = ve.getExceptionList();
236                     Iterator it = errlist.iterator();
237                     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(100);
238                     while (it.hasNext()) {
239                         sb.append(" "+((ValidationException) it.next()).getMessage());
240                     }
241                     errmsg = sb.toString();
242                 }
243             }
244         }
245         if (logger.isDebugEnabled()) logger.debug("errmsg: "+errmsg);
246         return errmsg;
247     }
248
249     /**
250      * This method creates a success message
251      *
252      * @param vc the view context
253      * @param keyName the key name which was used to store the
254      * ValidationException
255      * @return the resulting error message (blank is it doesn't exist)
256      */

257     public static String JavaDoc getSuccessMsg(ViewContext vc, String JavaDoc formName, String JavaDoc keyName) {
258         String JavaDoc msg = " ";
259         EventContext ec = vc.getEventContext();
260         if (ec!=null) {
261             FormMap formMap = (FormMap) ec.getState(formName);
262             if (formMap!=null) {
263                 if (formMap.getState(keyName)!=null) msg = "Update successful.";
264             }
265         }
266         return msg;
267     }
268 }
269
270
271
272
Popular Tags