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.Iterator;
38 import java.util.List;
39
40
41
42
43
44
45
46 public class ContextMenu extends ControlImpl {
47
48
49
50
51 private List menuItems = new ArrayList();
52
53
54
55
56
57 public ContextMenu(String menuName) {
58 super();
59 this.setName(menuName);
60 }
61
62
63
64
65 public List getMenuItems() {
66 return this.menuItems;
67 }
68
69
70
71
72
73 public void setMenuItems(List menuItems) {
74 this.menuItems = menuItems;
75 }
76
77
78
79
80
81 public ContextMenuItem getMenuItem(int col) {
82 return (ContextMenuItem) this.getMenuItems().get(col);
83 }
84
85
86
87
88
89 public ContextMenuItem getMenuItemByName(String name) {
90 List menuItems = this.getMenuItems();
91 for (Iterator iter = menuItems.iterator(); iter.hasNext();) {
92 ContextMenuItem menuItem = (ContextMenuItem) iter.next();
93 if (menuItem != null && menuItem.getName() != null && menuItem.getName().equals(name)) {
94 return menuItem;
95 }
96 }
97 return null;
98 }
99
100
101
102
103
104 public void addMenuItem(ContextMenuItem item) {
105 this.getMenuItems().add(item);
106 }
107
108
109
110
111
112 public String getHtml() {
113 StringBuffer html = new StringBuffer();
114 html
115 .append("<div id=\"" + getName() + "_DivMenu\" class=\"mgnlTreeMenu\" onmouseover=\"" + getName() + ".keepShowing();\" onmouseout=\"" + getName() + ".hide();\" >");
116 int counter = 0;
117
118 for (int i = 0; i < this.getMenuItems().size(); i++) {
119 ContextMenuItem item = this.getMenuItem(i);
120 if (item == null) {
121 html.append("<div class=\"mgnlTreeMenuLine\"><!-- ie --></div>");
122 }
123 else {
124 item.setJavascriptMenuName(getName());
125 String id = getName() + "_MenuItem" + i;
126 item.setId(id);
127 html.append(item.getHtml());
128 counter++;
129 }
130 html.append("\n");
131 }
132
133 html.append("</div>");
134 return html.toString();
135 }
136
137 public String getJavascript() {
138 StringBuffer menuJavascript = new StringBuffer();
139 menuJavascript.append("var " + getName() + "= new mgnlContextMenu('" + getName() + "');");
140 int counter = 0;
141 for (int i = 0; i < this.getMenuItems().size(); i++) {
142 ContextMenuItem item = this.getMenuItem(i);
143 if (item != null) {
144 item.setJavascriptMenuName(getName());
145 String id = getName() + "_MenuItem" + i;
146 item.setId(id);
147 menuJavascript.append(getName() + ".menuItems["
148 + counter
149 + "]=new mgnlContextMenuItem('"
150 + id
151 + "');\n");
152 menuJavascript.append(getName() + ".menuItems[" + counter + "].conditions=new Object();");
153 for (int cond = 0; cond < item.getJavascriptConditions().size(); cond++) {
154 menuJavascript.append(getName() + ".menuItems["
155 + counter
156 + "].conditions["
157 + cond
158 + "]="
159 + item.getJavascriptCondition(cond)
160 + ";");
161 }
162 counter++;
163 }
164 }
165
166 return menuJavascript.toString();
167 }
168 }