View Javadoc
1   /**
2    * This file Copyright (c) 2003-2014 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  import org.apache.commons.lang.StringUtils;
40  import info.magnolia.cms.i18n.Messages;
41  
42  import javax.servlet.http.HttpServletRequest;
43  
44  /**
45   * @author Vinzenz Wyser
46   * @version 2.0
47   */
48  public class ContextMenuItem extends ControlImpl {
49  
50      public static ContextMenuItem getRefreshMenuItem(Tree tree, Messages msgs, HttpServletRequest request) {
51          ContextMenuItem menuRefresh = new ContextMenuItem("refresh");
52          menuRefresh.setLabel(msgs.get("tree.menu.refresh")); //$NON-NLS-1$
53          menuRefresh.setIcon(request.getContextPath() + "/.resources/icons/16/refresh.gif"); //$NON-NLS-1$
54          menuRefresh.setOnclick(tree.getJavascriptTree() + ".refresh();"); //$NON-NLS-1$
55          return menuRefresh;
56      }
57  
58      private String icon;
59  
60      private String onclick;
61  
62      private String javascriptMenuName;
63  
64      private List javascriptConditions = new ArrayList();
65  
66      public ContextMenuItem() {
67      }
68  
69      public ContextMenuItem(String name) {
70          this.setName(name);
71      }
72  
73      public void setOnclick(String s) {
74          this.onclick = s;
75      }
76  
77      public String getOnclick() {
78          return this.onclick;
79      }
80  
81      /**
82       * Must be a object with a method test(): addJavascriptCondition("new MyCondition()").
83       */
84      public void addJavascriptCondition(String methodName) {
85          this.javascriptConditions.add(methodName);
86      }
87  
88      public List getJavascriptConditions() {
89          return this.javascriptConditions;
90      }
91  
92      public String getJavascriptCondition(int index) {
93          return (String) this.javascriptConditions.get(index);
94      }
95  
96      // todo: icons
97      @Override
98      public String getHtml() {
99          StringBuffer html = new StringBuffer();
100         html.append("<div class=\"mgnlTreeMenuItem\" id=\"" //$NON-NLS-1$
101             + this.getId()
102             + "\" onclick=\"" //$NON-NLS-1$
103             + this.getJavascriptMenuName()
104             + ".hide();"); //$NON-NLS-1$
105         if (StringUtils.isNotEmpty(this.onclick)) {
106             html.append(this.onclick);
107         }
108 
109         String label = this.getLabel();
110         if (StringUtils.isNotEmpty(this.getIcon())) {
111             label = "<img src=\"" //$NON-NLS-1$
112                 + this.getIcon()
113                 + "\" alt=\"\" /> <span style=\"position:relative;top:-3px\">" //$NON-NLS-1$
114                 + label
115                 + "</span>"; //$NON-NLS-1$
116         }
117 
118         html.append("\" onmouseover=\"" //$NON-NLS-1$
119             + this.getJavascriptMenuName()
120             + ".menuItemHighlight(this);\"  onmouseout=\"" //$NON-NLS-1$
121             + this.getJavascriptMenuName()
122             + ".menuItemReset(this);\">" //$NON-NLS-1$
123             + label
124             + "</div>"); //$NON-NLS-1$
125         return html.toString();
126     }
127 
128     public String getIcon() {
129         return this.icon;
130     }
131 
132     public void setIcon(String icon) {
133         this.icon = icon;
134     }
135 
136     public String getJavascriptMenuName() {
137         return this.javascriptMenuName;
138     }
139 
140     public void setJavascriptMenuName(String javascriptMenuName) {
141         this.javascriptMenuName = javascriptMenuName;
142     }
143 }