KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > layout > renderer > aspect > impl > TabContentAspect


1 /*
2  * Copyright 1999-2002,2004-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portal.layout.renderer.aspect.impl;
17
18 import java.util.Collections JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.ArrayList JavaDoc;
23
24 import org.apache.avalon.framework.parameters.ParameterException;
25 import org.apache.avalon.framework.parameters.Parameters;
26 import org.apache.cocoon.portal.PortalService;
27 import org.apache.cocoon.portal.aspect.impl.DefaultAspectDescription;
28 import org.apache.cocoon.portal.event.impl.ChangeAspectDataEvent;
29 import org.apache.cocoon.portal.layout.CompositeLayout;
30 import org.apache.cocoon.portal.layout.Item;
31 import org.apache.cocoon.portal.layout.Layout;
32 import org.apache.cocoon.portal.layout.NamedItem;
33 import org.apache.cocoon.portal.layout.renderer.aspect.RendererAspectContext;
34 import org.apache.cocoon.xml.AttributesImpl;
35 import org.apache.cocoon.xml.XMLUtils;
36 import org.xml.sax.ContentHandler JavaDoc;
37 import org.xml.sax.SAXException JavaDoc;
38
39 /**
40  * <h2>Example XML:</h2>
41  * <pre>
42  * &lt;composite&gt;
43  * &lt;named-item name="..." parameter="link-event"/&gt;
44  * &lt;named-item name="..." selected="true"&gt;
45  * &lt;!-- output from processing layout --&gt;
46  * &lt;/named-item&gt;
47  * &lt;named-item name="..." parameter="link-event"/&gt;
48  * &lt;named-item name="..." parameter="link-event"/&gt;
49  * &lt;/composite&gt;
50  * </pre>
51  *
52  * <h2>Example XML with sub-navigation (child-tag-name enabled):</h2>
53  * <pre>
54  * &lt;composite&gt;
55  * &lt;named-item name="..." parameter="link-event"/&gt;
56  * &lt;named-item name="..." selected="true"&gt;
57  * &lt;!-- output from processing layout --&gt;
58  * &lt;/named-item&gt;
59  * &lt;named-item name="..." parameter="link-event"/&gt;
60  * &lt;named-item name="..." parameter="link-event"&gt;
61  * &lt;<i>child-tag-name</i>&gt;
62  * &lt;named-item name="..." parameter="link-event"/&gt;
63  * &lt;named-item name="..." parameter="link-event"/&gt;
64  * &lt;/<i>child-tag-name</i>&gt;
65  * &lt;/named-item&gt;
66  * &lt;/composite&gt;
67  * </pre>
68  *
69  * <h2>Example XML with sub-navigation (show-all-nav without child-tag-name enabled):</h2>
70  * <pre>
71  * &lt;composite&gt;
72  * &lt;named-item name="..." parameter="link-event"/&gt;
73  * &lt;named-item name="..." selected="true"&gt;
74  * &lt;!-- output from processing layout --&gt;
75  * &lt;/named-item&gt;
76  * &lt;named-item name="..." parameter="link-event"/&gt;
77  * &lt;named-item name="..." parameter="link-event"&gt;
78  * &lt;named-item name="..." parameter="link-event"/&gt;
79  * &lt;named-item name="..." parameter="link-event"/&gt;
80  * &lt;/named-item&gt;
81  * &lt;/composite&gt;
82  * </pre>
83
84  *
85  * *
86  * <h2>Applicable to:</h2>
87  * <ul>
88  * <li>{@link org.apache.cocoon.portal.layout.CompositeLayout}</li>
89  * </ul>
90  *
91  * <h2>Parameters</h2>
92  * <table><tbody>
93  * <tr><th>store</th><td></td><td>req</td><td>String</td><td><code>null</code></td></tr>
94  * <tr><th>aspect-name</th><td>Aspect holding the current tab state.</td><td>req</td><td>String</td><td><code>"tab"</code></td></tr>
95  * <tr><th>tag-name</th><td>Name of the tag enclosing the following output.</td>
96  * <td></td><td>String</td><td><code>"composite"</code></td></tr>
97  * <tr><th>root-tag</th><td>Should a tag enclosing the following output be generated?</td>
98  * <td></td><td>boolean</td><td><code>true</code></td></tr>
99  * <tr><th>child-tag-name</th><td>The name of the tag to enclose named items (i.e. the subnavigation)
100  * of non-selected (default) items. Setting this parameter will enable show-all-nav.</td>
101  * <td></td><td>String</td><td><code>""</code></td></tr>
102  * <tr><th>show-all-nav</th><td>Setting this value to true will output the enclosed named-items</td>
103  * <td></td><td>boolean</td><td><code>false</code></td></tr>
104  * <tr><th>include-selected</th><td>Setting this value to true will output the enclosed named-items of the selected tab too.</td>
105  * <td></td><td>boolean</td><td><code>false</code></td></tr>
106  * </tbody></table>
107  *
108  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
109  * @author <a HREF="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
110  *
111  * @version CVS $Id: TabContentAspect.java 330865 2005-11-04 18:54:05Z rgoers $
112  */

