View Javadoc
1   /**
2    * This file Copyright (c) 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.admincentral.usermenu;
35  
36  import info.magnolia.admincentral.usermenu.actions.UserActionExecutor;
37  import info.magnolia.cms.security.MgnlUserManager;
38  import info.magnolia.cms.security.User;
39  import info.magnolia.icons.MagnoliaIcons;
40  import info.magnolia.ui.api.action.ActionExecutionException;
41  import info.magnolia.ui.api.message.Message;
42  import info.magnolia.ui.api.message.MessageType;
43  import info.magnolia.ui.api.view.View;
44  import info.magnolia.ui.framework.message.MessagesManager;
45  import info.magnolia.ui.theme.ResurfaceTheme;
46  
47  import org.apache.commons.lang3.StringUtils;
48  import org.slf4j.Logger;
49  import org.slf4j.LoggerFactory;
50  
51  import com.vaadin.contextmenu.ContextMenu;
52  import com.vaadin.ui.Button;
53  import com.vaadin.ui.Component;
54  import com.vaadin.ui.UI;
55  import com.vaadin.ui.VerticalLayout;
56  
57  /**
58   * View implementation of UserMenu admincentral component.
59   */
60  public class UserMenu implements View {
61  
62      private static final Logger log = LoggerFactory.getLogger(UserMenu.class);
63  
64      private final UI ui;
65      private final UserActionExecutor actionExecutor;
66      private final MessagesManager messagesManager;
67      private final User user;
68  
69      public UserMenu(UI ui, UserActionExecutor actionExecutor, MessagesManager messagesManager, User user) {
70          this.ui = ui;
71          this.actionExecutor = actionExecutor;
72          this.messagesManager = messagesManager;
73          this.user = user;
74      }
75  
76      @Override
77      public Component asVaadinComponent() {
78          VerticalLayout layout = new VerticalLayout();
79          layout.setSizeUndefined();
80          layout.setMargin(true);
81          layout.setSpacing(false);
82          layout.addStyleName("menu-component");
83  
84          // no-op button, just for the looks
85          Button hamburger = new Button(MagnoliaIcons.HAMBURGER_MENU);
86          hamburger.addStyleNames(ResurfaceTheme.BUTTON_ICON);
87          hamburger.setDescription(getUserName());
88          layout.addComponent(hamburger);
89  
90          ContextMenu contextMenu = new ContextMenu(hamburger, false);
91          hamburger.addClickListener(event -> contextMenu.open());
92  
93          actionExecutor.getActions().forEach(action -> contextMenu.addItem(action.getLabel(), MagnoliaIcons.forCssClass(action.getIcon()).orElse(null), selectedItem -> this.onAction(action.getName())));
94  
95          return layout;
96      }
97  
98      private String getUserName() {
99          String title = user.getProperty(MgnlUserManager.PROPERTY_TITLE);
100         String name = user.getName();
101         if (StringUtils.isNotEmpty(title) && !title.equals(name)) {
102             return String.format("%s (%s)", title, name);
103         } else {
104             return name;
105         }
106     }
107 
108     private void onAction(String actionName) {
109         try {
110             actionExecutor.execute(actionName);
111         } catch (ActionExecutionException e) {
112             Message error = new Message(MessageType.ERROR, "An error occurred while executing an action.", e.getMessage());
113             log.error("An error occurred while executing action [{}]", actionName, e);
114             messagesManager.sendLocalMessage(error);
115         }
116     }
117 }