1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.gui.control;
35
36 import java.util.ArrayList;
37 import java.util.List;
38
39
40
41
42
43
44
45 public class ContextMenu extends ControlImpl {
46
47
48
49
50 private List<ContextMenuItem> menuItems = new ArrayList<ContextMenuItem>();
51
52
53
54
55
56 public ContextMenu(String menuName) {
57 super();
58 this.setName(menuName);
59 }
60
61
62
63
64 public List<ContextMenuItem> getMenuItems() {
65 return this.menuItems;
66 }
67
68
69
70
71
72 public void setMenuItems(List<ContextMenuItem> menuItems) {
73 this.menuItems = menuItems;
74 }
75
76
77
78
79
80 public ContextMenuItem getMenuItem(int col) {
81 return this.getMenuItems().get(col);
82 }
83
84 public ContextMenuItem getMenuItemByName(String name) {
85 List<ContextMenuItem> menuItems = this.getMenuItems();
86 for (ContextMenuItem menuItem : menuItems) {
87 if (menuItem != null && menuItem.getName() != null && menuItem.getName().equals(name)) {
88 return menuItem;
89 }
90 }
91 return null;
92 }
93
94
95
96
97
98 public void addMenuItem(ContextMenuItem item) {
99 this.getMenuItems().add(item);
100 }
101
102
103
104
105
106 public String getHtml() {
107 StringBuffer html = new StringBuffer();
108 html.append("<div id=\"" + getName() + "_DivMenu\" class=\"mgnlTreeMenu\" onmouseover=\"" + getName() + ".keepShowing();\" onmouseout=\"" + getName() + ".hide();\" >");
109 int counter = 0;
110
111 for (int i = 0; i < this.getMenuItems().size(); i++) {
112 ContextMenuItem item = this.getMenuItem(i);
113 if (item == null) {
114 html.append("<div class=\"mgnlTreeMenuLine\"><!-- ie --></div>");
115 }
116 else {
117 item.setJavascriptMenuName(getName());
118 String id = getName() + "_MenuItem" + i;
119 item.setId(id);
120 html.append(item.getHtml());
121 counter++;
122 }
123 html.append("\n");
124 }
125
126 html.append("</div>");
127 return html.toString();
128 }
129
130 public String getJavascript() {
131 StringBuffer menuJavascript = new StringBuffer();
132 menuJavascript.append("var " + getName() + "= new mgnlContextMenu('" + getName() + "');");
133 int counter = 0;
134 for (int i = 0; i < this.getMenuItems().size(); i++) {
135 ContextMenuItem item = this.getMenuItem(i);
136 if (item != null) {
137 item.setJavascriptMenuName(getName());
138 String id = getName() + "_MenuItem" + i;
139 item.setId(id);
140 menuJavascript.append(getName() + ".menuItems["
141 + counter
142 + "]=new mgnlContextMenuItem('"
143 + id
144 + "');\n");
145 menuJavascript.append(getName() + ".menuItems[" + counter + "].conditions=new Object();");
146 for (int cond = 0; cond < item.getJavascriptConditions().size(); cond++) {
147 menuJavascript.append(getName() + ".menuItems["
148 + counter
149 + "].conditions["
150 + cond
151 + "]="
152 + item.getJavascriptCondition(cond)
153 + ";");
154 }
155 counter++;
156 }
157 }
158
159 return menuJavascript.toString();
160 }
161 }