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.inline;
35  
36  import info.magnolia.cms.beans.config.ServerConfiguration;
37  import info.magnolia.cms.core.AggregationState;
38  import info.magnolia.cms.gui.control.Bar;
39  import info.magnolia.cms.gui.control.Button;
40  import info.magnolia.cms.i18n.MessagesManager;
41  import info.magnolia.cms.security.Permission;
42  import info.magnolia.cms.security.PermissionUtil;
43  import info.magnolia.context.MgnlContext;
44  
45  import java.io.IOException;
46  import java.io.Writer;
47  
48  import javax.jcr.Node;
49  import javax.jcr.RepositoryException;
50  import javax.servlet.http.HttpServletRequest;
51  import javax.servlet.jsp.JspWriter;
52  
53  import org.apache.commons.lang.StringUtils;
54  
55  
56  /**
57   * @author Vinzenz Wyser
58   * @version 2.0
59   */
60  public class BarEdit extends Bar {
61  
62      private ButtonEdit buttonEdit = new ButtonEdit();
63  
64      private Button buttonMove = new Button();
65  
66      private Button buttonDelete = new Button();
67  
68      private String paragraph;
69  
70      /**
71       * @deprecated since 4.0 - use the empty constructor.
72       */
73      @Deprecated
74      public BarEdit(HttpServletRequest request) {
75      }
76  
77      public BarEdit() {
78  
79      }
80  
81      /**
82       * Sets the default buttons.
83       */
84      public void setDefaultButtons() {
85          this.setButtonEdit();
86          this.setButtonMove();
87          this.setButtonDelete();
88      }
89  
90      /**
91       * Places the default buttons to the very right/left position.
92       */
93      public void placeDefaultButtons() {
94          if (this.getButtonMove() != null) {
95              this.getButtonsLeft().add(0, this.getButtonMove());
96          }
97          if (this.getButtonEdit() != null) {
98              this.getButtonsLeft().add(0, this.getButtonEdit());
99          }
100         if (this.getButtonDelete() != null) {
101             this.getButtonsRight().add(this.getButtonsRight().size(), this.getButtonDelete());
102         }
103     }
104 
105     public ButtonEdit getButtonEdit() {
106         return this.buttonEdit;
107     }
108 
109     public void setButtonEdit(ButtonEdit b) {
110         this.buttonEdit = b;
111     }
112 
113     public void setButtonEdit() {
114         this.setButtonEdit(this.getPath(), this.getNodeCollectionName(StringUtils.EMPTY), this.getNodeName(StringUtils.EMPTY), this.getParagraph());
115     }
116 
117     /**
118      * Sets the default edit button.
119      *
120      * @param path , path of the current page
121      * @param nodeCollectionName , i.e. 'MainParagarphs'
122      * @param nodeName , i.e. '01'
123      * @param paragraph , paragraph type
124      */
125     public void setButtonEdit(String path, String nodeCollectionName, String nodeName, String paragraph) {
126         ButtonEdit b = new ButtonEdit(path, nodeCollectionName, nodeName, paragraph);
127         b.setDefaultOnclick();
128         this.setButtonEdit(b);
129     }
130 
131     public Button getButtonMove() {
132         return this.buttonMove;
133     }
134 
135     public void setButtonMove(Button b) {
136         this.buttonMove = b;
137     }
138 
139     public void setButtonMove() {
140         this.setButtonMove(this.getNodeCollectionName(StringUtils.EMPTY), this.getNodeName(StringUtils.EMPTY));
141     }
142 
143     /**
144      * Sets the default move button.
145      *
146      * @param nodeCollectionName , i.e. 'MainParagarphs'
147      * @param nodeName , i.e. '01'
148      */
149     public void setButtonMove(String nodeCollectionName, String nodeName) {
150         Button b = new Button();
151         b.setLabel(MessagesManager.get("buttons.move")); //$NON-NLS-1$
152         // sets the id of the bar
153         this.setId(nodeCollectionName + "__" + nodeName); //$NON-NLS-1$
154         b.setOnclick("mgnlMoveNodeStart('" + nodeCollectionName + "','" + nodeName + "','" + this.getId() + "');"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
155         this.setButtonMove(b);
156     }
157 
158     public Button getButtonDelete() {
159         return this.buttonDelete;
160     }
161 
162     public void setButtonDelete(Button b) {
163         this.buttonDelete = b;
164     }
165 
166     /**
167      * @deprecated use {@link #setButtonDelete(String)}
168      */
169     @Deprecated
170     public void setButtonDelete() {
171         this.setButtonDelete(this.getPath(), this.getNodeCollectionName(), this.getNodeName());
172     }
173 
174     /**
175      * Sets the default delete button.
176      *
177      * @param path , path of the current page
178      * @param nodeCollectionName , i.e. 'MainColumnParagraphs'
179      * @param nodeName , i.e. '01'
180      *
181      * @deprecated use {@link #setButtonDelete(String)}
182      */
183     @Deprecated
184     public void setButtonDelete(String path, String nodeCollectionName, String nodeName) {
185         Button b = new Button();
186         b.setLabel(MessagesManager.get("buttons.delete")); //$NON-NLS-1$
187         b.setOnclick("mgnlDeleteNode('" + path + "','" + nodeCollectionName + "','" + nodeName + "');"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
188         this.setButtonDelete(b);
189     }
190 
191     public void setButtonDelete(String pathToDelete) {
192         Button b = new Button();
193         b.setLabel(MessagesManager.get("buttons.delete"));
194         b.setOnclick("mgnlDeleteNode('" + pathToDelete + "');");
195         this.setButtonDelete(b);
196     }
197 
198     /**
199      * @deprecated use drawHtml(Writer out) instead.
200      */
201     @Deprecated
202     public void drawHtml(JspWriter out) throws IOException {
203         drawHtml((Writer) out);
204     }
205 
206     /**
207      * Draws the main bar (incl. all magnolia specific js and css sources).
208      */
209     public void drawHtml(Writer out) throws IOException {
210         final AggregationState aggregationState = MgnlContext.getAggregationState();
211         final Node mainContent = aggregationState.getMainContent().getJCRNode();
212         boolean isGranted;
213         try {
214             isGranted = PermissionUtil.isGranted(mainContent.getSession(), mainContent.getPath(), Permission.SET);
215         } catch (RepositoryException e) {
216             // TODO dlipp - apply consistent ExceptionHandling
217             throw new RuntimeException(e);
218         }
219         if (!aggregationState.isPreviewMode() && isGranted && ServerConfiguration.getInstance().isAdmin()) {
220             this.setEvent("onmousedown", "mgnlMoveNodeEnd(this,'" + this.getPath() + "');");
221             this.setEvent("onmouseover", "mgnlMoveNodeHigh(this);");
222             this.setEvent("onmouseout", "mgnlMoveNodeReset(this);");
223             println(out, getHtml());
224         }
225     }
226 
227     public String getParagraph() {
228         return this.paragraph;
229     }
230 
231     public void setParagraph(String paragraph) {
232         this.paragraph = paragraph;
233     }
234 }