View Javadoc
1   /**
2    * This file Copyright (c) 2011-2016 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.jcr.util.NodeTypes;
37  import info.magnolia.ui.vaadin.gwt.client.editor.dom.MgnlElement;
38  
39  import com.google.gwt.dom.client.Style;
40  import com.google.gwt.user.client.ui.FlowPanel;
41  import com.google.gwt.user.client.ui.Label;
42  import com.google.gwt.user.client.ui.Widget;
43  
44  /**
45   * Base class for horizontal bars with buttons.
46   */
47  public abstract class AbstractBar extends FlowPanel {
48  
49      private final static String EDITOR_BAR_CLASS_NAME = "mgnlEditorBar";
50      private final static String EDITOR_BAR_LABEL_CLASS_NAME = "mgnlEditorBarLabel";
51      private final static String EDITOR_BAR_BUTTONS_CLASS_NAME = "mgnlEditorBarButtons";
52      private final static String EDITOR_BAR_WITH_STATUS_CLASS_NAME = "mgnlEditorBarStatusIndicator";
53      private final static String FOCUS_CLASS_NAME = "focus";
54      private final static String CHILD_FOCUS_CLASS_NAME = "childFocus";
55      private final static String MGNL_LEVEL_CLASS_NAME = "mgnlLevel-";
56  
57      protected final static String EDITOR_CLASS_NAME = "mgnlEditor";
58      protected final static String AREA_CLASS_NAME = "area";
59      protected final static String COMPONENT_CLASS_NAME = "component";
60  
61      protected final static String ICON_CLASS_NAME = "editorIcon";
62      protected final static String EDIT_CLASS_NAME = "icon-edit";
63      protected final static String ADD_CLASS_NAME = "icon-add-item";
64  
65      private final static String STATUS_INDICATOR_CLASS_NAME = "status-indicator";
66      private final static String STATUS_INACTIVATED_CLASS_NAME = "background-color-red icon-status-red";
67      private final static String STATUS_MODIFIED_CLASS_NAME = "background-color-yellow icon-status-orange";
68  
69      private final static int MAX_LEVEL = 6;
70      private final int level;
71  
72      private FlowPanel buttonWrapper;
73  
74      public AbstractBar(MgnlElement mgnlElement) {
75  
76          setStyleName(EDITOR_BAR_CLASS_NAME);
77          addStyleName(EDITOR_CLASS_NAME);
78          this.level = mgnlElement.getLevel();
79  
80          setVisible(false);
81      }
82  
83      protected void initLayout() {
84          buttonWrapper = new FlowPanel();
85          buttonWrapper.setStylePrimaryName(EDITOR_BAR_BUTTONS_CLASS_NAME);
86          add(buttonWrapper);
87  
88          createStatusIndicator();
89  
90          String label = getLabel();
91          if (label != null && !label.isEmpty()) {
92              Label areaName = new Label(label);
93              // tooltip. Nice to have when area label is truncated because too long.
94              areaName.setTitle(label);
95              areaName.setStylePrimaryName(EDITOR_BAR_LABEL_CLASS_NAME);
96              String mgnlLevel = String.valueOf(level);
97              if (level > MAX_LEVEL) {
98                  mgnlLevel = "max";
99              }
100             areaName.addStyleName(MGNL_LEVEL_CLASS_NAME + mgnlLevel);
101             // setStylePrimaryName(..) replaces gwt default css class, in this case gwt-Label
102             add(areaName);
103         }
104 
105         createControls();
106     }
107 
108     protected void createStatusIndicator() {
109         Label label = new Label();
110         label.setStyleName(STATUS_INDICATOR_CLASS_NAME);
111 
112         switch (getActivationStatus()) {
113         case NodeTypes.Activatable.ACTIVATION_STATUS_ACTIVATED:
114             removeStyleName(EDITOR_BAR_WITH_STATUS_CLASS_NAME);
115             break;
116         case NodeTypes.Activatable.ACTIVATION_STATUS_MODIFIED:
117             label.addStyleName(STATUS_MODIFIED_CLASS_NAME);
118             add(label);
119             addStyleName(EDITOR_BAR_WITH_STATUS_CLASS_NAME);
120             break;
121         case NodeTypes.Activatable.ACTIVATION_STATUS_NOT_ACTIVATED:
122             label.addStyleName(STATUS_INACTIVATED_CLASS_NAME);
123             add(label);
124             addStyleName(EDITOR_BAR_WITH_STATUS_CLASS_NAME);
125             break;
126         default:
127             removeStyleName(EDITOR_BAR_WITH_STATUS_CLASS_NAME);
128             break;
129         }
130 
131     }
132 
133     protected abstract int getActivationStatus();
134 
135     protected abstract String getLabel();
136 
137     protected abstract void createControls();
138 
139     @Override
140     public void onAttach() {
141         super.onAttach();
142     }
143 
144     protected void setId(String id) {
145         getElement().setId(id);
146     }
147 
148     protected void addButton(final Widget button) {
149         buttonWrapper.add(button);
150     }
151 
152     /**
153      * Shorthand for <code>getElement().getStyle()</code>.
154      *
155      * @return the element's underlying {@link Style}. You can use this object to manipulate the css
156      *         style attribute of this bar widget.
157      */
158     protected Style getStyle() {
159         return getElement().getStyle();
160     }
161 
162     public void removeFocus() {
163         removeStyleName(FOCUS_CLASS_NAME);
164         removeStyleName(CHILD_FOCUS_CLASS_NAME);
165     }
166 
167     public void setFocus(boolean child) {
168         String CLASS_NAME = (child) ? CHILD_FOCUS_CLASS_NAME : FOCUS_CLASS_NAME;
169         addStyleName(CLASS_NAME);
170     }
171 }