001 // Copyright 2004, 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.contrib.link; 016 017 import org.apache.hivemind.HiveMind; 018 import org.apache.hivemind.service.BodyBuilder; 019 import org.apache.tapestry.IRequestCycle; 020 import org.apache.tapestry.PageRenderSupport; 021 import org.apache.tapestry.TapestryUtils; 022 import org.apache.tapestry.engine.ILink; 023 import org.apache.tapestry.link.DefaultLinkRenderer; 024 025 /** 026 * This renderer emits javascript to launch the link in a window. 027 * 028 * @author David Solis 029 * @since 3.0.1 030 */ 031 public class PopupLinkRenderer extends DefaultLinkRenderer 032 { 033 034 private String _windowName; 035 036 private String _features; 037 038 public PopupLinkRenderer() 039 { 040 } 041 042 /** 043 * Initializes the name and features for javascript window.open function. 044 * 045 * @param windowName 046 * the window name 047 * @param features 048 * the window features 049 */ 050 public PopupLinkRenderer(String windowName, String features) 051 { 052 _windowName = windowName; 053 _features = features; 054 } 055 056 /** 057 * @see DefaultLinkRenderer#constructURL(org.apache.tapestry.engine.ILink, String, 058 * org.apache.tapestry.IRequestCycle) 059 */ 060 protected String constructURL(ILink link, String anchor, IRequestCycle cycle) 061 { 062 String url = link.getURL(anchor, true); 063 064 PageRenderSupport support = (PageRenderSupport) cycle 065 .getAttribute(TapestryUtils.PAGE_RENDER_SUPPORT_ATTRIBUTE); 066 067 // TODO: Error if no Body! 068 069 String functionName = support.getUniqueString("popup_window"); 070 071 BodyBuilder builder = new BodyBuilder(); 072 073 builder.addln("function {0}()", functionName); 074 builder.begin(); 075 builder.addln( 076 "var newWindow = window.open({0}, {1}, {2});", 077 normalizeString(url), 078 normalizeString(getWindowName()), 079 normalizeString(getFeatures())); 080 builder.addln("newWindow.focus();"); 081 builder.end(); 082 083 support.addBodyScript(builder.toString()); 084 085 return "javascript:" + functionName + "();"; 086 } 087 088 private static final String QUOTE = "\""; 089 090 static String normalizeString(String str) 091 { 092 if (HiveMind.isBlank(str)) 093 return QUOTE + QUOTE; 094 095 int length = str.length(); 096 097 // Doing this char by char is inefficient but Good Enough 098 099 StringBuffer buffer = new StringBuffer(length + 2); 100 101 buffer.append(QUOTE); 102 103 for (int i = 0; i < length; i++) 104 { 105 char ch = str.charAt(i); 106 107 // Embedded quotes and backslashes are escaped with a backslash. 108 109 if (ch == '"' || ch == '\\') 110 buffer.append('\\'); 111 112 buffer.append(ch); 113 } 114 115 buffer.append(QUOTE); 116 117 return buffer.toString(); 118 } 119 120 public String getWindowName() 121 { 122 return _windowName; 123 } 124 125 public void setWindowName(String windowName) 126 { 127 _windowName = windowName; 128 } 129 130 public String getFeatures() 131 { 132 return _features; 133 } 134 135 public void setFeatures(String features) 136 { 137 _features = features; 138 } 139 }