From: Svjatoslav Agejenko Date: Sat, 18 Oct 2014 15:18:01 +0000 (+0300) Subject: slightly more type-safe and extending friendly navigation X-Git-Tag: svjatoslavcommons-1.8~94 X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=eed1c70de986cf74cf15de291abd7495e4a6bcd1;p=svjatoslav_commons.git slightly more type-safe and extending friendly navigation --- diff --git a/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java b/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java index 1bdf378..6ef5e68 100755 --- a/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java +++ b/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java @@ -11,17 +11,18 @@ package eu.svjatoslav.commons.network.navigation; import java.net.MalformedURLException; import java.net.URL; +import java.util.List; import javax.servlet.http.HttpServletRequest; import eu.svjatoslav.commons.network.Locale; import eu.svjatoslav.commons.network.LocaleConfiguration; -public class Navigation { +public class Navigation { - private NavigationItem rootNavigationItem; + private NI rootNavigationItem; - private NavigationItem defaultNavigationItem; + private NI defaultNavigationItem; protected final LocaleConfiguration localeConfiguration; @@ -29,7 +30,7 @@ public class Navigation { this.localeConfiguration = localeConfiguration; } - public NavigationItem getDefaultNavigationItem() { + public NI getDefaultNavigationItem() { return defaultNavigationItem; } @@ -44,17 +45,17 @@ public class Navigation { return localeConfiguration; } - public NavigationItem getRootNavigationItem() { + public NI getRootNavigationItem() { return rootNavigationItem; } - public NavigationItem getSelectedItem(final HttpServletRequest request) { + public NI getSelectedItem(final HttpServletRequest request) { final String requestUrl = request.getRequestURL().toString(); try { final String requestPath = new URL(requestUrl).getPath(); - final NavigationItem match = rootNavigationItem + final NI match = (NI) rootNavigationItem .getMatchingNavigationItem(requestPath); if (match != null) @@ -72,14 +73,14 @@ public class Navigation { final Locale currentLocale = localeConfiguration .detectCurrentLocale(request); - final NavigationItem selectedItem = getSelectedItem(request); + final NI selectedItem = getSelectedItem(request); final StringBuffer result = new StringBuffer(); result.append("
"); - for (final NavigationItem item : getRootNavigationItem() + for (final NI item : (List) getRootNavigationItem() .getSubElements()) { if (item == selectedItem) @@ -101,14 +102,12 @@ public class Navigation { return result.toString(); } - public NavigationItem setDefaultNavigationItem( - final NavigationItem defaultNavigationItem) { + public NI setDefaultNavigationItem(final NI defaultNavigationItem) { this.defaultNavigationItem = defaultNavigationItem; return defaultNavigationItem; } - public NavigationItem setRootNavigationItem( - final NavigationItem rootNavigationItem) { + public NI setRootNavigationItem(final NI rootNavigationItem) { this.rootNavigationItem = rootNavigationItem; return rootNavigationItem; } diff --git a/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java b/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java index 2a27c5d..76a7d07 100755 --- a/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java +++ b/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java @@ -17,43 +17,41 @@ import java.util.Map; import eu.svjatoslav.commons.network.Locale; import eu.svjatoslav.commons.string.WildCardMatcher; -public class NavigationItem { +public class NavigationItem { private String matchingPattern; + private final Map localeToTitle = new HashMap(); - final ArrayList subElements = new ArrayList(); - final private NavigationItem parent; - final private Navigation navigation; + private final ArrayList subElements = new ArrayList(); + 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 Navigation navigation) { + public NavigationItem(final N navigation) { this.navigation = navigation; parent = null; matchingPattern = null; linkUrl = null; } - private NavigationItem(final Navigation navigation, - final NavigationItem parent, final String linkUrl, + public NavigationItem(final NI parent, final String linkUrl, final String... titles) { - this.navigation = navigation; + this.navigation = (N) parent.getNavigation(); this.parent = parent; this.linkUrl = linkUrl; matchingPattern = linkUrl; initializeLocalizedTitles(titles); + + parent.addNavigationItem(this); } - public NavigationItem addChild(final String linkUrl, final String... titles) { - final NavigationItem item = new NavigationItem(navigation, this, - linkUrl, titles); - subElements.add(item); - return item; + public void addNavigationItem(final NI navigationItem) { + subElements.add(navigationItem); } protected String getCssPrefix() { @@ -70,12 +68,12 @@ public class NavigationItem { return linkUrl; } - NavigationItem getMatchingNavigationItem(final String requestPath) { + NI getMatchingNavigationItem(final String requestPath) { if (matchesUrl(requestPath)) - return this; + return (NI) this; - for (final NavigationItem childNavigationItem : subElements) { - final NavigationItem match = childNavigationItem + for (final NI childNavigationItem : subElements) { + final NI match = (NI) childNavigationItem .getMatchingNavigationItem(requestPath); if (match != null) @@ -84,7 +82,11 @@ public class NavigationItem { return null; } - public List getSubElements() { + public N getNavigation() { + return navigation; + } + + public List getSubElements() { return subElements; } @@ -93,7 +95,7 @@ public class NavigationItem { } private void initializeLocalizedTitles(final String... titles) { - final List locales = navigation.getLocaleConfiguration() + final List locales = getNavigation().getLocaleConfiguration() .getAllowedLocales(); if (locales.size() != titles.length) throw new RuntimeException("There should be exactly " @@ -116,4 +118,12 @@ public class NavigationItem { return this; } + @Override + public String toString() { + return "NavigationItem [matchingPattern=" + matchingPattern + + ", localeToTitle=" + localeToTitle + ", subElements count=" + + subElements.size() + ", linkUrl=" + linkUrl + ", cssPrefix=" + + cssPrefix + "]"; + } + }