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.core.AggregationState;
37  import info.magnolia.cms.gui.control.Bar;
38  import info.magnolia.cms.gui.control.Button;
39  import info.magnolia.cms.i18n.I18nContentSupport;
40  import info.magnolia.cms.i18n.I18nContentSupportFactory;
41  import info.magnolia.cms.i18n.MessagesManager;
42  import info.magnolia.cms.security.Permission;
43  import info.magnolia.cms.security.PermissionUtil;
44  import info.magnolia.context.MgnlContext;
45  
46  import java.io.IOException;
47  import java.io.Writer;
48  
49  import javax.jcr.Node;
50  import javax.jcr.RepositoryException;
51  import javax.servlet.http.HttpServletRequest;
52  import javax.servlet.jsp.JspWriter;
53  
54  import org.apache.commons.lang.StringUtils;
55  
56  
57  /**
58   * @author Vinzenz Wyser
59   * @version 2.0
60   */
61  public class BarNew extends Bar {
62  
63      private String paragraph;
64  
65      private Button buttonNew = new Button();
66  
67      private final I18nContentSupport i18nSupport = I18nContentSupportFactory.getI18nSupport();
68  
69      /**
70       * @deprecated since 4.0 - use the empty constructor.
71       */
72      @Deprecated
73      public BarNew(HttpServletRequest request) {
74      }
75  
76      public BarNew() {
77  
78      }
79  
80      /**
81       * Sets the default buttons.
82       */
83      public void setDefaultButtons() {
84          this.setButtonNew();
85      }
86  
87      /**
88       * Places the default buttons to the very right/left position.
89       */
90      public void placeDefaultButtons() {
91          if (this.getButtonNew() != null) {
92              this.getButtonsLeft().add(0, this.getButtonNew());
93          }
94      }
95  
96      public Button getButtonNew() {
97          return this.buttonNew;
98      }
99  
100     public void setButtonNew(Button b) {
101         this.buttonNew = b;
102     }
103 
104     public void setButtonNew() {
105         this.setButtonNew(this.getPath(), this.getNodeCollectionName(StringUtils.EMPTY), this
106                 .getNodeName(StringUtils.EMPTY), this.getParagraph());
107     }
108 
109     /**
110      * Sets the default edit button.
111      * @param path , path of the current page
112      * @param nodeCollectionName , i.e. 'MainParagarphs'
113      * @param nodeName , i.e. '01'
114      * @param paragraph , paragraph type
115      */
116     public void setButtonNew(String path, String nodeCollectionName, String nodeName, String paragraph) {
117         Button b = new Button();
118         final String labelKey = StringUtils.isBlank(paragraph) ? "buttons.noparagraph" : "buttons.new";
119         b.setLabel(MessagesManager.getMessages().get(labelKey));
120         String repository = MgnlContext.getAggregationState().getRepository();
121         // if there are multiple paragraphs show the selectParagraph dialog
122         if (StringUtils.contains(paragraph, ',')) {
123             b.setOnclick(onClickForNewButton(path, nodeCollectionName, nodeName, paragraph, repository, "selectParagraph"));
124         } else if (StringUtils.isNotBlank(paragraph)) {
125             // there is only one paragraph
126             b.setOnclick(onClickForNewButton(path, nodeCollectionName, nodeName, paragraph, repository, "editParagraph"));
127         }
128         this.setButtonNew(b);
129     }
130 
131     protected String onClickForNewButton(String path, String nodeCollectionName, String nodeName, String paragraph, String repository, String dialogName) {
132         return "mgnlOpenDialog('"
133         + path
134         + "','"
135         + nodeCollectionName
136         + "','"
137         + nodeName
138         + "','"
139         + paragraph
140         + "','"
141         + repository
142         + "','.magnolia/dialogs/" + dialogName + ".html'"
143         + ", null" //width
144         + ", null" //height
145         + (i18nSupport.isEnabled()? ", '" + i18nSupport.getLocale().toString() + "'":"")
146         +");";
147     }
148 
149     /**
150      * @deprecated use drawHtml(Writer out) instead.
151      */
152     @Deprecated
153     public void drawHtml(JspWriter out) throws IOException {
154         drawHtml((Writer) out);
155     }
156 
157     /**
158      * Draws the main bar (incl. all magnolia specific js and css sources).
159      */
160     public void drawHtml(Writer out) throws IOException {
161         final AggregationState aggregationState = MgnlContext.getAggregationState();
162         final Node mainContent = aggregationState.getMainContent().getJCRNode();
163         boolean isGranted;
164         try {
165             isGranted = PermissionUtil.isGranted(mainContent.getSession(), mainContent.getPath(), Permission.SET);
166         } catch (RepositoryException e) {
167             // TODO dlipp - apply consistent ExceptionHandling
168             throw new RuntimeException(e);
169         }
170         if (!aggregationState.isPreviewMode() && isGranted) {
171             this.setEvent("onmousedown", "mgnlMoveNodeEnd(this,'" + this.getPath() + "');"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
172             this.setEvent("onmouseover", "mgnlMoveNodeHigh(this);"); //$NON-NLS-1$ //$NON-NLS-2$
173             this.setEvent("onmouseout", "mgnlMoveNodeReset(this);"); //$NON-NLS-1$ //$NON-NLS-2$
174             this.setId(this.getNodeCollectionName() + "__" + this.getNodeName()); //$NON-NLS-1$
175             println(out, this.getHtml());
176         }
177     }
178 
179     public String getParagraph() {
180         return this.paragraph;
181     }
182 
183     public void setParagraph(String paragraph) {
184         this.paragraph = paragraph;
185     }
186 }