From: Svjatoslav Agejenko Date: Sun, 21 Apr 2013 13:18:13 +0000 (+0300) Subject: reusable logic for web site navigation X-Git-Tag: svjatoslavcommons-1.8~149 X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=a2d7f6f310a1c44e15574b4acf056894b4ed4b33;p=svjatoslav_commons.git reusable logic for web site navigation --- diff --git a/pom.xml b/pom.xml index 2cfd823..127bbe7 100755 --- a/pom.xml +++ b/pom.xml @@ -78,18 +78,18 @@ svjatoslav.eu svjatoslav.eu - scp://svjatoslav.eu:7022/var/www/maven + scp://svjatoslav.eu/var/www/svjatoslav.eu/maven svjatoslav.eu svjatoslav.eu - scp://svjatoslav.eu:7022/var/www/maven + scp://svjatoslav.eu/var/www/svjatoslav.eu/maven - scm:git:ssh://git@svjatoslav.eu:7022/home/git/repositories/svjatoslav_commons.git - scm:git:ssh://git@svjatoslav.eu:7022/home/git/repositories/svjatoslav_commons.git + scm:git:ssh://git@svjatoslav.eu/home/git/repositories/svjatoslav_commons.git + scm:git:ssh://git@svjatoslav.eu/home/git/repositories/svjatoslav_commons.git diff --git a/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java b/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java new file mode 100755 index 0000000..7a1f711 --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java @@ -0,0 +1,60 @@ +package eu.svjatoslav.commons.network.navigation; + +import java.net.MalformedURLException; +import java.net.URL; + +public class Navigation { + + private NavigationItem rootNavigationItem; + + private NavigationItem defaultNavigationItem; + + public NavigationItem getRootNavigationItem() { + return rootNavigationItem; + } + + public NavigationItem getSelectedItem(final String requestUrl) { + + try { + final String requestPath = new URL(requestUrl).getPath(); + + for (final NavigationItem item : rootNavigationItem.subElements) + if (item.matchesUrl(requestPath)) + return item; + } catch (final MalformedURLException e) { + System.out.println("Cannot handle URL: " + requestUrl); + e.printStackTrace(); + } + + return defaultNavigationItem; + } + + public String getTopMenu(final String requestUrl) { + + final NavigationItem selectedItem = getSelectedItem(requestUrl); + final StringBuffer result = new StringBuffer(); + + result.append("
"); + + for (final NavigationItem item : rootNavigationItem.getSubElements()) { + + if (item == selectedItem) + result.append(""); + else + result.append(""); + + result.append(item.getTitle()); + result.append(""); + } + + result.append("
"); + + return result.toString(); + } + + public void setRootNavigationItem(final NavigationItem rootNavigationItem) { + this.rootNavigationItem = 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 new file mode 100755 index 0000000..6392fa9 --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java @@ -0,0 +1,51 @@ +package eu.svjatoslav.commons.network.navigation; + +import java.util.ArrayList; +import java.util.List; + +import eu.svjatoslav.commons.string.WildCardMatcher; + +public class NavigationItem { + + private final String title; + private final String link; + private final String matchingWildcard; + + ArrayList subElements = new ArrayList(); + + public NavigationItem(final String title, final String link) { + this.title = title; + this.link = link; + matchingWildcard = link; + } + + public NavigationItem(final String title, final String link, + final String wildcard) { + this.title = title; + this.link = link; + matchingWildcard = wildcard; + } + + public void addSubNavigation(final NavigationItem item) { + subElements.add(item); + } + + public String getLink() { + return link; + } + + public List getSubElements() { + return subElements; + } + + public String getTitle() { + return title; + } + + public boolean matchesUrl(final String url) { + + return WildCardMatcher.match(url, matchingWildcard); + + } + +}