Some refactorings.
This commit is contained in:
parent
645dd41074
commit
2c46c7ecf3
1 changed files with 56 additions and 29 deletions
|
|
@ -31,10 +31,9 @@ import java.util.*;
|
||||||
*/
|
*/
|
||||||
public class Navigation implements Iterable{
|
public class Navigation implements Iterable{
|
||||||
private static final String NAVIGATION_URL_KEY = "i";
|
private static final String NAVIGATION_URL_KEY = "i";
|
||||||
private static int nextId = 0;
|
private static HashMap<String, Navigation> navMap = new HashMap<>();
|
||||||
private static HashMap<Integer, Navigation> navMap = new HashMap<Integer, Navigation>();
|
|
||||||
|
|
||||||
private final Integer id;
|
private final String id;
|
||||||
private String name;
|
private String name;
|
||||||
private int weight;
|
private int weight;
|
||||||
private Object resource;
|
private Object resource;
|
||||||
|
|
@ -44,8 +43,9 @@ public class Navigation implements Iterable{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private Navigation(String name) {
|
|
||||||
this.id = nextId++;
|
private Navigation(String id, String name) {
|
||||||
|
this.id = id;
|
||||||
this.navMap.put(this.id, this);
|
this.navMap.put(this.id, this);
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.subNav = new ArrayList<>();
|
this.subNav = new ArrayList<>();
|
||||||
|
|
@ -58,12 +58,12 @@ public class Navigation implements Iterable{
|
||||||
/**
|
/**
|
||||||
* Will create a new sub-nav if it does not already exist or return a existing one.
|
* Will create a new sub-nav if it does not already exist or return a existing one.
|
||||||
*/
|
*/
|
||||||
public Navigation createSubNav(String name) {
|
public Navigation createSubNav(String id, String name) {
|
||||||
Navigation nav = getSubNav(name);
|
Navigation nav = getSubNav(name);
|
||||||
if(nav != null)
|
if(nav != null)
|
||||||
return nav;
|
return nav;
|
||||||
|
|
||||||
nav = new Navigation(name);
|
nav = new Navigation(id, name);
|
||||||
nav.setParentNav(this);
|
nav.setParentNav(this);
|
||||||
subNav.add(nav);
|
subNav.add(nav);
|
||||||
sortSubNavs();
|
sortSubNavs();
|
||||||
|
|
@ -103,9 +103,6 @@ public class Navigation implements Iterable{
|
||||||
public String getName(){
|
public String getName(){
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
public String getUrl(){
|
|
||||||
return "?"+NAVIGATION_URL_KEY+"="+this.id;
|
|
||||||
}
|
|
||||||
public Object getResource(){
|
public Object getResource(){
|
||||||
return resource;
|
return resource;
|
||||||
}
|
}
|
||||||
|
|
@ -144,24 +141,35 @@ public class Navigation implements Iterable{
|
||||||
/**
|
/**
|
||||||
* Will create a clone of the navigation tree with some request instance specific information
|
* Will create a clone of the navigation tree with some request instance specific information
|
||||||
*/
|
*/
|
||||||
public NavInstance createNavInstance(Map<String, String> request){
|
public NavInstance createParameterizedNavInstance(Map<String, String> request){
|
||||||
return createNavInstance(getBreadcrumb(request));
|
return createParameterizedNavInstance(getBreadcrumb(getNavigation(request)));
|
||||||
}
|
}
|
||||||
private NavInstance createNavInstance(List<Navigation> activeList){
|
private NavInstance createParameterizedNavInstance(List<Navigation> activeList){
|
||||||
NavInstance instance = new NavInstance(this);
|
NavInstance instance = new ParameterizedNavInstance(this);
|
||||||
instance.setActive(activeList.contains(this));
|
instance.setActive(activeList.contains(this));
|
||||||
for (Navigation nav : subNav)
|
for (Navigation nav : subNav)
|
||||||
instance.addSubNav(nav.createNavInstance(activeList));
|
instance.addSubNav(nav.createParameterizedNavInstance(activeList));
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public NavInstance createPagedNavInstance(Map<String, String> request){
|
||||||
|
return createPagedNavInstance(getBreadcrumb(getNavigation(request)));
|
||||||
|
}
|
||||||
|
private NavInstance createPagedNavInstance(List<Navigation> activeList){
|
||||||
|
NavInstance instance = new PagedNavInstance(this);
|
||||||
|
instance.setActive(activeList.contains(this));
|
||||||
|
for (Navigation nav : subNav)
|
||||||
|
instance.addSubNav(nav.createPagedNavInstance(activeList));
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static Navigation createRootNav(){
|
public static Navigation createRootNav(){
|
||||||
return new Navigation(null);
|
return new Navigation(null, null);
|
||||||
}
|
}
|
||||||
public static Navigation getRootNav(Map<String, String> request) {
|
public static Navigation getRootNav(Map<String, String> request) {
|
||||||
List<Navigation> breadcrumb = getBreadcrumb(request);
|
List<Navigation> breadcrumb = getBreadcrumb(getNavigation(request));
|
||||||
if (!breadcrumb.isEmpty())
|
if (!breadcrumb.isEmpty())
|
||||||
return breadcrumb.get(0);
|
return breadcrumb.get(0);
|
||||||
return null;
|
return null;
|
||||||
|
|
@ -177,17 +185,19 @@ public class Navigation implements Iterable{
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param request A map of all url parameters sent from client
|
* @param pageId the ID of the requested page
|
||||||
* @return a List of Navigation objects depicting the navigation hierarchy for the
|
* @return a List of Navigation 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<Navigation> getBreadcrumb(Map<String, String> request) {
|
public static List<Navigation> getBreadcrumb(String pageId) {
|
||||||
|
return getBreadcrumb(navMap.get(pageId));
|
||||||
|
}
|
||||||
|
private static List<Navigation> getBreadcrumb(Navigation nav) {
|
||||||
LinkedList list = new LinkedList();
|
LinkedList list = new LinkedList();
|
||||||
Navigation current = getNavigation(request);
|
if (nav != null){
|
||||||
if (current != null){
|
while(nav != null){
|
||||||
while(current != null){
|
list.addFirst(nav);
|
||||||
list.addFirst(current);
|
nav = nav.parentNav;
|
||||||
current = current.parentNav;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
|
|
@ -196,10 +206,10 @@ public class Navigation implements Iterable{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static class NavInstance{
|
public abstract static class NavInstance{
|
||||||
private Navigation nav;
|
protected Navigation nav;
|
||||||
private boolean active;
|
protected boolean active;
|
||||||
private ArrayList<NavInstance> subNavs;
|
protected ArrayList<NavInstance> subNavs;
|
||||||
|
|
||||||
protected NavInstance(Navigation nav){
|
protected NavInstance(Navigation nav){
|
||||||
this.nav = nav;
|
this.nav = nav;
|
||||||
|
|
@ -220,9 +230,10 @@ public class Navigation implements Iterable{
|
||||||
|
|
||||||
// Mirror getters from Navigation
|
// Mirror getters from Navigation
|
||||||
public String getName(){ return nav.getName(); }
|
public String getName(){ return nav.getName(); }
|
||||||
public String getUrl(){ return nav.getUrl(); }
|
|
||||||
public Object getResource(){ return nav.getResource(); }
|
public Object getResource(){ return nav.getResource(); }
|
||||||
|
|
||||||
|
public abstract String getUrl();
|
||||||
|
|
||||||
|
|
||||||
public boolean equals(Object o){
|
public boolean equals(Object o){
|
||||||
if (o instanceof Navigation)
|
if (o instanceof Navigation)
|
||||||
|
|
@ -232,4 +243,20 @@ public class Navigation implements Iterable{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class ParameterizedNavInstance extends NavInstance{
|
||||||
|
protected ParameterizedNavInstance(Navigation nav) { super(nav); }
|
||||||
|
|
||||||
|
public String getUrl(){
|
||||||
|
return "?"+NAVIGATION_URL_KEY+"="+nav.id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class PagedNavInstance extends NavInstance{
|
||||||
|
protected PagedNavInstance(Navigation nav) { super(nav); }
|
||||||
|
|
||||||
|
public String getUrl(){
|
||||||
|
return nav.id;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue