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.service.BodyBuilder; 018 import org.apache.tapestry.IRequestCycle; 019 import org.apache.tapestry.PageRenderSupport; 020 import org.apache.tapestry.TapestryUtils; 021 import org.apache.tapestry.components.ILinkComponent; 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.components.ILinkComponent, 058 * org.apache.tapestry.IRequestCycle) 059 */ 060 protected String constructURL(ILinkComponent component, IRequestCycle cycle) 061 { 062 if (cycle.isRewinding()) 063 { 064 return null; 065 } 066 067 String anchor = component.getAnchor(); 068 ILink link = component.getLink(cycle); 069 070 String url = link.getURL(anchor, true); 071 072 PageRenderSupport support = TapestryUtils.getPageRenderSupport(cycle, component); 073 074 String functionName = support.getUniqueString("popup_window"); 075 076 BodyBuilder builder = new BodyBuilder(); 077 078 builder.addln("{0}=function()", functionName); 079 builder.begin(); 080 builder.add( 081 "var newWindow = window.open({0}, {1}, {2});", 082 TapestryUtils.enquote(url), 083 TapestryUtils.enquote(getWindowName()), 084 TapestryUtils.enquote(getFeatures())); 085 builder.add("newWindow.focus();"); 086 builder.end(); 087 builder.addln(";"); 088 089 support.addBodyScript(component, builder.toString()); 090 091 return "javascript:" + functionName + "();"; 092 } 093 094 public String getWindowName() 095 { 096 return _windowName; 097 } 098 099 public void setWindowName(String windowName) 100 { 101 _windowName = windowName; 102 } 103 104 public String getFeatures() 105 { 106 return _features; 107 } 108 109 public void setFeatures(String features) 110 { 111 _features = features; 112 } 113 }