Some navigation refactoring
This commit is contained in:
parent
f09ab62d52
commit
7eed07da38
6 changed files with 48 additions and 33 deletions
|
|
@ -33,7 +33,6 @@ 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);
|
||||
|
||||
private final int id;
|
||||
private String url;
|
||||
|
|
@ -53,13 +52,25 @@ public class WANavigation implements Iterable{
|
|||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Iterator iterator() {
|
||||
return subNav.iterator();
|
||||
}
|
||||
public List<WANavigation> getSubNavs() {
|
||||
return subNav;
|
||||
}
|
||||
/**
|
||||
* Will create a new sub-nav if it does not already exist or return a existing one.
|
||||
*/
|
||||
public WANavigation createSubNav(String name) {
|
||||
WANavigation nav = getSubNav(name);
|
||||
if(nav != null)
|
||||
return nav;
|
||||
|
||||
nav = new WANavigation(name);
|
||||
nav.setParentNav(this);
|
||||
subNav.add(nav);
|
||||
return nav;
|
||||
}
|
||||
/**
|
||||
* Searches for and returns the specified sub-nav or returns null if it was not found.
|
||||
*/
|
||||
private WANavigation getSubNav(String name) {
|
||||
for(WANavigation nav : subNav) {
|
||||
if(nav.equals(name))
|
||||
|
|
@ -75,21 +86,13 @@ public class WANavigation implements Iterable{
|
|||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* 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);
|
||||
if(nav != null)
|
||||
return nav;
|
||||
|
||||
nav = new WANavigation(name);
|
||||
nav.setParentNav(this);
|
||||
subNav.add(nav);
|
||||
return nav;
|
||||
@Override
|
||||
public Iterator iterator() {
|
||||
return subNav.iterator();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getName(){
|
||||
return name;
|
||||
}
|
||||
|
|
@ -121,6 +124,7 @@ public class WANavigation implements Iterable{
|
|||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o){
|
||||
if(o instanceof String)
|
||||
|
|
@ -129,7 +133,6 @@ public class WANavigation implements Iterable{
|
|||
(o != null && this.id == (((WANavigation)o).id));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Will create a clone of the navigation tree with some request instance specific information
|
||||
*/
|
||||
|
|
@ -147,13 +150,13 @@ public class WANavigation implements Iterable{
|
|||
/**
|
||||
* @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.
|
||||
* 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(NAVIGATION_URL_KEY)){
|
||||
WANavigation current = navMap.get(Integer.parseInt(request.get(NAVIGATION_URL_KEY)));
|
||||
while(current != null && current != rootNav){
|
||||
while(current != null){
|
||||
list.addFirst(current);
|
||||
current = current.parentNav;
|
||||
}
|
||||
|
|
@ -161,11 +164,14 @@ public class WANavigation implements Iterable{
|
|||
return list;
|
||||
}
|
||||
|
||||
public static List<WANavigation> getRootNav(){
|
||||
return rootNav.getSubNavs();
|
||||
public static WANavigation createRootNav(){
|
||||
return new WANavigation(null);
|
||||
}
|
||||
public static WANavigation createRootNav(String name){
|
||||
return rootNav.createSubNav(name);
|
||||
public static WANavigation getRootNav(Map<String, String> request) {
|
||||
List<WANavigation> breadcrumb = getBreadcrumb(request);
|
||||
if (!breadcrumb.isEmpty())
|
||||
return breadcrumb.get(0);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue