KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > html > PopupSupport


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License";
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.html;
19
20 import org.apache.beehive.netui.util.internal.InternalStringBuilder;
21
22 import org.apache.beehive.netui.pageflow.internal.InternalConstants;
23 import org.apache.beehive.netui.pageflow.scoping.ScopedRequest;
24 import org.apache.beehive.netui.pageflow.scoping.ScopedServletUtils;
25 import org.apache.beehive.netui.tags.internal.ReturnActionViewRenderer;
26 import org.apache.beehive.netui.tags.javascript.CoreScriptFeature;
27 import org.apache.beehive.netui.tags.javascript.IScriptReporter;
28 import org.apache.beehive.netui.tags.javascript.ScriptRequestState;
29 import org.apache.beehive.netui.tags.rendering.AbstractRenderAppender;
30
31 import javax.servlet.ServletRequest JavaDoc;
32 import javax.servlet.jsp.JspException JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Iterator JavaDoc;
36
37 class PopupSupport
38 {
39     private static final String JavaDoc VIEW_RENDERER_CLASS_NAME = ReturnActionViewRenderer.class.getName();
40     private static final String JavaDoc ON_POPUP_DONE_FUNC = "Netui_OnPopupDone";
41     private static final String JavaDoc POPUP_FUNC = "Netui_Popup";
42
43     private String JavaDoc _name = "";
44     private HashMap JavaDoc _features;
45     private boolean _replace = false;
46     private String JavaDoc _onPopupDone;
47     private String JavaDoc _popupFunc;
48
49     public void setName(String JavaDoc name)
50     {
51         _name = name;
52     }
53
54     public void setToolbar(boolean toolbar)
55     {
56         putFeature("toolbar", toolbar);
57     }
58
59     public void setLocation(boolean location)
60     {
61         putFeature("location", location);
62     }
63
64     public void setDirectories(boolean directories)
65     {
66         putFeature("directories", directories);
67     }
68
69     public void setStatus(boolean status)
70     {
71         putFeature("status", status);
72     }
73
74     public void setMenubar(boolean menubar)
75     {
76         putFeature("menubar", menubar);
77     }
78
79     public void setScrollbars(boolean scrollbars)
80     {
81         putFeature("scrollbars", scrollbars);
82     }
83
84     public void setResizable(boolean resizable)
85     {
86         putFeature("resizable", resizable);
87     }
88
89     public void setWidth(int width)
90     {
91         putFeature("width", new Integer JavaDoc(width));
92     }
93
94     public void setHeight(int height)
95     {
96         putFeature("height", new Integer JavaDoc(height));
97     }
98
99     public void setLeft(int left)
100     {
101         putFeature("left", new Integer JavaDoc(left));
102     }
103
104     public void setTop(int top)
105     {
106         putFeature("top", new Integer JavaDoc(top));
107     }
108
109     public void setReplace(boolean replace)
110     {
111         _replace = replace;
112     }
113
114     public void setOnPopupDone(String JavaDoc onPopupDone)
115     {
116         _onPopupDone = onPopupDone;
117     }
118
119     public void setPopupFunc(String JavaDoc popupFunc)
120     {
121         _popupFunc = popupFunc;
122     }
123
124     public String JavaDoc getOnClick(ServletRequest JavaDoc req, String JavaDoc url)
125     {
126         // Build up the string that's passed to javascript open() to specify window features.
127
InternalStringBuilder features = new InternalStringBuilder();
128         
129         if (_features != null) {
130             boolean firstOne = true;
131             for (Iterator JavaDoc i = _features.entrySet().iterator(); i.hasNext();)
132             {
133                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
134                 if (!firstOne) {
135                     features.append(',');
136                 }
137                 features.append(entry.getKey());
138                 features.append('=');
139                 features.append(entry.getValue());
140                 firstOne = false;
141             }
142         }
143         
144         String JavaDoc popupFunc = (_popupFunc != null ? _popupFunc : getScopedFunctionName(req, POPUP_FUNC));
145         Object JavaDoc[] args = new Object JavaDoc[]{popupFunc, url, _name, features.toString(), Boolean.valueOf(_replace)};
146         return ScriptRequestState.getString("popupSupportOnClick", args);
147     }
148
149     public void addParams(IUrlParams urlParams, ServletRequest JavaDoc request)
150             throws JspException JavaDoc
151     {
152         urlParams.addParameter(InternalConstants.RETURN_ACTION_VIEW_RENDERER_PARAM, VIEW_RENDERER_CLASS_NAME, null);
153         String JavaDoc onPopupDone = (_onPopupDone != null ? _onPopupDone : ON_POPUP_DONE_FUNC);
154         urlParams.addParameter(ReturnActionViewRenderer.getCallbackParamName(), onPopupDone, null);
155         
156         ScopedRequest scopedRequest = ScopedServletUtils.unwrapRequest(request);
157         if (scopedRequest != null) {
158             urlParams.addParameter(ScopedServletUtils.SCOPE_ID_PARAM, scopedRequest.getScopeKey().toString(), null);
159         }
160     }
161
162     public void writeScript(ServletRequest JavaDoc req, ScriptRequestState srs, IScriptReporter scriptReporter, AbstractRenderAppender results)
163     {
164         // Write the generic function for popping a window.
165
String JavaDoc popupFunc = getScopedFunctionName(req, POPUP_FUNC);
166         srs.writeFeature(scriptReporter, results, "popupSupportPopupWindow", new Object JavaDoc[]{popupFunc});
167
168         // Write the callback that's triggered when the popup window is closing.
169
srs.writeFeature(scriptReporter, results, CoreScriptFeature.POPUP_DONE, true, false, new Object JavaDoc[]{ON_POPUP_DONE_FUNC});
170     }
171     
172     private static String JavaDoc getScopedFunctionName(ServletRequest JavaDoc req, String JavaDoc funcName)
173     {
174         ScopedRequest scopedRequest = ScopedServletUtils.unwrapRequest(req);
175         return (scopedRequest != null ? funcName + '_' + scopedRequest.getScopeKey() : funcName);
176     }
177
178     private void putFeature(String JavaDoc featureName, boolean val)
179     {
180         putFeature(featureName, val ? new Integer JavaDoc(1) : new Integer JavaDoc(0));
181     }
182
183     private void putFeature(String JavaDoc featureName, Object JavaDoc val)
184     {
185         if (_features == null) {
186             _features = new HashMap JavaDoc();
187         }
188         _features.put(featureName, val);
189     }
190 }
191
192
Popular Tags