View Javadoc
1   /**
2    * This file Copyright (c) 2011-2015 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.widget.controlbar;
35  
36  import info.magnolia.ui.vaadin.gwt.client.editor.dom.MgnlElement;
37  
38  import com.google.gwt.dom.client.Style;
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  /**
44   * Base class for horizontal bars with buttons.
45   */
46  public abstract class AbstractBar extends FlowPanel {
47  
48      private final static String EDITOR_BAR_CLASS_NAME = "mgnlEditorBar";
49      private final static String EDITOR_BAR_LABEL_CLASS_NAME = "mgnlEditorBarLabel";
50      private final static String EDITOR_BAR_BUTTONS_CLASS_NAME = "mgnlEditorBarButtons";
51      private final static String FOCUS_CLASS_NAME = "focus";
52      private final static String CHILD_FOCUS_CLASS_NAME = "childFocus";
53      private final static String MGNL_LEVEL_CLASS_NAME = "mgnlLevel-";
54  
55      protected final static String EDITOR_CLASS_NAME = "mgnlEditor";
56      protected final static String AREA_CLASS_NAME = "area";
57      protected final static String COMPONENT_CLASS_NAME = "component";
58  
59      protected final static String ICON_CLASS_NAME = "editorIcon";
60      protected final static String EDIT_CLASS_NAME = "icon-edit";
61      protected final static String ADD_CLASS_NAME = "icon-add-item";
62      
63      private final static int MAX_LEVEL = 6;
64      private final int level;
65  
66      private FlowPanel buttonWrapper;
67  
68      public AbstractBar(MgnlElement mgnlElement) {
69  
70          setStyleName(EDITOR_BAR_CLASS_NAME);
71          addStyleName(EDITOR_CLASS_NAME);
72          this.level = mgnlElement.getLevel();
73  
74          setVisible(false);
75      }
76  
77      protected void initLayout() {
78          buttonWrapper = new FlowPanel();
79          buttonWrapper.setStylePrimaryName(EDITOR_BAR_BUTTONS_CLASS_NAME);
80          add(buttonWrapper);
81  
82          String label = getLabel();
83          if (label != null && !label.isEmpty()) {
84              Label areaName = new Label(label);
85              // tooltip. Nice to have when area label is truncated because too long.
86              areaName.setTitle(label);
87              areaName.setStylePrimaryName(EDITOR_BAR_LABEL_CLASS_NAME);
88              String mgnlLevel = String.valueOf(level);
89              if (level > MAX_LEVEL) {
90                  mgnlLevel = "max";
91              }
92              areaName.addStyleName(MGNL_LEVEL_CLASS_NAME + mgnlLevel);
93              // setStylePrimaryName(..) replaces gwt default css class, in this case gwt-Label
94              add(areaName);
95          }
96  
97          createControls();
98      }
99  
100     protected abstract String getLabel();
101 
102     protected abstract void createControls();
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     protected void addButton(final Widget button) {
114         buttonWrapper.add(button);
115     }
116 
117     /**
118      * Shorthand for <code>getElement().getStyle()</code>.
119      *
120      * @return the element's underlying {@link Style}. You can use this object to manipulate the css
121      *         style attribute of this bar widget.
122      */
123     protected Style getStyle() {
124         return getElement().getStyle();
125     }
126 
127     public void removeFocus() {
128         removeStyleName(FOCUS_CLASS_NAME);
129         removeStyleName(CHILD_FOCUS_CLASS_NAME);
130     }
131 
132     public void setFocus(boolean child) {
133         String CLASS_NAME = (child) ? CHILD_FOCUS_CLASS_NAME : FOCUS_CLASS_NAME;
134         addStyleName(CLASS_NAME);
135     }
136 }