KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > impl > PageLabelManager


1 /*
2  * Copyright 1999-2002,2004 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.impl;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.avalon.framework.context.Context;
23 import org.apache.avalon.framework.context.ContextException;
24 import org.apache.avalon.framework.context.Contextualizable;
25 import org.apache.avalon.framework.logger.AbstractLogEnabled;
26 import org.apache.avalon.framework.service.ServiceException;
27 import org.apache.avalon.framework.service.ServiceManager;
28 import org.apache.avalon.framework.service.Serviceable;
29 import org.apache.avalon.framework.thread.ThreadSafe;
30 import org.apache.avalon.framework.CascadingRuntimeException;
31 import org.apache.avalon.framework.component.Component;
32 import org.apache.avalon.framework.configuration.Configurable;
33 import org.apache.avalon.framework.configuration.Configuration;
34 import org.apache.cocoon.portal.PortalService;
35 import org.apache.cocoon.portal.layout.Layout;
36 import org.apache.cocoon.portal.layout.CompositeLayout;
37 import org.apache.cocoon.portal.layout.Item;
38 import org.apache.cocoon.portal.layout.NamedItem;
39 import org.apache.cocoon.portal.event.impl.ChangeAspectDataEvent;
40 import org.apache.cocoon.util.HashMap;
41 import org.apache.cocoon.environment.Request;
42 import org.apache.cocoon.environment.ObjectModelHelper;
43 import org.apache.cocoon.components.ContextHelper;
44
45 /**
46  * Manages the various activities required for page labels.
47  *
48  * The name of the request parameter used to identify the page labelmay be configured
49  * here by declaring
50  * <br><parameter-name><i>request-parm-name</i></parameter-name><br>
51  * in the configuration for this component. The default request parameter name is
52  * 'pageLabel'.
53  * @author Ralph Goers
54  *
55  * @version CVS $Id: $
56  */

