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: ScreenUtil.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 java.lang.ref.*;
028: import java.net.*;
029: import javax.servlet.*;
030: import javax.servlet.http.*;
031:
032: import org.apache.log4j.*;
033: import org.w3c.dom.*;
034: import org.w3c.dom.html.*;
035:
036: import org.barracudamvc.core.comp.*;
037: import org.barracudamvc.core.event.*;
038: import org.barracudamvc.core.forms.*;
039: import org.barracudamvc.plankton.l10n.*;
040:
041: /**
042: * This class represents the master Barracuda config screen. It is stored
043: * in the users session on a per-locale basis, and is used kind of as a
044: * master repository for objects that make up the system.
045: */
046: public class ScreenUtil {
047:
048: //public constants
049: protected static final Logger logger = Logger
050: .getLogger(ScreenUtil.class.getName());
051:
052: /**
053: * This method greats a typical debug level BSelect component
054: *
055: * @param vc the view context
056: * @param compName the component name
057: * @param selectedIndex the index to be selected
058: * @return the resulting BSelect component
059: */
060: public static BSelect getDebugLevelComp(ViewContext vc,
061: String compName, int selectedIndex) {
062: DefaultListModel dlm = new DefaultListModel();
063:
064: /*
065: dlm.add(new DefaultItemMap(0, "Debug Off"));
066: dlm.add(new DefaultItemMap(1, "Debug = 1"));
067: dlm.add(new DefaultItemMap(2, "Debug = 2"));
068: dlm.add(new DefaultItemMap(3, "Debug = 3"));
069: dlm.add(new DefaultItemMap(4, "Debug = 4"));
070: dlm.add(new DefaultItemMap(5, "Debug = 5"));
071: */
072:
073: Locale curloc = vc.getViewCapabilities().getClientLocale();
074: ResourceBundle rb = ResourceBundle.getBundle(
075: "org.enhydra.barracuda.config.xmlc.Config", curloc);
076: String debugOffStr = Localize.getString(rb,
077: "Config.General.Debug_Off");
078: String debugStr = Localize
079: .getString(rb, "Config.General.Debug");
080:
081: dlm.add(new DefaultItemMap(0, debugOffStr));
082: dlm.add(new DefaultItemMap(1, debugStr + " = 1"));
083: dlm.add(new DefaultItemMap(2, debugStr + " = 2"));
084: dlm.add(new DefaultItemMap(3, debugStr + " = 3"));
085: dlm.add(new DefaultItemMap(4, debugStr + " = 4"));
086: dlm.add(new DefaultItemMap(5, debugStr + " = 5"));
087:
088: return getSelectComp(vc, compName, dlm, selectedIndex);
089: }
090:
091: /**
092: * This method greats a typical debug level BSelect component, using the new
093: * Log4J options (Debug, Info, Warn, Error, Fatal)
094: *
095: * @param vc the view context
096: * @param compName the component name
097: * @param selectedIndex the index to be selected
098: * @return the resulting BSelect component
099: */
100: public static BSelect getDebugLevelComp2(ViewContext vc,
101: String compName, Class cl) {
102: DefaultListModel dlm = new DefaultListModel();
103:
104: //build the list
105: Locale curloc = vc.getViewCapabilities().getClientLocale();
106: ResourceBundle rb = ResourceBundle.getBundle(
107: "org.enhydra.barracuda.config.xmlc.Config", curloc);
108: String logShort = Localize.getString(rb,
109: "Config.General.Logging.Short");
110: dlm.add(new DefaultItemMap(0, logShort
111: + " "
112: + Localize.getString(rb,
113: "Config.General.Logging.Default")));
114: dlm
115: .add(new DefaultItemMap(1, logShort
116: + " "
117: + Localize.getString(rb,
118: "Config.General.Logging.Fatal")));
119: dlm
120: .add(new DefaultItemMap(2, logShort
121: + " "
122: + Localize.getString(rb,
123: "Config.General.Logging.Error")));
124: dlm
125: .add(new DefaultItemMap(3, logShort
126: + " "
127: + Localize.getString(rb,
128: "Config.General.Logging.Warn")));
129: dlm
130: .add(new DefaultItemMap(4, logShort
131: + " "
132: + Localize.getString(rb,
133: "Config.General.Logging.Info")));
134: dlm
135: .add(new DefaultItemMap(5, logShort
136: + " "
137: + Localize.getString(rb,
138: "Config.General.Logging.Debug")));
139:
140: return getSelectComp(vc, compName, dlm, cvtLevelToInt(cl));
141: }
142:
143: public static int cvtLevelToInt(Class cl) {
144: Level l = null;
145: if (cl == null)
146: l = Logger.getRootLogger().getLevel();
147: else
148: l = Logger.getLogger(cl).getLevel();
149: //System.out.println ("Checking level for class "+cl+":"+cvtLevelToInt(l));
150: return cvtLevelToInt(l);
151: }
152:
153: public static int cvtLevelToInt(Level l) {
154: if (l == null)
155: return 0;
156: if (l.equals(Level.DEBUG))
157: return 5;
158: else if (l.equals(Level.INFO))
159: return 4;
160: else if (l.equals(Level.WARN))
161: return 3;
162: else if (l.equals(Level.ERROR))
163: return 2;
164: else if (l.equals(Level.FATAL))
165: return 1;
166: else
167: return 0;
168: }
169:
170: public static Level cvtIntToLevel(int i) {
171: if (i == 5)
172: return Level.DEBUG;
173: else if (i == 4)
174: return Level.INFO;
175: else if (i == 3)
176: return Level.WARN;
177: else if (i == 2)
178: return Level.ERROR;
179: else if (i == 1)
180: return Level.FATAL;
181: else
182: return null;
183: }
184:
185: public static void setLevel(Class cl, int i) {
186: Logger logr = Logger.getLogger(cl);
187: logr.setLevel(cvtIntToLevel(i));
188: }
189:
190: /**
191: * This method greats a typical debug level BSelect component
192: *
193: * @param vc the view context
194: * @param compName the component name
195: * @param selectedIndex the index to be selected
196: * @return the resulting BSelect component
197: */
198: public static BSelect getSelectComp(ViewContext vc,
199: String compName, ListModel lm, int selectedIndex) {
200: // DefaultView dlv = new DefaultView(vc.getTemplateNode().cloneNode(true));
201: // BSelect bsComp = new BSelect(lm, dlv, null);
202: BSelect bsComp = new BSelect(lm);
203: bsComp.setName(compName);
204: bsComp.setSelectedIndex(selectedIndex);
205: if (logger.isDebugEnabled())
206: logger.debug("created BSelect component: " + bsComp);
207: return bsComp;
208: }
209:
210: /**
211: * This method greats a typical debug level BSelect component
212: *
213: * @param vc the view context
214: * @param lf the listener factory to be notified when the action occurs
215: * @return the resulting BAction component
216: */
217: public static BToggleButton getToggleButton(ViewContext vc,
218: String compName, String value, boolean selected) {
219: // DefaultView dv = new DefaultView(vc.getTemplateNode().cloneNode(true));
220: // BToggleButton btbComp = new BToggleButton(null, name, value, selected, dv, null);
221: BToggleButton btbComp = new BToggleButton(null, compName,
222: value, selected);
223: if (logger.isDebugEnabled())
224: logger.debug("created BToggleButton component: " + btbComp);
225: return btbComp;
226: }
227:
228: /**
229: * This method creates a BAction component and adds the specified
230: * listener to it.
231: *
232: * @param vc the view context
233: * @param lf the listener factory to be notified when the action occurs
234: * @return the resulting BAction component
235: */
236: public static BAction getActionLink(ViewContext vc,
237: ListenerFactory lf) {
238: // DefaultView dv = new DefaultView(vc.getTemplateNode().cloneNode(true));
239: // BAction baComp = new BAction(dv);
240: BAction baComp = new BAction();
241: baComp.addEventListener(lf);
242: baComp.setDisableBackButton(true);
243: if (logger.isDebugEnabled())
244: logger.debug("created BAction component: " + baComp);
245: return baComp;
246: }
247:
248: /**
249: * This method greats a typical debug level BSelect component
250: *
251: * @param vc the view context
252: * @param lf the listener factory to be notified when the action occurs
253: * @return the resulting BAction component
254: */
255: public static BAction getUpdateButtonComp(ViewContext vc,
256: ListenerFactory lf) {
257: // DefaultView dlv = new DefaultView(vc.getTemplateNode().cloneNode(true));
258: // BAction baComp = new BAction(dlv);
259: BAction baComp = new BAction();
260: baComp.addEventListener(lf);
261: //baComp.setDisableBackButton(true);
262: if (logger.isDebugEnabled())
263: logger.debug("created BAction component: " + baComp);
264: return baComp;
265: }
266:
267: /**
268: * This method extracts a ValidationException error message
269: * (if it exists) from a ViewContext object. This method expects
270: * the ViewContext to contain an EventContext, which in turn must
271: * contain a FormMap (specified by formName), which in turn must
272: * contain a ValidationException (specified by keyName).
273: *
274: * @param vc the view context
275: * @param keyName the key name which was used to store the
276: * ValidationException
277: * @return the resulting error message (blank is it doesn't exist)
278: */
279: public static String getErrMsg(ViewContext vc, String formName,
280: String keyName) {
281: String errmsg = " ";
282: EventContext ec = vc.getEventContext();
283: if (ec != null) {
284: FormMap formMap = (FormMap) ec.getState(formName);
285: if (formMap != null) {
286: ValidationException ve = (ValidationException) formMap
287: .getState(keyName);
288: if (ve != null) {
289: List errlist = ve.getExceptionList();
290: Iterator it = errlist.iterator();
291: StringBuffer sb = new StringBuffer(100);
292: while (it.hasNext()) {
293: sb.append(" "
294: + ((ValidationException) it.next())
295: .getMessage());
296: }
297: errmsg = sb.toString();
298: }
299: }
300: }
301: if (logger.isDebugEnabled())
302: logger.debug("errmsg: " + errmsg);
303: return errmsg;
304: }
305:
306: /**
307: * This method creates a success message
308: *
309: * @param vc the view context
310: * @param keyName the key name which was used to store the
311: * ValidationException
312: * @return the resulting error message (blank is it doesn't exist)
313: */
314: public static String getSuccessMsg(ViewContext vc, String formName,
315: String keyName) {
316: String msg = " ";
317: EventContext ec = vc.getEventContext();
318: if (ec != null) {
319: FormMap formMap = (FormMap) ec.getState(formName);
320: if (formMap != null) {
321: if (formMap.getState(keyName) != null)
322: msg = "Update successful.";
323: }
324: }
325: return msg;
326: }
327: }
|