KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > component > RendererParameters


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.component;
14
15 import java.util.Collections JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import javax.servlet.ServletContext JavaDoc;
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.http.HttpSession JavaDoc;
22
23 import com.tonbeller.wcf.controller.RequestContext;
24
25 /**
26  * keeps track of stylesheet parameters for the RendererTag.
27  * For the scopes 'request' (default),
28  * 'session' and 'application' keeps a Map of name/value pairs.
29  * <p>
30  * The special name "mode" may have values null, "excel", "print" and may
31  * be queried by isExcelMode() and isPrintMode().
32  */

33 public class RendererParameters {
34   private static final String JavaDoc WEBKEY = RendererParameters.class.getName();
35
36   public static final String JavaDoc MODE = "mode";
37   public static final String JavaDoc EXCEL = "excel";
38   public static final String JavaDoc PRINT = "print";
39
40   /** navigation buttons are not rendered in excel/print modes */
41   public static boolean isRenderActions(RequestContext context) {
42     return isRenderActions(context.getRequest());
43   }
44
45   public static boolean isExcelMode(RequestContext context) {
46     return isExcelMode(context.getRequest());
47   }
48
49   public static boolean isPrintMode(RequestContext context) {
50     return isPrintMode(context.getRequest());
51   }
52
53   /** navigation buttons are not rendered in excel/print modes */
54   public static boolean isRenderActions(HttpServletRequest JavaDoc request) {
55     return getParameterMap(request).get(MODE) == null;
56   }
57
58   public static boolean isExcelMode(HttpServletRequest JavaDoc request) {
59     return EXCEL.equals(getParameterMap(request).get(MODE));
60   }
61
62   public static boolean isPrintMode(HttpServletRequest JavaDoc request) {
63     return PRINT.equals(getParameterMap(request).get(MODE));
64   }
65   
66   public static void setParameter(HttpServletRequest JavaDoc req, String JavaDoc name, Object JavaDoc value, String JavaDoc scope) {
67     Map JavaDoc map = getMap(req, scope, true);
68     map.put(name, value);
69   }
70
71   public static void removeParameter(HttpServletRequest JavaDoc req, String JavaDoc name, Object JavaDoc value, String JavaDoc scope) {
72     Map JavaDoc map = getMap(req, scope, false);
73     if (map != null)
74       map.remove(name);
75   }
76
77   private static Map JavaDoc getMap(HttpServletRequest JavaDoc req, String JavaDoc scope, boolean create) {
78     if ("request".equals(scope))
79       return getRequestMap(req, create);
80     if ("session".equals(scope))
81       return getSessionMap(req, create);
82     if ("application".equals(scope))
83       return getContextMap(req, create);
84     throw new IllegalArgumentException JavaDoc("scope must be one of 'request', 'session', 'application'");
85
86   }
87
88   private static Map JavaDoc getRequestMap(HttpServletRequest JavaDoc req, boolean create) {
89     Map JavaDoc map = (Map JavaDoc) req.getAttribute(WEBKEY);
90     if (map != null || !create)
91       return map;
92     map = new HashMap JavaDoc();
93     req.setAttribute(WEBKEY, map);
94     return map;
95   }
96
97   private static Map JavaDoc getSessionMap(HttpServletRequest JavaDoc req, boolean create) {
98     HttpSession JavaDoc session = req.getSession(true);
99     Map JavaDoc map = (Map JavaDoc) session.getAttribute(WEBKEY);
100     if (map != null || !create)
101       return map;
102     map = new HashMap JavaDoc();
103     session.setAttribute(WEBKEY, map);
104     return map;
105   }
106
107   private static Map JavaDoc getContextMap(HttpServletRequest JavaDoc req, boolean create) {
108     ServletContext JavaDoc context = req.getSession(true).getServletContext();
109     Map JavaDoc map = (Map JavaDoc) context.getAttribute(WEBKEY);
110     if (map != null || !create)
111       return map;
112     map = new HashMap JavaDoc();
113     context.setAttribute(WEBKEY, map);
114     return map;
115   }
116
117   /**
118    * returns a read-only map of all current parameters
119    */

120   public static Map JavaDoc getParameterMap(HttpServletRequest JavaDoc request) {
121     HttpSession JavaDoc session = request.getSession(true);
122     ServletContext JavaDoc context = session.getServletContext();
123
124     Map JavaDoc map = new HashMap JavaDoc();
125     Map JavaDoc m = (Map JavaDoc) context.getAttribute(WEBKEY);
126     if (m != null)
127       map.putAll(m);
128     m = (Map JavaDoc) session.getAttribute(WEBKEY);
129     if (m != null)
130       map.putAll(m);
131     m = (Map JavaDoc) request.getAttribute(WEBKEY);
132     if (m != null)
133       map.putAll(m);
134     return Collections.unmodifiableMap(map);
135   }
136
137 }
Popular Tags