01: package simpleorm.simplewebapp.core;
02:
03: import simpleorm.simplewebapp.core.WMenuItemGlobal;
04: import simpleorm.simplewebapp.core.WPage;
05: import simpleorm.simplewebapp.core.WException;
06:
07: import java.util.LinkedHashMap;
08: import java.util.Collection;
09:
10: /**
11: * Represents all the WMenuItems in a menu, see WMenuAbstractGlobal. <p>
12: *
13: * WARNING These are generally Global (ie final values), see WGlobalState.
14: * Do not modify them on a per page basis, but do refer to them using
15: * WPageStructure.getPageItem etc. in case there are local coplies.<p>
16: */
17: public class WMenuParentGlobal extends WMenuAbstractGlobal {
18:
19: LinkedHashMap<String, WMenuAbstractGlobal> chlidren = new LinkedHashMap(
20: 20);
21:
22: public WMenuParentGlobal(WMenuParentGlobal parent) {
23: this (parent, null);
24: }
25:
26: /** Names are the prefix in the URL, eg. /swb/ in the top level menu. */
27: public WMenuParentGlobal(WMenuParentGlobal parent, String name) {
28: super (parent);
29: this .name = name == null ? getClass().getSimpleName() : name; // override class.SimpleName with "swb" for top level
30: setUrl((parent != null ? parent.getUrl() : "") + "/"
31: + this .name);
32: System.err.println("WMenuParentGlobal " + this + getUrl());
33: }
34:
35: public <M extends WMenuParentGlobal> M addSubMenu(M menu) {
36: chlidren.put(menu.getName(), menu);
37: return menu;
38: }
39:
40: public WMenuAbstractGlobal findSubMenu(String name) {
41: return chlidren.get(name);
42: }
43:
44: public WMenuAbstractGlobal findSubMenuNotNull(String name) {
45: WMenuAbstractGlobal menu = chlidren.get(name);
46: if (menu == null)
47: throw new WException("No menu named " + name);
48: return menu;
49: }
50:
51: public WMenuItemGlobal addNewMenuItem(Class<? extends WPage> page,
52: String name, String url, String role) {
53: WMenuItemGlobal mi = new WMenuItemGlobal(this );
54: if (page != null)
55: mi.setPageClassName(page);
56: if (name == null)
57: name = page.getSimpleName();
58: mi.setName(name);
59: if (url == null)
60: url = getUrl() + "/" + name + ".swb";
61: mi.setUrl(url);
62: mi.setRole(role);
63: putItem(mi);
64: return mi;
65: }
66:
67: public WMenuItemGlobal addNewMenuItem(Class<? extends WPage> page,
68: String role) {
69: return addNewMenuItem(page, null, null, role);
70: }
71:
72: public void putItem(WMenuItemGlobal item) {
73: chlidren.put(item.name, item);
74: }
75:
76: public Collection<WMenuAbstractGlobal> getItems() {
77: return chlidren.values();
78: }
79:
80: public WMenuItemGlobal findBySwbUrlNotNull(String url) {
81: for (WMenuAbstractGlobal item : chlidren.values()) {
82: if (url.equals(item.getUrl()))
83: return (WMenuItemGlobal) item;
84: }
85: throw new WException("URL " + url + " not found in " + this );
86: }
87:
88: ///////////// GENERATED //////////////////
89:
90: }
|