Coverage Report - org.apache.tapestry.contrib.inspector.Selector
 
Classes in this File Line Coverage Branch Coverage Complexity
Selector
0%
0/41
0%
0/12
2.333
 
 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.Collections;
 19  
 import java.util.HashSet;
 20  
 import java.util.List;
 21  
 import java.util.Set;
 22  
 
 23  
 import org.apache.tapestry.BaseComponent;
 24  
 import org.apache.tapestry.IComponent;
 25  
 import org.apache.tapestry.INamespace;
 26  
 import org.apache.tapestry.IRequestCycle;
 27  
 import org.apache.tapestry.engine.ISpecificationSource;
 28  
 import org.apache.tapestry.form.IPropertySelectionModel;
 29  
 import org.apache.tapestry.form.StringPropertySelectionModel;
 30  
 
 31  
 /**
 32  
  *  Component of the {@link Inspector} page used to select the page and "crumb trail"
 33  
  *  of the inspected component.
 34  
  *
 35  
  *  @author Howard Lewis Ship
 36  
  *
 37  
  **/
 38  
 
 39  0
 public abstract class Selector extends BaseComponent
 40  
 {
 41  
     public abstract ISpecificationSource getSpecificationSource();
 42  
     
 43  
     /**
 44  
      *  When the form is submitted,
 45  
      *  the inspectedPageName of the {@link Inspector} page will be updated,
 46  
      *  but we need to reset the inspectedIdPath as well.
 47  
      *
 48  
      **/
 49  
 
 50  
     public void formSubmit(IRequestCycle cycle)
 51  
     {
 52  0
         Inspector inspector = (Inspector) getPage();
 53  
 
 54  0
         inspector.selectComponent((String) null);
 55  0
     }
 56  
 
 57  
     /**
 58  
      *  Returns an {IPropertySelectionModel} used to select the name of the page
 59  
      *  to inspect.  The page names are sorted.
 60  
      *
 61  
      **/
 62  
 
 63  
     public IPropertySelectionModel getPageModel()
 64  
     {
 65  0
         return new StringPropertySelectionModel(getPageNames());
 66  
     }
 67  
 
 68  
     /**
 69  
      *  The crumb trail is all the components from the inspected component up to
 70  
      *  (but not including) the page.
 71  
      *
 72  
      **/
 73  
 
 74  
     public List getCrumbTrail()
 75  
     {
 76  0
         List result = null;
 77  
 
 78  0
         Inspector inspector = (Inspector) getPage();
 79  0
         IComponent component = inspector.getInspectedComponent();
 80  0
         IComponent container = null;
 81  
 
 82  
         while (true)
 83  
         {
 84  0
             container = component.getContainer();
 85  0
             if (container == null)
 86  0
                 break;
 87  
 
 88  0
             if (result == null)
 89  0
                 result = new ArrayList();
 90  
 
 91  0
             result.add(component);
 92  
 
 93  0
             component = container;
 94  
         }
 95  
 
 96  0
         if (result == null)
 97  0
             return null;
 98  
 
 99  
         // Reverse the list, such that the inspected component is last, and the
 100  
         // top-most container is first.
 101  
 
 102  0
         Collections.reverse(result);
 103  
 
 104  0
         return result;
 105  
     }
 106  
     
 107  
     private String[] getPageNames()
 108  
     {
 109  0
         Set names = new HashSet();
 110  
         
 111  0
         ISpecificationSource source = getSpecificationSource();
 112  
         
 113  0
         addPageNames(names, source.getFrameworkNamespace());
 114  0
         addPageNames(names, source.getApplicationNamespace());
 115  
         
 116  0
         List l = new ArrayList(names);
 117  0
         Collections.sort(l);
 118  
 
 119  0
         return (String[]) l.toArray(new String[l.size()]);
 120  
     }
 121  
 
 122  
     private void addPageNames(Set names, INamespace namespace)
 123  
     {
 124  0
         String idPrefix = namespace.getExtendedId();
 125  
 
 126  0
         List pageNames = namespace.getPageNames();
 127  0
         int count = pageNames.size();
 128  
 
 129  0
         for (int i = 0; i < count; i++)
 130  
         {
 131  0
             String name = (String) pageNames.get(i);
 132  
 
 133  0
             if (idPrefix == null)
 134  0
                 names.add(name);
 135  
             else
 136  0
                 names.add(idPrefix + ":" + name);
 137  
         }
 138  
         
 139  0
         List ids = namespace.getChildIds();
 140  0
         count = ids.size();
 141  
 
 142  0
         for (int i = 0; i < count; i++)
 143  
         {
 144  0
             String id = (String) ids.get(i);
 145  
 
 146  0
             addPageNames(names, namespace.getChildNamespace(id));
 147  
         }
 148  0
     }
 149  
 
 150  
 }