1 16 package org.apache.cocoon.portal.layout.renderer.aspect.impl; 17 18 import java.util.Collections ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 import java.util.List ; 22 import java.util.ArrayList ; 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 ; 37 import org.xml.sax.SAXException ; 38 39 113 public class TabContentAspect 114 extends CompositeContentAspect { 115 116 120 public boolean isRequired() { 121 return true; 122 } 123 124 127 public void toSAX(RendererAspectContext context, 128 Layout layout, 129 PortalService service, 130 ContentHandler handler) 131 throws SAXException { 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 Integer data = (Integer ) layout.getAspectData(config.aspectName); 144 int selected = data.intValue(); 145 146 for (int j = 0; j < tabLayout.getSize(); j++) { 148 Item tab = tabLayout.getItem(j); 149 ChangeAspectDataEvent event = null; 150 151 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 (j)); 160 attributes.addCDATAAttribute("parameter", service.getComponentManager().getLinkService().getLinkURI(event)); 161 162 final Iterator iter = tab.getParameters().entrySet().iterator(); 164 while ( iter.hasNext() ) { 165 final Map.Entry entry = (Map.Entry ) iter.next(); 166 attributes.addCDATAAttribute((String )entry.getKey(), (String )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 events = new ArrayList (); 174 events.add(event); 175 this.processNav(context, tab.getLayout(), service, handler, events); 176 } 177 } else if (config.showAllNav) { 178 List events = new ArrayList (); 179 events.add(event); 180 this.processNav(context, tab.getLayout(), service, handler, events); 181 } 182 183 XMLUtils.endElement(handler, "named-item"); 185 } 186 187 if ( config.rootTag ) { 188 XMLUtils.endElement(handler, config.tagName); 189 } 190 } else { 191 throw new SAXException ("Wrong layout type, TabLayout expected: " + layout.getClass().getName()); 192 } 193 } 194 195 199 public Iterator getAspectDescriptions(Object 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 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 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 243 private void processNav(RendererAspectContext context, 244 Layout layout, 245 PortalService service, 246 ContentHandler handler, 247 List parentEvents) 248 throws SAXException { 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 for (int j = 0; j < tabLayout.getSize(); j++) { 262 Item tab = tabLayout.getItem(j); 263 264 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 (j)); 275 List events = new ArrayList (parentEvents); 276 events.add(event); 277 278 attributes.addCDATAAttribute("parameter", 279 service.getComponentManager().getLinkService().getLinkURI(events)); 280 281 final Iterator iter = tab.getParameters().entrySet().iterator(); 283 while (iter.hasNext()) { 284 final Map.Entry entry = (Map.Entry ) iter.next(); 285 attributes.addCDATAAttribute((String ) entry.getKey(), 286 (String ) entry.getValue()); 287 } 288 289 XMLUtils.startElement(handler, "named-item", attributes); 290 291 this.processNav(context, tab.getLayout(), service, handler, events); 292 293 XMLUtils.endElement(handler, "named-item"); 295 } 296 297 298 } 299 if (subNav) { 301 XMLUtils.endElement(handler, config.childTagName); 302 } 303 } 304 } 305 306 protected class TabPreparedConfiguration extends PreparedConfiguration { 307 public String aspectName; 308 public String store; 309 public boolean showAllNav = false; 310 public boolean includeSelected = false; 311 public String 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 326 public Object 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 |