View Javadoc

1   /**
2    * This file Copyright (c) 2003-2013 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$ ($Author$)
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     @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();\" >"); //$NON-NLS-1$ //$NON-NLS-2$
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>"); //$NON-NLS-1$
116             }
117             else {
118                 item.setJavascriptMenuName(getName());
119                 String id = getName() + "_MenuItem" + i; //$NON-NLS-1$
120                 item.setId(id);
121                 html.append(item.getHtml());
122                 counter++;
123             }
124             html.append("\n");
125         }
126 
127         html.append("</div>"); //$NON-NLS-1$
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; //$NON-NLS-1$
140                 item.setId(id);
141                 menuJavascript.append(getName() + ".menuItems[" //$NON-NLS-1$
142                     + counter
143                     + "]=new mgnlContextMenuItem('" //$NON-NLS-1$
144                     + id
145                     + "');\n"); //$NON-NLS-1$
146                 menuJavascript.append(getName() + ".menuItems[" + counter + "].conditions=new Object();"); //$NON-NLS-1$ //$NON-NLS-2$
147                 for (int cond = 0; cond < item.getJavascriptConditions().size(); cond++) {
148                     menuJavascript.append(getName() + ".menuItems[" //$NON-NLS-1$
149                         + counter
150                         + "].conditions[" //$NON-NLS-1$
151                         + cond
152                         + "]=" //$NON-NLS-1$
153                         + item.getJavascriptCondition(cond)
154                         + ";"); //$NON-NLS-1$
155                 }
156                 counter++;
157             }
158         }
159 
160         return menuJavascript.toString();
161     }
162 }