/*
- * 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.
+ * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
+ * This project is released under Creative Commons Zero (CC0) license.
*/
-
package eu.svjatoslav.commons.network.navigation;
+import eu.svjatoslav.commons.string.GlobMatcher;
+
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
-
-import eu.svjatoslav.commons.string.WildCardMatcher;
public class NavigationItem {
- private String matchingPattern;
-
- private final Map<String, String> localeToTitle = new HashMap<String, String>();
- private final ArrayList<NavigationItem> subElements = new ArrayList<NavigationItem>();
- private final NavigationItem parent;
- private final Navigation<NavigationItem> navigation;
- private final String linkUrl;
- /**
- * CSS prefix is inherited to child menu items.
- */
- private String cssPrefix;
-
- public NavigationItem(final Navigation<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 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<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 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 + "]";
- }
-
+ private final ArrayList<NavigationItem> children = new ArrayList<>();
+ private final String url;
+ private String pattern;
+ private String title;
+ private boolean isDefault;
+
+ public NavigationItem(final String url, String pattern, String title ) {
+ this.url = url;
+ this.pattern = pattern;
+ this.title = title;
+ }
+
+ public NavigationItem setDefault(){
+ isDefault = true;
+ return this;
+ }
+
+ public boolean isDefault(){
+ return isDefault;
+ }
+
+ public void add(final NavigationItem navigationItem) {
+ children.add(navigationItem);
+ }
+
+ public String getUrl() {
+ return url;
+ }
+
+ NavigationItem getMatch(final String requestPath) {
+ if (matchesUrl(requestPath))
+ return this;
+
+ for (final NavigationItem childNavigationItem : children) {
+ final NavigationItem match = childNavigationItem.getMatch(requestPath);
+
+ if (match != null)
+ return match;
+ }
+ return null;
+ }
+
+ public List<NavigationItem> getChildren() {
+ return children;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public boolean matchesUrl(final String url) {
+ return GlobMatcher.match(url, pattern);
+ }
+
+ public NavigationItem getDefaultNavigationItem() {
+ for (NavigationItem child : children)
+ if (child.isDefault())
+ return child;
+ return null;
+ }
}