Some navigation refactoring
This commit is contained in:
parent
f09ab62d52
commit
7eed07da38
6 changed files with 48 additions and 33 deletions
|
|
@ -99,9 +99,9 @@ public class WAAbstractPage implements HttpPage{
|
|||
tmpl.clear();
|
||||
|
||||
tmpl.set("title", "WebAdmin");
|
||||
tmpl.set("top-nav", context.getNavigation());
|
||||
tmpl.set("top-nav", context.getNavigationInstance().getSubNavs());
|
||||
tmpl.set("side-nav-show", true);
|
||||
if(breadcrumb.size() >= 1)
|
||||
if(!breadcrumb.isEmpty())
|
||||
tmpl.set("side-nav", breadcrumb.get(0).getSubNavs());
|
||||
tmpl.set("breadcrumb", breadcrumb);
|
||||
tmpl.set("alerts", context.getAlerts());
|
||||
|
|
|
|||
|
|
@ -33,28 +33,37 @@ import java.util.Map;
|
|||
* Created by Ziver on 2015-04-06.
|
||||
*/
|
||||
public class WAContext {
|
||||
private static WANavigation rootNav = WANavigation.createRootNav();
|
||||
|
||||
private ArrayList<WAAlert> alerts;
|
||||
private List<WANavigation> nav;
|
||||
private WANavigation.NavInstance navInstance;
|
||||
private List<WANavigation> breadcrumb;
|
||||
|
||||
|
||||
public WAContext(Map<String, String> request){
|
||||
// Navigation
|
||||
nav = WANavigation.getRootNav();
|
||||
navInstance = rootNav.createNavInstance(request);
|
||||
|
||||
// Breadcrumb
|
||||
breadcrumb = WANavigation.getBreadcrumb(request);
|
||||
if(!breadcrumb.isEmpty())
|
||||
breadcrumb.remove(0);
|
||||
}
|
||||
|
||||
|
||||
public ArrayList<WAAlert> getAlerts() {
|
||||
return alerts;
|
||||
}
|
||||
public List<WANavigation> getNavigation(){
|
||||
return nav;
|
||||
public WANavigation.NavInstance getNavigationInstance(){
|
||||
return navInstance;
|
||||
}
|
||||
public List<WANavigation> getBreadcrumb(){
|
||||
return breadcrumb;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static WANavigation getRootNav(){
|
||||
return rootNav;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ public class ServicePage implements WAPage {
|
|||
this.logPages = new ArrayList<>();
|
||||
this.configPages = new ArrayList<>();
|
||||
|
||||
WANavigation nav = WANavigation.createRootNav(NAVIGATION_NAME);
|
||||
WANavigation nav = WAContext.getRootNav().createSubNav(NAVIGATION_NAME);
|
||||
nav.setResource(this);
|
||||
for(WAService plugin : services) {
|
||||
statusPages.add(new ServiceStatusPage(plugin.getStatus()));
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ public class ServiceStatusPage implements WAPage {
|
|||
public ServiceStatusPage(PluginManager pluginManager){
|
||||
this.services = pluginManager.toArray(WAServiceStatus.class);
|
||||
|
||||
WANavigation nav = WANavigation.createRootNav(ServicePage.NAVIGATION_NAME)
|
||||
WANavigation nav = WAContext.getRootNav().createSubNav(ServicePage.NAVIGATION_NAME)
|
||||
.createSubNav(NAVIGATION_NAME);
|
||||
nav.setResource(this);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ public class StatusPage implements WAPage {
|
|||
public StatusPage(PluginManager pluginManager){
|
||||
this.plugins = pluginManager.toArray(WAStatus.class);
|
||||
|
||||
WANavigation nav = WANavigation.createRootNav(NAVIGATION_NAME);
|
||||
WANavigation nav = WAContext.getRootNav().createSubNav(NAVIGATION_NAME);
|
||||
nav.setResource(this);
|
||||
for(WAStatus plugin : plugins)
|
||||
nav.createSubNav(plugin.getName()).setResource(plugin);
|
||||
|
|
|
|||
|
|
@ -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