View Javadoc

1   /**
2    * This file Copyright (c) 2010-2010 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.module.templatingcomponents.components;
35  
36  import info.magnolia.cms.beans.config.ServerConfiguration;
37  import info.magnolia.cms.core.AggregationState;
38  import info.magnolia.cms.core.Content;
39  import info.magnolia.cms.gui.inline.BarNew;
40  
41  import java.io.IOException;
42  import java.io.Writer;
43  import java.util.List;
44  
45  /**
46   *
47   * @author gjoseph
48   * @version $Revision: $ ($Author: $) 
49   */
50  public class NewBar extends AbstractAuthoringUiComponent {
51      private static final String DEFAULT_NEW_LABEL = "buttons.new";
52  
53      /**
54       * @param serverCfg
55       * @param aggState
56       * @param containerName the name of the node into which new paragraphs will be added; this is a child node of {@link #currentContent()}.
57       * @param allowedParagraphs the list of paragraph definitions (their names) that are allow to be added by this component
58       * @param newButtonLabel if null, default will be used
59       */
60      public static NewBar make(ServerConfiguration serverCfg, AggregationState aggState, String containerName, List<String> allowedParagraphs, String newButtonLabel) {
61          final NewBar bar = new NewBar(serverCfg, aggState);
62          /* TODO @param do we want this ? specificTarget override for {@link #currentContent()}.
63          if (specificTarget != null) {
64              bar.setContent(specificTarget);
65          }
66          */
67          bar.setContainerName(containerName);
68  
69          if (newButtonLabel != null) {
70              bar.setNewButtonLabel(newButtonLabel);
71          }
72  
73          bar.setAllowedParagraphs(allowedParagraphs);
74          return bar;
75      }
76  
77      private String containerName;
78      private String newButtonLabel = DEFAULT_NEW_LABEL;
79      private List<String> allowedParagraphs;
80  
81      public NewBar(ServerConfiguration server, AggregationState aggregationState) {
82          super(server, aggregationState);
83      }
84  
85      public void setContainerName(String containerName) {
86          this.containerName = containerName;
87      }
88  
89      public void setNewButtonLabel(String newButtonLabel) {
90          this.newButtonLabel = newButtonLabel;
91      }
92  
93      public void setAllowedParagraphs(List<String> allowedParagraphs) {
94          this.allowedParagraphs = allowedParagraphs;
95      }
96  
97      @Override
98      protected void doRender(Appendable out) throws IOException {
99          final BarNew bar = new BarNew();
100 
101         bar.setParagraph(asString(allowedParagraphs));
102 
103         final Content content = currentContent();
104         final String targetPath = content.getHandle();
105         bar.setPath(targetPath);
106 
107         // TODO - test combinations of containerName and target
108 
109         bar.setNodeCollectionName(containerName);
110         bar.setNodeName("mgnlNew"); // one of the quirks we'll have to get rid of.
111 
112         // don't set new button if there's no selectable paragraph
113         if (!allowedParagraphs.isEmpty()) {
114             bar.setDefaultButtons();
115             // final String label = allowedParagraphs.isEmpty() ? "buttons.new.noparagraph" : newButtonLabel;
116             bar.getButtonNew().setLabel(getMessage(content, newButtonLabel));
117 
118             bar.placeDefaultButtons();
119         }
120         bar.drawHtml((Writer) out);
121     }
122 
123 }