113 public class TabContentAspect
114     extends CompositeContentAspect {
115
116     /**
117      * Chains that include this aspect must always render
118      * @return true
119      */

120     public boolean isRequired() {
121         return true;
122     }
123
124     /* (non-Javadoc)
125     * @see org.apache.cocoon.portal.layout.renderer.RendererAspect#toSAX(org.apache.cocoon.portal.layout.renderer.RendererAspectContext, org.apache.cocoon.portal.layout.Layout, org.apache.cocoon.portal.PortalService, org.xml.sax.ContentHandler)
126     */

127     public void toSAX(RendererAspectContext context,
128                         Layout layout,
129                         PortalService service,
130                         ContentHandler JavaDoc handler)
131     throws SAXException JavaDoc {
132         if (layout instanceof CompositeLayout) {
133             TabPreparedConfiguration config = (TabPreparedConfiguration)context.getAspectConfiguration();
134
135             if ( config.rootTag ) {
136                 XMLUtils.startElement(handler, config.tagName);
137             }
138
139             AttributesImpl attributes = new AttributesImpl();
140             CompositeLayout tabLayout = (CompositeLayout) layout;
141
142             // selected tab
143
Integer JavaDoc data = (Integer JavaDoc) layout.getAspectData(config.aspectName);
144             int selected = data.intValue();
145             
146             // loop over all tabs
147
for (int j = 0; j < tabLayout.getSize(); j++) {
148                 Item tab = tabLayout.getItem(j);
149                 ChangeAspectDataEvent event = null;
150
151                 // open named-item tag
152
attributes.clear();
153                 if ( tab instanceof NamedItem ) {
154                     attributes.addCDATAAttribute("name", String.valueOf(((NamedItem)tab).getName()));
155                 }
156                 if (j == selected) {
157                     attributes.addCDATAAttribute("selected", "true");
158                 }
159                 event = new ChangeAspectDataEvent(tabLayout, config.aspectName, new Integer JavaDoc(j));
160                 attributes.addCDATAAttribute("parameter", service.getComponentManager().getLinkService().getLinkURI(event));
161
162                 // add parameters
163
final Iterator JavaDoc iter = tab.getParameters().entrySet().iterator();
164                 while ( iter.hasNext() ) {
165                     final Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
166                     attributes.addCDATAAttribute((String JavaDoc)entry.getKey(), (String JavaDoc)entry.getValue());
167                 }
168                 
169                 XMLUtils.startElement(handler, "named-item", attributes);
170                 if (j == selected) {
171                     this.processLayout(tab.getLayout(), service, handler);
172                     if (config.includeSelected) {
173                         List JavaDoc events = new ArrayList JavaDoc();
174                         events.add(event);
175                         this.processNav(context, tab.getLayout(), service, handler, events);
176                     }
177                 } else if (config.showAllNav) {
178                     List JavaDoc events = new ArrayList JavaDoc();
179                     events.add(event);
180                     this.processNav(context, tab.getLayout(), service, handler, events);
181                 }
182
183                 // close named-item tag
184
XMLUtils.endElement(handler, "named-item");
185             }
186
187             if ( config.rootTag ) {
188                 XMLUtils.endElement(handler, config.tagName);
189             }
190         } else {
191             throw new SAXException JavaDoc("Wrong layout type, TabLayout expected: " + layout.getClass().getName());
192         }
193     }
194
195     /**
196      * Return the aspects required for this renderer
197      * @return An iterator for the aspect descriptions or null.
198      */

199     public Iterator JavaDoc getAspectDescriptions(Object JavaDoc configuration) {
200         TabPreparedConfiguration pc = (TabPreparedConfiguration)configuration;
201
202         DefaultAspectDescription desc = new DefaultAspectDescription();
203         desc.setName(pc.aspectName);
204         desc.setClassName("java.lang.Integer");
205         desc.setPersistence(pc.store);
206         desc.setAutoCreate(true);
207
208         return Collections.singletonList(desc).iterator();
209     }
210
211     /**
212      *
213      * @param service
214      * @param tab
215      */

216     private Layout getNextLayout(PortalService service, Item tab) {
217         Layout tabLayout = tab.getLayout();
218         if (tabLayout instanceof CompositeLayout) {
219             CompositeLayout layout = (CompositeLayout)tabLayout;
220             List JavaDoc list = layout.getItems();
221             int size = list.size();
222             for (int i = 0; i < size; i++) {
223                 if (list.get(i) instanceof NamedItem) {
224                     return tabLayout;
225                 }
226             }
227         }
228         Layout nextLayout = service.getEntryLayout(null);
229         if (nextLayout == null) {
230             nextLayout = tab.getLayout();
231         }
232         return nextLayout;
233     }
234
235     /*
236      * Generate the sub navigation for non-selected tabs
237      * @param context
238      * @param layout
239      * @param service
240      * @param handler
241      * @throws SAXException
242      */

243     private void processNav(RendererAspectContext context,
244                             Layout layout,
245                             PortalService service,
246                             ContentHandler JavaDoc handler,
247                             List JavaDoc parentEvents)
248         throws SAXException JavaDoc {
249         if (layout instanceof CompositeLayout) {
250             CompositeLayout tabLayout = (CompositeLayout)layout;
251
252             if (tabLayout.getSize() == 0) {
253                 return;
254             }
255             TabPreparedConfiguration config =
256                 (TabPreparedConfiguration) context.getAspectConfiguration();
257             AttributesImpl attributes = new AttributesImpl();
258             boolean subNav = false;
259
260             // loop over all tabs
261
for (int j = 0; j < tabLayout.getSize(); j++) {
262                 Item tab = tabLayout.getItem(j);
263
264                 // open named-item tag
265
attributes.clear();
266                 if (tab instanceof NamedItem) {
267                     if (!subNav && !config.childTagName.equals("")) {
268                         XMLUtils.startElement(handler, config.childTagName);
269                         subNav = true;
270                     }
271                     attributes.addCDATAAttribute("name",
272                         String.valueOf(((NamedItem) tab).getName()));
273                     ChangeAspectDataEvent event = new ChangeAspectDataEvent(tabLayout,
274                         config.aspectName, new Integer JavaDoc(j));
275                     List JavaDoc events = new ArrayList JavaDoc(parentEvents);
276                     events.add(event);
277
278                     attributes.addCDATAAttribute("parameter",
279                         service.getComponentManager().getLinkService().getLinkURI(events));
280
281                     // add parameters
282
final Iterator JavaDoc iter = tab.getParameters().entrySet().iterator();
283                     while (iter.hasNext()) {
284                         final Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
285                         attributes.addCDATAAttribute((String JavaDoc) entry.getKey(),
286                             (String JavaDoc) entry.getValue());
287                     }
288
289                     XMLUtils.startElement(handler, "named-item", attributes);
290
291                     this.processNav(context, tab.getLayout(), service, handler, events);
292
293                     // close named-item tag
294
XMLUtils.endElement(handler, "named-item");
295                 }
296
297
298             }
299             // close sub-nav tag
300
if (subNav) {
301                 XMLUtils.endElement(handler, config.childTagName);
302             }
303         }
304     }
305
306     protected class TabPreparedConfiguration extends PreparedConfiguration {
307         public String JavaDoc aspectName;
308         public String JavaDoc store;
309         public boolean showAllNav = false;
310         public boolean includeSelected = false;
311         public String JavaDoc childTagName;
312
313         public void takeValues(TabPreparedConfiguration from) {
314             super.takeValues(from);
315             this.aspectName = from.aspectName;
316             this.store = from.store;
317             this.showAllNav = from.showAllNav;
318             this.includeSelected = from.includeSelected;
319             this.childTagName = from.childTagName;
320         }
321     }
322     
323     /* (non-Javadoc)
324      * @see org.apache.cocoon.portal.layout.renderer.aspect.RendererAspect#prepareConfiguration(org.apache.avalon.framework.parameters.Parameters)
325      */

326     public Object JavaDoc prepareConfiguration(Parameters configuration)
327     throws ParameterException {
328         TabPreparedConfiguration pc = new TabPreparedConfiguration();
329         pc.takeValues((PreparedConfiguration)super.prepareConfiguration(configuration));
330         pc.aspectName = configuration.getParameter("aspect-name", "tab");
331         pc.store = configuration.getParameter("store");
332         pc.childTagName = configuration.getParameter("child-tag-name", "");
333         if (!pc.childTagName.equals("")) {
334             pc.showAllNav = true;
335         } else {
336             pc.showAllNav = configuration.getParameterAsBoolean("show-all-nav", false);
337         }
338         pc.includeSelected = configuration.getParameterAsBoolean("include-selected", false);
339         return pc;
340     }
341
342 }
343
Popular Tags