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.clear();
|
||||||
|
|
||||||
tmpl.set("title", "WebAdmin");
|
tmpl.set("title", "WebAdmin");
|
||||||
tmpl.set("top-nav", context.getNavigation());
|
tmpl.set("top-nav", context.getNavigationInstance().getSubNavs());
|
||||||
tmpl.set("side-nav-show", true);
|
tmpl.set("side-nav-show", true);
|
||||||
if(breadcrumb.size() >= 1)
|
if(!breadcrumb.isEmpty())
|
||||||
tmpl.set("side-nav", breadcrumb.get(0).getSubNavs());
|
tmpl.set("side-nav", breadcrumb.get(0).getSubNavs());
|
||||||
tmpl.set("breadcrumb", breadcrumb);
|
tmpl.set("breadcrumb", breadcrumb);
|
||||||
tmpl.set("alerts", context.getAlerts());
|
tmpl.set("alerts", context.getAlerts());
|
||||||
|
|
|
||||||
|
|
@ -33,28 +33,37 @@ import java.util.Map;
|
||||||
* Created by Ziver on 2015-04-06.
|
* Created by Ziver on 2015-04-06.
|
||||||
*/
|
*/
|
||||||
public class WAContext {
|
public class WAContext {
|
||||||
|
private static WANavigation rootNav = WANavigation.createRootNav();
|
||||||
|
|
||||||
private ArrayList<WAAlert> alerts;
|
private ArrayList<WAAlert> alerts;
|
||||||
private List<WANavigation> nav;
|
private WANavigation.NavInstance navInstance;
|
||||||
private List<WANavigation> breadcrumb;
|
private List<WANavigation> breadcrumb;
|
||||||
|
|
||||||
|
|
||||||
public WAContext(Map<String, String> request){
|
public WAContext(Map<String, String> request){
|
||||||
// Navigation
|
// Navigation
|
||||||
nav = WANavigation.getRootNav();
|
navInstance = rootNav.createNavInstance(request);
|
||||||
|
|
||||||
// Breadcrumb
|
// Breadcrumb
|
||||||
breadcrumb = WANavigation.getBreadcrumb(request);
|
breadcrumb = WANavigation.getBreadcrumb(request);
|
||||||
|
if(!breadcrumb.isEmpty())
|
||||||
|
breadcrumb.remove(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public ArrayList<WAAlert> getAlerts() {
|
public ArrayList<WAAlert> getAlerts() {
|
||||||
return alerts;
|
return alerts;
|
||||||
}
|
}
|
||||||
public List<WANavigation> getNavigation(){
|
public WANavigation.NavInstance getNavigationInstance(){
|
||||||
return nav;
|
return navInstance;
|
||||||
}
|
}
|
||||||
public List<WANavigation> getBreadcrumb(){
|
public List<WANavigation> getBreadcrumb(){
|
||||||
return breadcrumb;
|
return breadcrumb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static WANavigation getRootNav(){
|
||||||
|
return rootNav;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -60,7 +60,7 @@ public class ServicePage implements WAPage {
|
||||||
this.logPages = new ArrayList<>();
|
this.logPages = new ArrayList<>();
|
||||||
this.configPages = new ArrayList<>();
|
this.configPages = new ArrayList<>();
|
||||||
|
|
||||||
WANavigation nav = WANavigation.createRootNav(NAVIGATION_NAME);
|
WANavigation nav = WAContext.getRootNav().createSubNav(NAVIGATION_NAME);
|
||||||
nav.setResource(this);
|
nav.setResource(this);
|
||||||
for(WAService plugin : services) {
|
for(WAService plugin : services) {
|
||||||
statusPages.add(new ServiceStatusPage(plugin.getStatus()));
|
statusPages.add(new ServiceStatusPage(plugin.getStatus()));
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ public class ServiceStatusPage implements WAPage {
|
||||||
public ServiceStatusPage(PluginManager pluginManager){
|
public ServiceStatusPage(PluginManager pluginManager){
|
||||||
this.services = pluginManager.toArray(WAServiceStatus.class);
|
this.services = pluginManager.toArray(WAServiceStatus.class);
|
||||||
|
|
||||||
WANavigation nav = WANavigation.createRootNav(ServicePage.NAVIGATION_NAME)
|
WANavigation nav = WAContext.getRootNav().createSubNav(ServicePage.NAVIGATION_NAME)
|
||||||
.createSubNav(NAVIGATION_NAME);
|
.createSubNav(NAVIGATION_NAME);
|
||||||
nav.setResource(this);
|
nav.setResource(this);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ public class StatusPage implements WAPage {
|
||||||
public StatusPage(PluginManager pluginManager){
|
public StatusPage(PluginManager pluginManager){
|
||||||
this.plugins = pluginManager.toArray(WAStatus.class);
|
this.plugins = pluginManager.toArray(WAStatus.class);
|
||||||
|
|
||||||
WANavigation nav = WANavigation.createRootNav(NAVIGATION_NAME);
|
WANavigation nav = WAContext.getRootNav().createSubNav(NAVIGATION_NAME);
|
||||||
nav.setResource(this);
|
nav.setResource(this);
|
||||||
for(WAStatus plugin : plugins)
|
for(WAStatus plugin : plugins)
|
||||||
nav.createSubNav(plugin.getName()).setResource(plugin);
|
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 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 final int id;
|
private final int id;
|
||||||
private String url;
|
private String url;
|
||||||
|
|
@ -53,13 +52,25 @@ public class WANavigation implements Iterable{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Iterator iterator() {
|
|
||||||
return subNav.iterator();
|
|
||||||
}
|
|
||||||
public List<WANavigation> getSubNavs() {
|
public List<WANavigation> getSubNavs() {
|
||||||
return subNav;
|
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) {
|
private WANavigation getSubNav(String name) {
|
||||||
for(WANavigation nav : subNav) {
|
for(WANavigation nav : subNav) {
|
||||||
if(nav.equals(name))
|
if(nav.equals(name))
|
||||||
|
|
@ -75,21 +86,13 @@ public class WANavigation implements Iterable{
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
/**
|
@Override
|
||||||
* Will create a new sub-nav if it does not already exist or return the existing one.
|
public Iterator iterator() {
|
||||||
*/
|
return subNav.iterator();
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public String getName(){
|
public String getName(){
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
@ -121,6 +124,7 @@ public class WANavigation implements Iterable{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o){
|
public boolean equals(Object o){
|
||||||
if(o instanceof String)
|
if(o instanceof String)
|
||||||
|
|
@ -129,7 +133,6 @@ public class WANavigation implements Iterable{
|
||||||
(o != null && this.id == (((WANavigation)o).id));
|
(o != null && this.id == (((WANavigation)o).id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
*/
|
*/
|
||||||
|
|
@ -147,13 +150,13 @@ public class WANavigation implements Iterable{
|
||||||
/**
|
/**
|
||||||
* @param request A map of all url parameters sent from client
|
* @param request A map of all url parameters sent from client
|
||||||
* @return a List of WANavigation objects depicting the navigation hierarchy for the
|
* @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) {
|
public static List<WANavigation> getBreadcrumb(Map<String, String> request) {
|
||||||
LinkedList list = new LinkedList();
|
LinkedList list = new LinkedList();
|
||||||
if(request.containsKey(NAVIGATION_URL_KEY)){
|
if(request.containsKey(NAVIGATION_URL_KEY)){
|
||||||
WANavigation current = navMap.get(Integer.parseInt(request.get(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);
|
list.addFirst(current);
|
||||||
current = current.parentNav;
|
current = current.parentNav;
|
||||||
}
|
}
|
||||||
|
|
@ -161,11 +164,14 @@ public class WANavigation implements Iterable{
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<WANavigation> getRootNav(){
|
public static WANavigation createRootNav(){
|
||||||
return rootNav.getSubNavs();
|
return new WANavigation(null);
|
||||||
}
|
}
|
||||||
public static WANavigation createRootNav(String name){
|
public static WANavigation getRootNav(Map<String, String> request) {
|
||||||
return rootNav.createSubNav(name);
|
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