From 8983e9776024e1d1046279d1eb6f1f5e93df28b4 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Wed, 25 Dec 2013 01:10:07 +0200 Subject: [PATCH] refactored navigation logic to be more fool-proof --- .../network/navigation/Navigation.java | 15 ++-- .../network/navigation/NavigationItem.java | 69 ++++++++++++------- 2 files changed, 55 insertions(+), 29 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 42720c9..1afd5c7 100755 --- a/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java +++ b/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java @@ -3,6 +3,8 @@ package eu.svjatoslav.commons.network.navigation; import java.net.MalformedURLException; import java.net.URL; +import javax.servlet.http.HttpServletRequest; + public class Navigation { private NavigationItem rootNavigationItem; @@ -17,7 +19,8 @@ public class Navigation { return rootNavigationItem; } - public NavigationItem getSelectedItem(final String requestUrl) { + public NavigationItem getSelectedItem(final HttpServletRequest request) { + final String requestUrl = request.getRequestURL().toString(); try { final String requestPath = new URL(requestUrl).getPath(); @@ -33,9 +36,9 @@ public class Navigation { return getDefaultNavigationItem(); } - public String getTopMenu(final String requestUrl) { + public String getTopMenu(final HttpServletRequest request) { - final NavigationItem selectedItem = getSelectedItem(requestUrl); + final NavigationItem selectedItem = getSelectedItem(request); final StringBuffer result = new StringBuffer(); result.append("
"); @@ -45,10 +48,10 @@ public class Navigation { if (item == selectedItem) result.append(""); + + item.getLinkUrl() + "\">"); else - result.append(""); + result.append(""); result.append("
" + item.getTitle() + "
"); 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 fdf6985..d40ff57 100755 --- a/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java +++ b/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java @@ -10,37 +10,48 @@ import eu.svjatoslav.commons.string.WildCardMatcher; public class NavigationItem { - private final String link; - private final String matchingWildcard; - private final Map titles = new HashMap(); + private String matchingWildcard; + private final Map localeToTitle = new HashMap(); ArrayList subElements = new ArrayList(); - - public NavigationItem(final String link) { - this.link = link; - matchingWildcard = link; + private Locale[] localeOrder; + private NavigationItem parent; + private final String linkUrl; + + public NavigationItem(final Locale... localeOrder) { + if (localeOrder.length == 0) + this.localeOrder = new Locale[] { Locale.ENG }; + else + this.localeOrder = localeOrder; + + matchingWildcard = 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; + + 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); - } - public NavigationItem addTitle(final Locale locale, final String title) { - titles.put(locale, title); - return this; + return item; } - 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(); } public List getSubElements() { @@ -48,15 +59,27 @@ public class NavigationItem { } public String getTitle() { - return titles.get(Locale.ENG); + if (localeToTitle.size() != 1) + throw new RuntimeException("there shall be exactly one title"); + + return localeToTitle.values().iterator().next(); } public String getTitle(final Locale locale) { - return titles.get(locale); + return localeToTitle.get(locale); } - public boolean matchesUrl(final String url) { + 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, matchingWildcard); } -- 2.20.1