+/*
+ * Svjatoslav Commons - shared library of common functionality.
+ * Copyright ©2012-2017, 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 eu.svjatoslav.commons.string.WildCardMatcher;
+
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 {
- private final String matchingWildcard;
- private final Map<Locale, String> localeToTitle = new HashMap<Locale, String>();
- ArrayList<NavigationItem> subElements = new ArrayList<NavigationItem>();
- private Locale[] localeOrder;
- private NavigationItem parent;
- private final String linkUrl;
-
- public NavigationItem(final Locale... localeOrder) {
- this.localeOrder = localeOrder;
- matchingWildcard = null;
- linkUrl = null;
- }
-
- public NavigationItem(final NavigationItem parent, final String linkUrl,
- final String... titles) {
- this.parent = parent;
- this.linkUrl = linkUrl;
- matchingWildcard = linkUrl;
-
- initializeLocalizedTitles(titles);
- }
-
- public NavigationItem addChild(final String linkUrl, final String... titles) {
-
- final NavigationItem item = new NavigationItem(this, linkUrl, titles);
- subElements.add(item);
-
- return item;
- }
-
- public String getLinkUrl() {
- return linkUrl;
- }
-
- public Locale[] getLocaleOrder() {
- if (localeOrder != null)
- return localeOrder;
-
- return parent.getLocaleOrder();
- }
-
- public List<NavigationItem> getSubElements() {
- return subElements;
- }
-
- public String getTitle() {
- if (localeToTitle.size() != 1)
- throw new RuntimeException("there shall be exactly one title");
-
- return localeToTitle.values().iterator().next();
- }
-
- public String getTitle(final Locale locale) {
- return localeToTitle.get(locale);
- }
-
- private void initializeLocalizedTitles(final String... titles) {
- final Locale[] locales = getLocaleOrder();
- if (locales.length != titles.length)
- throw new RuntimeException("There should be exactly "
- + locales.length + " title(s).");
-
- for (int i = 0; i < titles.length; i++)
- localeToTitle.put(locales[i], titles[i]);
- }
-
- public boolean matchesUrl(final String url) {
- return WildCardMatcher.match(url, matchingWildcard);
-
- }
+ private final Map<String, String> localeToTitle = new HashMap<>();
+ private final ArrayList<NavigationItem> subElements = new ArrayList<>();
+ private final NavigationItem parent;
+ private final Navigation<? extends NavigationItem> navigation;
+ private final String linkUrl;
+ private String matchingPattern;
+ /**
+ * CSS prefix is inherited to child menu items.
+ */
+ private String cssPrefix;
+
+ /**
+ * Used to build root navigation item.
+ */
+ public NavigationItem(final Navigation<? extends NavigationItem> navigation) {
+ this.navigation = navigation;
+ parent = null;
+ matchingPattern = null;
+ linkUrl = null;
+ }
+
+ public NavigationItem(final NavigationItem parent, final String linkUrl, final String... titles) {
+
+ navigation = parent.getNavigation();
+ this.parent = parent;
+ this.linkUrl = linkUrl;
+ matchingPattern = linkUrl;
+
+ initializeLocalizedTitles(titles);
+
+ parent.addNavigationItem(this);
+ }
+
+ public void addNavigationItem(final NavigationItem navigationItem) {
+ subElements.add(navigationItem);
+ }
+
+ protected String getCssPrefix() {
+ if (cssPrefix != null)
+ return cssPrefix;
+
+ if (parent == null)
+ return null;
+
+ return parent.getCssPrefix();
+ }
+
+ public void setCssPrefix(final String cssPrefix) {
+ this.cssPrefix = cssPrefix;
+ }
+
+ public String getLinkUrl() {
+ return linkUrl;
+ }
+
+ NavigationItem getMatchingNavigationItem(final String requestPath) {
+ if (matchesUrl(requestPath))
+ return this;
+
+ for (final NavigationItem childNavigationItem : subElements) {
+ final NavigationItem match = childNavigationItem.getMatchingNavigationItem(requestPath);
+
+ if (match != null)
+ return match;
+ }
+ return null;
+ }
+
+ public Navigation<? extends NavigationItem> getNavigation() {
+ return navigation;
+ }
+
+ public List<NavigationItem> getSubElements() {
+ return subElements;
+ }
+
+ public String getTitle(final String locale) {
+ return localeToTitle.get(locale);
+ }
+
+ private void initializeLocalizedTitles(final String... titles) {
+ final List<String> 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);
+ }
+
+ 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
+ + "]";
+ }
}