KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > contrib > inspector > Selector


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 JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Set JavaDoc;
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 public abstract class Selector extends BaseComponent
40 {
41     /**
42      * When the form is submitted,
43      * the inspectedPageName of the {@link Inspector} page will be updated,
44      * but we need to reset the inspectedIdPath as well.
45      *
46      **/

47
48     public void formSubmit(IRequestCycle cycle)
49     {
50         Inspector inspector = (Inspector) getPage();
51
52         inspector.selectComponent((String JavaDoc) null);
53     }
54
55     /**
56      * Returns an {IPropertySelectionModel} used to select the name of the page
57      * to inspect. The page names are sorted.
58      *
59      **/

60
61     public IPropertySelectionModel getPageModel()
62     {
63         return new StringPropertySelectionModel(getPageNames());
64     }
65
66     /**
67      * The crumb trail is all the components from the inspected component up to
68      * (but not including) the page.
69      *
70      **/

71
72     public List JavaDoc getCrumbTrail()
73     {
74         List JavaDoc result = null;
75
76         Inspector inspector = (Inspector) getPage();
77         IComponent component = inspector.getInspectedComponent();
78         IComponent container = null;
79
80         while (true)
81         {
82             container = component.getContainer();
83             if (container == null)
84                 break;
85
86             if (result == null)
87                 result = new ArrayList JavaDoc();
88
89             result.add(component);
90
91             component = container;
92         }
93
94         if (result == null)
95             return null;
96
97         // Reverse the list, such that the inspected component is last, and the
98
// top-most container is first.
99

100         Collections.reverse(result);
101
102         return result;
103     }
104
105     private String JavaDoc[] getPageNames()
106     {
107         Set JavaDoc names = new HashSet JavaDoc();
108
109         ISpecificationSource source = getPage().getEngine().getSpecificationSource();
110
111         addPageNames(names, source.getFrameworkNamespace());
112         addPageNames(names, source.getApplicationNamespace());
113
114         List JavaDoc l = new ArrayList JavaDoc(names);
115         Collections.sort(l);
116
117         return (String JavaDoc[]) l.toArray(new String JavaDoc[l.size()]);
118     }
119
120     private void addPageNames(Set JavaDoc names, INamespace namespace)
121     {
122         String JavaDoc idPrefix = namespace.getExtendedId();
123
124         List JavaDoc pageNames = namespace.getPageNames();
125         int count = pageNames.size();
126
127         for (int i = 0; i < count; i++)
128         {
129             String JavaDoc name = (String JavaDoc) pageNames.get(i);
130
131             if (idPrefix == null)
132                 names.add(name);
133             else
134                 names.add(idPrefix + ":" + name);
135         }
136
137         List JavaDoc ids = namespace.getChildIds();
138         count = ids.size();
139
140         for (int i = 0; i < count; i++)
141         {
142             String JavaDoc id = (String JavaDoc) ids.get(i);
143
144             addPageNames(names, namespace.getChildNamespace(id));
145         }
146     }
147
148 }
Popular Tags