001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.tags.html;
020:
021: import org.apache.beehive.netui.util.internal.InternalStringBuilder;
022:
023: import org.apache.beehive.netui.pageflow.internal.InternalConstants;
024: import org.apache.beehive.netui.pageflow.scoping.ScopedRequest;
025: import org.apache.beehive.netui.pageflow.scoping.ScopedServletUtils;
026: import org.apache.beehive.netui.tags.internal.ReturnActionViewRenderer;
027: import org.apache.beehive.netui.tags.javascript.CoreScriptFeature;
028: import org.apache.beehive.netui.tags.javascript.IScriptReporter;
029: import org.apache.beehive.netui.tags.javascript.ScriptRequestState;
030: import org.apache.beehive.netui.tags.rendering.AbstractRenderAppender;
031:
032: import javax.servlet.ServletRequest;
033: import javax.servlet.jsp.JspException;
034: import java.util.HashMap;
035: import java.util.Map;
036: import java.util.Iterator;
037:
038: class PopupSupport {
039: private static final String VIEW_RENDERER_CLASS_NAME = ReturnActionViewRenderer.class
040: .getName();
041: private static final String ON_POPUP_DONE_FUNC = "Netui_OnPopupDone";
042: private static final String POPUP_FUNC = "Netui_Popup";
043: private static final String POPUP_WINDOW = "Netui_Window";
044:
045: private String _name = "";
046: private HashMap _features;
047: private boolean _replace = false;
048: private String _onPopupDone;
049: private String _popupFunc;
050: private String _formName;
051:
052: public void setName(String name) {
053: _name = name;
054: }
055:
056: public void setToolbar(boolean toolbar) {
057: putFeature("toolbar", toolbar);
058: }
059:
060: public void setLocation(boolean location) {
061: putFeature("location", location);
062: }
063:
064: public void setDirectories(boolean directories) {
065: putFeature("directories", directories);
066: }
067:
068: public void setStatus(boolean status) {
069: putFeature("status", status);
070: }
071:
072: public void setMenubar(boolean menubar) {
073: putFeature("menubar", menubar);
074: }
075:
076: public void setScrollbars(boolean scrollbars) {
077: putFeature("scrollbars", scrollbars);
078: }
079:
080: public void setResizable(boolean resizable) {
081: putFeature("resizable", resizable);
082: }
083:
084: public void setWidth(int width) {
085: putFeature("width", new Integer(width));
086: }
087:
088: public void setHeight(int height) {
089: putFeature("height", new Integer(height));
090: }
091:
092: public void setLeft(int left) {
093: putFeature("left", new Integer(left));
094: }
095:
096: public void setTop(int top) {
097: putFeature("top", new Integer(top));
098: }
099:
100: public void setReplace(boolean replace) {
101: _replace = replace;
102: }
103:
104: public void setOnPopupDone(String onPopupDone) {
105: _onPopupDone = onPopupDone;
106: }
107:
108: public void setPopupFunc(String popupFunc) {
109: _popupFunc = popupFunc;
110: }
111:
112: /**
113: * Sets whether the JavaScript function that opens the popup window should add data
114: * from the form fields to the request. The form name is used in the JavaScript
115: * function of onClick to identify the form object correctly. The JavaScript
116: * will only be set up to pass the names and values for the form fields on the
117: * request if the form name is set.
118: *
119: * @param formName the name attribute of the form to pass field data from.
120: * This must be the real/generated form name and not the
121: * netui:form tagId attribute.
122: */
123: public void setFormName(String formName) {
124: _formName = formName;
125: }
126:
127: public String getOnClick(ServletRequest req, String url) {
128: // Build up the string that's passed to javascript open() to specify window features.
129: InternalStringBuilder features = new InternalStringBuilder();
130:
131: if (_features != null) {
132: boolean firstOne = true;
133: for (Iterator i = _features.entrySet().iterator(); i
134: .hasNext();) {
135: Map.Entry entry = (Map.Entry) i.next();
136: if (!firstOne) {
137: features.append(',');
138: }
139: features.append(entry.getKey());
140: features.append('=');
141: features.append(entry.getValue());
142: firstOne = false;
143: }
144: }
145:
146: String onClick = null;
147: String popupWindow = getScopedFunctionName(req, POPUP_WINDOW);
148:
149: String popupFunc = (_popupFunc != null ? _popupFunc
150: : POPUP_FUNC);
151: if (_formName != null && _formName.length() > 0) {
152: Object[] args = new Object[] { popupFunc, url, _name,
153: features.toString(), Boolean.valueOf(_replace),
154: popupWindow, _formName };
155: onClick = ScriptRequestState.getString(
156: "popupSupportUpdateFormOnClick", args);
157: } else {
158: Object[] args = new Object[] { popupFunc, url, _name,
159: features.toString(), Boolean.valueOf(_replace),
160: popupWindow };
161: onClick = ScriptRequestState.getString(
162: "popupSupportOnClick", args);
163: }
164:
165: return onClick;
166: }
167:
168: public void addParams(IUrlParams urlParams, ServletRequest request)
169: throws JspException {
170: urlParams.addParameter(
171: InternalConstants.RETURN_ACTION_VIEW_RENDERER_PARAM,
172: VIEW_RENDERER_CLASS_NAME, null);
173: String onPopupDone = (_onPopupDone != null ? _onPopupDone
174: : ON_POPUP_DONE_FUNC);
175: urlParams.addParameter(ReturnActionViewRenderer
176: .getCallbackParamName(), onPopupDone, null);
177:
178: ScopedRequest scopedRequest = ScopedServletUtils
179: .unwrapRequest(request);
180: if (scopedRequest != null) {
181: urlParams.addParameter(ScopedServletUtils.SCOPE_ID_PARAM,
182: scopedRequest.getScopeKey().toString(), null);
183: }
184: }
185:
186: public void writeScript(ServletRequest req, ScriptRequestState srs,
187: IScriptReporter scriptReporter,
188: AbstractRenderAppender results) {
189: // Write the generic function for popping a window.
190: srs.writeFeature(scriptReporter, results,
191: CoreScriptFeature.POPUP_OPEN, true, false,
192: new Object[] { POPUP_FUNC });
193:
194: // Write the callback that's triggered when the popup window is closing.
195: srs.writeFeature(scriptReporter, results,
196: CoreScriptFeature.POPUP_DONE, true, false,
197: new Object[] { ON_POPUP_DONE_FUNC });
198:
199: // Write the initialization of the popup window variable.
200: String popupWindow = getScopedFunctionName(req, POPUP_WINDOW);
201: srs.writeFeature(scriptReporter, results,
202: "popupSupportWindowVariable",
203: new Object[] { popupWindow });
204: }
205:
206: private static String getScopedFunctionName(ServletRequest req,
207: String funcName) {
208: ScopedRequest scopedRequest = ScopedServletUtils
209: .unwrapRequest(req);
210: return (scopedRequest != null ? funcName + '_'
211: + scopedRequest.getScopeKey() : funcName);
212: }
213:
214: private void putFeature(String featureName, boolean val) {
215: putFeature(featureName, val ? new Integer(1) : new Integer(0));
216: }
217:
218: private void putFeature(String featureName, Object val) {
219: if (_features == null) {
220: _features = new HashMap();
221: }
222: _features.put(featureName, val);
223: }
224: }
|