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  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      public String getHtml() {
98          StringBuffer html = new StringBuffer();
99          html.append("<div class=\"mgnlTreeMenuItem\" id=\"" //$NON-NLS-1$
100             + this.getId()
101             + "\" onclick=\"" //$NON-NLS-1$
102             + this.getJavascriptMenuName()
103             + ".hide();"); //$NON-NLS-1$
104         if (StringUtils.isNotEmpty(this.onclick)) {
105             html.append(this.onclick);
106         }
107 
108         String label = this.getLabel();
109         if (StringUtils.isNotEmpty(this.getIcon())) {
110             label = "<img src=\"" //$NON-NLS-1$
111                 + this.getIcon()
112                 + "\" alt=\"\" /> <span style=\"position:relative;top:-3px\">" //$NON-NLS-1$
113                 + label
114                 + "</span>"; //$NON-NLS-1$
115         }
116 
117         html.append("\" onmouseover=\"" //$NON-NLS-1$
118             + this.getJavascriptMenuName()
119             + ".menuItemHighlight(this);\"  onmouseout=\"" //$NON-NLS-1$
120             + this.getJavascriptMenuName()
121             + ".menuItemReset(this);\">" //$NON-NLS-1$
122             + label
123             + "</div>"); //$NON-NLS-1$
124         return html.toString();
125     }
126 
127     public String getIcon() {
128         return this.icon;
129     }
130 
131     public void setIcon(String icon) {
132         this.icon = icon;
133     }
134 
135     public String getJavascriptMenuName() {
136         return this.javascriptMenuName;
137     }
138 
139     public void setJavascriptMenuName(String javascriptMenuName) {
140         this.javascriptMenuName = javascriptMenuName;
141     }
142 }