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.gui.control.Control;
41  import info.magnolia.cms.gui.control.ControlImpl;
42  import info.magnolia.cms.gui.i18n.I18nAuthoringSupport;
43  import info.magnolia.cms.gui.misc.Sources;
44  import info.magnolia.cms.i18n.MessagesManager;
45  import info.magnolia.cms.security.Permission;
46  import info.magnolia.cms.security.PermissionUtil;
47  import info.magnolia.context.MgnlContext;
48  
49  import java.io.IOException;
50  import java.io.Writer;
51  
52  import javax.jcr.Node;
53  import javax.jcr.RepositoryException;
54  import javax.servlet.http.HttpServletRequest;
55  import javax.servlet.jsp.JspWriter;
56  
57  
58  /**
59   * @author Vinzenz Wyser
60   * @version 2.0
61   */
62  public class BarMain extends Bar {
63  
64      private Button buttonEditView = new Button();
65  
66      private Button buttonPreview = new ButtonEdit();
67  
68      private Button buttonProperties = new Button();
69  
70      private Button buttonSiteAdmin = new Button();
71  
72      private Control languageChooser;
73  
74      private String dialog;
75  
76      private int top;
77  
78      private int left;
79  
80      private String width = "100%"; //$NON-NLS-1$
81  
82      private boolean overlay = true;
83  
84      /**
85       * True if the AdminCentral button is visible.
86       */
87      private boolean adminButtonVisible = true;
88  
89      /**
90       * @deprecated since 4.0 - use the empty constructor.
91       */
92      @Deprecated
93      public BarMain(HttpServletRequest request) {
94      }
95  
96      /**
97       * @deprecated since 4.0
98       */
99      @Deprecated
100     public BarMain(HttpServletRequest request, String path, String nodeCollectionName, String nodeName, String paragraph) {
101         this(path, nodeCollectionName, nodeName, paragraph);
102     }
103 
104     public BarMain(String path, String nodeCollectionName, String nodeName, String dialog) {
105         this.setPath(path);
106         this.setNodeCollectionName(nodeCollectionName);
107         this.setNodeName(nodeName);
108         this.setDialog(dialog);
109     }
110 
111     public BarMain() {
112     }
113 
114     /**
115      * Sets the default buttons.
116      */
117     public void setDefaultButtons() {
118         this.setButtonEditView();
119         this.setButtonPreview();
120         this.setButtonSiteAdmin();
121         this.setButtonProperties();
122         this.setLanguageChooser();
123     }
124 
125     /**
126      * Places the default buttons to the very right/left position.
127      */
128     public void placeDefaultButtons() {
129         if (this.isAdminButtonVisible()) {
130             this.getButtonsLeft().add(0, this.getButtonSiteAdmin());
131         }
132         this.getButtonsLeft().add(0, this.getButtonPreview());
133 
134         if(this.languageChooser != null){
135             getButtonsRight().add(this.languageChooser);
136             getButtonsRight().add(new ControlImpl(){
137                 @Override
138                 public String getHtml() {
139                     return " ";
140                 }
141             });
142         }
143 
144         if (this.getDialog() != null) {
145             this.getButtonsRight().add(this.getButtonProperties());
146         }
147     }
148 
149     public Button getButtonProperties() {
150         return this.buttonProperties;
151     }
152 
153     public void setButtonProperties(Button b) {
154         this.buttonProperties = b;
155     }
156 
157     public void setButtonProperties() {
158         this.setButtonProperties(this.getPath(), this.getDialog());
159     }
160 
161     /**
162      * Sets the default page properties button.
163      *
164      * @param path , path of the current page
165      * @param dialog , paragraph type
166      */
167     public void setButtonProperties(String path, String dialog) {
168         ButtonEdit b = new ButtonEdit();
169         b.setLabel(MessagesManager.get("buttons.properties")); //$NON-NLS-1$
170         b.setPath(path);
171         b.setDialog(dialog);
172         b.setDefaultOnclick();
173         this.setButtonProperties(b);
174     }
175 
176     public Button getButtonPreview() {
177         return this.buttonPreview;
178     }
179 
180     public void setButtonPreview(Button b) {
181         this.buttonPreview = b;
182     }
183 
184     /**
185      * Sets the default preview button (to switch from edit to preview mode).
186      */
187     public void setButtonPreview() {
188         Button b = new Button();
189         b.setLabel(MessagesManager.get("buttons.preview")); //$NON-NLS-1$
190         b.setOnclick("mgnlPreview(true);"); //$NON-NLS-1$
191         this.setButtonPreview(b);
192     }
193 
194     public Button getButtonEditView() {
195         return this.buttonEditView;
196     }
197 
198     public void setButtonEditView(Button b) {
199         this.buttonEditView = b;
200     }
201 
202     /**
203      * Sets the default edit view button (to switch form preview to edit view mode).
204      */
205     public void setButtonEditView() {
206         Button b = new Button();
207         b.setLabel(MessagesManager.get("buttons.preview.hidden")); //$NON-NLS-1$
208         b.setOnclick("mgnlPreview(false);"); //$NON-NLS-1$
209         this.setButtonEditView(b);
210     }
211 
212     public Button getButtonSiteAdmin() {
213         return this.buttonSiteAdmin;
214     }
215 
216     public void setButtonSiteAdmin(Button b) {
217         this.buttonSiteAdmin = b;
218     }
219 
220     public void setButtonSiteAdmin() {
221         this.setButtonSiteAdmin(this.getPath());
222     }
223 
224     /**
225      * Sets the default site admin button.
226      * @param path , path of the current page (will show up in site admin)
227      */
228     public void setButtonSiteAdmin(String path) {
229         Button b = new Button();
230         b.setLabel(MessagesManager.get("buttons.admincentral")); //$NON-NLS-1$
231         String repository = MgnlContext.getAggregationState().getRepository();
232         b.setOnclick("MgnlAdminCentral.showTree('"+repository+"','" + path + "');"); //$NON-NLS-1$ //$NON-NLS-2$
233         this.setButtonSiteAdmin(b);
234     }
235 
236     protected void setLanguageChooser() {
237         I18nAuthoringSupport i18nAuthoringSupport = I18nAuthoringSupport.Factory.getInstance();
238         if(i18nAuthoringSupport.isEnabled()){
239             languageChooser = i18nAuthoringSupport.getLanguageChooser();
240         }
241     }
242 
243     public Control getLanguageChooser() {
244         return this.languageChooser;
245     }
246 
247     public void setLanguageChooser(Control languageChooser) {
248         this.languageChooser = languageChooser;
249     }
250 
251     public void setTop(int i) {
252         this.top = i;
253     }
254 
255     public int getTop() {
256         return this.top;
257     }
258 
259     public void setLeft(int i) {
260         this.left = i;
261     }
262 
263     public int getLeft() {
264         return this.left;
265     }
266 
267     public void setWidth(String s) {
268         this.width = s;
269     }
270 
271     public String getWidth() {
272         return this.width;
273     }
274 
275     /**
276      * Sets if the main bar overlays the content (true, default) or if it is moving it downward (false).
277      */
278     public void setOverlay(boolean b) {
279         this.overlay = b;
280     }
281 
282     public boolean getOverlay() {
283         return this.overlay;
284     }
285 
286     public String getDialog() {
287         return this.dialog;
288     }
289 
290     public void setDialog(String dialog) {
291         this.dialog = dialog;
292     }
293 
294     /**
295      * @deprecated use drawHtml(Writer out) instead.
296      */
297     @Deprecated
298     public void drawHtml(JspWriter out) throws IOException {
299         drawHtml((Writer) out);
300     }
301 
302     /**
303      * Draws the main bar (incl. all magnolia specific js and css links).
304      */
305     public void drawHtml(Writer out) throws IOException {
306         if (ServerConfiguration.getInstance().isAdmin()) {
307 
308             final AggregationState aggregationState = MgnlContext.getAggregationState();
309             final Node mainContent = aggregationState.getMainContent().getJCRNode();
310             boolean isGranted;
311             try {
312                 isGranted = PermissionUtil.isGranted(mainContent.getSession(), mainContent.getPath(), Permission.SET);
313             } catch (RepositoryException e) {
314                 // TODO dlipp - apply consistent ExceptionHandling
315                 throw new RuntimeException(e);
316             }
317             if (isGranted) {
318 
319                 // check if links have already been added.
320                 if (this.getRequest().getAttribute(Sources.REQUEST_LINKS_DRAWN) == null) {
321                     this.drawHtmlLinks(out);
322                     this.getRequest().setAttribute(Sources.REQUEST_LINKS_DRAWN, Boolean.TRUE);
323                 }
324 
325                 int top = this.getTop();
326                 int left = this.getLeft();
327 
328                 if (!aggregationState.isPreviewMode()) {
329                     // is edit mode
330                     this.setSmall(false);
331                     if (this.getOverlay()) {
332                         println(out, "<div class=\"mgnlMainbar\" style=\"top:" //$NON-NLS-1$
333                                 + top
334                                 + "px;left:" //$NON-NLS-1$
335                                 + left
336                                 + "px;width:" //$NON-NLS-1$
337                                 + this.getWidth()
338                                 + ";\">"); //$NON-NLS-1$
339                     }
340                     println(out, this.getHtml());
341                     if (this.getOverlay()) {
342                         println(out, "</div>"); //$NON-NLS-1$
343                     }
344                 }
345                 else {
346                     // is in preview mode
347                     top += 4;
348                     left += 4;
349                     println(out, "<div class=\"mgnlMainbarPreview\" style=\"top:" + top + "px;left:" + left + "px;\">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
350                     println(out, this.getButtonEditView().getHtml());
351                     println(out, "</div>"); //$NON-NLS-1$
352                 }
353             }
354         }
355     }
356 
357     /**
358      * @deprecated use drawHtmlLinks(Writer out) instead.
359      */
360     @Deprecated
361     public void drawHtmlLinks(JspWriter out) throws IOException {
362         drawHtmlLinks((Writer) out);
363     }
364 
365     /**
366      * Draws the magnolia specific js and css links).
367      */
368     public void drawHtmlLinks(Writer out) throws IOException {
369         println(out, new Sources(this.getRequest().getContextPath()).getHtmlCss());
370         println(out, new Sources(this.getRequest().getContextPath()).getHtmlJs());
371     }
372 
373     public boolean isAdminButtonVisible() {
374         return this.adminButtonVisible;
375     }
376 
377     public void setAdminButtonVisible(boolean adminButtonVisible) {
378         this.adminButtonVisible = adminButtonVisible;
379     }
380 }