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
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Created by Ziver on 2015-04-02.
|
|
|
|
|
*/
|
|
|
|
|
public class WANavigation {
|
2015-04-25 15:22:44 +00:00
|
|
|
private static int nextId;
|
|
|
|
|
private static List<WANavigation> root_nav = new ArrayList<WANavigation>();
|
|
|
|
|
private static HashMap<Integer, WANavigation> nav_map = new HashMap<Integer, WANavigation>();
|
2015-04-22 15:33:08 +00:00
|
|
|
|
2015-04-25 15:22:44 +00:00
|
|
|
private int id;
|
|
|
|
|
private String url;
|
2015-04-09 21:22:47 +00:00
|
|
|
private String name;
|
2015-04-25 15:22:44 +00:00
|
|
|
private ArrayList<WANavigation> sub_navs;
|
|
|
|
|
|
|
|
|
|
private WANavigation parent_nav;
|
|
|
|
|
private Object resource;
|
2015-04-09 21:22:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
public WANavigation(String name) {
|
2015-04-25 15:22:44 +00:00
|
|
|
this.id = nextId++;
|
|
|
|
|
this.nav_map.put(this.id, this);
|
|
|
|
|
this.url = "?i="+this.id;
|
2015-04-09 21:22:47 +00:00
|
|
|
this.name = name;
|
2015-04-25 15:22:44 +00:00
|
|
|
this.sub_navs = new ArrayList<>();
|
2015-04-09 21:22:47 +00:00
|
|
|
}
|
2015-04-25 15:22:44 +00:00
|
|
|
public WANavigation(String name, Object resource) {
|
2015-04-22 15:33:08 +00:00
|
|
|
this(name);
|
2015-04-25 15:22:44 +00:00
|
|
|
this.setResource(resource);
|
2015-04-22 15:33:08 +00:00
|
|
|
}
|
|
|
|
|
|
2015-04-25 15:22:44 +00:00
|
|
|
|
2015-04-22 15:33:08 +00:00
|
|
|
public void addSubNav(WANavigation subNav) {
|
2015-04-25 15:22:44 +00:00
|
|
|
this.sub_navs.add(subNav);
|
|
|
|
|
subNav.setParentNav(this );
|
2015-04-22 15:33:08 +00:00
|
|
|
}
|
|
|
|
|
public List<WANavigation> getSubNav() {
|
2015-04-25 15:22:44 +00:00
|
|
|
return sub_navs;
|
2015-04-09 21:22:47 +00:00
|
|
|
}
|
2015-04-22 15:33:08 +00:00
|
|
|
public Object getSubNav(String name) {
|
2015-04-25 15:22:44 +00:00
|
|
|
int index = sub_navs.indexOf(name);
|
2015-04-22 15:33:08 +00:00
|
|
|
if(index >= 0)
|
2015-04-25 15:22:44 +00:00
|
|
|
return sub_navs.get(index);
|
2015-04-22 15:33:08 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-25 15:22:44 +00:00
|
|
|
public Object getResource(){
|
|
|
|
|
return resource;
|
|
|
|
|
}
|
|
|
|
|
public void setResource(Object obj){
|
|
|
|
|
resource = obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setParentNav(WANavigation nav){
|
|
|
|
|
this.parent_nav = nav;
|
|
|
|
|
}
|
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 ||
|
|
|
|
|
(o != null && this.name.equals(((WANavigation)o).name));
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-09 21:22:47 +00:00
|
|
|
|
2015-04-25 15:22:44 +00:00
|
|
|
public static void addRootNav(WANavigation nav){
|
|
|
|
|
getRootNav().add(nav);
|
|
|
|
|
}
|
|
|
|
|
public static List<WANavigation> getRootNav(){
|
|
|
|
|
return root_nav;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<WANavigation> getNavResource(Map<String, String> request) {
|
|
|
|
|
LinkedList list = new LinkedList();
|
|
|
|
|
if(request.containsKey("i")){
|
|
|
|
|
WANavigation current = nav_map.get(Integer.parseInt(request.get("i")));
|
|
|
|
|
while(current != null){
|
|
|
|
|
list.addFirst(current);
|
|
|
|
|
current = current.parent_nav;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return list;
|
2015-04-09 21:22:47 +00:00
|
|
|
}
|
|
|
|
|
}
|