webadmin/src/wa/server/page/struct/WANavigation.java

216 lines
7 KiB
Java
Raw Normal View History

2015-04-09 21:22:47 +00:00
/*
* Copyright (c) 2015 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package wa.server.page.struct;
2015-04-25 15:22:44 +00:00
import java.util.*;
2015-04-09 21:22:47 +00:00
/**
* A class representing a navigation hierarchy/tree for a web application.
2016-04-05 12:37:45 +02:00
*
2015-04-09 21:22:47 +00:00
* Created by Ziver on 2015-04-02.
*/
public class WANavigation implements Iterable{
2016-04-05 12:37:45 +02:00
private static final String NAVIGATION_URL_KEY = "i";
private static int nextId = 0;
private static HashMap<Integer, WANavigation> navMap = new HashMap<Integer, WANavigation>();
2015-04-22 15:33:08 +00:00
2015-07-28 12:06:51 +00:00
private final int id;
2015-04-25 15:22:44 +00:00
private String url;
2015-04-09 21:22:47 +00:00
private String name;
2016-04-05 12:37:45 +02:00
private int weight;
private WANavigation parentNav;
2015-07-28 12:06:51 +00:00
private ArrayList<WANavigation> subNav;
2015-04-25 15:22:44 +00:00
private Object resource;
2015-04-09 21:22:47 +00:00
private WANavigation(String name) {
2015-04-25 15:22:44 +00:00
this.id = nextId++;
this.navMap.put(this.id, this);
2016-04-05 12:37:45 +02:00
this.url = "?"+NAVIGATION_URL_KEY+"="+this.id;
2015-04-09 21:22:47 +00:00
this.name = name;
this.subNav = new ArrayList<>();
2015-04-22 15:33:08 +00:00
}
2015-04-25 15:22:44 +00:00
public List<WANavigation> getSubNavs() {
return subNav;
2015-04-09 21:22:47 +00:00
}
2016-04-06 14:28:47 +02:00
/**
* 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))
return nav;
}
return null;
}
2016-04-05 12:37:45 +02:00
private void sortSubNavs(){
Collections.sort(subNav, new Comparator<WANavigation>() {
@Override
public int compare(WANavigation o1, WANavigation o2) {
return o1.weight - o2.weight;
}
});
}
2016-04-06 14:28:47 +02:00
@Override
public Iterator iterator() {
return subNav.iterator();
2015-04-22 15:33:08 +00:00
}
2016-04-05 12:37:45 +02:00
2016-04-06 14:28:47 +02:00
2015-06-03 15:08:41 +00:00
public String getName(){
return name;
}
2016-04-02 01:17:13 +02:00
public String getUrl(){
return url;
}
private void setParentNav(WANavigation nav){
this.parentNav = nav;
2015-04-25 15:22:44 +00:00
}
/**
* Assign a resource object specific to this navigation object.
* This can be used if target page needs some additional information.
*/
2015-04-25 15:22:44 +00:00
public void setResource(Object obj){
resource = obj;
}
public Object getResource(){
return resource;
2015-04-25 15:22:44 +00:00
}
2016-04-05 12:37:45 +02:00
/**
* 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();
}
2015-04-22 15:33:08 +00:00
2016-04-06 14:28:47 +02:00
@Override
2015-04-22 15:33:08 +00:00
public boolean equals(Object o){
if(o instanceof String)
return this.name.equals(o);
return this == o ||
2016-04-05 12:37:45 +02:00
(o != null && this.id == (((WANavigation)o).id));
2015-04-22 15:33:08 +00:00
}
/**
* Will create a clone of the navigation tree with some request instance specific information
*/
public NavInstance createNavInstance(Map<String, String> request){
return createNavInstance(getBreadcrumb(request));
2015-04-25 15:22:44 +00:00
}
private NavInstance createNavInstance(List<WANavigation> activeList){
NavInstance instance = new NavInstance(this);
instance.setActive(activeList.contains(this));
for (WANavigation nav : subNav)
instance.addSubNav(nav.createNavInstance(activeList));
return instance;
2015-06-03 15:08:41 +00:00
}
2015-04-25 15:22:44 +00:00
2016-04-05 12:37:45 +02:00
/**
* @param request A map of all url parameters sent from client
* @return a List of WANavigation objects depicting the navigation hierarchy for the
2016-04-06 14:28:47 +02:00
* requested page from the client. First entry will be the root navigation object.
2016-04-05 12:37:45 +02:00
*/
public static List<WANavigation> getBreadcrumb(Map<String, String> request) {
2015-04-25 15:22:44 +00:00
LinkedList list = new LinkedList();
2016-04-05 12:37:45 +02:00
if(request.containsKey(NAVIGATION_URL_KEY)){
WANavigation current = navMap.get(Integer.parseInt(request.get(NAVIGATION_URL_KEY)));
2016-04-06 14:28:47 +02:00
while(current != null){
2015-04-25 15:22:44 +00:00
list.addFirst(current);
current = current.parentNav;
2015-04-25 15:22:44 +00:00
}
}
return list;
2015-04-09 21:22:47 +00:00
}
2016-04-06 14:28:47 +02:00
public static WANavigation createRootNav(){
return new WANavigation(null);
}
2016-04-06 14:28:47 +02:00
public static WANavigation getRootNav(Map<String, String> request) {
List<WANavigation> breadcrumb = getBreadcrumb(request);
if (!breadcrumb.isEmpty())
return breadcrumb.get(0);
return null;
}
public static class NavInstance{
private WANavigation nav;
private boolean active;
private ArrayList<NavInstance> subNavs;
protected NavInstance(WANavigation nav){
this.nav = nav;
this.subNavs = new ArrayList<>();
}
protected void setActive(boolean active){
this.active = active;
}
protected void addSubNav(NavInstance subNav){
subNavs.add(subNav);
}
public boolean isActive(){
return active;
}
public List<NavInstance> getSubNavs() { return subNavs; }
// Mirror getters from WANavigation
public String getName(){ return nav.getName(); }
public String getUrl(){ return nav.getUrl(); }
public Object getResource(){ return nav.getResource(); }
2016-04-06 17:20:47 +02:00
public boolean equals(Object o){
if (o instanceof WANavigation)
return nav.equals(o);
else if (o instanceof NavInstance)
return nav.equals(((NavInstance) o).nav);
return false;
}
}
2015-04-09 21:22:47 +00:00
}