View Javadoc
1   /**
2    * This file Copyright (c) 2013-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;
35  
36  import info.magnolia.cms.security.User;
37  import info.magnolia.context.MgnlContext;
38  import info.magnolia.i18nsystem.SimpleTranslator;
39  import info.magnolia.ui.admincentral.shellapp.pulse.data.PulseConstants;
40  import info.magnolia.ui.admincentral.shellapp.pulse.task.TasksListView;
41  import info.magnolia.ui.vaadin.actionbar.ActionPopup;
42  
43  import java.util.ArrayList;
44  import java.util.List;
45  import java.util.Set;
46  
47  import org.vaadin.peter.contextmenu.ContextMenu;
48  import org.vaadin.peter.contextmenu.ContextMenu.ContextMenuItem;
49  import org.vaadin.peter.contextmenu.ContextMenu.ContextMenuItemClickEvent;
50  import org.vaadin.peter.contextmenu.ContextMenu.ContextMenuItemClickListener;
51  
52  import com.vaadin.server.ExternalResource;
53  import com.vaadin.ui.Button.ClickEvent;
54  import com.vaadin.ui.Button.ClickListener;
55  import com.vaadin.ui.CustomComponent;
56  import com.vaadin.ui.HorizontalLayout;
57  import com.vaadin.ui.Label;
58  import com.vaadin.ui.NativeButton;
59  import com.vaadin.ui.Table;
60  
61  /**
62   * Footer view implementation displayed underneath the items list.
63   */
64  public final class PulseListFooter extends CustomComponent {
65  
66      private HorizontalLayout footer = new HorizontalLayout();
67      private ContextMenu contextMenu = new ActionPopup();
68      private Label status = new Label();
69      private NativeButton actionPopupTrigger = new NativeButton();
70  
71      private Table itemsTable;
72      private SimpleTranslator i18n;
73      private PulseListView.Listener messagesListener;
74      private TasksListView.Listener tasksListener;
75  
76      // can't get the menu items from ContextMenu
77      private List<ContextMenuItem> menuItems = new ArrayList<ContextMenuItem>();
78  
79      private long totalAmount;
80  
81      public PulseListFooter(final Table itemsTable, final SimpleTranslator i18n, boolean withTaskContextMenu) {
82          super();
83          this.itemsTable = itemsTable;
84          this.i18n = i18n;
85          if (withTaskContextMenu) {
86              buildTaskContextMenu();
87          } else {
88              buildMessageContextMenu();
89          }
90          construct();
91          setCompositionRoot(footer);
92      }
93  
94      private void construct() {
95          footer.setSizeUndefined();
96          footer.addStyleName("footer");
97  
98          final Label actionLabel = new Label(i18n.translate("pulse.footer.title"));
99  
100         actionPopupTrigger.setHtmlContentAllowed(true);
101         actionPopupTrigger.setCaption("<span class=\"icon-arrow2_e\"></span>");
102         actionPopupTrigger.addStyleName("action-popup-trigger");
103 
104         actionPopupTrigger.addClickListener(new ClickListener() {
105 
106             @Override
107             public void buttonClick(ClickEvent event) {
108                 contextMenu.open(event.getClientX(), event.getClientY());
109             }
110         });
111         footer.addComponent(actionLabel);
112         footer.addComponent(actionPopupTrigger);
113 
114         status.addStyleName("status");
115         footer.addComponent(status);
116 
117         contextMenu.setAsContextMenuOf(actionPopupTrigger);
118         contextMenu.setOpenAutomatically(false);
119     }
120 
121     private void buildTaskContextMenu() {
122 
123         final ExternalResource iconAssignResource = new ExternalResource(ActionPopup.ICON_FONT_CODE + "icon-user-public");
124 
125         final ContextMenuItem claim = contextMenu.addItem(i18n.translate("publish.actions.claim"), iconAssignResource);
126 
127         claim.addItemClickListener(new ContextMenuItemClickListener() {
128 
129             @Override
130             public void contextMenuItemClicked(ContextMenuItemClickEvent event) {
131                 final Set<String> selectedItems = (Set<String>) itemsTable.getValue();
132                 if (selectedItems == null || selectedItems.isEmpty()) {
133                     // nothing to do here
134                     return;
135                 }
136 
137                 tasksListener.claimTask(selectedItems);
138             }
139         });
140         claim.setEnabled(false);
141         menuItems.add(claim);
142 
143         User user = MgnlContext.getUser();
144         // TODO ideally context menu action availability should use the same mechanism and rules defined in the messageView config
145         // but as this is not straightforward, for the time being we hack it like this
146         if (user.getAllRoles().contains("superuser")) {
147             addRemoveMenuItem("publish.actions.archive");
148         }
149     }
150 
151     private void buildMessageContextMenu() {
152         addRemoveMenuItem("publish.actions.delete");
153     }
154 
155     private void addRemoveMenuItem(final String i18nKey) {
156         final ExternalResource iconDeleteResource = new ExternalResource(ActionPopup.ICON_FONT_CODE + "icon-delete");
157 
158         final ContextMenuItem remove = contextMenu.addItem(i18n.translate(i18nKey), iconDeleteResource);
159 
160         remove.addItemClickListener(new ContextMenuItemClickListener() {
161 
162             @Override
163             public void contextMenuItemClicked(ContextMenuItemClickEvent event) {
164                 final Set<String> selectedItems = (Set<String>) itemsTable.getValue();
165                 if (selectedItems == null || selectedItems.isEmpty()) {
166                     // nothing to do here
167                     return;
168                 }
169                 messagesListener.deleteItems(selectedItems);
170             }
171         });
172 
173         remove.setEnabled(false);
174 
175         menuItems.add(remove);
176     }
177 
178     public void updateStatus() {
179         int totalSelected = 0;
180 
181         for (String id : (Set<String>) itemsTable.getValue()) {
182             // skip generated header rows when grouping messages
183             if (id.startsWith(PulseConstants.GROUP_PLACEHOLDER_ITEMID)) {
184                 continue;
185             }
186             totalSelected++;
187         }
188         // TODO ideally context menu action availability should use the same mechanism and rules defined in the messageView config
189         // but as this is not straightforward, for the time being we hack it like this
190         enableActions(totalSelected > 0);
191 
192 
193         final String selectedMessagesAsString = totalSelected > 0 ? Integer.toString(totalSelected) : i18n.translate("pulse.footer.status.none");
194         status.setValue(i18n.translate("pulse.footer.status", totalAmount, selectedMessagesAsString));
195     }
196 
197     public void setMessagesListener(final PulseListView.Listener listener) {
198         this.messagesListener = listener;
199     }
200 
201     public void setTasksListener(final TasksListView.Listener listener) {
202         this.tasksListener = listener;
203     }
204 
205     private void enableActions(boolean enable) {
206         for (ContextMenuItem item : menuItems) {
207             item.setEnabled(enable);
208         }
209     }
210 
211     public void setTotalAmount(long amount) {
212         this.totalAmount = amount;
213         updateStatus();
214     }
215 }