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