View Javadoc

1   /*
2    * SiteMap.java
3    */
4   
5   package net.sf.tacos.components;
6   
7   import java.util.List;
8   import net.sf.tacos.services.CategoryInfo;
9   import net.sf.tacos.services.SiteMap;
10  import org.apache.tapestry.BaseComponent;
11  
12  /**
13   * Useful methods for generating a site map.
14   * @author andyhot
15   */
16  public abstract class SiteMapComponent extends BaseComponent
17  {
18      public abstract SiteMap getSiteMap();
19      public abstract String getName();
20      public abstract String getCategory();    
21      
22      public abstract boolean getShowAll();
23      public abstract boolean getIgnoreCurrentPage();
24      
25      /**
26       * If the sitemap should be shown.
27       */ 
28      public boolean getShowSiteMap()
29      {
30          return getIgnoreCurrentPage() 
31              || (getCurrentPageCategory() != null);
32      }
33      
34      /**
35       * The category where the given page belongs.
36       */ 
37      public CategoryInfo getCategoryOf(String page)
38      {
39          return getSiteMap().getCategoryFromPage(page);
40      }
41      
42      /**
43       * The category of the currently displaying page.
44       */ 
45      public CategoryInfo getCurrentPageCategory()
46      {
47          return getCategoryOf(getPage().getPageName());
48      }
49      
50      /**
51       * If the given page is part of the currently rendering category.
52       */ 
53      public boolean isPageInCategory(String page)
54      {
55          return getSiteMap().inCategory(page, getCategory());
56      }
57      
58      /**
59       * If the currently displaying page is part of the currently rendering category.
60       */     
61      public boolean isCurrentPageInCategory()
62      {
63          return isPageInCategory(getPage().getPageName());
64      }    
65      
66      /**
67       * The default page for the currently rendering category.
68       */      
69      public String getDefaultPage()
70      {
71          return getSiteMap().getDefaultPage(getCategory()).getName();
72      }
73      
74      /**
75       * The css class to be applied for the currently rendering category.
76       * It is 'current' if the category in question contains the displaying page.
77       */      
78      public String getCategoryClass()
79      {
80          return isCurrentPageInCategory() ? "current" : null;
81      }
82      
83      /**
84       * The display name for the currently rendering category.
85       */ 
86      public String getCategoryDisplayName()
87      {
88          if (getShowAll())
89              return getSiteMap().getCategoryInfo(getCategory()).getName();
90          else
91              return getSiteMap().getDefaultPageDesc(getCategory());
92      }
93      
94      /**
95       * The display name for the currently rendering page.
96       */     
97      public String getPageDisplayName()
98      {
99          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 }