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.control.Button;
40  import info.magnolia.cms.gui.inline.BarEdit;
41  import info.magnolia.cms.gui.inline.ButtonEdit;
42  
43  import javax.jcr.RepositoryException;
44  import java.io.IOException;
45  import java.io.Writer;
46  
47  /**
48   * This describes an "edit bar" for paragraphs. It typically displays edit, move and delete buttons.
49   * The dialog to use is normally deduced from the given content node, but can be overridden by a specific one.
50   *
51   * @author gjoseph
52   * @version $Revision: $ ($Author: $) 
53   */
54  public class EditBar extends AbstractAuthoringUiComponent {
55      private static final String DEFAULT_MOVE_LABEL = "buttons.move";
56      private static final String DEFAULT_DELETE_LABEL = "buttons.delete";
57      private static final String DEFAULT_EDIT_LABEL = "buttons.edit";
58  
59      /**
60       * @param serverCfg
61       * @param aggState
62       * @param specificContent if null, will deduce a default
63       * @param specificDialogName if null, deduced from paragraph definition of current node
64       * @param editButtonLabel if null, will use default
65       * @param enableMoveButton true should be the default
66       * @param enableDeleteButton true should be the default
67       */
68      public static EditBar make(ServerConfiguration serverCfg, AggregationState aggState, Content specificContent, String specificDialogName, String editButtonLabel, boolean enableMoveButton, boolean enableDeleteButton) {
69          // TODO - it would be nicer if this was available from some RenderingContext
70          final boolean isInSingleton = SingletonParagraphBar.isInSingleton();
71  
72          final EditBar bar = new EditBar(serverCfg, aggState);
73          if (specificContent != null) {
74              bar.setContent(specificContent);
75          } else {
76              bar.setContent(bar.currentContent());
77          }
78  
79          if (specificDialogName != null) {
80              bar.setSpecificDialogName(specificDialogName);
81          }
82  
83          if (editButtonLabel != null) {
84              bar.setEditButtonLabel(editButtonLabel);
85          }
86  
87          if (!isInSingleton) {
88              bar.setEnableMoveButton(enableMoveButton);
89          } else {
90              // force move to false for singletons
91              bar.setEnableMoveButton(false);
92          }
93          
94          bar.setEnableDeleteButton(enableDeleteButton);
95  
96          return bar;
97      }
98  
99      private Content content;
100     private String specificDialogName;
101     private String editButtonLabel = DEFAULT_EDIT_LABEL;
102     private boolean enableMoveButton = true;
103     private boolean enableDeleteButton = true;
104 
105     public EditBar(ServerConfiguration serverConfiguration, AggregationState aggregationState) {
106         super(serverConfiguration, aggregationState);
107     }
108 
109     public void setContent(Content content) {
110         this.content = content;
111     }
112 
113     public void setSpecificDialogName(String specificDialogName) {
114         this.specificDialogName = specificDialogName;
115     }
116 
117     public void setEditButtonLabel(String editButtonLabel) {
118         this.editButtonLabel = editButtonLabel;
119     }
120 
121     public void setEnableMoveButton(boolean enableMoveButton) {
122         this.enableMoveButton = enableMoveButton;
123     }
124 
125     public void setEnableDeleteButton(boolean enableDeleteButton) {
126         this.enableDeleteButton = enableDeleteButton;
127     }
128 
129     @Override
130     protected void doRender(Appendable out) throws IOException, RepositoryException {
131         final BarEdit bar = new BarEdit();
132         final String paragraphTemplateName = content.getMetaData().getTemplate();
133         // it's called "setParagraph", but have a look further, once you reach ButtonEdit, this is called "dialog".
134         if (specificDialogName != null) {
135             bar.setParagraph(specificDialogName);
136         } else {
137             bar.setParagraph(paragraphTemplateName);
138         }
139 
140         // needed for move ...
141         final Content parent = content.getParent();
142         final Content parentParent = parent.getParent();
143 
144         bar.setPath(parentParent.getHandle());
145         bar.setNodeCollectionName(parent.getName());
146         bar.setNodeName(content.getName());
147         // bar.setPath(target.getHandle()); // would work for edit but not for move
148 
149         // bar.setDefaultButtons();
150         if (enableMoveButton) {
151             // doing the below instead of setButtonMove() for clarity, but see comment above for the bar.setPath method
152             bar.setButtonMove(parent.getName(), content.getName());
153             bar.getButtonMove().setLabel(getMessage(content, DEFAULT_MOVE_LABEL));
154         } else {
155             // buttonMove is initially set to new Button, so if we don't do this, we end up with a label-less, action-less, button
156             bar.setButtonMove(null);
157         }
158         if (enableDeleteButton) {
159             // simplified delete function -
160             bar.setButtonDelete(content.getHandle());
161             // these paths would otherwise get concatenated in inline.js#mgnlDeleteNode:
162             // parentParent.getHandle(), targetParent.getName(), target.getName());
163             bar.getButtonDelete().setLabel(getMessage(content, DEFAULT_DELETE_LABEL));
164         } else {
165             // buttonDelete is initially set to new Button, so if we don't do this, we end up with a label-less, action-less, button
166             bar.setButtonDelete((Button) null);
167         }
168 
169         bar.setButtonEdit();
170         final ButtonEdit edit = bar.getButtonEdit();
171         // TODO - fix circular dependency edit.setDialogPath(ParagraphSelectDialog.EDITPARAGRAPH_DIALOG_URL);
172         edit.setDialogPath(".magnolia/dialogs/editParagraph.html");
173 
174         // TODO - yes this is a bit ugly - 1) we should in fact open the correct dialog immediately instead of faking the paragraph parameter - 2) the gui elements should not have defaults nor know anything about urls and onclick functions
175         // TODO - test with specific dialog
176         //  if (this.dialog == null) {
177         edit.setDefaultOnclick(); // re-set the onclick after having set the dialog path.
178         edit.setLabel(getMessage(content, editButtonLabel));
179 
180 
181         // TODO : display useful paragraph info (if we had the ParagraphDefinition instance, maybe ?)
182 
183         bar.placeDefaultButtons();
184 
185         bar.drawHtml((Writer) out);
186     }
187 
188 }