2 * Svjatoslav Commons - shared library of common functionality.
3 * Copyright ©2012-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
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.
10 package eu.svjatoslav.commons.network.navigation;
12 import eu.svjatoslav.commons.string.WildCardMatcher;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
19 public class NavigationItem {
21 private final Map<String, String> localeToTitle = new HashMap<>();
22 private final ArrayList<NavigationItem> subElements = new ArrayList<>();
23 private final NavigationItem parent;
24 private final Navigation<? extends NavigationItem> navigation;
25 private final String linkUrl;
26 private String matchingPattern;
28 * CSS prefix is inherited to child menu items.
30 private String cssPrefix;
33 * Used to build root navigation item.
35 public NavigationItem(final Navigation<? extends NavigationItem> navigation) {
36 this.navigation = navigation;
38 matchingPattern = null;
42 public NavigationItem(final NavigationItem parent, final String linkUrl, final String... titles) {
44 navigation = parent.getNavigation();
46 this.linkUrl = linkUrl;
47 matchingPattern = linkUrl;
49 initializeLocalizedTitles(titles);
51 parent.addNavigationItem(this);
54 public void addNavigationItem(final NavigationItem navigationItem) {
55 subElements.add(navigationItem);
58 protected String getCssPrefix() {
59 if (cssPrefix != null)
65 return parent.getCssPrefix();
68 public void setCssPrefix(final String cssPrefix) {
69 this.cssPrefix = cssPrefix;
72 public String getLinkUrl() {
76 NavigationItem getMatchingNavigationItem(final String requestPath) {
77 if (matchesUrl(requestPath))
80 for (final NavigationItem childNavigationItem : subElements) {
81 final NavigationItem match = childNavigationItem.getMatchingNavigationItem(requestPath);
89 public Navigation<? extends NavigationItem> getNavigation() {
93 public List<NavigationItem> getSubElements() {
97 public String getTitle(final String locale) {
98 return localeToTitle.get(locale);
101 private void initializeLocalizedTitles(final String... titles) {
102 final List<String> locales = getNavigation().getLocaleConfiguration().getAllowedLocales();
103 if (locales.size() != titles.length)
104 throw new RuntimeException("There should be exactly " + locales.size() + " title(s).");
106 for (int i = 0; i < titles.length; i++)
107 localeToTitle.put(locales.get(i), titles[i]);
110 public boolean matchesUrl(final String url) {
111 return WildCardMatcher.match(url, matchingPattern);
114 public NavigationItem setPattern(final String pattern) {
115 matchingPattern = pattern;
120 public String toString() {
121 return "NavigationItem [matchingPattern=" + matchingPattern + ", localeToTitle=" + localeToTitle
122 + ", subElements count=" + subElements.size() + ", linkUrl=" + linkUrl + ", cssPrefix=" + cssPrefix