From: Svjatoslav Agejenko Date: Tue, 19 Jun 2018 23:34:16 +0000 (+0300) Subject: Simplified navigation. X-Git-Tag: svjatoslavcommons-1.8~30 X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=f7500732a4ba759bebc5680d0027ae903312a7c9;p=svjatoslav_commons.git Simplified navigation. --- diff --git a/pom.xml b/pom.xml index b925251..090631d 100644 --- a/pom.xml +++ b/pom.xml @@ -120,7 +120,7 @@ scm:git:ssh://git@svjatoslav.eu/home/git/repositories/svjatoslav_commons.git scm:git:ssh://git@svjatoslav.eu/home/git/repositories/svjatoslav_commons.git - HEAD + HEAD diff --git a/src/main/java/eu/svjatoslav/commons/network/LocaleConfiguration.java b/src/main/java/eu/svjatoslav/commons/network/LocaleConfiguration.java deleted file mode 100755 index 447f530..0000000 --- a/src/main/java/eu/svjatoslav/commons/network/LocaleConfiguration.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Svjatoslav Commons - shared library of common functionality. - * Copyright ©2012-2017, 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; - -import javax.servlet.http.HttpServletRequest; -import java.util.ArrayList; -import java.util.List; - -public class LocaleConfiguration { - - final String defaultLocale; - private final List allowedLocales = new ArrayList<>(); - - public LocaleConfiguration(final String defaultLocale, - final String... allowedLocales) { - - this.defaultLocale = defaultLocale; - - for (final String locale : allowedLocales) - getAllowedLocales().add(locale); - } - - public String detectCurrentLocale(final HttpServletRequest request) { - - final String sessionLocaleString = (String) request.getSession() - .getAttribute("locale"); - - String result = defaultLocale; - if (isAllowedLocale(sessionLocaleString)) - result = sessionLocaleString; - - final String requestLocale = request.getParameter("locale"); - - if (isAllowedLocale(requestLocale)) - result = requestLocale; - - request.getSession().setAttribute("locale", result); - - return result; - } - - public List getAllowedLocales() { - return allowedLocales; - } - - public boolean isAllowedLocale(final String locale) { - return allowedLocales.contains(locale); - } - -} 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 e6ed8ef..2c23e1d 100755 --- a/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java +++ b/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java @@ -9,80 +9,54 @@ package eu.svjatoslav.commons.network.navigation; -import eu.svjatoslav.commons.network.LocaleConfiguration; - import javax.servlet.http.HttpServletRequest; import java.net.MalformedURLException; import java.net.URL; -import java.util.List; - -public class Navigation { - protected final LocaleConfiguration localeConfiguration; - private NI rootNavigationItem; - private NI defaultNavigationItem; +public class Navigation { - public Navigation(final LocaleConfiguration localeConfiguration) { - this.localeConfiguration = localeConfiguration; - } + private NavigationItem root = new NavigationItem(null, null, null); - public NI getDefaultNavigationItem() { - return defaultNavigationItem; + public NavigationItem getRoot(){ + return root; } - private String getGetNotNullString(final String string) { - if (string != null) - return string; - - return ""; - } - - public LocaleConfiguration getLocaleConfiguration() { - return localeConfiguration; - } - - public NI getRootNavigationItem() { - return rootNavigationItem; - } - - public NI getSelectedItem(final HttpServletRequest request) { + public NavigationItem getSelectedItem(final HttpServletRequest request) { final String requestUrl = request.getRequestURL().toString(); + final String requestPath; try { - final String requestPath = new URL(requestUrl).getPath(); + requestPath = new URL(requestUrl).getPath(); + } catch (MalformedURLException e) { + throw new IllegalArgumentException("Illegal request URL provided." , e); + } - @SuppressWarnings("unchecked") final NI match = (NI) rootNavigationItem.getMatchingNavigationItem(requestPath); + @SuppressWarnings("unchecked") final NavigationItem match = root.getMatch(requestPath); - if (match != null) - return match; - } catch (final MalformedURLException e) { - System.out.println("Cannot handle URL: " + requestUrl); - e.printStackTrace(); - } + if (match != null) + return match; - return getDefaultNavigationItem(); + return root.getDefaultNavigationItem(); } @SuppressWarnings("unchecked") public String getTopMenu(final HttpServletRequest request) { - final String currentLocale = localeConfiguration.detectCurrentLocale(request); - - final NI selectedItem = getSelectedItem(request); + final NavigationItem selectedItem = getSelectedItem(request); final StringBuilder result = new StringBuilder(); - result.append("
"); + result.append("
"); - for (final NI item : (List) getRootNavigationItem().getSubElements()) { + for (final NavigationItem item : root.getChildren()) { if (item == selectedItem) - result.append(""); + result.append(""); else - result.append(""); + result.append(""); - result.append("
" + item.getTitle(currentLocale) + "
"); + result.append("
" + item.getTitle() + "
"); result.append("
"); } @@ -91,14 +65,4 @@ public class Navigation { return result.toString(); } - public NI setDefaultNavigationItem(final NI defaultNavigationItem) { - this.defaultNavigationItem = defaultNavigationItem; - return defaultNavigationItem; - } - - 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 0edc44e..0359440 100755 --- a/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java +++ b/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java @@ -12,73 +12,45 @@ package eu.svjatoslav.commons.network.navigation; import eu.svjatoslav.commons.string.WildCardMatcher; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; 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 String linkUrl; - private String matchingPattern; - /** - * CSS prefix is inherited to child menu items. - */ - private String cssPrefix; - - /** - * 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) { - - navigation = parent.getNavigation(); - this.parent = parent; - this.linkUrl = linkUrl; - matchingPattern = linkUrl; + private final ArrayList children = new ArrayList<>(); + private final String url; + private String pattern; + private String title; + private boolean isDefault; - initializeLocalizedTitles(titles); - - parent.addNavigationItem(this); + public NavigationItem(final String url, String pattern, String title ) { + this.url = url; + this.pattern = pattern; + this.title = title; } - public void addNavigationItem(final NavigationItem navigationItem) { - subElements.add(navigationItem); + public NavigationItem setDefault(){ + isDefault = true; + return this; } - protected String getCssPrefix() { - if (cssPrefix != null) - return cssPrefix; - - if (parent == null) - return null; - - return parent.getCssPrefix(); + public boolean isDefault(){ + return isDefault; } - public void setCssPrefix(final String cssPrefix) { - this.cssPrefix = cssPrefix; + public void add(final NavigationItem navigationItem) { + children.add(navigationItem); } - public String getLinkUrl() { - return linkUrl; + public String getUrl() { + return url; } - NavigationItem getMatchingNavigationItem(final String requestPath) { + NavigationItem getMatch(final String requestPath) { if (matchesUrl(requestPath)) return this; - for (final NavigationItem childNavigationItem : subElements) { - final NavigationItem match = childNavigationItem.getMatchingNavigationItem(requestPath); + for (final NavigationItem childNavigationItem : children) { + final NavigationItem match = childNavigationItem.getMatch(requestPath); if (match != null) return match; @@ -86,41 +58,22 @@ public class NavigationItem { return null; } - public Navigation getNavigation() { - return navigation; - } - - public List getSubElements() { - return subElements; + public List getChildren() { + return children; } - public String getTitle(final String locale) { - return localeToTitle.get(locale); - } - - private void initializeLocalizedTitles(final String... titles) { - final List locales = getNavigation().getLocaleConfiguration().getAllowedLocales(); - if (locales.size() != titles.length) - 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]); + public String getTitle() { + return title; } public boolean matchesUrl(final String url) { - return WildCardMatcher.match(url, matchingPattern); - } - - public NavigationItem setPattern(final String pattern) { - matchingPattern = pattern; - return this; + return WildCardMatcher.match(url, pattern); } - @Override - public String toString() { - return "NavigationItem [matchingPattern=" + matchingPattern + ", localeToTitle=" + localeToTitle - + ", subElements count=" + subElements.size() + ", linkUrl=" + linkUrl + ", cssPrefix=" + cssPrefix - + "]"; + public NavigationItem getDefaultNavigationItem() { + for (NavigationItem child : children) + if (child.isDefault()) + return child; + return null; } - }