Added weights to navigation object
This commit is contained in:
parent
6a90b291ae
commit
e2d596152d
2 changed files with 36 additions and 8 deletions
|
|
@ -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<Integer, WANavigation> navMap = new HashMap<Integer, WANavigation>();
|
||||
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<WANavigation> 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<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) {
|
||||
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<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();
|
||||
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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue