View Javadoc
1   /**
2    * This file Copyright (c) 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.admincentral.shellapp.pulse.item.list.footer;
35  
36  import info.magnolia.i18nsystem.SimpleTranslator;
37  import info.magnolia.ui.vaadin.actionbar.ActionPopup;
38  
39  import java.util.HashMap;
40  import java.util.Map;
41  
42  import javax.inject.Inject;
43  
44  import org.vaadin.peter.contextmenu.ContextMenu;
45  import org.vaadin.peter.contextmenu.ContextMenu.ContextMenuItem;
46  import org.vaadin.peter.contextmenu.ContextMenu.ContextMenuItemClickEvent;
47  
48  import com.vaadin.server.ExternalResource;
49  import com.vaadin.ui.Button.ClickEvent;
50  import com.vaadin.ui.Button.ClickListener;
51  import com.vaadin.ui.Component;
52  import com.vaadin.ui.CustomComponent;
53  import com.vaadin.ui.HorizontalLayout;
54  import com.vaadin.ui.Label;
55  import com.vaadin.ui.NativeButton;
56  
57  /**
58   * Implementation for {@link PulseListFooterView}.
59   */
60  public class PulseListFooterViewImpl extends CustomComponent implements PulseListFooterView {
61  
62      private final SimpleTranslator i18n;
63  
64      private HorizontalLayout footer = new HorizontalLayout();
65      private ContextMenu contextMenu = new ActionPopup();
66      private Label status = new Label();
67      private NativeButton actionPopupTrigger = new NativeButton();
68      private Label actionLabel;
69      private PulseListFooterView.Listener listener;
70  
71      // can't get the menu items from ContextMenu
72      private Map<String, ContextMenuItem> menuItems = new HashMap<>();
73  
74      @Inject
75      public PulseListFooterViewImpl(final SimpleTranslator i18n) {
76          this.i18n = i18n;
77          initView();
78          setCompositionRoot(footer);
79      }
80  
81      @Override
82      public void setListener(PulseListFooterView.Listener listener) {
83          this.listener = listener;
84      }
85  
86      private void initView() {
87          footer.setSizeUndefined();
88          footer.addStyleName("footer");
89  
90          actionPopupTrigger.setHtmlContentAllowed(true);
91          actionPopupTrigger.setCaption("<span class=\"icon-arrow2_e\"></span>");
92          actionPopupTrigger.addStyleName("action-popup-trigger");
93  
94          actionPopupTrigger.addClickListener(new ClickListener() {
95  
96              @Override
97              public void buttonClick(ClickEvent event) {
98                  contextMenu.open(event.getClientX(), event.getClientY());
99              }
100         });
101 
102         actionLabel = new Label(i18n.translate("pulse.footer.title"));
103         footer.addComponent(actionLabel);
104         footer.addComponent(actionPopupTrigger);
105         toggleActionTriggerVisibility(false);
106 
107         status.addStyleName("status");
108         footer.addComponent(status);
109 
110         contextMenu.addItemClickListener(new ContextMenu.ContextMenuItemClickListener() {
111 
112             @Override
113             public void contextMenuItemClicked(ContextMenuItemClickEvent event) {
114                 ContextMenuItem obj = (ContextMenuItem) event.getSource();
115                 String eventActionName = (String) obj.getData();
116                 if (listener != null) {
117                     listener.onBulkActionTriggered(eventActionName);
118                 }
119             }
120         });
121 
122         contextMenu.setAsContextMenuOf(actionPopupTrigger);
123         contextMenu.setOpenAutomatically(false);
124     }
125 
126     private void toggleActionTriggerVisibility(boolean visible) {
127         actionLabel.setVisible(visible);
128         actionPopupTrigger.setVisible(visible);
129     }
130 
131     @Override
132     public Component asVaadinComponent() {
133         return this;
134     }
135 
136     @Override
137     public void updateStatus(long totalAmount, int totalSelected) {
138         toggleActionTriggerVisibility(totalSelected != 0);
139         final String selectedMessages = totalSelected > 0 ? Integer.toString(totalSelected) : i18n.translate("pulse.footer.status.none");
140         status.setValue(i18n.translate("pulse.footer.status", totalAmount, selectedMessages));
141     }
142 
143     @Override
144     public void setActionEnabled(String actionName, boolean isEnabled) {
145         ContextMenuItem item = menuItems.get(actionName);
146         if (item != null) {
147             item.setEnabled(isEnabled);
148         }
149     }
150 
151     @Override
152     public void addAction(String actionId, String label, String icon) {
153         ExternalResource iconFontResource = new ExternalResource(ActionPopup.ICON_FONT_CODE + icon);
154         ContextMenu.ContextMenuItem menuItem = contextMenu.addItem(label, iconFontResource);
155         // Set data variable so that the event handler can determine which action to launch.
156         menuItem.setData(actionId);
157         menuItems.put(actionId, menuItem);
158     }
159 
160 }