From b7ff4e43cfb83aabfd5cde8ac37e5115938b2c51 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Thu, 2 Jul 2015 19:49:21 +0300 Subject: [PATCH 1/1] improved navigation --- .../network/navigation/Navigation.java | 24 +++++---------- .../network/navigation/NavigationItem.java | 29 +++++++++---------- 2 files changed, 22 insertions(+), 31 deletions(-) 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 ff16177..2c5aa27 100755 --- a/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java +++ b/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java @@ -55,8 +55,7 @@ public class Navigation { final String requestPath = new URL(requestUrl).getPath(); @SuppressWarnings("unchecked") - final NI match = (NI) rootNavigationItem - .getMatchingNavigationItem(requestPath); + final NI match = (NI) rootNavigationItem.getMatchingNavigationItem(requestPath); if (match != null) return match; @@ -71,28 +70,21 @@ public class Navigation { @SuppressWarnings("unchecked") public String getTopMenu(final HttpServletRequest request) { - final String currentLocale = localeConfiguration - .detectCurrentLocale(request); + final String currentLocale = localeConfiguration.detectCurrentLocale(request); final NI selectedItem = getSelectedItem(request); final StringBuffer result = new StringBuffer(); - result.append("
"); + result.append("
"); - for (final NI item : (List) getRootNavigationItem() - .getSubElements()) { + for (final NI item : (List) getRootNavigationItem().getSubElements()) { if (item == selectedItem) - result.append(""); + result.append(""); else - result.append(""); + result.append(""); result.append("
" + item.getTitle(currentLocale) + "
"); result.append("
"); 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 0152c26..661ed3f 100755 --- a/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java +++ b/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java @@ -23,22 +23,25 @@ public class NavigationItem { private final Map localeToTitle = new HashMap(); private final ArrayList subElements = new ArrayList(); private final NavigationItem parent; - private final Navigation navigation; + private final Navigation navigation; private final String linkUrl; /** * CSS prefix is inherited to child menu items. */ private String cssPrefix; - public NavigationItem(final Navigation navigation) { + /** + * Used to build root navigation item. + */ + public NavigationItem(final Navigation navigation) { this.navigation = navigation; parent = null; matchingPattern = null; linkUrl = null; } - public NavigationItem(final NavigationItem parent, final String linkUrl, - final String... titles) { + public NavigationItem(final NavigationItem parent, final String linkUrl, final String... titles) { + navigation = parent.getNavigation(); this.parent = parent; this.linkUrl = linkUrl; @@ -72,8 +75,7 @@ public class NavigationItem { return this; for (final NavigationItem childNavigationItem : subElements) { - final NavigationItem match = childNavigationItem - .getMatchingNavigationItem(requestPath); + final NavigationItem match = childNavigationItem.getMatchingNavigationItem(requestPath); if (match != null) return match; @@ -81,7 +83,7 @@ public class NavigationItem { return null; } - public Navigation getNavigation() { + public Navigation getNavigation() { return navigation; } @@ -94,11 +96,9 @@ public class NavigationItem { } private void initializeLocalizedTitles(final String... titles) { - final List locales = getNavigation().getLocaleConfiguration() - .getAllowedLocales(); + final List locales = getNavigation().getLocaleConfiguration().getAllowedLocales(); if (locales.size() != titles.length) - throw new RuntimeException("There should be exactly " - + locales.size() + " title(s)."); + 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]); @@ -119,10 +119,9 @@ public class NavigationItem { @Override public String toString() { - return "NavigationItem [matchingPattern=" + matchingPattern - + ", localeToTitle=" + localeToTitle + ", subElements count=" - + subElements.size() + ", linkUrl=" + linkUrl + ", cssPrefix=" - + cssPrefix + "]"; + return "NavigationItem [matchingPattern=" + matchingPattern + ", localeToTitle=" + localeToTitle + + ", subElements count=" + subElements.size() + ", linkUrl=" + linkUrl + ", cssPrefix=" + cssPrefix + + "]"; } } -- 2.20.1