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.inline;
35  
36  import info.magnolia.cms.gui.control.Button;
37  import info.magnolia.cms.i18n.I18nContentSupport;
38  import info.magnolia.cms.i18n.I18nContentSupportFactory;
39  import info.magnolia.cms.i18n.MessagesManager;
40  import info.magnolia.cms.security.Permission;
41  import info.magnolia.cms.core.AggregationState;
42  import info.magnolia.context.MgnlContext;
43  import org.apache.commons.lang.StringUtils;
44  
45  import javax.servlet.http.HttpServletRequest;
46  import javax.servlet.jsp.JspWriter;
47  import java.io.IOException;
48  import java.io.Writer;
49  
50  
51  /**
52   * @author Vinzenz Wyser
53   * @version 2.0
54   */
55  public class ButtonEdit extends Button {
56      private String label = "buttons.edit"; //$NON-NLS-1$
57      private String dialogPath;
58      private String dialog;
59      
60      private I18nContentSupport i18nSupport = I18nContentSupportFactory.getI18nSupport();
61  
62      public ButtonEdit() {
63      }
64  
65      /**
66       * @deprecated since 4.0 - use the empty constructor.
67       */
68      public ButtonEdit(HttpServletRequest request) {
69      }
70  
71      /**
72       * @deprecated since 4.0 - do not pass an HttpServletRequest
73       */
74      public ButtonEdit(HttpServletRequest request, String path, String nodeCollectionName, String nodeName, String dialog) {
75          this(path, nodeCollectionName, nodeName, dialog);
76      }
77  
78      public ButtonEdit(String path, String nodeCollectionName, String nodeName, String dialog) {
79          this.setPath(path);
80          this.setNodeCollectionName(nodeCollectionName);
81          this.setNodeName(nodeName);
82          this.setDialog(dialog);
83      }
84  
85      /**
86       * @deprecated since 4.0 - do not pass an HttpServletRequest
87       */
88      public void setDefaultOnclick(HttpServletRequest request) {
89          setDefaultOnclick();
90      }
91  
92      public void setDefaultOnclick() {
93          String nodeCollectionName = this.getNodeCollectionName();
94          if (nodeCollectionName == null) {
95              nodeCollectionName = StringUtils.EMPTY;
96          }
97          String nodeName = this.getNodeName();
98          if (nodeName == null) {
99              nodeName = StringUtils.EMPTY;
100         }
101 
102         String repository = MgnlContext.getAggregationState().getRepository();
103         this.setOnclick("mgnlOpenDialog('" //$NON-NLS-1$
104             + this.getPath()
105             + "','" //$NON-NLS-1$
106             + nodeCollectionName
107             + "','" //$NON-NLS-1$
108             + nodeName
109             + "','" //$NON-NLS-1$
110             + this.getDialog()
111             + "','" //$NON-NLS-1$
112             + repository
113             + "',"
114             + (dialogPath != null ? "'" + dialogPath + "'" : "null")
115             + ", null" //width
116             + ", null" //height
117             + (i18nSupport.isEnabled()? ", '" + i18nSupport.getLocale().toString() + "'":"")
118             +");");
119     }
120 
121     public String getLabel() {
122         return MessagesManager.getWithDefault(label, label);
123     }
124 
125     public void setLabel(String s) {
126         this.label = s;
127     }
128 
129     public void setDialogPath(String dialogPath) {
130         this.dialogPath = dialogPath;
131     }
132     
133     public void setDialog (String dialog) {
134         this.dialog = dialog;
135     }
136     
137     public String getDialog () {
138         return this.dialog;
139     }
140 
141     /**
142      * @deprecated use drawHtml(Writer out) instead.
143      */
144     public void drawHtml(JspWriter out) throws IOException {
145         drawHtml((Writer)out);
146     }
147 
148     /**
149      * Draws the edit button. The request has to be set!
150      */
151     public void drawHtml(Writer out) throws IOException {
152         if (this.getRequest() != null) {
153             final AggregationState aggregationState = MgnlContext.getAggregationState();
154             boolean isGranted = aggregationState.getMainContent().isGranted(Permission.SET);
155             if (!aggregationState.isPreviewMode() && isGranted) {
156                 println(out, this.getHtml());
157             }
158         }
159     }
160 }