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 @Override
107 public String getHtml() {
108 StringBuffer html = new StringBuffer();
109 html.append("<div id=\"" + getName() + "_DivMenu\" class=\"mgnlTreeMenu\" onmouseover=\"" + getName() + ".keepShowing();\" onmouseout=\"" + getName() + ".hide();\" >");
110 int counter = 0;
111
112 for (int i = 0; i < this.getMenuItems().size(); i++) {
113 ContextMenuItem item = this.getMenuItem(i);
114 if (item == null) {
115 html.append("<div class=\"mgnlTreeMenuLine\"><!-- ie --></div>");
116 }
117 else {
118 item.setJavascriptMenuName(getName());
119 String id = getName() + "_MenuItem" + i;
120 item.setId(id);
121 html.append(item.getHtml());
122 counter++;
123 }
124 html.append("\n");
125 }
126
127 html.append("</div>");
128 return html.toString();
129 }
130
131 public String getJavascript() {
132 StringBuffer menuJavascript = new StringBuffer();
133 menuJavascript.append("var " + getName() + "= new mgnlContextMenu('" + getName() + "');");
134 int counter = 0;
135 for (int i = 0; i < this.getMenuItems().size(); i++) {
136 ContextMenuItem item = this.getMenuItem(i);
137 if (item != null) {
138 item.setJavascriptMenuName(getName());
139 String id = getName() + "_MenuItem" + i;
140 item.setId(id);
141 menuJavascript.append(getName() + ".menuItems["
142 + counter
143 + "]=new mgnlContextMenuItem('"
144 + id
145 + "');\n");
146 menuJavascript.append(getName() + ".menuItems[" + counter + "].conditions=new Object();");
147 for (int cond = 0; cond < item.getJavascriptConditions().size(); cond++) {
148 menuJavascript.append(getName() + ".menuItems["
149 + counter
150 + "].conditions["
151 + cond
152 + "]="
153 + item.getJavascriptCondition(cond)
154 + ";");
155 }
156 counter++;
157 }
158 }
159
160 return menuJavascript.toString();
161 }
162 }