KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > gui > control > ContextMenu


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.gui.control;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19
20 /**
21  * This class encapsulates the context menu, used in the tree.
22  * @author Philipp Bracher
23  * @version $Revision: 6341 $ ($Author: philipp $)
24  */

25 public class ContextMenu extends ControlImpl {
26
27     /**
28      * All the menu items showed by the context menu
29      */

30     private List JavaDoc menuItems = new ArrayList JavaDoc();
31
32     /**
33      * Create a context menu and provide the name (used in javascript)
34      * @param menuName the name used for the menu
35      */

36     public ContextMenu(String JavaDoc menuName) {
37         super();
38         this.setName(menuName);
39     }
40
41     /**
42      * @return all menu items
43      */

44     public List JavaDoc getMenuItems() {
45         return this.menuItems;
46     }
47
48     /**
49      * Populate it with a list
50      * @param menuItems the list
51      */

52     public void setMenuItems(List JavaDoc menuItems) {
53         this.menuItems = menuItems;
54     }
55
56     /**
57      * @param col index
58      * @return the item
59      */

60     public ContextMenuItem getMenuItem(int col) {
61         return (ContextMenuItem) this.getMenuItems().get(col);
62     }
63
64     /**
65      * @param col index
66      * @return the item
67      */

68     public ContextMenuItem getMenuItemByName(String JavaDoc name) {
69         java.util.List JavaDoc menuItems = this.getMenuItems();
70         for (Iterator JavaDoc iter = menuItems.iterator(); iter.hasNext();) {
71             ContextMenuItem menuItem = (ContextMenuItem) iter.next();
72             if (menuItem != null && menuItem.getName() != null && menuItem.getName() == name) {
73                 return menuItem;
74             }
75         }
76         return null;
77     }
78
79     /**
80      * Add a item
81      * @param item the item object
82      */

83     public void addMenuItem(ContextMenuItem item) {
84         this.getMenuItems().add(item);
85     }
86
87     /**
88      * Renders the HTML Code. Creates a div with all the containing menuitems and adds the initialization in javascript
89      * @return html code
90      */

91     public String JavaDoc getHtml() {
92         StringBuffer JavaDoc html = new StringBuffer JavaDoc();
93         html
94             .append("<div id=\"" + getName() + "_DivMenu\" class=\"mgnlTreeMenu\" onmouseover=\"" + getName() + ".keepShowing();\" onmouseout=\"" + getName() + ".hide();\" >"); //$NON-NLS-1$ //$NON-NLS-2$
95
int counter = 0;
96
97         for (int i = 0; i < this.getMenuItems().size(); i++) {
98             ContextMenuItem item = this.getMenuItem(i);
99             if (item == null) {
100                 html.append("<div class=\"mgnlTreeMenuLine\"><!-- ie --></div>"); //$NON-NLS-1$
101
}
102             else {
103                 item.setJavascriptMenuName(getName());
104                 String JavaDoc id = getName() + "_MenuItem" + i; //$NON-NLS-1$
105
item.setId(id);
106                 html.append(item.getHtml());
107                 counter++;
108             }
109             html.append("\n");
110         }
111
112         html.append("</div>"); //$NON-NLS-1$
113
return html.toString();
114     }
115
116     public String JavaDoc getJavascript() {
117         StringBuffer JavaDoc menuJavascript = new StringBuffer JavaDoc();
118         menuJavascript.append("var " + getName() + "= new mgnlContextMenu('" + getName() + "');");
119         int counter = 0;
120         for (int i = 0; i < this.getMenuItems().size(); i++) {
121             ContextMenuItem item = this.getMenuItem(i);
122             if (item != null) {
123                 item.setJavascriptMenuName(getName());
124                 String JavaDoc id = getName() + "_MenuItem" + i; //$NON-NLS-1$
125
item.setId(id);
126                 menuJavascript.append(getName() + ".menuItems[" //$NON-NLS-1$
127
+ counter
128                     + "]=new mgnlContextMenuItem('" //$NON-NLS-1$
129
+ id
130                     + "');\n"); //$NON-NLS-1$
131
menuJavascript.append(getName() + ".menuItems[" + counter + "].conditions=new Object();"); //$NON-NLS-1$ //$NON-NLS-2$
132
for (int cond = 0; cond < item.getJavascriptConditions().size(); cond++) {
133                     menuJavascript.append(getName() + ".menuItems[" //$NON-NLS-1$
134
+ counter
135                         + "].conditions[" //$NON-NLS-1$
136
+ cond
137                         + "]=" //$NON-NLS-1$
138
+ item.getJavascriptCondition(cond)
139                         + ";"); //$NON-NLS-1$
140
}
141                 counter++;
142             }
143         }
144
145         return menuJavascript.toString();
146     }
147 }
Popular Tags