1 15 package org.apache.tapestry.contrib.inspector; 16 17 import java.util.ArrayList ; 18 import java.util.Collection ; 19 import java.util.Collections ; 20 import java.util.Comparator ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import org.apache.tapestry.BaseComponent; 25 import org.apache.tapestry.IAsset; 26 import org.apache.tapestry.IBinding; 27 import org.apache.tapestry.IComponent; 28 import org.apache.tapestry.event.PageEvent; 29 import org.apache.tapestry.event.PageRenderListener; 30 import org.apache.tapestry.spec.IBeanSpecification; 31 import org.apache.tapestry.spec.IComponentSpecification; 32 import org.apache.tapestry.spec.IContainedComponent; 33 import org.apache.tapestry.spec.IParameterSpecification; 34 35 42 43 public abstract class ShowSpecification extends BaseComponent implements PageRenderListener 44 { 45 private IComponent _inspectedComponent; 46 private IComponentSpecification _inspectedSpecification; 47 private String _parameterName; 48 private String _assetName; 49 private List _sortedComponents; 50 private IComponent _component; 51 private List _assetNames; 52 private List _formalParameterNames; 53 private List _informalParameterNames; 54 private List _sortedPropertyNames; 55 private String _propertyName; 56 private List _beanNames; 57 private String _beanName; 58 private IBeanSpecification _beanSpecification; 59 60 private static class ComponentComparitor implements Comparator 61 { 62 public int compare(Object left, Object right) 63 { 64 IComponent leftComponent; 65 String leftId; 66 IComponent rightComponent; 67 String rightId; 68 69 if (left == right) 70 return 0; 71 72 leftComponent = (IComponent) left; 73 rightComponent = (IComponent) right; 74 75 leftId = leftComponent.getId(); 76 rightId = rightComponent.getId(); 77 78 return leftId.compareTo(rightId); 79 } 80 } 81 82 90 91 public void pageEndRender(PageEvent event) 92 { 93 _inspectedComponent = null; 94 _inspectedSpecification = null; 95 _parameterName = null; 96 _assetName = null; 97 _sortedComponents = null; 98 _component = null; 99 _assetNames = null; 100 _formalParameterNames = null; 101 _informalParameterNames = null; 102 _sortedPropertyNames = null; 103 _propertyName = null; 104 _beanNames = null; 105 _beanName = null; 106 _beanSpecification = null; 107 } 108 109 114 115 public void pageBeginRender(PageEvent event) 116 { 117 Inspector inspector = (Inspector) getPage(); 118 119 _inspectedComponent = inspector.getInspectedComponent(); 120 _inspectedSpecification = _inspectedComponent.getSpecification(); 121 } 122 123 public IComponent getInspectedComponent() 124 { 125 return _inspectedComponent; 126 } 127 128 public IComponentSpecification getInspectedSpecification() 129 { 130 return _inspectedSpecification; 131 } 132 133 137 138 public List getFormalParameterNames() 139 { 140 if (_formalParameterNames == null) 141 _formalParameterNames = sort(_inspectedSpecification.getParameterNames()); 142 143 return _formalParameterNames; 144 } 145 146 152 153 public List getInformalParameterNames() 154 { 155 if (_informalParameterNames != null) 156 return _informalParameterNames; 157 158 Collection names = _inspectedComponent.getBindingNames(); 159 if (names != null && names.size() > 0) 160 { 161 _informalParameterNames = new ArrayList (names); 162 163 168 names = _inspectedSpecification.getParameterNames(); 169 if (names != null) 170 _informalParameterNames.removeAll(names); 171 172 Collections.sort(_informalParameterNames); 173 } 174 175 return _informalParameterNames; 176 } 177 178 public String getParameterName() 179 { 180 return _parameterName; 181 } 182 183 public void setParameterName(String value) 184 { 185 _parameterName = value; 186 } 187 188 193 194 public IParameterSpecification getParameterSpecification() 195 { 196 return _inspectedSpecification.getParameter(_parameterName); 197 } 198 199 204 205 public IBinding getBinding() 206 { 207 return _inspectedComponent.getBinding(_parameterName); 208 } 209 210 public void setAssetName(String value) 211 { 212 _assetName = value; 213 } 214 215 public String getAssetName() 216 { 217 return _assetName; 218 } 219 220 225 226 public IAsset getAsset() 227 { 228 return (IAsset) _inspectedComponent.getAssets().get(_assetName); 229 } 230 231 236 237 public List getAssetNames() 238 { 239 if (_assetNames == null) 240 _assetNames = sort(_inspectedComponent.getAssets().keySet()); 241 242 return _assetNames; 243 } 244 245 public List getSortedComponents() 246 { 247 if (_sortedComponents != null) 248 return _sortedComponents; 249 250 Inspector inspector = (Inspector) getPage(); 251 IComponent inspectedComponent = inspector.getInspectedComponent(); 252 253 256 Map components = inspectedComponent.getComponents(); 257 258 _sortedComponents = new ArrayList (components.values()); 259 260 Collections.sort(_sortedComponents, new ComponentComparitor()); 261 262 return _sortedComponents; 263 } 264 265 public void setComponent(IComponent value) 266 { 267 _component = value; 268 } 269 270 public IComponent getComponent() 271 { 272 return _component; 273 } 274 275 280 281 public String getComponentType() 282 { 283 IComponent container = _component.getContainer(); 284 285 IComponentSpecification containerSpecification = container.getSpecification(); 286 287 String id = _component.getId(); 288 IContainedComponent contained = containerSpecification.getComponent(id); 289 290 293 if (contained == null) 294 return null; 295 296 return contained.getType(); 297 } 298 299 305 306 public List getSortedPropertyNames() 307 { 308 if (_sortedPropertyNames == null) 309 _sortedPropertyNames = sort(_inspectedSpecification.getPropertyNames()); 310 311 return _sortedPropertyNames; 312 } 313 314 public void setPropertyName(String value) 315 { 316 _propertyName = value; 317 } 318 319 public String getPropertyName() 320 { 321 return _propertyName; 322 } 323 324 public String getPropertyValue() 325 { 326 return _inspectedSpecification.getProperty(_propertyName); 327 } 328 329 public List getBeanNames() 330 { 331 if (_beanNames == null) 332 _beanNames = sort(_inspectedSpecification.getBeanNames()); 333 334 return _beanNames; 335 } 336 337 public void setBeanName(String value) 338 { 339 _beanName = value; 340 _beanSpecification = _inspectedSpecification.getBeanSpecification(_beanName); 341 } 342 343 public String getBeanName() 344 { 345 return _beanName; 346 } 347 348 public IBeanSpecification getBeanSpecification() 349 { 350 return _beanSpecification; 351 } 352 353 private List sort(Collection c) 354 { 355 if (c == null || c.size() == 0) 356 return null; 357 358 List result = new ArrayList (c); 359 360 Collections.sort(result); 361 362 return result; 363 } 364 } | Popular Tags |