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;
35  
36  
37  import info.magnolia.templating.editor.client.dom.MgnlElement;
38  import info.magnolia.templating.editor.client.jsni.LegacyJavascript;
39  
40  import com.google.gwt.dom.client.Element;
41  import com.google.gwt.dom.client.Node;
42  import com.google.gwt.dom.client.Style;
43  import com.google.gwt.dom.client.Style.Float;
44  
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 AbstractBarWidget extends FlowPanel {
54  
55      protected boolean hasControls = false;
56      private String label = "";
57      private FlowPanel buttonWrapper;
58      private MgnlElement mgnlElement;
59  
60      public AbstractBarWidget(MgnlElement mgnlElement) {
61  
62          this.setMgnlElement(mgnlElement);
63          if (mgnlElement != null) {
64              this.label = mgnlElement.getComment().getAttribute("label");
65              if (label != null && !label.isEmpty()) {
66                  Label areaName = new Label(this.label);
67                  //tooltip. Nice to have when area label is truncated because too long.
68                  areaName.setTitle(this.label);
69                  areaName.setStylePrimaryName("mgnlEditorBarLabel");
70  
71                  //setStylePrimaryName(..) replaces gwt default css class, in this case gwt-Label
72                  add(areaName);
73              }
74          }
75          buttonWrapper = new FlowPanel();
76          buttonWrapper.setStylePrimaryName("mgnlEditorBarButtons");
77  
78          add(buttonWrapper);
79  
80  
81          setClassName("mgnlEditorBar");
82  
83      }
84  
85      protected void setId(String id){
86          getElement().setId(id);
87      }
88  
89      /**
90       * Adds this widget to this bar as a button. The default (primary) style applied is <code>mgnlEditorButton</code>. See also <code>editor.css</code>.
91       */
92      protected void addButton(final Widget button, final Float cssFloat) {
93          button.setStylePrimaryName("mgnlEditorButton");
94          button.getElement().getStyle().setFloat(cssFloat);
95  
96          buttonWrapper.add(button);
97          hasControls = true;
98      }
99  
100     /**
101      * 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>.
102      */
103     protected void addButton(final Widget button, final Float cssFloat, final String primaryStyleName) {
104         if(LegacyJavascript.isEmpty(primaryStyleName)) {
105              addButton(button, cssFloat);
106              return;
107         }
108         button.setStylePrimaryName(primaryStyleName);
109         button.getElement().getStyle().setFloat(cssFloat);
110 
111         buttonWrapper.add(button);
112         hasControls = true;
113     }
114 
115     protected void setClassName(String className) {
116         getElement().setClassName(className);
117     }
118 
119     /**
120      * @return the element's underlying {@link Style}. You can use this object to manipulate the css style attribute of this bar widget.
121      */
122     protected Style getStyle() {
123         return getElement().getStyle();
124     }
125 
126     /**
127      *  TODO: we should not have to call onAttach ourself?
128      */
129     public void attach(Element element) {
130         final Node parentNode = element.getParentNode();
131         parentNode.insertAfter(getElement(), element);
132         onAttach();
133     }
134 
135     public void toggleVisible() {
136         setVisible(!isVisible());
137     }
138 
139     @Override
140     protected void onAttach() {
141         PageEditor.model.addElements(this.getMgnlElement(), getElement());
142         super.onAttach();
143     }
144 
145     public void setMgnlElement(MgnlElement mgnlElement) {
146         this.mgnlElement = mgnlElement;
147     }
148 
149     public MgnlElement getMgnlElement() {
150         return mgnlElement;
151     }
152 
153 }