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