KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.Comparator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
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 /**
36  * Component of the {@link Inspector} page used to display
37  * the specification, parameters and bindings and assets of the inspected component.
38  *
39  * @author Howard Lewis Ship
40  *
41  **/

42
43 public abstract class ShowSpecification extends BaseComponent implements PageRenderListener
44 {
45     private IComponent _inspectedComponent;
46     private IComponentSpecification _inspectedSpecification;
47     private String JavaDoc _parameterName;
48     private String JavaDoc _assetName;
49     private List JavaDoc _sortedComponents;
50     private IComponent _component;
51     private List JavaDoc _assetNames;
52     private List JavaDoc _formalParameterNames;
53     private List JavaDoc _informalParameterNames;
54     private List JavaDoc _sortedPropertyNames;
55     private String JavaDoc _propertyName;
56     private List JavaDoc _beanNames;
57     private String JavaDoc _beanName;
58     private IBeanSpecification _beanSpecification;
59
60     private static class ComponentComparitor implements Comparator JavaDoc
61     {
62         public int compare(Object JavaDoc left, Object JavaDoc right)
63         {
64             IComponent leftComponent;
65             String JavaDoc leftId;
66             IComponent rightComponent;
67             String JavaDoc 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     /**
83      * Clears all cached information about the component and such after
84      * each render (including the rewind phase render used to process
85      * the tab view).
86      *
87      * @since 1.0.5
88      *
89      **/

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     /**
110      * Gets the inspected component and specification from the {@link Inspector} page.
111      *
112      * @since 1.0.5
113      **/

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     /**
134      * Returns a sorted list of formal parameter names.
135      *
136      **/

137
138     public List JavaDoc getFormalParameterNames()
139     {
140         if (_formalParameterNames == null)
141             _formalParameterNames = sort(_inspectedSpecification.getParameterNames());
142
143         return _formalParameterNames;
144     }
145
146     /**
147      * Returns a sorted list of informal parameter names. This is
148      * the list of all bindings, with the list of parameter names removed,
149      * sorted.
150      *
151      **/

152
153     public List JavaDoc getInformalParameterNames()
154     {
155         if (_informalParameterNames != null)
156             return _informalParameterNames;
157
158         Collection JavaDoc names = _inspectedComponent.getBindingNames();
159         if (names != null && names.size() > 0)
160         {
161             _informalParameterNames = new ArrayList JavaDoc(names);
162
163             // Remove the names of any formal parameters. This leaves
164
// just the names of informal parameters (informal parameters
165
// are any parameters/bindings that don't match a formal parameter
166
// name).
167

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 JavaDoc getParameterName()
179     {
180         return _parameterName;
181     }
182
183     public void setParameterName(String JavaDoc value)
184     {
185         _parameterName = value;
186     }
187
188     /**
189      * Returns the {@link org.apache.tapestry.spec.ParameterSpecification} corresponding to
190      * the value of the parameterName property.
191      *
192      **/

193
194     public IParameterSpecification getParameterSpecification()
195     {
196         return _inspectedSpecification.getParameter(_parameterName);
197     }
198
199     /**
200      * Returns the {@link IBinding} corresponding to the value of
201      * the parameterName property.
202      *
203      **/

204
205     public IBinding getBinding()
206     {
207         return _inspectedComponent.getBinding(_parameterName);
208     }
209
210     public void setAssetName(String JavaDoc value)
211     {
212         _assetName = value;
213     }
214
215     public String JavaDoc getAssetName()
216     {
217         return _assetName;
218     }
219
220     /**
221      * Returns the {@link IAsset} corresponding to the value
222      * of the assetName property.
223      *
224      **/

225
226     public IAsset getAsset()
227     {
228         return (IAsset) _inspectedComponent.getAssets().get(_assetName);
229     }
230
231     /**
232      * Returns a sorted list of asset names, or null if the
233      * component contains no assets.
234      *
235      **/

236
237     public List JavaDoc getAssetNames()
238     {
239         if (_assetNames == null)
240             _assetNames = sort(_inspectedComponent.getAssets().keySet());
241
242         return _assetNames;
243     }
244
245     public List JavaDoc getSortedComponents()
246     {
247         if (_sortedComponents != null)
248             return _sortedComponents;
249
250         Inspector inspector = (Inspector) getPage();
251         IComponent inspectedComponent = inspector.getInspectedComponent();
252
253         // Get a Map of the components and simply return null if there
254
// are none.
255

256         Map JavaDoc components = inspectedComponent.getComponents();
257
258         _sortedComponents = new ArrayList JavaDoc(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     /**
276      * Returns the type of the component, as specified in the container's
277      * specification (i.e., the component alias if known).
278      *
279      **/

280
281     public String JavaDoc getComponentType()
282     {
283         IComponent container = _component.getContainer();
284
285         IComponentSpecification containerSpecification = container.getSpecification();
286
287         String JavaDoc id = _component.getId();
288         IContainedComponent contained = containerSpecification.getComponent(id);
289
290         // Temporary: An implicit component will not be in the containing
291
// component's specification as a ContainedComponent.
292

293         if (contained == null)
294             return null;
295
296         return contained.getType();
297     }
298
299     /**
300      * Returns a list of the properties for the component
301      * (from its specification), or null if the component
302      * has no properties.
303      *
304      **/

305
306     public List JavaDoc getSortedPropertyNames()
307     {
308         if (_sortedPropertyNames == null)
309             _sortedPropertyNames = sort(_inspectedSpecification.getPropertyNames());
310
311         return _sortedPropertyNames;
312     }
313
314     public void setPropertyName(String JavaDoc value)
315     {
316         _propertyName = value;
317     }
318
319     public String JavaDoc getPropertyName()
320     {
321         return _propertyName;
322     }
323
324     public String JavaDoc getPropertyValue()
325     {
326         return _inspectedSpecification.getProperty(_propertyName);
327     }
328
329     public List JavaDoc getBeanNames()
330     {
331         if (_beanNames == null)
332             _beanNames = sort(_inspectedSpecification.getBeanNames());
333
334         return _beanNames;
335     }
336
337     public void setBeanName(String JavaDoc value)
338     {
339         _beanName = value;
340         _beanSpecification = _inspectedSpecification.getBeanSpecification(_beanName);
341     }
342
343     public String JavaDoc getBeanName()
344     {
345         return _beanName;
346     }
347
348     public IBeanSpecification getBeanSpecification()
349     {
350         return _beanSpecification;
351     }
352
353     private List JavaDoc sort(Collection JavaDoc c)
354     {
355         if (c == null || c.size() == 0)
356             return null;
357
358         List JavaDoc result = new ArrayList JavaDoc(c);
359
360         Collections.sort(result);
361
362         return result;
363     }
364 }
Popular Tags