Added weights to navigation object

This commit is contained in:
Ziver Koc 2016-04-05 12:37:45 +02:00
parent 6a90b291ae
commit e2d596152d
2 changed files with 36 additions and 8 deletions

2
src/wa/server/WAContext.java Normal file → Executable file
View file

@ -43,7 +43,7 @@ public class WAContext {
nav = WANavigation.getRootNav(); nav = WANavigation.getRootNav();
// Breadcrumb // Breadcrumb
breadcrumb = WANavigation.getNavBreadcrumb(request); breadcrumb = WANavigation.getBreadcrumb(request);
} }

View file

@ -25,9 +25,12 @@ package wa.server.page.struct;
import java.util.*; import java.util.*;
/** /**
* A class representing a navigation hierarchy.
*
* Created by Ziver on 2015-04-02. * Created by Ziver on 2015-04-02.
*/ */
public class WANavigation implements Iterable{ public class WANavigation implements Iterable{
private static final String NAVIGATION_URL_KEY = "i";
private static int nextId = 0; private static int nextId = 0;
private static HashMap<Integer, WANavigation> navMap = new HashMap<Integer, WANavigation>(); private static HashMap<Integer, WANavigation> navMap = new HashMap<Integer, WANavigation>();
private static WANavigation rootNav = new WANavigation(null); private static WANavigation rootNav = new WANavigation(null);
@ -35,6 +38,7 @@ public class WANavigation implements Iterable{
private final int id; private final int id;
private String url; private String url;
private String name; private String name;
private int weight;
private WANavigation parentNav; private WANavigation parentNav;
private ArrayList<WANavigation> subNav; private ArrayList<WANavigation> subNav;
private Object resource; private Object resource;
@ -43,7 +47,7 @@ public class WANavigation implements Iterable{
private WANavigation(String name) { private WANavigation(String name) {
this.id = nextId++; this.id = nextId++;
this.navMap.put(this.id, this); this.navMap.put(this.id, this);
this.url = "?i="+this.id; this.url = "?"+NAVIGATION_URL_KEY+"="+this.id;
this.name = name; this.name = name;
this.subNav = new ArrayList<>(); this.subNav = new ArrayList<>();
} }
@ -63,8 +67,16 @@ public class WANavigation implements Iterable{
} }
return null; return null;
} }
private void sortSubNavs(){
Collections.sort(subNav, new Comparator<WANavigation>() {
@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) { public WANavigation createSubNav(String name) {
WANavigation nav = getSubNav(name); WANavigation nav = getSubNav(name);
@ -77,6 +89,7 @@ public class WANavigation implements Iterable{
return nav; return nav;
} }
public String getName(){ public String getName(){
return name; return name;
} }
@ -89,17 +102,27 @@ public class WANavigation implements Iterable{
public void setResource(Object obj){ public void setResource(Object obj){
resource = obj; resource = obj;
} }
private void setParentNav(WANavigation nav){ private void setParentNav(WANavigation nav){
this.parentNav = 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 @Override
public boolean equals(Object o){ public boolean equals(Object o){
if(o instanceof String) if(o instanceof String)
return this.name.equals(o); return this.name.equals(o);
return this == 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); return rootNav.createSubNav(name);
} }
public static List<WANavigation> getNavBreadcrumb(Map<String, String> 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<WANavigation> getBreadcrumb(Map<String, String> request) {
LinkedList list = new LinkedList(); LinkedList list = new LinkedList();
if(request.containsKey("i")){ if(request.containsKey(NAVIGATION_URL_KEY)){
WANavigation current = navMap.get(Integer.parseInt(request.get("i"))); WANavigation current = navMap.get(Integer.parseInt(request.get(NAVIGATION_URL_KEY)));
while(current != null && current != rootNav){ while(current != null && current != rootNav){
list.addFirst(current); list.addFirst(current);
current = current.parentNav; current = current.parentNav;