View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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.List;
38  
39  
40  /**
41   * This class encapsulates the context menu, used in the tree.
42   * @author Philipp Bracher
43   * @version $Revision: 41137 $ ($Author: gjoseph $)
44   */
45  public class ContextMenu extends ControlImpl {
46  
47      /**
48       * All the menu items showed by the context menu.
49       */
50      private List<ContextMenuItem> menuItems = new ArrayList<ContextMenuItem>();
51  
52      /**
53       * Create a context menu and provide the name (used in javascript)
54       * @param menuName the name used for the menu
55       */
56      public ContextMenu(String menuName) {
57          super();
58          this.setName(menuName);
59      }
60  
61      /**
62       * @return all menu items
63       */
64      public List<ContextMenuItem> getMenuItems() {
65          return this.menuItems;
66      }
67  
68      /**
69       * Populate it with a list
70       * @param menuItems the list
71       */
72      public void setMenuItems(List<ContextMenuItem> menuItems) {
73          this.menuItems = menuItems;
74      }
75  
76      /**
77       * @param col index
78       * @return the item
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       * Add a item
96       * @param item the item object
97       */
98      public void addMenuItem(ContextMenuItem item) {
99          this.getMenuItems().add(item);
100     }
101 
102     /**
103      * Renders the HTML Code. Creates a div with all the containing menuitems and adds the initialization in javascript
104      * @return html code
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();\" >"); //$NON-NLS-1$ //$NON-NLS-2$
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>"); //$NON-NLS-1$
115             }
116             else {
117                 item.setJavascriptMenuName(getName());
118                 String id = getName() + "_MenuItem" + i; //$NON-NLS-1$
119                 item.setId(id);
120                 html.append(item.getHtml());
121                 counter++;
122             }
123             html.append("\n");
124         }
125 
126         html.append("</div>"); //$NON-NLS-1$
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; //$NON-NLS-1$
139                 item.setId(id);
140                 menuJavascript.append(getName() + ".menuItems[" //$NON-NLS-1$
141                     + counter
142                     + "]=new mgnlContextMenuItem('" //$NON-NLS-1$
143                     + id
144                     + "');\n"); //$NON-NLS-1$
145                 menuJavascript.append(getName() + ".menuItems[" + counter + "].conditions=new Object();"); //$NON-NLS-1$ //$NON-NLS-2$
146                 for (int cond = 0; cond < item.getJavascriptConditions().size(); cond++) {
147                     menuJavascript.append(getName() + ".menuItems[" //$NON-NLS-1$
148                         + counter
149                         + "].conditions[" //$NON-NLS-1$
150                         + cond
151                         + "]=" //$NON-NLS-1$
152                         + item.getJavascriptCondition(cond)
153                         + ";"); //$NON-NLS-1$
154                 }
155                 counter++;
156             }
157         }
158 
159         return menuJavascript.toString();
160     }
161 }