slightly more type-safe and extending friendly navigation
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / network / navigation / NavigationItem.java
index 6392fa9..76a7d07 100755 (executable)
+/*
+ * Svjatoslav Commons - shared library of common functionality.
+ * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 3 of the GNU Lesser General Public License
+ * or later as published by the Free Software Foundation.
+ */
+
 package eu.svjatoslav.commons.network.navigation;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
+import eu.svjatoslav.commons.network.Locale;
 import eu.svjatoslav.commons.string.WildCardMatcher;
 
-public class NavigationItem {
+public class NavigationItem<NI extends NavigationItem, N extends Navigation> {
+
+       private String matchingPattern;
+
+       private final Map<Locale, String> localeToTitle = new HashMap<Locale, String>();
+       private final ArrayList<NI> subElements = new ArrayList<NI>();
+       private final NI parent;
+       private final N navigation;
+       private final String linkUrl;
+       /**
+        * CSS prefix is inherited to child menu items.
+        */
+       private String cssPrefix;
+
+       public NavigationItem(final N navigation) {
+               this.navigation = navigation;
+               parent = null;
+               matchingPattern = null;
+               linkUrl = null;
+       }
+
+       public NavigationItem(final NI parent, final String linkUrl,
+                       final String... titles) {
+               this.navigation = (N) parent.getNavigation();
+               this.parent = parent;
+               this.linkUrl = linkUrl;
+               matchingPattern = linkUrl;
+
+               initializeLocalizedTitles(titles);
+
+               parent.addNavigationItem(this);
+       }
+
+       public void addNavigationItem(final NI navigationItem) {
+               subElements.add(navigationItem);
+       }
 
-       private final String title;
-       private final String link;
-       private final String matchingWildcard;
+       protected String getCssPrefix() {
+               if (cssPrefix != null)
+                       return cssPrefix;
 
-       ArrayList<NavigationItem> subElements = new ArrayList<NavigationItem>();
+               if (parent == null)
+                       return null;
 
-       public NavigationItem(final String title, final String link) {
-               this.title = title;
-               this.link = link;
-               matchingWildcard = link;
+               return parent.getCssPrefix();
        }
 
-       public NavigationItem(final String title, final String link,
-                       final String wildcard) {
-               this.title = title;
-               this.link = link;
-               matchingWildcard = wildcard;
+       public String getLinkUrl() {
+               return linkUrl;
        }
 
-       public void addSubNavigation(final NavigationItem item) {
-               subElements.add(item);
+       NI getMatchingNavigationItem(final String requestPath) {
+               if (matchesUrl(requestPath))
+                       return (NI) this;
+
+               for (final NI childNavigationItem : subElements) {
+                       final NI match = (NI) childNavigationItem
+                                       .getMatchingNavigationItem(requestPath);
+
+                       if (match != null)
+                               return match;
+               }
+               return null;
        }
 
-       public String getLink() {
-               return link;
+       public N getNavigation() {
+               return navigation;
        }
 
-       public List<NavigationItem> getSubElements() {
+       public List<NI> getSubElements() {
                return subElements;
        }
 
-       public String getTitle() {
-               return title;
+       public String getTitle(final Locale locale) {
+               return localeToTitle.get(locale);
+       }
+
+       private void initializeLocalizedTitles(final String... titles) {
+               final List<Locale> locales = getNavigation().getLocaleConfiguration()
+                               .getAllowedLocales();
+               if (locales.size() != titles.length)
+                       throw new RuntimeException("There should be exactly "
+                                       + locales.size() + " title(s).");
+
+               for (int i = 0; i < titles.length; i++)
+                       localeToTitle.put(locales.get(i), titles[i]);
        }
 
        public boolean matchesUrl(final String url) {
+               return WildCardMatcher.match(url, matchingPattern);
+       }
 
-               return WildCardMatcher.match(url, matchingWildcard);
+       public void setCssPrefix(final String cssPrefix) {
+               this.cssPrefix = cssPrefix;
+       }
+
+       public NavigationItem setPattern(final String pattern) {
+               matchingPattern = pattern;
+               return this;
+       }
 
+       @Override
+       public String toString() {
+               return "NavigationItem [matchingPattern=" + matchingPattern
+                               + ", localeToTitle=" + localeToTitle + ", subElements count="
+                               + subElements.size() + ", linkUrl=" + linkUrl + ", cssPrefix="
+                               + cssPrefix + "]";
        }
 
 }