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.ui.vaadin.gwt.client.editor.widget.controlbar;
35  
36  import com.google.gwt.dom.client.Style;
37  import com.google.gwt.dom.client.Style.Float;
38  import com.google.gwt.event.shared.EventBus;
39  import com.google.gwt.user.client.ui.FlowPanel;
40  import com.google.gwt.user.client.ui.Label;
41  import com.google.gwt.user.client.ui.Widget;
42  
43  import info.magnolia.ui.vaadin.gwt.client.editor.dom.CmsNode;
44  import info.magnolia.ui.vaadin.gwt.client.editor.dom.MgnlElement;
45  import info.magnolia.ui.vaadin.gwt.client.editor.jsni.JavascriptUtils;
46  
47  
48  /**
49   * Base class for horizontal bars with buttons.
50   */
51  public abstract class AbstractBar extends FlowPanel {
52  
53      protected final static String ICON_CLASSNAME = "editorIcon";
54      protected final static String EDIT_CLASSNAME = "icon-edit";
55      protected final static String REMOVE_CLASSNAME = "icon-trash";
56      protected final static String ADD_CLASSNAME = "icon-add";
57  
58      private String workspace;
59  
60      private String path;
61  
62      private String label = "";
63  
64      private final FlowPanel buttonWrapper;
65  
66      private CmsNode cmsNode;
67  
68      private FlowPanel primaryButtons;
69  
70      private FlowPanel secondaryButtons;
71  
72      private final EventBus eventBus;
73  
74      private final static String FOCUS_CLASSNAME = "focus";
75  
76      private final static String CHILD_FOCUS_CLASSNAME = "childFocus";
77  
78      public AbstractBar(EventBus eventBus, MgnlElement mgnlElement) {
79          this.eventBus = eventBus;
80  
81          this.setCmsNode(mgnlElement);
82  
83          buttonWrapper = new FlowPanel();
84          buttonWrapper.setStylePrimaryName("mgnlEditorBarButtons");
85  
86          add(buttonWrapper);
87  
88          if (mgnlElement != null) {
89              this.label = mgnlElement.getAttribute("label");
90              if (label != null && !label.isEmpty()) {
91                  Label areaName = new Label(this.label);
92                  // tooltip. Nice to have when area label is truncated because too long.
93                  areaName.setTitle(this.label);
94                  areaName.setStylePrimaryName("mgnlEditorBarLabel");
95  
96                  // setStylePrimaryName(..) replaces gwt default css class, in this case gwt-Label
97                  add(areaName);
98              }
99          }
100 
101         setStyleName("mgnlEditor mgnlEditorBar");
102     }
103 
104     @Override
105     public void onAttach() {
106         super.onAttach();
107     }
108 
109     protected void setId(String id) {
110         getElement().setId(id);
111     }
112 
113     /**
114      * Adds this widget to this bar as a button. The default (primary) style applied is
115      * <code>mgnlEditorButton</code>. See also <code>editor.css</code>.
116      */
117     protected void addButton(final Widget button, final Float cssFloat) {
118         button.setStylePrimaryName("mgnlEditorButton");
119         button.getElement().getStyle().setFloat(cssFloat);
120 
121         buttonWrapper.add(button);
122     }
123 
124     /**
125      * Adds this widget to this bar as a button. The default (primary) style applied is
126      * <code>mgnlEditorButton</code>. See also <code>editor.css</code>.
127      */
128     protected void addSecondaryButton(final Widget button) {
129         if (secondaryButtons == null) {
130             secondaryButtons = new FlowPanel();
131             secondaryButtons.setStylePrimaryName("mgnlEditorBarSecondaryButtons");
132             buttonWrapper.add(secondaryButtons);
133         }
134         secondaryButtons.add(button);
135     }
136 
137     /**
138      * Adds this widget to this bar as a button. The default (primary) style applied is
139      * <code>mgnlEditorButton</code>. See also <code>editor.css</code>.
140      */
141     protected void addPrimaryButton(final Widget button) {
142         if (primaryButtons == null) {
143             primaryButtons = new FlowPanel();
144             primaryButtons.setStylePrimaryName("mgnlEditorBarPrimaryButtons");
145             buttonWrapper.add(primaryButtons);
146         }
147 
148         primaryButtons.add(button);
149     }
150 
151     /**
152      * Adds this widget to this bar as a button. It allows overriding the default (primary) style
153      * applied <code>mgnlEditorButton</code>. See also <code>editor.css</code>.
154      */
155     protected void addButton(final Widget button, final Float cssFloat, final String primaryStyleName) {
156         if (JavascriptUtils.isEmpty(primaryStyleName)) {
157             addButton(button, cssFloat);
158             return;
159         }
160         button.setStylePrimaryName(primaryStyleName);
161         button.getElement().getStyle().setFloat(cssFloat);
162 
163         buttonWrapper.add(button);
164     }
165 
166     /**
167      * Shorthand for <code>getElement().getStyle()</code>.
168      * @return the element's underlying {@link Style}. You can use this object to manipulate the css
169      * style attribute of this bar widget.
170      */
171     protected Style getStyle() {
172         return getElement().getStyle();
173     }
174 
175 
176 
177     public void toggleVisible() {
178         setVisible(!isVisible());
179     }
180 
181 
182     public void setCmsNode(CmsNode cmsNode) {
183         this.cmsNode = cmsNode;
184     }
185 
186     public CmsNode getCmsNode() {
187         return cmsNode;
188     }
189 
190     public void toggleButtons(boolean visible) {
191         CmsNode parent = cmsNode.getParent();
192         if (parent != null) {
193             for (CmsNode component : parent.getComponents()) {
194                 component.asMgnlElement().getControlBar().primaryButtons.setVisible(visible);
195                 component.asMgnlElement().getControlBar().secondaryButtons.setVisible(visible);
196             }
197         }
198     }
199 
200     public EventBus getEventBus() {
201         return eventBus;
202     }
203 
204     public void removeFocus() {
205         removeStyleName(FOCUS_CLASSNAME);
206         removeStyleName(CHILD_FOCUS_CLASSNAME);
207     }
208 
209     public void setFocus(boolean child) {
210         String className = (child) ? CHILD_FOCUS_CLASSNAME : FOCUS_CLASSNAME;
211         addStyleName(className);
212     }
213 
214     public String getPath() {
215         return path;
216     }
217 
218     public void setPath(String path) {
219         this.path = path;
220     }
221 
222     public String getWorkspace() {
223         return workspace;
224     }
225 
226     public void setWorkspace(String workspace) {
227         this.workspace = workspace;
228     }
229 
230     public abstract String getDialog();
231 
232 }