1 15 package org.apache.tapestry.contrib.inspector; 16 17 import java.util.Iterator ; 18 import java.util.Map ; 19 20 import org.apache.tapestry.BaseComponent; 21 import org.apache.tapestry.IComponent; 22 import org.apache.tapestry.IDirect; 23 import org.apache.tapestry.IMarkupWriter; 24 import org.apache.tapestry.IRender; 25 import org.apache.tapestry.IRequestCycle; 26 import org.apache.tapestry.Tapestry; 27 import org.apache.tapestry.engine.DirectServiceParameter; 28 import org.apache.tapestry.engine.IEngineService; 29 import org.apache.tapestry.engine.ILink; 30 import org.apache.tapestry.link.DirectLink; 31 import org.apache.tapestry.parse.CloseToken; 32 import org.apache.tapestry.parse.ComponentTemplate; 33 import org.apache.tapestry.parse.LocalizationToken; 34 import org.apache.tapestry.parse.OpenToken; 35 import org.apache.tapestry.parse.TemplateToken; 36 import org.apache.tapestry.parse.TextToken; 37 import org.apache.tapestry.parse.TokenType; 38 import org.apache.tapestry.services.TemplateSource; 39 40 46 47 public abstract class ShowTemplate extends BaseComponent implements IDirect 48 { 49 50 public abstract TemplateSource getTemplateSource(); 51 52 public boolean getHasTemplate() 53 { 54 Inspector inspector; 55 56 inspector = (Inspector) getPage(); 57 58 61 return inspector.getInspectedComponent() instanceof BaseComponent; 62 } 63 64 public IRender getTemplateDelegate() 65 { 66 return new IRender() 67 { 68 public void render(IMarkupWriter writer, IRequestCycle cycle) 69 { 70 writeTemplate(writer, cycle); 71 } 72 }; 73 } 74 75 81 82 private void writeTemplate(IMarkupWriter writer, IRequestCycle cycle) 83 { 84 IComponent inspectedComponent = getInspectedComponent(); 85 ComponentTemplate template = null; 86 87 try 88 { 89 template = getTemplateSource().getTemplate(cycle, inspectedComponent); 90 } 91 catch (Exception ex) 92 { 93 return; 94 } 95 96 writer.begin("pre"); 97 98 int count = template.getTokenCount(); 99 100 for (int i = 0; i < count; i++) 101 { 102 TemplateToken token = template.getToken(i); 103 TokenType type = token.getType(); 104 105 if (type == TokenType.TEXT) 106 { 107 write(writer, (TextToken) token); 108 continue; 109 } 110 111 if (type == TokenType.CLOSE) 112 { 113 write(writer, (CloseToken) token); 114 115 continue; 116 } 117 118 if (token.getType() == TokenType.LOCALIZATION) 119 { 120 121 write(writer, (LocalizationToken) token); 122 continue; 123 } 124 125 if (token.getType() == TokenType.OPEN) 126 { 127 boolean nextIsClose = (i + 1 < count) 128 && (template.getToken(i + 1).getType() == TokenType.CLOSE); 129 130 write(writer, nextIsClose, (OpenToken) token); 131 132 if (nextIsClose) 133 i++; 134 135 continue; 136 } 137 138 } 140 141 writer.end(); } 143 144 145 146 private IComponent getInspectedComponent() 147 { 148 Inspector page = (Inspector) getPage(); 149 150 return page.getInspectedComponent(); 151 } 152 153 154 155 private void write(IMarkupWriter writer, TextToken token) 156 { 157 161 writer.print(token.getTemplateDataAsString()); 162 } 163 164 165 166 private void write(IMarkupWriter writer, CloseToken token) 167 { 168 writer.begin("span"); 169 writer.attribute("class", "jwc-tag"); 170 171 writer.print("</"); 172 writer.print(token.getTag()); 173 writer.print(">"); 174 175 writer.end(); } 177 178 179 180 private void write(IMarkupWriter writer, LocalizationToken token) 181 { 182 IComponent component = getInspectedComponent(); 183 184 writer.begin("span"); 185 writer.attribute("class", "jwc-tag"); 186 187 writer.print("<span key=\""); 188 writer.print(token.getKey()); 189 writer.print('"'); 190 191 Map attributes = token.getAttributes(); 192 if (attributes != null && !attributes.isEmpty()) 193 { 194 Iterator it = attributes.entrySet().iterator(); 195 while (it.hasNext()) 196 { 197 Map.Entry entry = (Map.Entry ) it.next(); 198 String attributeName = (String ) entry.getKey(); 199 String attributeValue = (String ) entry.getValue(); 200 201 writer.print(' '); 202 writer.print(attributeName); 203 writer.print("=\""); 204 writer.print(attributeValue); 205 writer.print('"'); 206 207 } 208 } 209 210 writer.print('>'); 211 writer.begin("span"); 212 writer.attribute("class", "localized-string"); 213 214 writer.print(component.getMessages().getMessage(token.getKey())); 215 writer.end(); 217 writer.print("</span>"); 218 219 writer.end(); } 221 222 223 224 private void write(IMarkupWriter writer, boolean nextIsClose, OpenToken token) 225 { 226 IComponent component = getInspectedComponent(); 227 IEngineService service = getPage().getEngine().getService(Tapestry.DIRECT_SERVICE); 228 229 232 String id = token.getId(); 233 IComponent embedded = component.getComponent(id); 234 Object [] serviceParameters = new Object [] 235 { embedded.getIdPath() }; 236 237 240 DirectServiceParameter dsp = new DirectServiceParameter(this, serviceParameters); 241 ILink link = service.getLink(getPage().getRequestCycle(), dsp); 242 243 writer.begin("span"); 244 writer.attribute("class", "jwc-tag"); 245 246 writer.print("<"); 247 writer.print(token.getTag()); 248 249 writer.print(" jwcid=\""); 250 251 writer.begin("span"); 252 writer.attribute("class", "jwc-id"); 253 254 writer.begin("a"); 255 writer.attribute("href", link.getURL()); 256 writer.print(id); 257 258 writer.end(); writer.end(); writer.print('"'); 261 262 Map attributes = token.getAttributesMap(); 263 264 if (attributes != null) 265 { 266 Iterator ii = attributes.entrySet().iterator(); 267 268 while (ii.hasNext()) 269 { 270 Map.Entry e = (Map.Entry ) ii.next(); 271 272 String value = (String ) e.getValue(); 273 274 writer.print(' '); 275 writer.print(e.getKey().toString()); 276 writer.print("=\""); 277 writer.print(value); 278 writer.print('"'); 279 } 280 } 281 282 284 if (nextIsClose) 285 writer.print('/'); 286 287 writer.print('>'); 288 writer.end(); } 290 291 294 295 public void trigger(IRequestCycle cycle) 296 { 297 Inspector inspector = (Inspector) getPage(); 298 299 String componentId = (String ) cycle.getListenerParameters()[0]; 300 inspector.selectComponent(componentId); 301 302 IComponent newComponent = inspector.getInspectedComponent(); 303 304 307 if (!(newComponent instanceof BaseComponent)) 308 inspector.setView(View.SPECIFICATION); 309 } 310 311 316 317 public boolean isStateful() 318 { 319 return true; 320 } 321 } | Popular Tags |