1 15 package org.apache.tapestry.link; 16 17 import java.util.ArrayList ; 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import org.apache.tapestry.AbstractComponent; 24 import org.apache.tapestry.IMarkupWriter; 25 import org.apache.tapestry.IRequestCycle; 26 import org.apache.tapestry.PageRenderSupport; 27 import org.apache.tapestry.TapestryUtils; 28 import org.apache.tapestry.components.ILinkComponent; 29 import org.apache.tapestry.components.LinkEventType; 30 import org.apache.tapestry.engine.IEngineService; 31 import org.apache.tapestry.engine.ILink; 32 33 42 43 public abstract class AbstractLinkComponent extends AbstractComponent implements ILinkComponent 44 { 45 private Map _eventHandlers; 46 47 public abstract boolean isDisabled(); 48 49 53 54 public void addEventHandler(LinkEventType eventType, String functionName) 55 { 56 Object currentValue; 57 58 if (_eventHandlers == null) 59 _eventHandlers = new HashMap (); 60 61 currentValue = _eventHandlers.get(eventType); 62 63 65 if (currentValue == null) 66 { 67 _eventHandlers.put(eventType, functionName); 68 return; 69 } 70 71 73 if (currentValue instanceof String ) 74 { 75 List list = new ArrayList (); 76 list.add(currentValue); 77 list.add(functionName); 78 79 _eventHandlers.put(eventType, list); 80 return; 81 } 82 83 85 List list = (List ) currentValue; 86 list.add(functionName); 87 } 88 89 92 93 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) 94 { 95 getRenderer().renderLink(writer, cycle, this); 96 } 97 98 protected void cleanupAfterRender(IRequestCycle cycle) 99 { 100 _eventHandlers = null; 101 102 super.cleanupAfterRender(cycle); 103 } 104 105 protected void writeEventHandlers(IMarkupWriter writer, IRequestCycle cycle) 106 { 107 String name = null; 108 109 if (_eventHandlers == null) 110 return; 111 112 PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this); 113 114 Iterator i = _eventHandlers.entrySet().iterator(); 115 116 while (i.hasNext()) 117 { 118 Map.Entry entry = (Map.Entry ) i.next(); 119 LinkEventType type = (LinkEventType) entry.getKey(); 120 121 name = writeEventHandler( 122 writer, 123 pageRenderSupport, 124 name, 125 type.getAttributeName(), 126 entry.getValue()); 127 } 128 129 } 130 131 protected String writeEventHandler(IMarkupWriter writer, PageRenderSupport pageRenderSupport, 132 String name, String attributeName, Object value) 133 { 134 String wrapperFunctionName; 135 136 if (value instanceof String ) 137 { 138 wrapperFunctionName = (String ) value; 139 } 140 else 141 { 142 String finalName = name == null ? pageRenderSupport.getUniqueString("Link") : name; 143 144 wrapperFunctionName = attributeName + "_" + finalName; 145 146 StringBuffer buffer = new StringBuffer (); 147 148 buffer.append("function "); 149 buffer.append(wrapperFunctionName); 150 buffer.append(" ()\n{\n"); 151 152 Iterator i = ((List ) value).iterator(); 153 while (i.hasNext()) 154 { 155 String functionName = (String ) i.next(); 156 buffer.append(" "); 157 buffer.append(functionName); 158 buffer.append("();\n"); 159 } 160 161 buffer.append("}\n\n"); 162 163 pageRenderSupport.addBodyScript(buffer.toString()); 164 } 165 166 writer.attribute(attributeName, "javascript:" + wrapperFunctionName + "();"); 167 168 return name; 169 } 170 171 172 173 public abstract ILinkRenderer getRenderer(); 174 175 public abstract void setRenderer(ILinkRenderer renderer); 176 177 public void renderAdditionalAttributes(IMarkupWriter writer, IRequestCycle cycle) 178 { 179 writeEventHandlers(writer, cycle); 180 181 183 renderInformalParameters(writer, cycle); 184 } 185 186 194 195 protected ILink getLink(IRequestCycle cycle, String serviceName, Object parameter) 196 { 197 IEngineService service = cycle.getEngine().getService(serviceName); 198 199 return service.getLink(cycle, parameter); 200 } 201 202 public abstract String getAnchor(); 203 204 public ILink getLink(IRequestCycle cycle) 205 { 206 return null; 207 } 208 209 215 protected void finishLoad() 216 { 217 setRenderer(DefaultLinkRenderer.SHARED_INSTANCE); 218 } 219 220 } | Popular Tags |