/* * 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; import java.util.*; /** * A class representing a navigation hierarchy. * * Created by Ziver on 2015-04-02. */ public class WANavigation implements Iterable{ private static final String NAVIGATION_URL_KEY = "i"; private static int nextId = 0; private static HashMap navMap = new HashMap(); private static WANavigation rootNav = new WANavigation(null); private final int id; private String url; private String name; private int weight; private WANavigation parentNav; private ArrayList subNav; private Object resource; private WANavigation(String name) { this.id = nextId++; this.navMap.put(this.id, this); this.url = "?"+NAVIGATION_URL_KEY+"="+this.id; this.name = name; this.subNav = new ArrayList<>(); } @Override public Iterator iterator() { return subNav.iterator(); } public List getSubNavs() { return subNav; } private WANavigation getSubNav(String name) { for(WANavigation nav : subNav) { if(nav.equals(name)) return nav; } return null; } private void sortSubNavs(){ Collections.sort(subNav, new Comparator() { @Override public int compare(WANavigation o1, WANavigation o2) { return o1.weight - o2.weight; } }); } /** * 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; } public String getName(){ return name; } public String getUrl(){ return url; } public Object getResource(){ return resource; } public void setResource(Object obj){ resource = obj; } private void setParentNav(WANavigation nav){ this.parentNav = nav; } /** * 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(); } @Override public boolean equals(Object o){ if(o instanceof String) return this.name.equals(o); return this == o || (o != null && this.id == (((WANavigation)o).id)); } public static List getRootNav(){ return rootNav.getSubNavs(); } public static WANavigation createRootNav(String name){ return rootNav.createSubNav(name); } /** * @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. */ public static List getBreadcrumb(Map 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){ list.addFirst(current); current = current.parentNav; } } return list; } }