View Javadoc

1   /**
2    * This file Copyright (c) 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.templating.editor.client.widget.controlbar;
35  
36  
37  import info.magnolia.templating.editor.client.PageEditor;
38  import info.magnolia.templating.editor.client.dom.MgnlElement;
39  import info.magnolia.templating.editor.client.jsni.JavascriptUtils;
40  
41  import com.google.gwt.dom.client.Element;
42  import com.google.gwt.dom.client.Node;
43  import com.google.gwt.dom.client.Style;
44  import com.google.gwt.dom.client.Style.Float;
45  
46  import com.google.gwt.user.client.ui.FlowPanel;
47  import com.google.gwt.user.client.ui.Label;
48  import com.google.gwt.user.client.ui.Widget;
49  
50  /**
51   * Base class for horizontal bars with buttons.
52   */
53  public abstract class AbstractBar extends FlowPanel {
54  
55      private String label = "";
56      private FlowPanel buttonWrapper;
57      private MgnlElement mgnlElement;
58  
59      private FlowPanel primaryButtons;
60      private FlowPanel secondaryButtons;
61  
62      public AbstractBar(MgnlElement mgnlElement) {
63  
64          this.setMgnlElement(mgnlElement);
65  
66          buttonWrapper = new FlowPanel();
67          buttonWrapper.setStylePrimaryName("mgnlEditorBarButtons");
68  
69          add(buttonWrapper);
70  
71          if (mgnlElement != null) {
72              this.label = mgnlElement.getAttribute("label");
73              if (label != null && !label.isEmpty()) {
74                  Label areaName = new Label(this.label);
75                  //tooltip. Nice to have when area label is truncated because too long.
76                  areaName.setTitle(this.label);
77                  areaName.setStylePrimaryName("mgnlEditorBarLabel");
78  
79                  //setStylePrimaryName(..) replaces gwt default css class, in this case gwt-Label
80                  add(areaName);
81              }
82          }
83  
84          setClassName("mgnlEditorBar");
85      }
86  
87      protected void setId(String id){
88          getElement().setId(id);
89      }
90  
91      /**
92       * Adds this widget to this bar as a button. The default (primary) style applied is <code>mgnlEditorButton</code>. See also <code>editor.css</code>.
93       */
94      protected void addButton(final Widget button, final Float cssFloat) {
95          button.setStylePrimaryName("mgnlEditorButton");
96          button.getElement().getStyle().setFloat(cssFloat);
97  
98          buttonWrapper.add(button);
99      }
100 
101     /**
102      * Adds this widget to this bar as a button. The default (primary) style applied is <code>mgnlEditorButton</code>. See also <code>editor.css</code>.
103      */
104     protected void addSecondaryButton(final Widget button) {
105         if (secondaryButtons == null) {
106             secondaryButtons = new FlowPanel();
107             secondaryButtons.setStylePrimaryName("mgnlEditorBarSecondaryButtons");
108             buttonWrapper.add(secondaryButtons);
109         }
110         secondaryButtons.add(button);
111     }
112 
113     /**
114      * Adds this widget to this bar as a button. The default (primary) style applied is <code>mgnlEditorButton</code>. See also <code>editor.css</code>.
115      */
116     protected void addPrimaryButton(final Widget button) {
117         if (primaryButtons == null) {
118             primaryButtons = new FlowPanel();
119             primaryButtons.setStylePrimaryName("mgnlEditorBarPrimaryButtons");
120             buttonWrapper.add(primaryButtons);
121         }
122 
123         primaryButtons.add(button);
124     }
125 
126     /**
127      * Adds this widget to this bar as a button. It allows overriding the default (primary) style applied <code>mgnlEditorButton</code>. See also <code>editor.css</code>.
128      */
129     protected void addButton(final Widget button, final Float cssFloat, final String primaryStyleName) {
130         if(JavascriptUtils.isEmpty(primaryStyleName)) {
131              addButton(button, cssFloat);
132              return;
133         }
134         button.setStylePrimaryName(primaryStyleName);
135         button.getElement().getStyle().setFloat(cssFloat);
136 
137         buttonWrapper.add(button);
138     }
139 
140     protected void setClassName(String className) {
141         getElement().setClassName(className);
142     }
143 
144     /**
145      * Shorthand for <code>getElement().getStyle()</code>.
146      * @return the element's underlying {@link Style}. You can use this object to manipulate the css style attribute of this bar widget.
147      */
148     protected Style getStyle() {
149         return getElement().getStyle();
150     }
151 
152     /**
153      *  TODO: we should not have to call onAttach ourself?
154      */
155     public void attach() {
156         if (getMgnlElement().getEditElement() != null) {
157             Element parent = getMgnlElement().getEditElement();
158             parent.insertFirst(getElement());
159             onAttach();
160         }
161         else if (getMgnlElement().getFirstElement() != null && getMgnlElement().getFirstElement() == getMgnlElement().getLastElement()) {
162             attach(getMgnlElement());
163         }
164         else {
165             attach(getMgnlElement().getComment().getElement());
166         }
167     }
168 
169     public void attach(MgnlElement mgnlElement) {
170         Element element = mgnlElement.getFirstElement();
171         if (element != null) {
172             element.insertFirst(getElement());
173         }
174         onAttach();
175     }
176 
177     public void attach(Element element) {
178         final Node parentNode = element.getParentNode();
179         parentNode.insertAfter(getElement(), element);
180         onAttach();
181     }
182 
183     public void toggleVisible() {
184         setVisible(!isVisible());
185     }
186 
187     @Override
188     protected void onAttach() {
189         PageEditor.model.addElements(this.getMgnlElement(), getElement());
190         super.onAttach();
191     }
192 
193     public void setMgnlElement(MgnlElement mgnlElement) {
194         this.mgnlElement = mgnlElement;
195     }
196 
197     public MgnlElement getMgnlElement() {
198         return mgnlElement;
199     }
200 
201     public void toggleButtons(boolean visible) {
202         MgnlElement parent = mgnlElement.getParent();
203         if (parent != null) {
204             for (MgnlElement component : parent.getComponents()) {
205                 PageEditor.model.getEditBar(component).primaryButtons.setVisible(visible);
206                 PageEditor.model.getEditBar(component).secondaryButtons.setVisible(visible);
207             }
208         }
209 
210     }
211 }