From e2d596152d5751c455c5903f0e472e011c60e016 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Tue, 5 Apr 2016 12:37:45 +0200 Subject: [PATCH] Added weights to navigation object --- src/wa/server/WAContext.java | 2 +- src/wa/server/page/struct/WANavigation.java | 42 +++++++++++++++++---- 2 files changed, 36 insertions(+), 8 deletions(-) mode change 100644 => 100755 src/wa/server/WAContext.java diff --git a/src/wa/server/WAContext.java b/src/wa/server/WAContext.java old mode 100644 new mode 100755 index 48b2242..428cd5e --- a/src/wa/server/WAContext.java +++ b/src/wa/server/WAContext.java @@ -43,7 +43,7 @@ public class WAContext { nav = WANavigation.getRootNav(); // Breadcrumb - breadcrumb = WANavigation.getNavBreadcrumb(request); + breadcrumb = WANavigation.getBreadcrumb(request); } diff --git a/src/wa/server/page/struct/WANavigation.java b/src/wa/server/page/struct/WANavigation.java index 86abcda..cdc382d 100755 --- a/src/wa/server/page/struct/WANavigation.java +++ b/src/wa/server/page/struct/WANavigation.java @@ -25,9 +25,12 @@ package wa.server.page.struct; import java.util.*; /** + * A class representing a navigation hierarchy. + * * Created by Ziver on 2015-04-02. */ public class WANavigation implements Iterable{ + private static final String NAVIGATION_URL_KEY = "i"; private static int nextId = 0; private static HashMap navMap = new HashMap(); private static WANavigation rootNav = new WANavigation(null); @@ -35,6 +38,7 @@ public class WANavigation implements Iterable{ private final int id; private String url; private String name; + private int weight; private WANavigation parentNav; private ArrayList subNav; private Object resource; @@ -43,7 +47,7 @@ public class WANavigation implements Iterable{ private WANavigation(String name) { this.id = nextId++; this.navMap.put(this.id, this); - this.url = "?i="+this.id; + this.url = "?"+NAVIGATION_URL_KEY+"="+this.id; this.name = name; this.subNav = new ArrayList<>(); } @@ -63,8 +67,16 @@ public class WANavigation implements Iterable{ } return null; } + private void sortSubNavs(){ + Collections.sort(subNav, new Comparator() { + @Override + public int compare(WANavigation o1, WANavigation o2) { + return o1.weight - o2.weight; + } + }); + } /** - * Will create a new subnav if it does not already exist or return the existing one. + * Will create a new sub-nav if it does not already exist or return the existing one. */ public WANavigation createSubNav(String name) { WANavigation nav = getSubNav(name); @@ -77,6 +89,7 @@ public class WANavigation implements Iterable{ return nav; } + public String getName(){ return name; } @@ -89,17 +102,27 @@ public class WANavigation implements Iterable{ public void setResource(Object obj){ resource = obj; } - private void setParentNav(WANavigation nav){ this.parentNav = nav; } + /** + * Sets the weight of this navigation object. The weight is + * used for deciding the order the parent will sort all sub navigation. + * Lower values will be at the top of sub-nav list. + */ + public void setWeight(int weightOrder){ + this.weight = weightOrder; + if(parentNav != null) + parentNav.sortSubNavs(); + } + @Override public boolean equals(Object o){ if(o instanceof String) return this.name.equals(o); return this == o || - (o != null && this.name.equals(((WANavigation)o).name)); + (o != null && this.id == (((WANavigation)o).id)); } @@ -110,10 +133,15 @@ public class WANavigation implements Iterable{ return rootNav.createSubNav(name); } - public static List getNavBreadcrumb(Map request) { + /** + * @param request A map of all url parameters sent from client + * @return a List of WANavigation objects depicting the navigation hierarchy for the + * requested page from the client. first entry will be the root navigation object. + */ + public static List getBreadcrumb(Map request) { LinkedList list = new LinkedList(); - if(request.containsKey("i")){ - WANavigation current = navMap.get(Integer.parseInt(request.get("i"))); + if(request.containsKey(NAVIGATION_URL_KEY)){ + WANavigation current = navMap.get(Integer.parseInt(request.get(NAVIGATION_URL_KEY))); while(current != null && current != rootNav){ list.addFirst(current); current = current.parentNav;