View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * This class encapsulates the context menu, used in the tree.
43   * @author Philipp Bracher
44   * @version $Revision: 32667 $ ($Author: gjoseph $)
45   */
46  public class ContextMenu extends ControlImpl {
47  
48      /**
49       * All the menu items showed by the context menu
50       */
51      private List menuItems = new ArrayList();
52  
53      /**
54       * Create a context menu and provide the name (used in javascript)
55       * @param menuName the name used for the menu
56       */
57      public ContextMenu(String menuName) {
58          super();
59          this.setName(menuName);
60      }
61  
62      /**
63       * @return all menu items
64       */
65      public List getMenuItems() {
66          return this.menuItems;
67      }
68  
69      /**
70       * Populate it with a list
71       * @param menuItems the list
72       */
73      public void setMenuItems(List menuItems) {
74          this.menuItems = menuItems;
75      }
76  
77      /**
78       * @param col index
79       * @return the item
80       */
81      public ContextMenuItem getMenuItem(int col) {
82          return (ContextMenuItem) this.getMenuItems().get(col);
83      }
84  
85      /**
86       * @param col index
87       * @return the item
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      * Add a item
102      * @param item the item object
103      */
104     public void addMenuItem(ContextMenuItem item) {
105         this.getMenuItems().add(item);
106     }
107 
108     /**
109      * Renders the HTML Code. Creates a div with all the containing menuitems and adds the initialization in javascript
110      * @return html code
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();\" >"); //$NON-NLS-1$ //$NON-NLS-2$
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>"); //$NON-NLS-1$
122             }
123             else {
124                 item.setJavascriptMenuName(getName());
125                 String id = getName() + "_MenuItem" + i; //$NON-NLS-1$
126                 item.setId(id);
127                 html.append(item.getHtml());
128                 counter++;
129             }
130             html.append("\n");
131         }
132 
133         html.append("</div>"); //$NON-NLS-1$
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; //$NON-NLS-1$
146                 item.setId(id);
147                 menuJavascript.append(getName() + ".menuItems[" //$NON-NLS-1$
148                     + counter
149                     + "]=new mgnlContextMenuItem('" //$NON-NLS-1$
150                     + id
151                     + "');\n"); //$NON-NLS-1$
152                 menuJavascript.append(getName() + ".menuItems[" + counter + "].conditions=new Object();"); //$NON-NLS-1$ //$NON-NLS-2$
153                 for (int cond = 0; cond < item.getJavascriptConditions().size(); cond++) {
154                     menuJavascript.append(getName() + ".menuItems[" //$NON-NLS-1$
155                         + counter
156                         + "].conditions[" //$NON-NLS-1$
157                         + cond
158                         + "]=" //$NON-NLS-1$
159                         + item.getJavascriptCondition(cond)
160                         + ";"); //$NON-NLS-1$
161                 }
162                 counter++;
163             }
164         }
165 
166         return menuJavascript.toString();
167     }
168 }