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