improved navigation
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Thu, 2 Jul 2015 16:49:21 +0000 (19:49 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Thu, 2 Jul 2015 16:49:21 +0000 (19:49 +0300)
src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java
src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java

index ff16177..2c5aa27 100755 (executable)
@@ -55,8 +55,7 @@ public class Navigation<NI extends NavigationItem> {
                        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<NI extends NavigationItem> {
        @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("<div class=\""
-                               + getGetNotNullString(rootNavigationItem.getCssPrefix())
-                               + "navigationMenu\">");
+               result.append("<div class=\"" + getGetNotNullString(rootNavigationItem.getCssPrefix()) + "navigationMenu\">");
 
-               for (final NI item : (List<NI>) getRootNavigationItem()
-                               .getSubElements()) {
+               for (final NI item : (List<NI>) getRootNavigationItem().getSubElements()) {
 
                        if (item == selectedItem)
-                               result.append("<a class=\""
-                                               + getGetNotNullString(item.getCssPrefix())
-                                               + "menuItemSelected\" href=\"" + item.getLinkUrl()
-                                               + "\">");
+                               result.append("<a class=\"" + getGetNotNullString(item.getCssPrefix()) + "menuItemSelected\" href=\""
+                                               + item.getLinkUrl() + "\">");
                        else
-                               result.append("<a class=\""
-                                               + getGetNotNullString(item.getCssPrefix())
-                                               + "menuItem\" href=\"" + item.getLinkUrl() + "\">");
+                               result.append("<a class=\"" + getGetNotNullString(item.getCssPrefix()) + "menuItem\" href=\""
+                                               + item.getLinkUrl() + "\">");
 
                        result.append("<div>" + item.getTitle(currentLocale) + "</div>");
                        result.append("</a>");
index 0152c26..661ed3f 100755 (executable)
@@ -23,22 +23,25 @@ public class NavigationItem {
        private final Map<String, String> localeToTitle = new HashMap<String, String>();
        private final ArrayList<NavigationItem> subElements = new ArrayList<NavigationItem>();
        private final NavigationItem parent;
-       private final Navigation<NavigationItem> navigation;
+       private final Navigation<? extends NavigationItem> navigation;
        private final String linkUrl;
        /**
         * CSS prefix is inherited to child menu items.
         */
        private String cssPrefix;
 
-       public NavigationItem(final Navigation<NavigationItem> navigation) {
+       /**
+        * Used to build root navigation item.
+        */
+       public NavigationItem(final Navigation<? extends NavigationItem> 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<NavigationItem> getNavigation() {
+       public Navigation<? extends NavigationItem> getNavigation() {
                return navigation;
        }
 
@@ -94,11 +96,9 @@ public class NavigationItem {
        }
 
        private void initializeLocalizedTitles(final String... titles) {
-               final List<String> locales = getNavigation().getLocaleConfiguration()
-                               .getAllowedLocales();
+               final List<String> 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
+                               + "]";
        }
 
 }