57 public class PageLabelManager
58     extends AbstractLogEnabled
59     implements ThreadSafe, Serviceable, Configurable, Contextualizable, Component {
60
61     public static final String JavaDoc ROLE = PageLabelManager.class.getName();
62
63     /** The service manager */
64     protected ServiceManager manager;
65     /** The cocoon context */
66     protected Context context;
67     protected String JavaDoc aspectName = null;
68     private String JavaDoc requestParameterName;
69     private boolean nonStickyTabs;
70     private boolean marshallEvents;
71
72     protected static final String JavaDoc LABEL_ARRAY = PageLabelManager.class.getName() + "A";
73     protected static final String JavaDoc LABEL_MAP = PageLabelManager.class.getName() + "M";
74     protected static final String JavaDoc EVENT_MAP = PageLabelManager.class.getName() + "E";
75     private static final String JavaDoc DEFAULT_REQUEST_PARAMETER_NAME = "pageLabel";
76
77
78     /* (non-Javadoc)
79      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
80      */

81     public void service(ServiceManager manager) throws ServiceException {
82         this.manager = manager;
83     }
84
85     /* (non-Javadoc)
86     * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
87     */

88     public void configure(Configuration config)
89     {
90         this.requestParameterName =
91             config.getChild("parameterName").getValue(DEFAULT_REQUEST_PARAMETER_NAME);
92         this.aspectName = config.getChild("aspectName").getValue("tab");
93         this.nonStickyTabs =
94             Boolean.valueOf(config.getChild("nonStickyTabs").getValue("false")).booleanValue();
95         this.marshallEvents =
96             Boolean.valueOf(config.getChild("marshallEvents").getValue("false")).booleanValue();
97     }
98
99     /* (non-Javadoc)
100     * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
101     */

102     public void contextualize(Context context) throws ContextException {
103         this.context = context;
104     }
105
106     /**
107      * Return the current page label.
108      * @return The current page label.
109      */

110     public String JavaDoc getCurrentLabel() {
111         String JavaDoc[] labels = getLabels();
112
113         return labels[0];
114     }
115
116     /**
117      * Return the page label from the previous request.
118      * @return The previous page label.
119      */

120     public String JavaDoc getPreviousLabel() {
121         String JavaDoc[] labels = getLabels();
122
123         return labels[1];
124     }
125
126     /**
127      * Sets the current page label.
128      * @return The current page label.
129      */

130     public String JavaDoc setCurrentLabel() {
131         final Request request =
132             ObjectModelHelper.getRequest(ContextHelper.getObjectModel(this.context));
133         String JavaDoc value = request.getParameter(this.requestParameterName);
134         String JavaDoc[] labels = getLabels();
135
136         if (value != null) {
137             labels[1] = labels[0];
138             labels[0] = value;
139         }
140         return labels[0];
141     }
142
143     /**
144      * Returns the request parameter being used to identify the page label.
145      * @return A String containing the request parameter name used for page labels.
146      */

147     public String JavaDoc getRequestParameterName() {
148         return this.requestParameterName;
149     }
150
151
152     /**
153      * Return the Map that contains events for all the page labels.
154      * @return The Map to use for converting events to and from urls.
155      */

156     public Map JavaDoc getPageEventMap()
157     {
158         PortalService service = null;
159         try {
160             service = (PortalService) this.manager.lookup(PortalService.ROLE);
161             Map JavaDoc map = (Map JavaDoc) service.getAttribute(EVENT_MAP);
162             if (null == map) {
163                 map = new HashMap();
164                 service.setAttribute(EVENT_MAP, map);
165             }
166
167             return map;
168         }
169         catch (ServiceException ce) {
170             throw new CascadingRuntimeException("Unable to lookup component.", ce);
171         }
172         finally {
173             this.manager.release(service);
174         }
175     }
176
177     /**
178      * Retrieve the events associated with the specified page label.
179      *
180      * @param pageLabel The label to retrieve the events for.
181      * @return A List containing all the events associated with the page label in the order they
182      * should occur.
183      */

184     public List JavaDoc getPageLabelEvents(String JavaDoc pageLabel) {
185         PortalService service = null;
186         try {
187             service = (PortalService) this.manager.lookup(PortalService.ROLE);
188             Map JavaDoc map = (Map JavaDoc) service.getAttribute(LABEL_MAP);
189             if (null == map) {
190                 map = initializeLabels(service);
191                 service.setAttribute(LABEL_MAP, map);
192             }
193
194             List JavaDoc list = (List JavaDoc) map.get(pageLabel);
195
196             if (list == null) {
197                 list = new ArrayList JavaDoc();
198                 map.put(pageLabel, list);
199             }
200
201             return list;
202         }
203         catch (ServiceException ce) {
204             throw new CascadingRuntimeException("Unable to lookup component.", ce);
205         }
206         finally {
207             this.manager.release(service);
208         }
209     }
210
211     /**
212      * Returns true if events are not to be exposed as request parameters
213      */

214     public boolean isMarshallEvents() {
215         return this.marshallEvents;
216     }
217
218     /*
219      * Return the array containing the current and previous labels.
220      */

221     private String JavaDoc[] getLabels() {
222         PortalService service = null;
223         try {
224             service = (PortalService) this.manager.lookup(PortalService.ROLE);
225             String JavaDoc[] labels = (String JavaDoc[]) service.getAttribute(LABEL_ARRAY);
226             if (null == labels) {
227                 labels = new String JavaDoc[2];
228                 service.setAttribute(LABEL_ARRAY, labels);
229             }
230             return labels;
231         }
232         catch (ServiceException ce) {
233             throw new CascadingRuntimeException("Unable to lookup component.", ce);
234         }
235         finally {
236             this.manager.release(service);
237         }
238     }
239
240     /**
241      * Create the page label event map and return it.
242      *
243      * @param service The portal service
244      * @return The page label map.
245      */

246     private Map JavaDoc initializeLabels(PortalService service) {
247         Map JavaDoc map = new HashMap();
248
249         Layout portalLayout = service.getEntryLayout(null);
250         if (portalLayout == null) {
251             portalLayout =
252                 service.getComponentManager().getProfileManager().getPortalLayout(null, null);
253         }
254
255         if (portalLayout instanceof CompositeLayout) {
256             populate((CompositeLayout) portalLayout, map, "", new ArrayList JavaDoc());
257         }
258
259         return map;
260     }
261
262
263     /**
264      * Populate the event map
265      *
266      * @param layout
267      * @param map
268      * @param name
269      * @param parentEvents
270      */

271     private List JavaDoc populate(CompositeLayout layout, Map JavaDoc map, String JavaDoc name, List JavaDoc parentEvents) {
272         List JavaDoc lhList = null;
273         for (int j = 0; j < layout.getSize(); j++) {
274             Item tab = layout.getItem(j);
275             ChangeAspectDataEvent event =
276                 new ChangeAspectDataEvent(layout, this.aspectName, new Integer JavaDoc(j));
277             StringBuffer JavaDoc label = new StringBuffer JavaDoc(name);
278             if (label.length() > 0) {
279                 label.append(".");
280             }
281             label.append((tab instanceof NamedItem) ? ((NamedItem) tab).getName() :
282                 Integer.toString(j));
283             List JavaDoc events = new ArrayList JavaDoc(parentEvents);
284             events.add(event);
285             Layout child = tab.getLayout();
286             List JavaDoc allEvents = null;
287             if (child != null && child instanceof CompositeLayout) {
288                 allEvents = populate((CompositeLayout) child, map, label.toString(), events);
289             }
290             if (this.nonStickyTabs) {
291                 // With non-sticky tabs the non-leaf nodes always display
292
// the left-most child tabs
293
if (lhList == null)
294                 {
295                     if (allEvents != null)
296                     {
297                         lhList = allEvents;
298                     }
299                     else
300                     {
301                         lhList = events;
302                     }
303                 }
304                 if (allEvents != null) {
305                     map.put(label.toString(), allEvents);
306                 } else {
307                     map.put(label.toString(), events);
308                 }
309             } else {
310                 map.put(label.toString(), events);
311             }
312         }
313         return lhList;
314     }
315 }
Popular Tags