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