+/*
+ * 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;
public class NavigationItem {
- private final String link;
- private final String matchingWildcard;
- private final Map<Locale, String> titles = new HashMap<Locale, String>();
+ private String matchingPattern;
+ private final Map<Locale, String> localeToTitle = new HashMap<Locale, String>();
ArrayList<NavigationItem> subElements = new ArrayList<NavigationItem>();
-
- public NavigationItem(final String link) {
- this.link = link;
- matchingWildcard = link;
+ private Locale[] localeOrder;
+ private NavigationItem parent;
+ private final String linkUrl;
+
+ /**
+ * CSS prefix is inherited to child menu items.
+ */
+ private String cssPrefix;
+
+ public NavigationItem(final Locale... localeOrder) {
+ if (localeOrder.length == 0)
+ this.localeOrder = new Locale[] { Locale.ENG };
+ else
+ this.localeOrder = localeOrder;
+
+ matchingPattern = null;
+ linkUrl = null;
}
- public NavigationItem(final String link, final String wildcard) {
- this.link = link;
- matchingWildcard = wildcard;
+ public NavigationItem(final NavigationItem parent, final String linkUrl,
+ final String... titles) {
+ this.parent = parent;
+ this.linkUrl = linkUrl;
+ matchingPattern = linkUrl;
+
+ initializeLocalizedTitles(titles);
}
- public void addSubNavigation(final NavigationItem item) {
+ public NavigationItem addChild(final String linkUrl, final String... titles) {
+
+ final NavigationItem item = new NavigationItem(this, linkUrl, titles);
subElements.add(item);
+
+ return item;
}
- public NavigationItem addTitle(final Locale locale, final String title) {
- titles.put(locale, title);
- return this;
+ protected String getCssPrefix() {
+ if (cssPrefix != null)
+ return cssPrefix;
+
+ if (parent == null)
+ return null;
+
+ return parent.getCssPrefix();
}
- public NavigationItem addTitle(final String title) {
- titles.put(Locale.ENG, title);
- return this;
+ public String getLinkUrl() {
+ return linkUrl;
}
- public String getLink() {
- return link;
+ public Locale[] getLocaleOrder() {
+ if (localeOrder != null)
+ return localeOrder;
+
+ return parent.getLocaleOrder();
+ }
+
+ 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 List<NavigationItem> getSubElements() {
}
public String getTitle() {
- return titles.get(Locale.ENG);
+ if (localeToTitle.size() != 1)
+ throw new RuntimeException(
+ "NavigationItem.getTitle() was called without locale parameter, but there there are multiple titles in diffirent locales available.");
+
+ return localeToTitle.values().iterator().next();
}
public String getTitle(final Locale locale) {
- return titles.get(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, 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;
}
}