Coverage Report - org.apache.tapestry.contrib.inspector.ShowSpecification
 
Classes in this File Line Coverage Branch Coverage Complexity
ShowSpecification
0%
0/75
0%
0/22
1.593
ShowSpecification$1
N/A
N/A
1.593
ShowSpecification$ComponentComparitor
0%
0/8
0%
0/2
1.593
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 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.PageBeginRenderListener;
 29  
 import org.apache.tapestry.event.PageEndRenderListener;
 30  
 import org.apache.tapestry.event.PageEvent;
 31  
 import org.apache.tapestry.spec.IBeanSpecification;
 32  
 import org.apache.tapestry.spec.IComponentSpecification;
 33  
 import org.apache.tapestry.spec.IParameterSpecification;
 34  
 
 35  
 /**
 36  
  * Component of the {@link Inspector} page used to display the specification, parameters and
 37  
  * bindings and assets of the inspected component.
 38  
  * 
 39  
  * @author Howard Lewis Ship
 40  
  */
 41  
 
 42  0
 public abstract class ShowSpecification extends BaseComponent implements PageBeginRenderListener,
 43  
         PageEndRenderListener
 44  
 {
 45  
     private IComponent _inspectedComponent;
 46  
 
 47  
     private IComponentSpecification _inspectedSpecification;
 48  
 
 49  
     private String _parameterName;
 50  
 
 51  
     private String _assetName;
 52  
 
 53  
     private List _sortedComponents;
 54  
 
 55  
     private List _assetNames;
 56  
 
 57  
     private List _formalParameterNames;
 58  
 
 59  
     private List _informalParameterNames;
 60  
 
 61  
     private List _sortedPropertyNames;
 62  
 
 63  
     private String _propertyName;
 64  
 
 65  
     private List _beanNames;
 66  
 
 67  
     private String _beanName;
 68  
 
 69  
     private IBeanSpecification _beanSpecification;
 70  
 
 71  
     /**
 72  
      * 
 73  
      * @author hls
 74  
      */
 75  0
     private static class ComponentComparitor implements Comparator
 76  
     {
 77  
         public int compare(Object left, Object right)
 78  
         {
 79  
             IComponent leftComponent;
 80  
             String leftId;
 81  
             IComponent rightComponent;
 82  
             String rightId;
 83  
 
 84  0
             if (left == right)
 85  0
                 return 0;
 86  
 
 87  0
             leftComponent = (IComponent) left;
 88  0
             rightComponent = (IComponent) right;
 89  
 
 90  0
             leftId = leftComponent.getId();
 91  0
             rightId = rightComponent.getId();
 92  
 
 93  0
             return leftId.compareTo(rightId);
 94  
         }
 95  
     }
 96  
 
 97  
     /**
 98  
      * Clears all cached information about the component and such after each render (including the
 99  
      * rewind phase render used to process the tab view).
 100  
      * 
 101  
      * @since 1.0.5
 102  
      */
 103  
 
 104  
     public void pageEndRender(PageEvent event)
 105  
     {
 106  0
         _inspectedComponent = null;
 107  0
         _inspectedSpecification = null;
 108  0
         _parameterName = null;
 109  0
         _assetName = null;
 110  0
         _sortedComponents = null;
 111  0
         _assetNames = null;
 112  0
         _formalParameterNames = null;
 113  0
         _informalParameterNames = null;
 114  0
         _sortedPropertyNames = null;
 115  0
         _propertyName = null;
 116  0
         _beanNames = null;
 117  0
         _beanName = null;
 118  0
         _beanSpecification = null;
 119  0
     }
 120  
 
 121  
     /**
 122  
      * Gets the inspected component and specification from the {@link Inspector} page.
 123  
      * 
 124  
      * @since 1.0.5
 125  
      */
 126  
 
 127  
     public void pageBeginRender(PageEvent event)
 128  
     {
 129  0
         Inspector inspector = (Inspector) getPage();
 130  
 
 131  0
         _inspectedComponent = inspector.getInspectedComponent();
 132  0
         _inspectedSpecification = _inspectedComponent.getSpecification();
 133  0
     }
 134  
 
 135  
     public IComponent getInspectedComponent()
 136  
     {
 137  0
         return _inspectedComponent;
 138  
     }
 139  
 
 140  
     public IComponentSpecification getInspectedSpecification()
 141  
     {
 142  0
         return _inspectedSpecification;
 143  
     }
 144  
 
 145  
     /**
 146  
      * Returns a sorted list of formal parameter names.
 147  
      */
 148  
 
 149  
     public List getFormalParameterNames()
 150  
     {
 151  0
         if (_formalParameterNames == null)
 152  0
             _formalParameterNames = sort(_inspectedSpecification.getParameterNames());
 153  
 
 154  0
         return _formalParameterNames;
 155  
     }
 156  
 
 157  
     /**
 158  
      * Returns a sorted list of informal parameter names. This is the list of all bindings, with the
 159  
      * list of parameter names removed, sorted.
 160  
      */
 161  
 
 162  
     public List getInformalParameterNames()
 163  
     {
 164  0
         if (_informalParameterNames != null)
 165  0
             return _informalParameterNames;
 166  
 
 167  0
         Collection names = _inspectedComponent.getBindingNames();
 168  0
         if (names != null && names.size() > 0)
 169  
         {
 170  0
             _informalParameterNames = new ArrayList(names);
 171  
 
 172  
             // Remove the names of any formal parameters. This leaves
 173  
             // just the names of informal parameters (informal parameters
 174  
             // are any parameters/bindings that don't match a formal parameter
 175  
             // name).
 176  
 
 177  0
             names = _inspectedSpecification.getParameterNames();
 178  0
             if (names != null)
 179  0
                 _informalParameterNames.removeAll(names);
 180  
 
 181  0
             Collections.sort(_informalParameterNames);
 182  
         }
 183  
 
 184  0
         return _informalParameterNames;
 185  
     }
 186  
 
 187  
     public String getParameterName()
 188  
     {
 189  0
         return _parameterName;
 190  
     }
 191  
 
 192  
     public void setParameterName(String value)
 193  
     {
 194  0
         _parameterName = value;
 195  0
     }
 196  
 
 197  
     /**
 198  
      * Returns the {@link org.apache.tapestry.spec.ParameterSpecification} corresponding to the
 199  
      * value of the parameterName property.
 200  
      */
 201  
 
 202  
     public IParameterSpecification getParameterSpecification()
 203  
     {
 204  0
         return _inspectedSpecification.getParameter(_parameterName);
 205  
     }
 206  
 
 207  
     /**
 208  
      * Returns the {@link IBinding} corresponding to the value of the parameterName property.
 209  
      */
 210  
 
 211  
     public IBinding getBinding()
 212  
     {
 213  0
         return _inspectedComponent.getBinding(_parameterName);
 214  
     }
 215  
 
 216  
     public void setAssetName(String value)
 217  
     {
 218  0
         _assetName = value;
 219  0
     }
 220  
 
 221  
     public String getAssetName()
 222  
     {
 223  0
         return _assetName;
 224  
     }
 225  
 
 226  
     /**
 227  
      * Returns the {@link IAsset} corresponding to the value of the assetName property.
 228  
      */
 229  
 
 230  
     public IAsset getAsset()
 231  
     {
 232  0
         return (IAsset) _inspectedComponent.getAssets().get(_assetName);
 233  
     }
 234  
 
 235  
     /**
 236  
      * Returns a sorted list of asset names, or null if the component contains no assets.
 237  
      */
 238  
 
 239  
     public List getAssetNames()
 240  
     {
 241  0
         if (_assetNames == null)
 242  0
             _assetNames = sort(_inspectedComponent.getAssets().keySet());
 243  
 
 244  0
         return _assetNames;
 245  
     }
 246  
 
 247  
     public List getSortedComponents()
 248  
     {
 249  0
         if (_sortedComponents != null)
 250  0
             return _sortedComponents;
 251  
 
 252  0
         Inspector inspector = (Inspector) getPage();
 253  0
         IComponent inspectedComponent = inspector.getInspectedComponent();
 254  
 
 255  
         // Get a Map of the components and simply return null if there
 256  
         // are none.
 257  
 
 258  0
         Map components = inspectedComponent.getComponents();
 259  
 
 260  0
         _sortedComponents = new ArrayList(components.values());
 261  
 
 262  0
         Collections.sort(_sortedComponents, new ComponentComparitor());
 263  
 
 264  0
         return _sortedComponents;
 265  
     }
 266  
 
 267  
     public abstract void setCurrentComponent(IComponent value);
 268  
 
 269  
     public abstract IComponent getCurrentComponent();
 270  
 
 271  
     /**
 272  
      * Returns a list of the properties for the component (from its specification), or null if the
 273  
      * component has no properties.
 274  
      */
 275  
 
 276  
     public List getSortedPropertyNames()
 277  
     {
 278  0
         if (_sortedPropertyNames == null)
 279  0
             _sortedPropertyNames = sort(_inspectedSpecification.getPropertyNames());
 280  
 
 281  0
         return _sortedPropertyNames;
 282  
     }
 283  
 
 284  
     public void setPropertyName(String value)
 285  
     {
 286  0
         _propertyName = value;
 287  0
     }
 288  
 
 289  
     public String getPropertyName()
 290  
     {
 291  0
         return _propertyName;
 292  
     }
 293  
 
 294  
     public String getPropertyValue()
 295  
     {
 296  0
         return _inspectedSpecification.getProperty(_propertyName);
 297  
     }
 298  
 
 299  
     public List getBeanNames()
 300  
     {
 301  0
         if (_beanNames == null)
 302  0
             _beanNames = sort(_inspectedSpecification.getBeanNames());
 303  
 
 304  0
         return _beanNames;
 305  
     }
 306  
 
 307  
     public void setBeanName(String value)
 308  
     {
 309  0
         _beanName = value;
 310  0
         _beanSpecification = _inspectedSpecification.getBeanSpecification(_beanName);
 311  0
     }
 312  
 
 313  
     public String getBeanName()
 314  
     {
 315  0
         return _beanName;
 316  
     }
 317  
 
 318  
     public IBeanSpecification getBeanSpecification()
 319  
     {
 320  0
         return _beanSpecification;
 321  
     }
 322  
 
 323  
     private List sort(Collection c)
 324  
     {
 325  0
         if (c == null || c.size() == 0)
 326  0
             return null;
 327  
 
 328  0
         List result = new ArrayList(c);
 329  
 
 330  0
         Collections.sort(result);
 331  
 
 332  0
         return result;
 333  
     }
 334  
 }