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.context.Context;
40  import info.magnolia.icons.MagnoliaIcons;
41  import info.magnolia.ui.api.action.ActionExecutionException;
42  import info.magnolia.ui.api.message.Message;
43  import info.magnolia.ui.api.message.MessageType;
44  import info.magnolia.ui.framework.message.MessagesManager;
45  import info.magnolia.ui.theme.ResurfaceTheme;
46  
47  import javax.inject.Inject;
48  
49  import org.apache.commons.lang3.StringUtils;
50  import org.slf4j.Logger;
51  import org.slf4j.LoggerFactory;
52  
53  import com.vaadin.contextmenu.ContextMenu;
54  import com.vaadin.shared.ui.ContentMode;
55  import com.vaadin.ui.Button;
56  import com.vaadin.ui.Composite;
57  import com.vaadin.ui.CssLayout;
58  import com.vaadin.ui.Label;
59  import com.vaadin.ui.VerticalLayout;
60  
61  /**
62   * UserMenu admincentral component.
63   */
64  public class UserMenu extends Composite {
65  
66      private static final Logger log = LoggerFactory.getLogger(UserMenu.class);
67  
68      private final UserActionExecutor actionExecutor;
69      private final MessagesManager messagesManager;
70  
71      @Inject
72      public UserMenu(UserActionExecutor actionExecutor, MessagesManager messagesManager, Context context) {
73  
74          this.actionExecutor = actionExecutor;
75          this.messagesManager = messagesManager;
76  
77          VerticalLayout layout = new VerticalLayout();
78          layout.setSizeUndefined();
79          layout.setMargin(true);
80          layout.setSpacing(false);
81          layout.addStyleNames("header-component", "menu-component");
82  
83          Label userIcon = new Label();
84          userIcon.setStyleName("user-icon");
85          userIcon.setContentMode(ContentMode.HTML);
86          userIcon.setValue("<span class='" + MagnoliaIcons.USER_PUBLIC.getCssClass() + "'></span>");
87  
88          Button openArrow = new Button(MagnoliaIcons.ARROW1_S);
89          openArrow.addStyleNames(ResurfaceTheme.BUTTON_ICON, "open-arrow");
90          openArrow.setDescription(getUserName(context.getUser()));
91  
92          CssLayout cssLayout = new CssLayout();
93          cssLayout.addComponents(userIcon, openArrow);
94  
95          Label label = new Label(getUserName(context.getUser()));
96          label.addStyleName("label");
97  
98          layout.addComponents(cssLayout, label);
99  
100         ContextMenu contextMenu = new ContextMenu(openArrow, false);
101         openArrow.addClickListener(event -> contextMenu.open(event.getClientX(), event.getClientY()));
102 
103         actionExecutor.getActions()
104                 .forEach(action -> contextMenu.addItem(action.getLabel(),
105                         MagnoliaIcons.forCssClass(action.getIcon())
106                                 .orElse(null), selectedItem -> this.onAction(action.getName())));
107 
108         setCompositionRoot(layout);
109     }
110 
111     private String getUserName(User user) {
112         String title = user.getProperty(MgnlUserManager.PROPERTY_TITLE);
113         String name = user.getName();
114         if (StringUtils.isNotEmpty(title) && !title.equals(name)) {
115             return String.format("%s (%s)", title, name);
116         } else {
117             return name;
118         }
119     }
120 
121     private void onAction(String actionName) {
122         try {
123             actionExecutor.execute(actionName);
124         } catch (ActionExecutionException e) {
125             Message error = new Message(MessageType.ERROR, "An error occurred while executing an action.", e.getMessage());
126             log.error("An error occurred while executing action [{}]", actionName, e);
127             messagesManager.sendLocalMessage(error);
128         }
129     }
130 }