001    /*
002     * SiteMap.java
003     */
004    
005    package net.sf.tacos.components;
006    
007    import java.util.List;
008    import net.sf.tacos.services.CategoryInfo;
009    import net.sf.tacos.services.SiteMap;
010    import org.apache.tapestry.BaseComponent;
011    
012    /**
013     * Useful methods for generating a site map.
014     * @author andyhot
015     */
016    public abstract class SiteMapComponent extends BaseComponent
017    {
018        public abstract SiteMap getSiteMap();
019        public abstract String getName();
020        public abstract String getCategory();    
021        
022        public abstract boolean getShowAll();
023        public abstract boolean getIgnoreCurrentPage();
024        
025        /**
026         * If the sitemap should be shown.
027         */ 
028        public boolean getShowSiteMap()
029        {
030            return getIgnoreCurrentPage() 
031                || (getCurrentPageCategory() != null);
032        }
033        
034        /**
035         * The category where the given page belongs.
036         */ 
037        public CategoryInfo getCategoryOf(String page)
038        {
039            return getSiteMap().getCategoryFromPage(page);
040        }
041        
042        /**
043         * The category of the currently displaying page.
044         */ 
045        public CategoryInfo getCurrentPageCategory()
046        {
047            return getCategoryOf(getPage().getPageName());
048        }
049        
050        /**
051         * If the given page is part of the currently rendering category.
052         */ 
053        public boolean isPageInCategory(String page)
054        {
055            return getSiteMap().inCategory(page, getCategory());
056        }
057        
058        /**
059         * If the currently displaying page is part of the currently rendering category.
060         */     
061        public boolean isCurrentPageInCategory()
062        {
063            return isPageInCategory(getPage().getPageName());
064        }    
065        
066        /**
067         * The default page for the currently rendering category.
068         */      
069        public String getDefaultPage()
070        {
071            return getSiteMap().getDefaultPage(getCategory()).getName();
072        }
073        
074        /**
075         * The css class to be applied for the currently rendering category.
076         * It is 'current' if the category in question contains the displaying page.
077         */      
078        public String getCategoryClass()
079        {
080            return isCurrentPageInCategory() ? "current" : null;
081        }
082        
083        /**
084         * The display name for the currently rendering category.
085         */ 
086        public String getCategoryDisplayName()
087        {
088            if (getShowAll())
089                return getSiteMap().getCategoryInfo(getCategory()).getName();
090            else
091                return getSiteMap().getDefaultPageDesc(getCategory());
092        }
093        
094        /**
095         * The display name for the currently rendering page.
096         */     
097        public String getPageDisplayName()
098        {
099            return getSiteMap().getPageInfo(getName()).getDesc();
100        }    
101        
102        /**
103         * Gets the pages that belong to the same category as the given one.
104         */      
105        public List getPagesOfCategoryOfPage(String page)
106        {
107            return getSiteMap().getCategoryPages(page);
108        }
109        
110        /**
111         * Gets the pages that belong to the same category as the currently displaying one.
112         */     
113        public List getPagesOfCategoryOfCurrentPage()
114        {
115            return getPagesOfCategoryOfPage(getPage().getPageName());
116        } 
117        
118        /**
119         * Gets the pages of the given category.
120         */     
121        public List getPagesOfCategory(String category)
122        {
123            return getSiteMap().getCategoryInfo(category).getPageNames();
124        }     
125        
126        /**
127         * Gets the pages of the currently rendering category.
128         */     
129        public List getPagesOfCurrentCategory()
130        {
131            return getPagesOfCategory(getCategory());
132        }     
133        
134        public boolean getShowPage()
135        {
136            if (getShowAll())
137                return true;
138            else
139                return !getName().equals( getCategoryOf(getPage().getPageName()).getDefaultPage() );
140        }
141        
142        /**
143         * The css class to be applied for the currently rendering page.
144         * It is 'here' if the page in question is in fact the one currently displayed.
145         */    
146        public String getPageClass()
147        {
148            String name = getName();
149            String pageName = getPage().getPageName();
150            
151            boolean selected = name.equals(pageName) 
152                || getSiteMap().contains(name, pageName);
153            return selected ? "here" : null;
154        }
155    }