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.components.ILinkComponent; 023 import org.apache.tapestry.engine.ILink; 024 import org.apache.tapestry.link.DefaultLinkRenderer; 025 026 /** 027 * This renderer emits javascript to launch the link in a window. 028 * 029 * @author David Solis 030 * @since 3.0.1 031 */ 032 public class PopupLinkRenderer extends DefaultLinkRenderer 033 { 034 035 private String _windowName; 036 037 private String _features; 038 039 public PopupLinkRenderer() 040 { 041 } 042 043 /** 044 * Initializes the name and features for javascript window.open function. 045 * 046 * @param windowName 047 * the window name 048 * @param features 049 * the window features 050 */ 051 public PopupLinkRenderer(String windowName, String features) 052 { 053 _windowName = windowName; 054 _features = features; 055 } 056 057 /** 058 * @see DefaultLinkRenderer#constructURL(org.apache.tapestry.engine.ILink, String, 059 * org.apache.tapestry.IRequestCycle) 060 */ 061 protected String constructURL(ILinkComponent component, IRequestCycle cycle) 062 { 063 String anchor = component.getAnchor(); 064 ILink link = component.getLink(cycle); 065 066 String url = link.getURL(anchor, true); 067 068 PageRenderSupport support = TapestryUtils.getPageRenderSupport(cycle, component); 069 070 String functionName = support.getUniqueString("popup_window"); 071 072 BodyBuilder builder = new BodyBuilder(); 073 074 builder.addln("function {0}()", functionName); 075 builder.begin(); 076 builder.addln( 077 "var newWindow = window.open({0}, {1}, {2});", 078 TapestryUtils.enquote(url), 079 TapestryUtils.enquote(getWindowName()), 080 TapestryUtils.enquote(getFeatures())); 081 builder.addln("newWindow.focus();"); 082 builder.end(); 083 084 support.addBodyScript(builder.toString()); 085 086 return "javascript:" + functionName + "();"; 087 } 088 089 public String getWindowName() 090 { 091 return _windowName; 092 } 093 094 public void setWindowName(String windowName) 095 { 096 _windowName = windowName; 097 } 098 099 public String getFeatures() 100 { 101 return _features; 102 } 103 104 public void setFeatures(String features) 105 { 106 _features = features; 107 } 108 }