/*
* Copyright 2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Header:$
*/
package org.apache.beehive.netui.tags.html;
import org.apache.beehive.netui.util.internal.InternalStringBuilder;
import org.apache.beehive.netui.pageflow.internal.InternalConstants;
import org.apache.beehive.netui.pageflow.scoping.ScopedRequest;
import org.apache.beehive.netui.pageflow.scoping.ScopedServletUtils;
import org.apache.beehive.netui.tags.internal.ReturnActionViewRenderer;
import org.apache.beehive.netui.tags.javascript.CoreScriptFeature;
import org.apache.beehive.netui.tags.javascript.IScriptReporter;
import org.apache.beehive.netui.tags.javascript.ScriptRequestState;
import org.apache.beehive.netui.tags.rendering.AbstractRenderAppender;
import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
class PopupSupport
{
private static final String VIEW_RENDERER_CLASS_NAME = ReturnActionViewRenderer.class.getName();
private static final String ON_POPUP_DONE_FUNC = "Netui_OnPopupDone";
private static final String POPUP_FUNC = "Netui_Popup";
private String _name = "";
private HashMap _features;
private boolean _replace = false;
private String _onPopupDone;
private String _popupFunc;
public void setName(String name)
{
_name = name;
}
public void setToolbar(boolean toolbar)
{
putFeature("toolbar", toolbar);
}
public void setLocation(boolean location)
{
putFeature("location", location);
}
public void setDirectories(boolean directories)
{
putFeature("directories", directories);
}
public void setStatus(boolean status)
{
putFeature("status", status);
}
public void setMenubar(boolean menubar)
{
putFeature("menubar", menubar);
}
public void setScrollbars(boolean scrollbars)
{
putFeature("scrollbars", scrollbars);
}
public void setResizable(boolean resizable)
{
putFeature("resizable", resizable);
}
public void setWidth(int width)
{
putFeature("width", new Integer(width));
}
public void setHeight(int height)
{
putFeature("height", new Integer(height));
}
public void setLeft(int left)
{
putFeature("left", new Integer(left));
}
public void setTop(int top)
{
putFeature("top", new Integer(top));
}
public void setReplace(boolean replace)
{
_replace = replace;
}
public void setOnPopupDone(String onPopupDone)
{
_onPopupDone = onPopupDone;
}
public void setPopupFunc(String popupFunc)
{
_popupFunc = popupFunc;
}
public String getOnClick(ServletRequest req, String url)
{
// Build up the string that's passed to javascript open() to specify window features.
InternalStringBuilder features = new InternalStringBuilder();
if (_features != null) {
boolean firstOne = true;
for (Iterator i = _features.entrySet().iterator(); i.hasNext();)
{
Map.Entry entry = (Map.Entry) i.next();
if (!firstOne) {
features.append(',');
}
features.append(entry.getKey());
features.append('=');
features.append(entry.getValue());
firstOne = false;
}
}
String popupFunc = (_popupFunc != null ? _popupFunc : getScopedFunctionName(req, POPUP_FUNC));
Object[] args = new Object[]{popupFunc, url, _name, features.toString(), Boolean.valueOf(_replace)};
return ScriptRequestState.getString("popupSupportOnClick", args);
}
public void addParams(IUrlParams urlParams, ServletRequest request)
throws JspException
{
urlParams.addParameter(InternalConstants.RETURN_ACTION_VIEW_RENDERER_PARAM, VIEW_RENDERER_CLASS_NAME, null);
String onPopupDone = (_onPopupDone != null ? _onPopupDone : ON_POPUP_DONE_FUNC);
urlParams.addParameter(ReturnActionViewRenderer.getCallbackParamName(), onPopupDone, null);
ScopedRequest scopedRequest = ScopedServletUtils.unwrapRequest(request);
if (scopedRequest != null) {
urlParams.addParameter(ScopedServletUtils.SCOPE_ID_PARAM, scopedRequest.getScopeKey().toString(), null);
}
}
public void writeScript(ServletRequest req, ScriptRequestState srs, IScriptReporter scriptReporter, AbstractRenderAppender results)
{
// Write the generic function for popping a window.
String popupFunc = getScopedFunctionName(req, POPUP_FUNC);
srs.writeFeature(scriptReporter, results, "popupSupportPopupWindow", new Object[]{popupFunc});
// Write the callback that's triggered when the popup window is closing.
srs.writeFeature(scriptReporter, results, CoreScriptFeature.POPUP_DONE, true, false, new Object[]{ON_POPUP_DONE_FUNC});
}
private static String getScopedFunctionName(ServletRequest req, String funcName)
{
ScopedRequest scopedRequest = ScopedServletUtils.unwrapRequest(req);
return (scopedRequest != null ? funcName + '_' + scopedRequest.getScopeKey() : funcName);
}
private void putFeature(String featureName, boolean val)
{
putFeature(featureName, val ? new Integer(1) : new Integer(0));
}
private void putFeature(String featureName, Object val)
{
if (_features == null) {
_features = new HashMap();
}
_features.put(featureName, val);
}
}