2a27c5d4ac26238dbede7f5879549ba14fc6ef47
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / network / navigation / NavigationItem.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality.
3  * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 3 of the GNU Lesser General Public License
7  * or later as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.commons.network.navigation;
11
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16
17 import eu.svjatoslav.commons.network.Locale;
18 import eu.svjatoslav.commons.string.WildCardMatcher;
19
20 public class NavigationItem {
21
22         private String matchingPattern;
23         private final Map<Locale, String> localeToTitle = new HashMap<Locale, String>();
24         final ArrayList<NavigationItem> subElements = new ArrayList<NavigationItem>();
25         final private NavigationItem parent;
26         final private Navigation navigation;
27         private final String linkUrl;
28
29         /**
30          * CSS prefix is inherited to child menu items.
31          */
32         private String cssPrefix;
33
34         public NavigationItem(final Navigation navigation) {
35                 this.navigation = navigation;
36                 parent = null;
37                 matchingPattern = null;
38                 linkUrl = null;
39         }
40
41         private NavigationItem(final Navigation navigation,
42                         final NavigationItem parent, final String linkUrl,
43                         final String... titles) {
44                 this.navigation = navigation;
45                 this.parent = parent;
46                 this.linkUrl = linkUrl;
47                 matchingPattern = linkUrl;
48
49                 initializeLocalizedTitles(titles);
50         }
51
52         public NavigationItem addChild(final String linkUrl, final String... titles) {
53                 final NavigationItem item = new NavigationItem(navigation, this,
54                                 linkUrl, titles);
55                 subElements.add(item);
56                 return item;
57         }
58
59         protected String getCssPrefix() {
60                 if (cssPrefix != null)
61                         return cssPrefix;
62
63                 if (parent == null)
64                         return null;
65
66                 return parent.getCssPrefix();
67         }
68
69         public String getLinkUrl() {
70                 return linkUrl;
71         }
72
73         NavigationItem getMatchingNavigationItem(final String requestPath) {
74                 if (matchesUrl(requestPath))
75                         return this;
76
77                 for (final NavigationItem childNavigationItem : subElements) {
78                         final NavigationItem match = childNavigationItem
79                                         .getMatchingNavigationItem(requestPath);
80
81                         if (match != null)
82                                 return match;
83                 }
84                 return null;
85         }
86
87         public List<NavigationItem> getSubElements() {
88                 return subElements;
89         }
90
91         public String getTitle(final Locale locale) {
92                 return localeToTitle.get(locale);
93         }
94
95         private void initializeLocalizedTitles(final String... titles) {
96                 final List<Locale> locales = navigation.getLocaleConfiguration()
97                                 .getAllowedLocales();
98                 if (locales.size() != titles.length)
99                         throw new RuntimeException("There should be exactly "
100                                         + locales.size() + " title(s).");
101
102                 for (int i = 0; i < titles.length; i++)
103                         localeToTitle.put(locales.get(i), titles[i]);
104         }
105
106         public boolean matchesUrl(final String url) {
107                 return WildCardMatcher.match(url, matchingPattern);
108         }
109
110         public void setCssPrefix(final String cssPrefix) {
111                 this.cssPrefix = cssPrefix;
112         }
113
114         public NavigationItem setPattern(final String pattern) {
115                 matchingPattern = pattern;
116                 return this;
117         }
118
119 }