KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.apache.tapestry.BaseComponent;
21 import org.apache.tapestry.IComponent;
22 import org.apache.tapestry.IDirect;
23 import org.apache.tapestry.IMarkupWriter;
24 import org.apache.tapestry.IRender;
25 import org.apache.tapestry.IRequestCycle;
26 import org.apache.tapestry.Tapestry;
27 import org.apache.tapestry.engine.DirectServiceParameter;
28 import org.apache.tapestry.engine.IEngineService;
29 import org.apache.tapestry.engine.ILink;
30 import org.apache.tapestry.link.DirectLink;
31 import org.apache.tapestry.parse.CloseToken;
32 import org.apache.tapestry.parse.ComponentTemplate;
33 import org.apache.tapestry.parse.LocalizationToken;
34 import org.apache.tapestry.parse.OpenToken;
35 import org.apache.tapestry.parse.TemplateToken;
36 import org.apache.tapestry.parse.TextToken;
37 import org.apache.tapestry.parse.TokenType;
38 import org.apache.tapestry.services.TemplateSource;
39
40 /**
41  * Component of the {@link Inspector}page used to display the ids and types of all embedded
42  * components.
43  *
44  * @author Howard Lewis Ship
45  */

46
47 public abstract class ShowTemplate extends BaseComponent implements IDirect
48 {
49     /** @since 4.0 */
50     public abstract TemplateSource getTemplateSource();
51
52     public boolean getHasTemplate()
53     {
54         Inspector inspector;
55
56         inspector = (Inspector) getPage();
57
58         // Components that inherit from BaseComponent have templates,
59
// others do not.
60

61         return inspector.getInspectedComponent() instanceof BaseComponent;
62     }
63
64     public IRender getTemplateDelegate()
65     {
66         return new IRender()
67         {
68             public void render(IMarkupWriter writer, IRequestCycle cycle)
69             {
70                 writeTemplate(writer, cycle);
71             }
72         };
73     }
74
75     /**
76      * Writes the HTML template for the component. When <jwc> tags are written, the id is made
77      * a link (that selects the named component). We use some magic to accomplish this, creating
78      * links as if we were a {@link DirectLink}component, and attributing those links to the
79      * captive {@link DirectLink}component embedded here.
80      */

81
82     private void writeTemplate(IMarkupWriter writer, IRequestCycle cycle)
83     {
84         IComponent inspectedComponent = getInspectedComponent();
85         ComponentTemplate template = null;
86
87         try
88         {
89             template = getTemplateSource().getTemplate(cycle, inspectedComponent);
90         }
91         catch (Exception JavaDoc ex)
92         {
93             return;
94         }
95
96         writer.begin("pre");
97
98         int count = template.getTokenCount();
99
100         for (int i = 0; i < count; i++)
101         {
102             TemplateToken token = template.getToken(i);
103             TokenType type = token.getType();
104
105             if (type == TokenType.TEXT)
106             {
107                 write(writer, (TextToken) token);
108                 continue;
109             }
110
111             if (type == TokenType.CLOSE)
112             {
113                 write(writer, (CloseToken) token);
114
115                 continue;
116             }
117
118             if (token.getType() == TokenType.LOCALIZATION)
119             {
120
121                 write(writer, (LocalizationToken) token);
122                 continue;
123             }
124
125             if (token.getType() == TokenType.OPEN)
126             {
127                 boolean nextIsClose = (i + 1 < count)
128                         && (template.getToken(i + 1).getType() == TokenType.CLOSE);
129
130                 write(writer, nextIsClose, (OpenToken) token);
131
132                 if (nextIsClose)
133                     i++;
134
135                 continue;
136             }
137
138             // That's all the types known at this time.
139
}
140
141         writer.end(); // <pre>
142
}
143
144     /** @since 3.0 * */
145
146     private IComponent getInspectedComponent()
147     {
148         Inspector page = (Inspector) getPage();
149
150         return page.getInspectedComponent();
151     }
152
153     /** @since 3.0 * */
154
155     private void write(IMarkupWriter writer, TextToken token)
156     {
157         // Print the section of the template ... print() will
158
// escape and invalid characters as HTML entities. Also,
159
// we show the full stretch of text, not the trimmed version.
160

161         writer.print(token.getTemplateDataAsString());
162     }
163
164     /** @since 3.0 * */
165
166     private void write(IMarkupWriter writer, CloseToken token)
167     {
168         writer.begin("span");
169         writer.attribute("class", "jwc-tag");
170
171         writer.print("</");
172         writer.print(token.getTag());
173         writer.print(">");
174
175         writer.end(); // <span>
176
}
177
178     /** @since 3.0 * */
179
180     private void write(IMarkupWriter writer, LocalizationToken token)
181     {
182         IComponent component = getInspectedComponent();
183
184         writer.begin("span");
185         writer.attribute("class", "jwc-tag");
186
187         writer.print("<span key=\"");
188         writer.print(token.getKey());
189         writer.print('"');
190
191         Map JavaDoc attributes = token.getAttributes();
192         if (attributes != null && !attributes.isEmpty())
193         {
194             Iterator JavaDoc it = attributes.entrySet().iterator();
195             while (it.hasNext())
196             {
197                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
198                 String JavaDoc attributeName = (String JavaDoc) entry.getKey();
199                 String JavaDoc attributeValue = (String JavaDoc) entry.getValue();
200
201                 writer.print(' ');
202                 writer.print(attributeName);
203                 writer.print("=\"");
204                 writer.print(attributeValue);
205                 writer.print('"');
206
207             }
208         }
209
210         writer.print('>');
211         writer.begin("span");
212         writer.attribute("class", "localized-string");
213
214         writer.print(component.getMessages().getMessage(token.getKey()));
215         writer.end(); // <span>
216

217         writer.print("</span>");
218
219         writer.end(); // <span>
220
}
221
222     /** @since 3.0 * */
223
224     private void write(IMarkupWriter writer, boolean nextIsClose, OpenToken token)
225     {
226         IComponent component = getInspectedComponent();
227         IEngineService service = getPage().getEngine().getService(Tapestry.DIRECT_SERVICE);
228
229         // Each id references a component embedded in the inspected component.
230
// Get that component.
231

232         String JavaDoc id = token.getId();
233         IComponent embedded = component.getComponent(id);
234         Object JavaDoc[] serviceParameters = new Object JavaDoc[]
235         { embedded.getIdPath() };
236
237         // Build a URL to select that component, as if by the captive
238
// component itself (it's a Direct).
239

240         DirectServiceParameter dsp = new DirectServiceParameter(this, serviceParameters);
241         ILink link = service.getLink(getPage().getRequestCycle(), dsp);
242
243         writer.begin("span");
244         writer.attribute("class", "jwc-tag");
245
246         writer.print("<");
247         writer.print(token.getTag());
248
249         writer.print(" jwcid=\"");
250
251         writer.begin("span");
252         writer.attribute("class", "jwc-id");
253
254         writer.begin("a");
255         writer.attribute("href", link.getURL());
256         writer.print(id);
257
258         writer.end(); // <a>
259
writer.end(); // <span>
260
writer.print('"');
261
262         Map JavaDoc attributes = token.getAttributesMap();
263
264         if (attributes != null)
265         {
266             Iterator JavaDoc ii = attributes.entrySet().iterator();
267
268             while (ii.hasNext())
269             {
270                 Map.Entry JavaDoc e = (Map.Entry JavaDoc) ii.next();
271
272                 String JavaDoc value = (String JavaDoc) e.getValue();
273
274                 writer.print(' ');
275                 writer.print(e.getKey().toString());
276                 writer.print("=\"");
277                 writer.print(value);
278                 writer.print('"');
279             }
280         }
281
282         // Collapse an open & close down to a single tag.
283

284         if (nextIsClose)
285             writer.print('/');
286
287         writer.print('>');
288         writer.end(); // <span>
289
}
290
291     /**
292      * Invoked when a component id is clicked.
293      */

294
295     public void trigger(IRequestCycle cycle)
296     {
297         Inspector inspector = (Inspector) getPage();
298
299         String JavaDoc componentId = (String JavaDoc) cycle.getListenerParameters()[0];
300         inspector.selectComponent(componentId);
301
302         IComponent newComponent = inspector.getInspectedComponent();
303
304         // If the component is not a BaseComponent then it won't have
305
// a template, so switch to the specification view.
306

307         if (!(newComponent instanceof BaseComponent))
308             inspector.setView(View.SPECIFICATION);
309     }
310
311     /**
312      * Always returns true.
313      *
314      * @since 2.3
315      */

316
317     public boolean isStateful()
318     {
319         return true;
320     }
321 }
Popular Tags