View Javadoc
1   /**
2    * This file Copyright (c) 2012-2018 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.actionbar.widget;
35  
36  import info.magnolia.ui.vaadin.gwt.client.actionbar.event.ActionTriggerEvent;
37  import info.magnolia.ui.vaadin.gwt.client.actionbar.shared.ActionbarItem;
38  
39  import com.google.gwt.event.dom.client.KeyCodes;
40  import com.google.gwt.event.dom.client.KeyPressEvent;
41  import com.google.gwt.event.dom.client.KeyPressHandler;
42  import com.google.gwt.event.dom.client.MouseDownEvent;
43  import com.google.gwt.event.dom.client.MouseDownHandler;
44  import com.google.gwt.event.dom.client.MouseOutEvent;
45  import com.google.gwt.event.dom.client.MouseOutHandler;
46  import com.google.gwt.event.dom.client.MouseUpEvent;
47  import com.google.gwt.event.dom.client.MouseUpHandler;
48  import com.google.gwt.user.client.DOM;
49  import com.google.gwt.user.client.Element;
50  import com.google.gwt.user.client.Event;
51  import com.google.gwt.user.client.ui.FocusWidget;
52  import com.google.web.bindery.event.shared.EventBus;
53  import com.googlecode.mgwt.ui.client.widget.touch.TouchDelegate;
54  import com.vaadin.client.ApplicationConnection;
55  import com.vaadin.client.ui.Icon;
56  import com.vaadin.client.ui.aria.AriaHelper;
57  
58  /**
59   * The Class VAction, which displays a single action with label and icon within an action group.
60   */
61  public class ActionbarItemWidget extends FocusWidget {
62  
63      private static final String CLASSNAME = "v-action";
64  
65      private final Element root = DOM.createElement("li");
66  
67      private final Element text = DOM.createSpan();
68  
69      // private final Element flyoutIndicator = DOM.createSpan();
70  
71      private final Element icon = DOM.createSpan();
72  
73      private final Icon iconImage;
74  
75      protected final ActionbarItem data;
76  
77      protected final EventBus eventBus;
78  
79      protected VActionbarGroup group;
80  
81      protected TouchDelegateient/widget/touch/TouchDelegate.html#TouchDelegate">TouchDelegate delegate = new TouchDelegate(this);
82  
83      protected boolean isEnabled = true;
84  
85      /**
86       * Instantiates a new action in action bar.
87       *
88       * @param data the data json object
89       * @param eventBus the event bus
90       * @param icon the icon
91       *
92       * Use {@link #VActionbarItem(VActionbarItemJSO, VActionbarGroup, EventBus)} instead.
93       */
94      @Deprecated
95      public ActionbarItemWidget(ActionbarItem data, VActionbarGroup group, EventBus eventBus, Icon icon) {
96          super();
97          this.data = data;
98          this.group = group;
99          this.eventBus = eventBus;
100         this.iconImage = icon;
101 
102         constructDOM();
103         bindHandlers();
104         update();
105     }
106 
107     /**
108      * Instantiates a new action in action bar.
109      *
110      * @param data the data json object
111      * @param group the group
112      * @param eventBus the event bus
113      */
114     public ActionbarItemWidget(ActionbarItem data, VActionbarGroup group, EventBus eventBus) {
115         super();
116         this.data = data;
117         this.group = group;
118         this.eventBus = eventBus;
119         this.iconImage = null;
120 
121         constructDOM();
122         bindHandlers();
123         update();
124     }
125 
126     private void constructDOM() {
127         setElement(root);
128         setStyleName(CLASSNAME);
129 
130         text.addClassName("v-text");
131         icon.addClassName("v-icon");
132         root.appendChild(iconImage == null ? icon : iconImage.getElement());
133         root.appendChild(text);
134         AriaHelper.bindCaption(this, text);
135     }
136 
137     protected void bindHandlers() {
138 
139         DOM.sinkEvents(getElement(), Event.MOUSEEVENTS);
140 
141         addDomHandler(new MouseDownHandler() {
142             @Override
143             public void onMouseDown(MouseDownEvent event) {
144                 addStyleName("mousedown");
145             }
146         }, MouseDownEvent.getType());
147 
148         addDomHandler(new MouseOutHandler() {
149             @Override
150             public void onMouseOut(MouseOutEvent event) {
151                 removeStyleName("mousedown");
152             }
153         }, MouseOutEvent.getType());
154 
155         addDomHandler(new MouseUpHandler() {
156             @Override
157             public void onMouseUp(MouseUpEvent event) {
158                 removeStyleName("mousedown");
159                 if (isEnabled) {
160                     eventBus.fireEvent(new ActionTriggerEvent(data.getName(), ActionbarItemWidget.this));
161                 }
162             }
163         }, MouseUpEvent.getType());
164         
165         addDomHandler(new KeyPressHandler() {
166             @Override
167             public void onKeyPress(KeyPressEvent event) {
168                 if (KeyCodes.KEY_ENTER == event.getNativeEvent().getKeyCode())
169                     eventBus.fireEvent(new ActionTriggerEvent(data.getName(), ActionbarItemWidget.this));
170             }
171         }, KeyPressEvent.getType());
172                
173     }
174 
175     public String getName() {
176         return data.getName();
177     }
178 
179     @Override
180     public void setEnabled(boolean enabled) {
181         this.isEnabled = enabled;
182         update();
183     }
184 
185     @Override
186     public boolean isEnabled() {
187         return isEnabled;
188     }
189 
190     public void update() {
191         text.setInnerText(data.getLabel());
192         if (data.getIconFontId() != null) {
193             icon.setClassName("v-icon");
194             if (data.getIconFontId() != null && !data.getIconFontId().isEmpty()) {
195                 icon.addClassName(data.getIconFontId());
196             }
197         } else if (iconImage != null) {
198             iconImage.setUri(data.getResourceUrl());
199         }
200 
201         if (isEnabled() && root.getClassName().contains(ApplicationConnection.DISABLED_CLASSNAME)) {
202             root.removeClassName(ApplicationConnection.DISABLED_CLASSNAME);
203         } else if (!isEnabled() && !root.getClassName().contains(ApplicationConnection.DISABLED_CLASSNAME)) {
204             root.addClassName(ApplicationConnection.DISABLED_CLASSNAME);
205         }
206     }
207 
208     public ActionbarItem getData() {
209         return data;
210     }     
211 }