01: package org.wings.session;
02:
03: import java.util.HashMap;
04: import java.util.HashSet;
05: import java.util.Iterator;
06: import java.util.Map;
07: import java.util.Set;
08:
09: import org.wings.SComponent;
10: import org.wings.SFrame;
11: import org.wings.SMenu;
12: import org.wings.SPopupMenu;
13:
14: public class MenuManager {
15:
16: private Map menuLinkMap = new HashMap();
17:
18: public void registerMenuLink(SMenu menu, SComponent component) {
19: register(menu, component);
20: }
21:
22: public void registerMenuLink(SPopupMenu menu, SComponent component) {
23: register(menu, component);
24: }
25:
26: private void register(SComponent menu, SComponent component) {
27: if (menu == null)
28: throw new IllegalArgumentException("Menu must not be null!");
29:
30: Set components = getComponents(menu);
31: components.add(component);
32: menuLinkMap.put(menu, components);
33: }
34:
35: public void deregisterMenuLink(SMenu menu, SComponent component) {
36: deregister(menu, component);
37: }
38:
39: public void deregisterMenuLink(SPopupMenu menu, SComponent component) {
40: deregister(menu, component);
41: }
42:
43: private void deregister(SComponent menu, SComponent component) {
44: if (menu == null)
45: throw new IllegalArgumentException("Menu must not be null!");
46:
47: Set components = getComponents(menu);
48: components.remove(component);
49:
50: if (components.isEmpty()) {
51: menuLinkMap.remove(menu);
52: } else {
53: menuLinkMap.put(menu, components);
54: }
55: }
56:
57: public boolean isMenuLinked(SMenu menu) {
58: return !getComponents(menu).isEmpty();
59: }
60:
61: public boolean isMenuLinked(SPopupMenu menu) {
62: return !getComponents(menu).isEmpty();
63: }
64:
65: public Set getMenueLinks(SMenu menu) {
66: return getComponents(menu);
67: }
68:
69: public Set getMenueLinks(SPopupMenu menu) {
70: return getComponents(menu);
71: }
72:
73: public Set getMenues() {
74: return menuLinkMap.keySet();
75: }
76:
77: public Set getMenues(SFrame frame) {
78: Set menuesUsedInFrame = new HashSet(menuLinkMap.keySet());
79: for (Iterator i = menuesUsedInFrame.iterator(); i.hasNext();) {
80: if (((SComponent) i.next()).getParentFrame() != frame) {
81: i.remove();
82: }
83: }
84: return menuesUsedInFrame;
85: }
86:
87: private Set getComponents(SComponent menu) {
88: Set links = (Set) menuLinkMap.get(menu);
89: if (links == null) {
90: links = new HashSet(2);
91: }
92: return links;
93: }
94:
95: }
|